分别使用FCFS、SJF(非抢占)、优先级调度(非抢占)、RR四种调度算法来模拟CPU调度的过程。(Linux、Windows下皆可)
输入:存储需要调度的作业信息的job.txt文档
输出:每个作业的编号、作业开始执行时间、作业结束时间以及该调度算法的平均等待时间
选作:SJF(抢占)、优先级调度(抢占)
#include<iostream>
#include<fstream>
using namespace std;
int h_start=0;
int m_start=0;
int mission;
int time_piece;
int wait_time;
char w;
class process
{
public:
int index;
int exe_time;
int h;
int m;
int priority;
char mark;
process()
{
mark='0';
}
};
void FCFS(process pro[])
{
process temp;
process pro1[5];
int i=0,j=0;
int key;
h_start=8;
m_start=0;
wait_time=0;
for(; i<5; i++)
pro1[i]=pro[i];
//按申请时间先后对5个进程进行排序
for(i=0; i<4; i++)
{
key=i;
for(j=i+1; j<5; j++)
if((pro1[j].h*60+pro1[j].m) < (pro1[key].h*60+pro1[key].m))
key=j;
temp=pro1[key];
pro1[key]=pro1[i];
pro1[i]=temp;
}
cout<<"FCFS:"<<endl;
for(i=0; i<5; i++)
{
wait_time+=h_start*60+m_start-pro1[i].h*60-pro1[i].m;
cout<<pro1[i].index<<' '<<h_start<<':'<<m_start<<' ';
m_start+=pro1[i].exe_time;
if(m_start>=60)
{
h_start++;
m_start-=60;
}
cout<<h_start<<':'<<m_start<<endl;
}
wait_time/=5;
cout<<"Averag
四种CPU调度算法模拟实现

本文详细介绍了操作系统中四种常见的CPU调度算法:先来先服务(FCFS)、最短作业优先(SJF)、非抢占优先级调度和时间片轮转(RR)。通过模拟不同算法,展示了各算法如何决定作业的执行顺序,以及计算平均等待时间。
最低0.47元/天 解锁文章
3093

被折叠的 条评论
为什么被折叠?



