> 文档中心 > 操作系统 实验四 进程调度算法 先来先服务

操作系统 实验四 进程调度算法 先来先服务


操作系统 实验四 进程调度算法 先来先服务

先来先服务是指,按时间顺序执行的一种调度算法,先来先服务是非抢占式的,就是说一旦开始执行那么就要一直到执行结束。在一个进程在运行时别的进程不能够抢占资源。

数据结构设计

结构体:存储进程的基本信息

struct PCB //存储进程信息{  string name;     //进程的名字  int arrive_time; //进入进程队列的时间  char state;      //进程状态,"R"-运行态,"W"表示就绪态  int time_len;    //进程运行完需要的时间。单位:秒};

进程就绪队列:

vector<PCB> EnterReadyQueue;//采用动态数组的方式,创建进程模拟就绪队列,运行完就从队头删除

具体调度算法

先来先服务调度算法是指,按时间顺序执行的一种调度算法,先来先服务是非抢占式的,就是说一旦开始执行那么就要一直到执行结束。在一个进程在运行时别的进程不能够抢占资源。

程序输出设计

void print_queue() { //打印当前就绪队列的情况。  cout << "" << endl;  cout << "当前就绪队列的进程总个数:" << EnterReadyQueue.size() << endl;  cout << "进程名字   进程状态   需要运行的时间" << endl;  for (int i = 0; i < EnterReadyQueue.size(); i++) {    cout << EnterReadyQueue[i].name << "      "  << EnterReadyQueue[i].state << "      "  << EnterReadyQueue[i].time_len << endl;  }  cout << "" << endl << endl;}

完整代码

/*选取先来的服务的进程调度模拟算法*/#include #include #include using namespace std;struct PCB //存储进程信息{  string name;     //进程的名字  int arrive_time; //进入进程队列的时间  char state;      //进程状态,"R"-运行态,"W"表示就绪态  int time_len;    //进程运行完需要的时间。单位:秒};bool cmp(PCB a, PCB b) {  if(a.arrive_time==b.arrive_time){    return a.time_len<b.time_len;  }  return a.arrive_time < b.arrive_time; //按照先来先服务的规则,对时间进行排序。}vector<PCB> EnterReadyQueue;//采用动态数组的方式,创建进程模拟就绪队列,运行完就从队头删除void print_queue() { //打印当前就绪队列的情况。  cout << "" << endl;  cout << "当前就绪队列的进程总个数:" << EnterReadyQueue.size() << endl;  cout << "进程名字   进程状态   需要运行的时间" << endl;  for (int i = 0; i < EnterReadyQueue.size(); i++) {    cout << EnterReadyQueue[i].name << "      "  << EnterReadyQueue[i].state << "      "  << EnterReadyQueue[i].time_len << endl;  }  cout << "" << endl << endl;}int main() {  int n;  cout << "请输入要运行的进程数量: ";  cin >> n;  for (int i = 1; i <= n; i++) {    PCB Pro;    cout << "请输入第 " << i << " 个进程的名字: ";    cin >> Pro.name;    cout << endl;    cout << "请输入第 " << i << " 个进程的到达的时间: ";    cin >> Pro.arrive_time;    cout << endl;    cout << "请输入第 " << i << " 个进程预计需要运行的时间: ";    cin >> Pro.time_len;    cout << endl;    Pro.state = 'W';    EnterReadyQueue.push_back(Pro); //加入进程就绪队列  }  sort(EnterReadyQueue.begin(), EnterReadyQueue.end(), cmp); //对到来的时间排序  int sum_time;  while (EnterReadyQueue.size() > 0) {    print_queue();    PCB now = EnterReadyQueue.front(); //就绪队列最前面先执行    cout << "当前进程的名字   进程到来的时间     运行时间" << now.name << endl;    cout << now.name << "      " << now.arrive_time << ""<< now.time_len << endl;    sum_time = now.time_len + now.arrive_time;    cout << now.name << "进程在第 " << sum_time<< " 秒运行结束" << endl<< endl;    EnterReadyQueue.erase(EnterReadyQueue.begin()); //运行结束从就绪队列删除  }  cout << "" << endl << endl;  cout << "进程总花费时间为 " << sum_time << " 秒" << endl << endl;}

运行截图

操作系统 实验四 进程调度算法 先来先服务

操作系统 实验四 进程调度算法 先来先服务

老人咖美文网