📄 jinc.txt
字号:
#include"stdafx.h"
#include "iostream.h"
//define pcb
typedef struct pcb
{
char name[10]; //进程名
char state; //状态w(就绪)r(运行)f(结束)
int id; //id号
int super; //优先级
int ntime; //需运行的时间
int rtime; //已运行的时间
struct pcb *next;
}*pcb1;
pcb1 s,w;//define two publiced linknode ,one is s(ready queue),one is w(blocked queue)
//initialize two queues
void init(pcb1 &r)
{
r=NULL;//both queues is the kind of head index
}
//print the information of the ready queue
void print()
{
cout< cout<<"您现在查看的是就绪队列的信息:"< pcb1 p;
cout<<"进程号 "<<"进程名 "<<"优先级 "<<"状态"<<"已运行时间 "<<"需运行时间"< for(p=s;p!=NULL;p=p->next)
{
cout<id<<" "<name<<" "<super<<" "<state<<" "<rtime<<" "<ntime< }
}
//print the information of the blocked queue
void print1()
{
cout< cout<<"您现在查看的是阻塞队列的信息"< pcb1 p;
cout<<"进程号 "<<"进程名 "<<"优先级 "<<"状态 "<<"已运行时间 "<<"需运行时间"< for(p=w;p!=NULL;p=p->next)
{
cout<id<<" "<name<<" "<super<<" "<state<<" "<rtime<<" "<ntime< }
}
//check the queue if empty
int empty(pcb1 &r)
{
if(r==NULL)
return 0;
else
return 1;
}
//check the first process of the ready queue if finshed
int check()
{
pcb1 p;
p=s;
if(p->rtime==p->ntime)
{
p->state='F';//if one process finshed then change ti's state
cout< cout<<"进程"<id<<" 已经结束"< return 0;
}
else
return 1;
}
//sort process according to the super of the process and insert to the ready(blocked) queue
void sort(pcb1 &r,pcb1 p)
{
pcb1 p1,p2;
int in=0;
if(r==NULL)//the queue is empty
{
r=p;
}
else
{
if(p->super>=r->super)//the super of the process which wait insert to the queue is highter than the super of the first process of the queue
{
p->next=r;
r=p;
}
else
{
p1=r;
p2=r->next;
if(p2==NULL)//only one process in the queue
{
r->next=p;
}
else
{
while(in==0&&p2!=NULL)//insert to the middle of the queue
{
if(p->super>=p2->super)
{
p->next=p2;
p1->next=p;
in=1;
}
else
{
p1=p1->next;
p2=p2->next;
}
}
}
if(in==0)//link to the last of ready queue
p1->next=p;
}
}
}
//block one process and insert to block queue
void block()
{
if(empty(s))
{
if(s->next==NULL)
{
sort(w,s);
s=s->next;
}
else
{
pcb1 p1;
p1=s;
s=s->next;
p1->next=NULL;
sort(w,p1);
}
}
else
{
cout< cout<<"现在就绪队列已经为空,再没有进程需要阻塞"< }
}
//wake one process of block queue and insert to ready queue
void wake()
{
if(empty(w))
{
pcb1 p1;
p1=w;
w=w->next;
p1->next=NULL;
sort(s,p1);
}
else
{
cout< cout<<"阻塞队列已经为空,没有进程再需要唤醒"< }
}
//runing
void runing()
{
if(empty(s))
{
pcb1 p;
p=s;
if(check())//check the first process of the queue if finished
{//no
s=s->next;
p->rtime++;
p->super--;
p->next=NULL;
sort(s,p);
}
else
{//yes
s=s->next;
}
}
else
{
cout< cout<<"就绪队列已经为空"< }
}
//creat process
void input()
{
pcb1 p2;
p2=new pcb;
cout<<"请输入 进程号、进程名、进程优先级、需要运行时间";
cout< cin>>p2->id>>p2->name>>p2->super>>p2->ntime;
p2->rtime=0;
p2->state='W';
p2->rtime=0;
p2->next=NULL;
sort(s,p2);
}
//main function
void main()
{
char ch;
init(s);
init(w);
cout<<"*****************************进程调度模拟程序开始*******************************"< cout<<"----w/唤醒进程-----r/运行进程-----z/阻塞进程----q/退出程序--"< cout<<"--------c/创建进程---------s /查看就绪进程---------l/查看阻塞队列----"< while(ch!='q')
{
cout<<"请输入一个字符"< cin>>ch;
switch(ch)
{
case 'c':input(); break;
case 'r':runing(); break;
case 's':print(); break;
case 'w':wake(); break;
case 'l':print1(); break;
case 'z':block(); break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -