📄 process.cpp
字号:
// process.cpp : 定义控制台应用程序的入口点。
#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>
#define PROCESS_NUM 5
#define PROCESS_TIME 50
/*
* 进程状态
*/
enum state{
ready,
execute,
block,
finish
};
/*
* 进程控制块
*/
struct pcb{
char pname[4];
int priority;
int cputime;
int needtime;
int count;
int round;
state pstate;
pcb * next;
};
/*
* 创建进程控制块链表
*/
pcb * createProcess(){
pcb *q;
pcb *t;
pcb *p;
int i=0;
cout<<"输入进程名和时间"<<endl;
while (i<PROCESS_NUM){
q=(struct pcb *)malloc(sizeof(pcb));
cin>>q->pname;
cin>>q->needtime;
q->cputime=0;
q->priority=PROCESS_TIME-q->needtime;
q->pstate=ready;
q->next=NULL;
if (i==0){
p=q;
t=q;
}
else{
t->next=q;
t=q;
}
i++;
}
return p;
}
/*
* 显示全部进程状态
*/
void display(pcb *p){
cout<<"进程名"<<" "<<"CPU时间"<<" "<<"所需时间"
<<" "<<"进程优先级"<<" "<<"进程状态"<<endl;
while(p){
cout<<p->pname;
cout<<" ";
cout<<p->cputime;
cout<<" ";
cout<<p->needtime;
cout<<" ";
cout<<p->priority;
cout<<" ";
switch(p->pstate){
case ready:cout<<"就绪态"<<endl;break;
case execute:cout<<"执行态"<<endl;break;
case block:cout<<"阻塞态"<<endl;break;
case finish:cout<<"完成态"<<endl;break;
}
p=p->next;
}
}
/*
* 进程完成
*/
int finishProcess(pcb *q){
int bl=1;
while(bl&&q){
bl=bl&&q->needtime==0;
q=q->next;
}
return bl;
}
/*
* 执行进程
*/
void executeProcess(pcb *q){
pcb *t=q;
int tp=0;
while(q){
if (q->pstate!=finish){
q->pstate=ready;
if(q->needtime==0){
q->pstate=finish;
}
}
if(tp<q->priority&&q->pstate!=finish){
tp=q->priority;
t=q;
}
q=q->next;
}
if(t->needtime!=0){
t->priority-=3;
t->needtime--;
t->pstate=execute;
t->cputime++;
}
}
/*
* 计算进程新的优先级
*/
void calculatePriority(){
pcb * p;
// clrscr();
p=createProcess();
int cpu=0;
// clrscr();
while(!finishProcess(p)){
cpu++;
cout<<"cputime:"<<cpu<<endl;
executeProcess(p);
display(p);
// sleep(2);
// clrscr();
}
printf("所有进行已经处理完毕!");
getch();
}
/*
* 显示主菜单
*/
void displayMenu(){
cout<<"选择进行调度算法:"<<endl;
cout<<"1. 优先级调度算法"<<endl;
cout<<"2. 循环轮转调度算法"<<endl;
cout<<"3. 退出"<<endl;
}
/*
* 得到进程轮回次数
*/
pcb* getProcessRound(){
pcb *q;
pcb *t;
pcb *p;
int i=0;
cout<<"输入进程名和时间"<<endl;
while (i<PROCESS_NUM){
q=(struct pcb *)malloc(sizeof(pcb));
cin>>q->pname;
cin>>q->needtime;
q->cputime=0;
q->round=0;
q->count=0;
q->pstate=ready;
q->next=NULL;
if (i==0){
p=q;
t=q;
}
else{
t->next=q;
t=q;
}
i++;
} //while
return p;
}
/*
* 增加进程轮回
*/
void addProcessRound(pcb *q){
q->cputime+=2;
q->needtime-=2;
if(q->needtime<0) {
q->needtime=0;
}
q->count++;
q->round++;
q->pstate=execute;
}
/*
* 获得下一个进程PCB
*/
pcb * getNextPCB(pcb * k,pcb * head){
pcb * t;
t=k;
do{
t=t->next;
}
while (t && t->pstate==finish);
if(t==NULL){
t=head;
while (t->next!=k && t->pstate==finish){
t=t->next;
}
}
return t;
}
/*
* 设定进程状态
*/
void setProcessState(pcb *p){
while(p){
if (p->needtime==0){
p->pstate=finish;
}
if (p->pstate==execute){
p->pstate=ready;
}
p=p->next;
}
}
/*
* 显示进行轮回
*/
void displayRound(pcb *p){
cout<<"进程名"<<" "<<"CPU时间"<<" "<<"所需时间"
<<" "<<"计数"<<" "<<"轮回次数"<<" "<<"进程状态"<<endl;
while(p){
cout<<p->pname;
cout<<" ";
cout<<p->cputime;
cout<<" ";
cout<<p->needtime;
cout<<" ";
cout<<p->count;
cout<<" ";
cout<<p->round;
cout<<" ";
switch(p->pstate){
case ready:cout<<"ready"<<endl;break;
case execute:cout<<"execute"<<endl;break;
case finish:cout<<"finish"<<endl;break;
}
p=p->next;
}
}
/*
* 计算进程轮回次数
*/
void calculateRound(){
pcb * p;
pcb * r;
// clrscr();
p=getProcessRound();
int cpu=0;
// clrscr();
r=p;
while(!finishProcess(p)){
cpu+=2;
addProcessRound(r);
r=getNextPCB(r,p);
cout<<"cpu "<<cpu<<endl;
displayRound(p);
setProcessState(p);
// sleep(5);
// clrscr();
}
}
/*
* 主程序
*/
void main(){
displayMenu();
int k;
scanf("%d",&k);
switch(k){
case 1:calculatePriority();break;
case 2:calculateRound();break;
case 3:break;
displayMenu();
scanf("%d",&k);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -