进程调度算法(先来先服务/短作业优先)代码实现

最近在复习408操作系统进程时,决定用代码模拟调度算法来熟悉详细过程!
选择两个好写点的算法进行练习!!!
以下代码使用c++语言。优先队列和队列直接使用STL容器!!!

引入头文件
#include<iostream>
#include<queue>
#include<map>
using namespace std;
数据结构设计
//进程控制块
struct PCB {
	string ID;//进程ID
	double arrivalTime;//到达处理机时间
	double remainTime;//剩余服务时间
	double priority;//优先级(越小优先级越高)
	//其他信息省略...
};
由于有两个算法,为了减少冗余度。将公共部分使用自定义基类Base实现。
//调度基础类
class Base {
public:
	map<string,pair<double,double>>times;//进程ID->[等待时间,运行时间]
	queue<PCB*>Queue;//普通队列
	priority_queue<PCB*, vector<PCB*>, cmp>pq;//优先队列
	double avWait;//平均等待时间
	double avTurnaround;//平均周转时间
	double avWeightedTurnaround;//平均带权周转时间
	Base() {
		this->avWait = this->avTurnaround = this->avWeightedTurnaround = 0.0;
	}
	//输出各进程时间
	void printTime() {
		for (map<string, pair<double, double>>::iterator it = times.begin(); it != times.end(); ++it) {
			printf("进程%s运行了%.3lf秒,等待了%.3lf秒\n", it->first.c_str(), it->second.second, it->second.first);
		}
		calculate_time();//计算平均时间
		printf("平均等待时间为%.3lf秒,平均周转时间%.3lf秒,平均带权周转时间%.3lf秒\n",avWait,avTurnaround,avWeightedTurnaround);
	}
	//计算各平均时间
	void calculate_time() {
		double sumWait = 0.0, sumTurnaround = 0.0, sumWeightedTurnaround = 0.0;//总等待时间 总周转时间 总带权周转时间
		for (map<string, pair<double, double>>::iterator it = times.begin(); it != times.end(); ++it) {
			sumWait += it->second.first;
			sumTurnaround += it->second.first + it->second.second;
			sumWeightedTurnaround += (it->second.first + it->second.second) / it->second.second;
		}
		avWait = sumWait / times.size();
		avTurnaround = sumTurnaround / times.size();
		avWeightedTurnaround = sumWeightedTurnaround / times.size();
	}
};
先来先服务调度类

解释:
在FCFS构造函数中将进程控制块添加到普通队列Queue中
过程:
①.每次从Queue中取出一个元素
②.如果当前时间小于即将需要运行的进程到达时间,就将当前时间改成下一个需要运行进程到达的时间。
③.times成员时用于记录进程等待时间和运行时间。
④.运行完成后,当前时间+=上一个进程运行的时间

//先来先服务调度类(FCFS)[非抢占式]
class FCFS :private Base {
public:
	FCFS(PCB* pcbs,int len) {
		for (int i = 0; i < len; ++i) {
			Queue.push(pcbs+i);//加入到进程队列
		}
	}
	void run() {
		double curTime = 0;//当前时间
		while (!Queue.empty()) {
			PCB* curRun = Queue.front();//①从队列头部获取进程控制块
			Queue.pop();//弹出进程控制块
			if (curTime < curRun->arrivalTime) {//②当前时间比现在需要运行的进程时间小,需要将当前时间改成进程到达时间
				curTime = curRun->arrivalTime;
			}
			times[curRun->ID] = { curTime - curRun->arrivalTime,curRun->remainTime };//③
			curTime += curRun->remainTime;//④当前时间 += 进程剩余服务时间
		}
	}
	void show() {
		this->printTime();
	}
};

测试函数

void fcfs() {
	cout << "先来先服务算法:" << endl;
	PCB pcbs[4] = { {"1号",8.0,2.0},{"2号",8.4,1.0},{"3号",8.8,0.5},{"4号",9.0,0.2} };
	FCFS fcfs(pcbs, 4);
	fcfs.run();
	fcfs.show();
}

短作业优先调度类

解释:
将进程的剩余时间作为优先级的比较       (每次调度需要使用剩余时间最小的)
过程:
①.while循坏的作用时只要还有进程没运行完,就继续调度
②.首先将到达时间比当前时间小的进程的PCB放入优先队列中。
③.如果优先队列为空就
             如果后续没有进程加入->调度结束
             如果后续还有进程加入->将当前时间改为下一个需要运行的进程到达时间
④.向后遍历(查看后面是否有多个同时到达的进程)
⑤.从优先队列取出需要服务时间最短的进程
⑥.记录等待时间和运行时间

//短作业优先调度类(SJF)[非抢占式]
class SJF :private Base {
public:
	SJF(PCB* pcbs, int len) {
		run(pcbs,len);
	}
	void run(PCB* pcbs, int len) {
		double curTime = 0.0;//当前时间
		int i = 0;//pcbs的下标
		while (!pq.empty() || i < len) {//①当优先队列不为空或还有进程没运行
			//每个进程运行期间可能有其他进程加入
			//因此需要将运行期间加入的进程放入优先队列中
			for (; i < len; ++i) {
				if (pcbs[i].arrivalTime <= curTime) {//②进程到达时间小于等于当前时间
					pq.push(&pcbs[i]);
				}
				else {
					break;
				}
			}
			//如果当前优先队列中没有进程
			//说明在当前时间运行完了已到达的进程
			//但是可能后续还有进程
			//当前时间就要跳到后续进程的到达时间
			if (pq.empty()) {//③
				if (i == len) {
					//说明运行结束,可以退出了
					break;
				}
				else {//后续还有进程到来
					curTime = pcbs[i].arrivalTime;
				}
			}
			//可能同时有多个进程到来
			//多以需要便利加入
			for (; i < len; ++i) {//④
				if (curTime == pcbs[i].arrivalTime) {
					pq.push(&pcbs[i]);
				}
				else {
					break;
				}
			}

			PCB* curRun = pq.top();//⑤
			pq.pop();
			times[curRun->ID] = { curTime - curRun->arrivalTime,curRun->remainTime };//⑥
			curTime += curRun->remainTime;
		}
	}
	void show() {
		this->printTime();
	}
};

测试函数

void sjf() {
	cout << "短作业优先算法:" << endl;
/*
	由于在设计优先队列对比的优先级是根据pcb中的优先级对比的,
	但是这里需要根据时间来对比优先级,
	将剩余时间直接赋值给优先级用于对比
*/
	PCB pcbs[4] = { {"1号",8.0,2.0,2.0},{"2号",8.4,1.0,1.0},{"3号",8.8,0.5,0.5},{"4号",9.0,0.2,0.2} };
	SJF sjf(pcbs,4);
	sjf.show();
}

主函数

int main() {
	fcfs();
	sjf();
}

运行结果

先来先服务算法:
进程1号运行了2.000秒,等待了0.000秒
进程2号运行了1.000秒,等待了1.600秒
进程3号运行了0.500秒,等待了2.200秒
进程4号运行了0.200秒,等待了2.500秒
平均等待时间为1.575秒,平均周转时间2.500秒,平均带权周转时间5.625秒
短作业优先算法:
进程1号运行了2.000秒,等待了0.000秒
进程2号运行了1.000秒,等待了2.300秒
进程3号运行了0.500秒,等待了1.400秒
进程4号运行了0.200秒,等待了1.000秒
平均等待时间为1.175秒,平均周转时间2.100秒,平均带权周转时间3.525秒
在这里插入图片描述
测试数据来自王道考研操作系统2024版64页或2025版71页!!!

  • 9
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
先来先服务算法 #include "stdio.h" #include #define max 100 #define pfree 0 /*process end*/ #define running 1 /*process running status*/ #define aready 2 /*process aready status */ #define blocking 3 /*process aready blocking status*/ typedef struct node { char name; int status; int ax,bx,cx,dx; int pc; int psw; struct node *next; /*pcb define*/ }pcb; pcb *createprocess(pcb *head) { pcb *p,*q; int a,b,c,d,m,n; char ID; q=NULL; printf("input the first seven status pcb:"); scanf("\n%c%d%d%d%d%d%d",&ID,&a,&b,&c,&d,&m,&n); while(1) { p=(pcb*)malloc(sizeof(pcb)); p->name=ID; p->ax=a; p->bx=b; p->cx=c; p->dx=d; p->pc=m; p->psw=n; p->status=aready; if(head==NULL) head=p; else q->next=p; q=p; printf("input the next seven status pcb: "); scanf("\n%c",&ID); if (ID == '*') break; scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&m,&n); } if(q!=NULL) q->next=NULL; q=head; while(q) { printf("\n process name. status.ax. bx. cx. dx. pc. psw.\n "); printf("%10c%5d%5d%5d%5d%5d%5d%5d",q->name,q->status,q->ax,q->bx,q->cx,q->dx,q->pc,q->psw); q=q->next; } return head;/*createprocess end*/ } void processfcfs(pcb *head) /*use fifo */ { pcb *p; p=head; printf("\n the process use fcfs method.\n"); printf("running the frist process:\n"); while(p!=NULL) { p->status=running; printf("\nprocess name status. ax. bx. cx. dx. pc. psw."); printf("\n%10c%5d%8d%5d%5d%5d%5d%5d",p->name,p->status,p->ax,p->bx,p->cx,p->dx,p->pc,p->psw); /*check process running status */ p->status=0; p=p->next; } printf("\n检查进程是否结束:"); p=head; while(p) { printf("\n%3c%3d",p->name,p->status); p=p->next; } printf("\ngame is over!\n"); } main() { pcb *head; head=NULL; head=createprocess(head); processfcfs(head); }
先来先服务(FCFS)是一种简单的调度算法,它按照进程到达的先后顺序进行调度。当一个进程到达时,它会被添加到就绪队列的末尾,然后在CPU空闲时被调度执行。下面是先来先服务调度算法的伪代码: ``` // 声明一个进程结构体 struct Process { int arrivalTime; // 到达时间 int burstTime; // 执行时间 }; // 声明先来先服务调度函数 void FCFS(Process *processes, int n) { int currentTime = 0; // 当前时间 for (int i = 0; i < n; i++) { if (currentTime < processes[i].arrivalTime) { currentTime = processes[i].arrivalTime; } printf("进程%d执行,执行时间:%d 到 %d\n", i+1, currentTime, currentTime + processes[i].burstTime); currentTime += processes[i].burstTime; } } ``` 作业优先调度算法(SJF)是根据进程的执行时间长进行调度的算法。当一个进程到达时,系统会选择执行时间最的进程来执行。下面是作业优先调度算法的伪代码: ``` // 声明作业优先调度函数 void SJF(Process *processes, int n) { int currentTime = 0; // 当前时间 for (int i = 0; i < n; i++) { int shortest = i; for (int j = i + 1; j < n; j++) { if (processes[j].burstTime < processes[shortest].burstTime) { shortest = j; } } if (currentTime < processes[shortest].arrivalTime) { currentTime = processes[shortest].arrivalTime; } printf("进程%d执行,执行时间:%d 到 %d\n", shortest+1, currentTime, currentTime + processes[shortest].burstTime); currentTime += processes[shortest].burstTime; processes[shortest].burstTime = INT_MAX; // 标记为已执行 } } ``` 这两种调度算法都是比较基础的算法,可以用于操作系统进程调度先来先服务适合执行时间大致相同时的进程,而作业优先适合执行时间差异较大的进程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mystic Musings

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

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

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

打赏作者

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

抵扣说明:

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

余额充值