操作系统上机实习

实验一  多级队列调度算法

题目描述

设RQ分为RQ1和RQ2,RQ1采用轮转法,时间片q=7.
        RQ1>RQ2,RQ2采用短进程优先调度算法。
测试数据如下:RQ1: P1-P5, RQ2: P6-P10
进程 P1 P2 P3 P4 P5 P6 P7 P8 P9 P10
运行时间 16 11 14 13 15 21 18 10 7 14
已等待时间 6 5 4 3 2 1 2 3 4 5

伪代码实现描述

 “`
typedef  struct  tag_pcb
    {  char  name[8];
       int need;//须运行的时间
       int turn;//周转时间
      struct  tag_pcb  next;
   } PCB;
PCB 
RQ1,RQ2,Finish;
int clock=0;  //时钟

main ( )
{  输入RQ1;
   输入RQ2;(最好从文件读入)
   while(RQ1!=NULL)
{ 从RQ1中选取一进程Pi准备运行;
      计算其运行的时间t;
      clock+=t;  //表示Pi运行t;
      if (Pi完成)  计算其turn;
      否则   Pi加入到队尾;
   }
   while(RQ2!=NULL)
   { 从RQ2中选取一进程Pi准备运行;   
      clock+=Pi.need;
      计算Pi的turn;
   }
输出进程的周转时间;
}

### 题解之储备知识
#### 文件读取
本题建议使用cin流
[C++文件读写](https://blog.csdn.net/lz20120808/article/details/49622787)
[C文件读写](https://blog.csdn.net/hjl240/article/details/47132477)
#### c++ vector利用迭代器删除元素会发生什么?
对于关联容器(如map,set,multimap,multiset),删除当前的iterator,仅仅会使当前的iterator失效,只要在erase时,递增当前的iterator即可。这是因为map之类的容器,使用了红黑树来实现,插入,删除一个结点不会对其他结点造成影响。
?

对于序列式容器(如vector,deque等),删除当前的iterator会使后面所有元素的iterator都失效??这是因为vector,deque使用了连续分配的内存,删除一个元素导致后面所有的元素会向前移动一个位置。不过erase方法可以返回下一个有效的iterator。使用方式如下,例如:

    vector<int> val = { 1,2,3,4,5,6 };
    vector<int>::iterator iter;
    for (iter = val.begin(); iter != val.end(); )
    {
         if (3 == *iter)
              iter = val.erase(iter);     //返回下一个有效的迭代器,无需+1
              //也可只写val.erase(iter),不用再赋值
         else
              ++iter;
    }
#### vector遍历的四种方法
[教程](https://blog.csdn.net/HW140701/article/details/78833486)
void push_back(const T& x):向量尾部增加一个元素X
#### 优先级队列
[详解](https://blog.csdn.net/weixin_36888577/article/details/79937886)
### 参考解答代码
```cpp
#include 
#include 
#include 
#include 
#include 
using namespace std;

typedef struct tag_pcb
{
    char name[8];
    int need; //须运行的时间
    int turn; //周转时间
    struct tag_pcb *next;
    bool operator>(const tag_pcb &a) const
    {
        return need > a.need;
    }
} PCB;

PCB *RQ1, *RQ2, *Finish;
int myclock = 0; //时钟

int main()
{
    vector RQ1;
    priority_queue, greater> RQ2;

    char buffer[256];
    ifstream myfile("source/input1");
    ifstream myfile2("source/input2");
    if (!myfile)
    {
        cout << "Unable to open myfile";
        exit(1); // terminate with error
    }
    if (!myfile2)
    {
        cout << "Unable to open myfile";
        exit(1); // terminate with error
    }

    while (!myfile.eof())
    {
        PCB temp;
        myfile.getline(buffer, 10); //读入每行
        sscanf(buffer, "%s %d %d", &temp.name, &temp.need, &temp.turn);
        cout << temp.name << " " << temp.need << " " << temp.turn << endl;
        RQ1.push_back(temp);
    }
    myfile.close();

    while (!myfile2.eof())
    {
        PCB temp;
        myfile2.getline(buffer, 10); //读入每行
        sscanf(buffer, "%s %d %d", &temp.name, &temp.need, &temp.turn);
        cout << temp.name << " " << temp.need << " " << temp.turn << endl;
        RQ2.push(temp);
    }
    myfile2.close();
    while (!RQ1.empty())
    {
        for (vector::iterator iter = RQ1.begin(); iter != RQ1.end();)
        {
            if ((*iter).need > 7)
            {
                myclock += 7;
                (*iter).need -= 7;
                iter++;
            }
            else
            {
                myclock += (*iter).need;
                (*iter).turn += myclock;
                cout << (*iter).name <<" "<< (*iter).turn<