2021-10-31

实验五 使用动态优先权的进程调度算法模拟**

                             ----东北大学操作系统实验----

1、实验目的
通过动态优先权算法的模拟加深对进程概念进程调度过程的理解。
2、实验内容
(1)用C语言来实现对N个进程采用动态优先权优先算法的进程调度。
(2)每个用来标识进程的进程控制块PCB用结构来描述,包括以下字段:
•••• 进程标识数 ID。
•••• 进程优先数 PRIORITY,并规定优先数越大的进程,其优先权越高。
•••• 进程已占用的CPU时间CPUTIME。
•••• 进程还需占用的CPU时间ALLTIME。当进程运行完毕时,ALLTIME变为0。
•••• 进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,将进入阻塞状态。
•••• 进程被阻塞的时间BLOCKTIME,表示已阻塞的进程再等待BLOCKTIME个时间片后,将转换成就绪状态。
•••• 进程状态STATE。
•••• 队列指针NEXT,用来将PCB排成队列。
(3)优先数改变的原则:
•••进程在就绪队列中呆一个时间片,优先数加1。
•••进程每运行一个时间片,优先数减3。
(4)假设在调度前,系统中有5个进程,它们的初始状态如下:
在这里插入图片描述

(3) 为了清楚的观察各进程的调度过程,程序应将每个时间片内的情况显示出来,参照的具体格式如下:

在这里插入图片描述
完整代码:

#include<iomanip> 
#include<iostream>
#include<algorithm>
using namespace std;
//Author:沉默的抽屉 
//time:2021/10/31

enum State {READY,BLOCK,DONE
};

struct Process{
	int ID;
	int PRIORITY;//优先级 
	int CPUTIME;//进程已占用CPU时间 
	int ALLTIME;//还需占用CPU时间,全为0,则结束 
	int STARTBLOCK;//start block个时间片后进入阻塞 
	int BLOCKTIME;//被阻塞时间 
	State STATE;//状态 
}; 
Process process[5];
void print();
int main()
{	
	/*process0*/
	process[0].ID = 0;
	process[0].PRIORITY = 9;
	process[0].CPUTIME = 0;
	process[0].ALLTIME = 3;
	process[0].STARTBLOCK = 2;
	process[0].BLOCKTIME = 3;
	process[0].STATE = READY;
	/*process1*/
	process[1].ID = 1;
	process[1].PRIORITY = 38;
	process[1].CPUTIME = 0;
	process[1].ALLTIME = 3;
	process[1].STARTBLOCK = -1;
	process[1].BLOCKTIME = 0;
	process[1].STATE = READY;
	/*process2*/
	process[2].ID = 2;
	process[2].PRIORITY = 30;
	process[2].CPUTIME = 0;
	process[2].ALLTIME = 6;
	process[2].STARTBLOCK = -1;
	process[2].BLOCKTIME = 0;
	process[2].STATE = READY;
	/*process3*/
	process[3].ID = 3;
	process[3].PRIORITY = 29;
	process[3].CPUTIME = 0;
	process[3].ALLTIME = 3;
	process[3].STARTBLOCK = -1;
	process[3].BLOCKTIME = 0;
	process[3].STATE = READY;
	/*process4*/
	process[4].ID = 4;
	process[4].PRIORITY = 0;
	process[4].CPUTIME = 0;
	process[4].ALLTIME = 4;
	process[4].STARTBLOCK = -1;
	process[4].BLOCKTIME = 0;
	process[4].STATE = READY;
	
	int timeslice = 0;
	int temp[5];// 存放阻塞块的优先级的值 

	int finishseq[5];//用于存放进程完成的次序 
	int b=0;
	while(process[0].ALLTIME||process[1].ALLTIME||process[2].ALLTIME||process[3].ALLTIME||process[4].ALLTIME){
	for(int i=0;i<5;i++)
	{
		int array[]={process[0].PRIORITY,process[1].PRIORITY,process[2].PRIORITY,process[3].PRIORITY,process[4].PRIORITY};
		if((READY==process[i].STATE)&&(process[i].PRIORITY==*max_element(array,array+5)))
		{
			timeslice++;
			process[i].PRIORITY-=3;
			process[i].CPUTIME+=1;
			process[i].ALLTIME-=1;
		
		for(int j=0;j<5;j++){
			if(j==i) continue;
			if(process[j].ALLTIME>0 && process[j].STATE == READY)
			{
				process[j].PRIORITY+=1;
			}
		}
		
		if(process[i].ALLTIME == 0)
		{
			process[i].STATE=DONE;
			process[i].PRIORITY=-1;
			finishseq[b]=process[i].ID;//输出进程完成次序 
			b++;
			

		}
	
		for(int j=0;j<5;j++)
		{
			if(BLOCK==process[j].STATE && process[j].BLOCKTIME>0)
			{
				process[j].BLOCKTIME -=1;
				if(process[j].BLOCKTIME==0){
					process[j].STATE=READY;
					process[j].PRIORITY=temp[j];
				}	
			}
			
		}
		
		if(process[i].STARTBLOCK>0){
			process[i].STARTBLOCK-=1;
			if(process[i].STARTBLOCK==0)
			{
				process[i].STATE=BLOCK;
				temp[i]=process[i].PRIORITY;
				process[i].PRIORITY=-1;
			}
		}
		cout<<"RUNNING PROG: "<<timeslice<<endl;
		print();
	}
	}
	
}
	cout<<"执行次序:";
	for(int i=0;i<5;i++)
	{
		cout<<" ->id"<<finishseq[i];
		
	}

	return 0;	

	
}

void print(){
	cout<<"READY-QUEUE:";
	for(int i=0;i<5;i++){
		if(READY==process[i].STATE)
		{
			cout<<"->id"<<process[i].ID;	
		}
	}
	
	cout<<endl;
	cout<<"BLOCK-QUEUE:";
	for(int i=0;i<5;i++)
	{
		if(BLOCK==process[i].STATE)
		{
			cout<<"->id"<<process[i].ID;
		}
	}
	cout<<endl;
		cout<<"---------------------------------------------"<<endl;
		cout<<"---------------------------------------------"<<endl;
	cout<<std::left<<setw(15)<<"ID";
	for(int i=0;i<5;i++)
		{
			cout<<std::right<<setw(6)<<process[i].ID;
		}
	cout<<endl;
	cout<<std::left<<setw(15)<<"PRIORITY";
	for(int i=0;i<5;i++)
		{
			cout<<std::right<<setw(6)<<process[i].PRIORITY;
		}
	cout<<endl;
	
	cout<<std::left<<setw(15)<<"CPUTIME";
	for(int i=0;i<5;i++)
		{
			cout<<std::right<<setw(6)<<process[i].CPUTIME;
		}
	cout<<endl;
	cout<<std::left<<setw(15)<<"ALLTIME";
	for(int i=0;i<5;i++)
		{
			cout<<std::right<<setw(6)<<process[i].ALLTIME;
		}
	cout<<endl;
	cout<<std::left<<setw(15)<<"STARTBLOCK";
	for(int i=0;i<5;i++)
		{
			cout<<std::right<<setw(6)<<process[i].STARTBLOCK;
		}
	cout<<endl;
	cout<<std::left<<setw(15)<<"BLOCKTIME";
	for(int i=0;i<5;i++)
		{
			cout<<std::right<<setw(6)<<process[i].BLOCKTIME;
		}
	cout<<endl;
	cout<<std::left<<setw(15)<<"STATE";
	for(int i=0;i<5;i++)
		{
			if(process[i].STATE==0)
			cout<<std::right<<setw(6)<<"READY";
			else if(process[i].STATE==1)
				cout<<std::right<<setw(6)<<"BLOCK";
				else 
					cout<<std::right<<setw(6)<<"DONE";
				
		}
	cout<<endl;
	cout<<endl;


}
 

 

可根据进程数量的多少进行删改,或改用在程序执行中输入进程相关的值。

(1)用C语言来实现对N个进程采用动态优先权优先算法进程调度。 (2)每个用来标识进程进程控制块PCB用结构来描述,包括以下字段: •••• 进程标识数 ID。 •••• 进程优先数 PRIORITY,并规定优先数越大的进程,其优先权越高。 •••• 进程已占用的CPU时间CPUTIME。 •••• 进程还需占用的CPU时间ALLTIME。当进程运行完毕时,ALLTIME变为0。•••• 进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,将进入阻塞状态。 •••• 进程被阻塞的时间BLOCKTIME,表示已足赛的进程再等待BLOCKTIME个时间片后,将转换成就绪状态。 •••• 进程状态START。 •••• 队列指针NEXT,用来将PCB排成队列。 (3)优先数改变的原则: •••进程在就绪队列中呆一个时间片,优先数加1。 •••进程每运行一个时间片,优先数减3。 (4)假设在调度前,系统中有5个进程,它们的初始状态如下: ID 0 1 2 3 4 PRIORITY 9 38 30 29 0 CPUTIME 0 0 0 0 0 ALLTIME 3 3 6 3 4 STARTBLOCK 2 -1 -1 -1 -1 BLOCKTIME 3 0 0 0 0 STATE READY READY READY READY READY (5)为了清楚的观察各进程的调度过程,程序应将每个时间片内的情况显示出来,参照的具体格式如下: RUNNING PROG:i READY-QUEUE:->id1->id2 BLOCK-QUEUE:->id3->id4 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ID 0 1 2 3 4 PRIORITY P0 P1 P2 P3 P4 CUPTIME C0 C1 C2 C3 C4 ALLTIME A0 A1 A2 A3 A4 STARTBLOCK T0 T1 T2 T3 T4 BLOCKTIME B0 B1 B2 B3 B4 STATE S0 S1 S2 S3 S4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值