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

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

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

数据结构设计

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

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 <algorithm>
#include <iostream>
#include <vector>
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;
}

运行截图

image-20220331152346440

image-20220331152408311

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万伏小太阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值