⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gg.cpp

📁 用C++模拟实现操作系统是如何调度进程的
💻 CPP
字号:
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<windows.h>
#include<time.h>
//#include<math.h>


#define MAXSIZE 10;
#define OK 1;
#define error 0;
typedef struct {
	int PrecedenceNum;
	int RunTime;
	int RotateTime;
	int StayCPUTime;
	int info;
	int run,wait,finish;
}Process,QElemType;
typedef struct {
	QElemType *base;
	int head;
	int root;
}Queue,*PQueue;
int InitializeProcess(Process &p)
{
	//设置种子,使程序每次运行时rand()都生成一组新的随机数。
    srand((unsigned)time(NULL));
	p.PrecedenceNum=rand()%28+3;//产生3~30的随机数字
	Sleep(100);
	p.RunTime=rand()%10+1;//产生1~10的随机数字
	Sleep(100);
	p.RotateTime=rand()%15+1;//产生1~15的随机数字
	p.StayCPUTime=0;
	p.run=false;
	p.finish=false;
	p.wait=true;
	p.info=0;
	return OK;
}
int InitializeQueue(Queue &Q)
{
	Q.base=(QElemType*)malloc(10*sizeof(QElemType));
	if(!Q.base)exit(-1);
	Q.head=Q.root=0;
	return OK;
}

int push(Queue &Q,Process &p)
{
	//if(!Q.head)return error;
		int i;i=Q.head;
	    while(Q.base[i].PrecedenceNum>p.PrecedenceNum)
		{
			Q.base[i+1]=Q.base[i];i--;
		}
		Q.base[i+1]=p;
		Q.head++;
	return OK;
}
int pop(Queue &Q,QElemType &e)
{ 
	if(Q.head==Q.root) exit(-1);
	e=Q.base[Q.head];
	Q.head--;
	return OK;
}
int Run(Process &p)
{
	p.run=true;
	p.wait=false;
	p.PrecedenceNum=p.PrecedenceNum-3;
	p.RunTime--;
	p.StayCPUTime++;
	return OK;
}
int Precedence(Queue &Q)
{
	Process p;
	pop(Q,p);
	if(p.RunTime==0) return OK;
	p.run=true;
	p.wait=false;
	Run(p);
	if(p.RunTime==0)
	{
		p.PrecedenceNum=0;
		p.finish=true;
		p.run=false;
	    cout<<"这是第"<<p.info<<"的进程调用情况"<<endl;
		push(Q,p);
		Precedence(Q);
	}
		while(Q.base[Q.head].PrecedenceNum>=p.PrecedenceNum)
		{
			if(p.RunTime!=0) Run(p);
			else
			{
				p.PrecedenceNum=0;
	        	p.finish=true;
		        p.run=false;
		        cout<<"这是第"<<p.info<<"的进程调用情况"<<endl;
				pop(Q,p);Run(p);break;
			}
        } 
		p.finish=true;
		    p.run=false;
			p.wait=true;
			push(Q,p);
			Precedence(Q);
		
		return OK;
}
int Rotate(Queue &Q)
{
	Process p;
	pop(Q,p);
    if(p.RotateTime==p.StayCPUTime) return OK;
    p.run=true;
	p.wait=false;
	while(p.StayCPUTime<p.RotateTime)
	    Run(p);
	if(p.RotateTime==p.StayCPUTime)
		{
		   p.PrecedenceNum=0;
		   p.finish=true;
		   p.run=false;
		   cout<<"这是第"<<p.info<<"的进程调用情况"<<endl;
		   Precedence(Q);
		   push(Q,p);
		   Rotate(Q);
		}
	return OK;
}
int main()
{ 
	Queue Q;
	Process p1,p2,p3,p4,p5,p6,p7;

	InitializeQueue(Q);
	InitializeProcess(p1);
	InitializeProcess(p2);
	InitializeProcess(p3);
	InitializeProcess(p4);
	InitializeProcess(p5);
	InitializeProcess(p6);
	InitializeProcess(p7);
	

	p1.info=1;
	p2.info=2;
	p3.info=3;
	p4.info=4;
	p5.info=5;
	p6.info=6;
	p7.info=7;

	push(Q,p1);
	push(Q,p2);
	push(Q,p3);
	push(Q,p4);
	push(Q,p5);
	push(Q,p6);
	push(Q,p7);
	cout<<"请选择进程调度的方法:"<<"P代表优先调度;R代表轮转调度。"<<endl;
	cout<<"请选择:"<<endl;

	char ch;
	ch=getchar();
	if(ch=='P'||ch=='p')
		Precedence(Q);
	if(ch=='R'||ch=='r')
		Rotate(Q);
	if(ch!='R'&&ch!='r'&&ch!='P'&&ch!='p') cout<<"输入错误!!!"<<endl;
	return OK;
}








	
	

	    







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -