操作系统——进程调度

1、进程调度算法:采用动态最高优先数优先的调度算法(即把处理机分配给优先数最高的进程)。 

2、每个进程有一个进程控制块( PCB)表示。进程控制块可以包含如下信息:

进程名---进程标示数  ID

优先数 PRIORITY  优先数越大优先权越高

到达时间---进程的到达时间为进程输入的时间。、

进程还需要运行时间ALLTIME,进程运行完毕ALLTIME=0,

已用CPU时间----CPUTIME、

进程的阻塞时间STARTBLOCK-表示当进程在运行STARTBLOCK个时间片后,进程将进入阻塞状态

进程的阻塞时间BLOCKTIME--表示当进程阻塞BLOCKTIME个时间片后,进程将进入就绪状态

进程状态—STATE

队列指针NEXT 用来将PCB排成队列。 

3、调度原则:

进程的优先数及需要的运行时间可以事先人为地指定(也可以由随机数产生)。进程的到达时间为进程输入的时间。 

进程的运行时间以时间片为单位进行计算。 

进程在就绪队列中待一个时间片,优先数加1

  每个进程的状态可以是就绪 R(READY)、运行R(Run)阻塞B(BLOCK)、或完成F(Finish)四种状态之一。 

  就绪进程获得 CPU后都只能运行一个时间片。用已占用CPU时间加1来表示。 

  如果运行一个时间片后,进程的已占用CPU时间已达到所需要的运行时间,则撤消该进程,如果运行一个时间片后进程的已占用CPU时间还未达所需要的运行时间,也就是进程还需要继续运行,此时应将进程的优先数减3,然后把它插入就绪队列等待CPU。 

  每进行一次调度程序都打印一次运行进程、就绪队列、以及各个进程的 PCB,以便进行检查。    

重复以上过程,直到所要进程都完成为止。


#include <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <cstdlib>
#include <time.h>
#include <cstdlib>
using namespace std;
int Time=0;
struct Node
{
	int ID;
    int PRIORITY;
    int ALLTIME;
    int CPUTIME;
    int STARTBLOCK;
	int stbk;
    int BLOCKTIME;
	int bktm;
    char STATE;
    Node *next;
};
void GetintoReadylist(Node s);
list<Node >lst1;
list<Node >Readylist;
list<Node >Waitlist;
list<Node >Finishlist;
void init()
{
	Node s;
    for(int i=1;i<=10;i++)
    { 
	    s.ID=i;
	    s.PRIORITY=rand()%5;   //进程的优先级*
		s.ALLTIME=rand()%5;    //进程还需要的运行时间*
		s.CPUTIME=0;            //系统的已用时间,初始值为零
        s.STARTBLOCK=3;         //系统运行n个时间片的时候开始进入阻塞队列	
		s.stbk=0;
        s.BLOCKTIME=2;	        //进程阻塞BLOCKTIME个时间片后,进程将进入就绪状态
		s.bktm=0;
		s.STATE='R';            //没有初始状态
		GetintoReadylist(s);    //进入等待队列
    }
}
bool cmp(const Node &a,const Node &b)
{
	return a.PRIORITY>b.PRIORITY;
}
void GetintoReadylist(Node s)
{
	Readylist.push_back(s);
}
void OutPut()
{
	cout<<"Time: "<<Time++<<endl;
	cout<<"Readtlist SIZE: "<<Readylist.size()<<endl;
	list<Node >::iterator it;
	for(it=Readylist.begin();it!=Readylist.end();it++)
		cout<<"ID:"<<it->ID<<" PRIORITY:"<<it->PRIORITY<<" ALLTIME:"<<it->ALLTIME<<" CUP Time:"<<it->CPUTIME<<" STARTBLOCK Time"<<it->STARTBLOCK<<" Have Ran Time"<<it->stbk<<" STATE:"<<it->STATE<<endl;
	
	cout<<"Waitlist SIZE: "<<Waitlist.size()<<endl;
	for(it=Waitlist.begin();it!=Waitlist.end();it++)
		cout<<"ID:"<<it->ID<<" PRIORITY:"<<it->PRIORITY<<" ALLTIME:"<<it->ALLTIME<<" CUP Time:"<<it->CPUTIME<<" BLOCK Time"<<it->BLOCKTIME<<" Have Waited Time"<<it->bktm<<" STATE:"<<it->STATE<<endl;
	
}
int main()
{
	freopen("out.txt","w",stdout);
    list<Node >::iterator it;
    init(); 
	OutPut();
	while(!Readylist.empty()||!Waitlist.empty())
	{
	    if(Time == 19)
		{
			int m=1;
			OutPut();
		}
		if(!Waitlist.empty())
		{
			list<Node >::iterator it1;
			it1=Waitlist.begin();
			for(;it1!=Waitlist.end();it1++)
			{
				if(it1->bktm>=it1->BLOCKTIME)
				{
					Node tmp;
					tmp.ALLTIME=it1->ALLTIME;
				    tmp.BLOCKTIME=it1->BLOCKTIME;
					tmp.CPUTIME=it1->CPUTIME;
				    tmp.ID=it1->ID;
				    tmp.PRIORITY=it1->PRIORITY;
				    tmp.STARTBLOCK=it1->STARTBLOCK;
				    tmp.STATE='R';
				    tmp.stbk=0;
				    tmp.bktm=0;
				
					Readylist.push_back(tmp);
					it1 = Waitlist.erase(it1);
				}
			}
		}
		if(!Readylist.empty())
		{
	        Readylist.sort(cmp);
		    OutPut();
	        it=Readylist.begin();                                  //取出优先级最高的进程开始运行
		    it->CPUTIME++;
            if(it->CPUTIME>=it->ALLTIME)
		    {
			    it->STATE='F';
			    cout<<"ID: "<<it->ID<<" is end"<<endl;
			    Readylist.erase(it);
		    }
		    else 
		    {
			    it->PRIORITY-=3;
			    it->stbk++;
			    if(it->stbk>=it->STARTBLOCK)                       //进程的阻塞时间STARTBLOCK-表示当进程在运行STARTBLOCK个时间片后,进程将进入阻塞状态
			    {
				    Node tmp;
				    tmp.ALLTIME=it->ALLTIME;
				    tmp.BLOCKTIME=it->BLOCKTIME;
				    tmp.CPUTIME=it->CPUTIME;
				    tmp.ID=it->ID;
				    tmp.PRIORITY=it->PRIORITY;
				    tmp.STARTBLOCK=it->STARTBLOCK;
				    tmp.STATE='W';
				    tmp.stbk=0;
				    tmp.bktm=-1;
				
				    Waitlist.push_back(tmp);
				    it=Readylist.erase(it);
			    }
		    }
		}
		if(!Readylist.empty())
        {	
		    for(it=Readylist.begin();it!=Readylist.end();it++)  //就绪队列中没等待一个时间片优先数加1
		        it->PRIORITY++;
		}
		if(!Waitlist.empty())
		{
		     for(it=Waitlist.begin();it!=Waitlist.end();it++)
			     it->bktm++;
		}
		
	}
	
	system("pause");
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值