#include <iostream>
#include <math.h>
#include <malloc.h>
#include<windows.h>
#include<wincon.h>
#include<winuser.h>
#include<time.h>
#include<conio.h>
#include<cstdlib>
#include "stdio.h"
using namespace std;
HANDLE hstdout,hstdin;
#define OK 1
#define TRUE 1
#define FALSE 0
typedef int status;
typedef struct LNode
{
char name; //进程标识符(进程名)
char state; //进程状态,B-就绪,R-运行,w-阻塞
int needtime; //进程到完成还需要的CPU时间
int cputime; //进程占用的CPU时间
int lastime; //进程剩余时间
int number;
//进程占用资源数,number==5 进程运行;number == 4 进程就绪;number == 3 进程阻塞
}Elemtype;
typedef struct QNode
{
LNode pdata;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
status InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)return ERROR;
Q.front->next=NULL;
return OK;
}
status EnQueue(LinkQueue &Q,Elemtype e)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p)return ERROR;
p->pdata=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
status DeQueue(LinkQueue &Q,Elemtype &e){
QueuePtr p;
if(Q.front==Q.rear)return ERROR;
p=Q.front->next;
e=p->pdata;
Q.front->next=p->next;
if(Q.rear==p)Q.rear=Q.front;
free(p);
return OK;
}
status QueueEmpty(LinkQueue Q)
{//运行队列为空
if(Q.front == Q.rear)
return TRUE;
return FALSE;
}
status Beready(LinkQueue &Ready,LinkQueue &Run, Elemtype &e)
{//就绪状态------>运行状态
if(!(QueueEmpty(Ready)) && QueueEmpty(Run))
{
DeQueue(Ready,e);
e.number=5;
int t=rand()%10+1;
if(t<=e.lastime)
e.cputime=t;
else e.cputime=e.lastime;
e.lastime=e.lastime-e.cputime;
e.state='R';
EnQueue(Run,e);
return TRUE;
}
return FALSE;
}
status CPURun(LinkQueue &Ready,LinkQueue &Run,LinkQueue &Block,Elemtype &e)
{//进程:运行---->阻塞,运行----->就绪,运行------>完成
if(QueueEmpty(Run))
if(!(QueueEmpty(Ready)))
Beready(Ready,Run,e);
if(!QueueEmpty(Run))
{
DeQueue(Run,e);
if(e.cputime == 0)
{
if(e.lastime == 0)
return 4; //4进程运行完成
e.number=4;
e.state='B';
EnQueue(Ready,e);
return 3; //3进程调入就绪队列
}
int t=rand()%9+1;
e.number=t>=5?5:3;
if(e.number<5)
{
e.lastime=e.lastime+e.cputime;
e.state='W';
e.cputime = 0;
EnQueue(Block,e);
return 2; //2进程调入阻塞队列
}
e.cputime=e.cputime - 1;
EnQueue(Run,e);
return 1;
}
return FALSE;
}
status Weakup(LinkQueue &Block,LinkQueue &Ready,Elemtype &e)
{//阻塞----->就绪
QueuePtr wait;
int i=0,t;
wait=Block.front;
if(!(QueueEmpty(Block)))
{
e.number = wait->pdata.number;
t = rand()%6+1;
e.number = t>=4?4:e.number;
if(e.number >= 4)
{
DeQueue(Block,e);
e.state = 'B';
e.number=4;
EnQueue(Ready,e);
return OK;
}
}
return FALSE;
}
void CreatPcb(LinkQueue &Ready,LinkQueue &Block,int n)
{//创建进程,使进程入就绪队列
Elemtype p;
int i,t;
char str='a';
for(i=1;i<=n;i++)
{
p.name = str;
p.cputime=0;
p.needtime=rand()%15+5;
p.lastime=p.needtime;
t=rand()%7;
p.number=t>=4?4:t;
if(p.number<4)
{
p.state='W';
EnQueue(Block,p);
}
else
{
p.state='B';
EnQueue(Ready,p);
}
cout<<p.name<<" "<<p.state<<" "<<p.needtime<<" "<<p.cputime<<" "<<p.lastime<<" "<<p.number<<endl;
str=str+1;
}
}
void Process(LinkQueue &Ready,LinkQueue &Run,LinkQueue &Block)
{
Elemtype p;
Sleep(500);
if(Beready(Ready,Run,p))
{
cout<<p.name<<" "<<"进程入运行队列"<<endl;
cout<<p.name<<" "<<p.state<<" "<<p.needtime<<" "<<p.cputime<<" "<<p.lastime<<" "<<p.number<<endl;
}
Sleep(500);
int n=CPURun(Ready,Run,Block,p);
if(n ==1 )
{
cout<<p.name<<" "<<p.state<<" "<<p.needtime<<" "<<p.cputime<<" "<<p.lastime<<" "<<p.number<<endl;
}
else if(n ==2)
{
cout<<p.name<<" "<<"中断请求,进程入阻塞队列"<<endl;
cout<<p.name<<" "<<p.state<<" "<<p.needtime<<" "<<p.cputime<<" "<<p.lastime<<" "<<p.number<<endl;
}
else if(n == 3)
{
cout<<p.name<<" "<<"时间片用完,进程入就绪队列"<<endl;
cout<<p.name<<" "<<p.state<<" "<<p.needtime<<" "<<p.cputime<<" "<<p.lastime<<" "<<p.number<<endl;
}
else if(n == 4)
{
cout<<"进程 :"<<p.name<<" "<<"已处理完成!"<<endl;
}
if(Weakup(Block,Ready,p))
{
cout<<p.name<<" "<<"中断发生,进程唤醒入就绪队列"<<endl;
cout<<p.name<<" "<<p.state<<" "<<p.needtime<<" "<<p.cputime<<" "<<p.lastime<<" "<<p.number<<endl;
}
Sleep(500);
}
void main()
{
LinkQueue Ready,Run,Block;
int t;
InitQueue(Ready);
InitQueue(Run);
InitQueue(Block);
cout<<"请输入进程数 : ";
cin>>t;
cout<<"********************************************************************************"<<endl;
cout<<"进程名"<<" "<<"状态"<<" "<<"运行时间"<<" "<<"时间片"<<" "<<"剩余时间片"<<" "<<"占用资源数"<<endl;
cout<<"********************************************************************************"<<endl;
CreatPcb(Ready,Block,t); //创建PCB,使进程入就绪队列
cout<<"********************************************************************************"<<endl;
while(!(QueueEmpty(Ready)) || !(QueueEmpty(Run)) || !(QueueEmpty(Block)))
Process(Ready,Run,Block);
cout<<"********************************************************************************"<<endl;
cout<<"PCB任务全部完成!!"<<endl;
}
OS.zip_os_模拟进程调度_进程调度
版权申诉
44 浏览量
2022-09-23
22:28:52
上传
评论
收藏 2KB ZIP 举报
林当时
- 粉丝: 100
- 资源: 1万+