?? 進程調度.cpp
字號:
//THIS IS THE PRIORITY ALGORITHM
//display the processes manager
//write by Li Xin
//June 23,2006
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#define P_NUM 3
#define P_PRI 40
enum state{ //枚舉類型,進程的三種狀態
ready,
working,
finish
};
struct pcb{ //結構體,pcb的結構
char name[4];
int priority;
int cputime;
int needtime;
state process;
pcb * next;
};
pcb * get_process(){ //初始化進程
pcb *q;
pcb *t;
pcb *p;
int i=0;
cout<<"input ProcessNames and ProcessTime"<<endl;
while (i<P_NUM){
q=(struct pcb *)malloc(sizeof(pcb));
cin>>q->name;
cin>>q->needtime;
q->cputime=0;
q->priority=P_PRI-q->needtime;
q->process=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<<"name"<<" "<<"cputime"<<" "<<"needtime"<<" "<<"priority"<<" "<<"state"<<endl;
while(p){
cout<<p->name;
cout<<" ";
cout<<p->cputime;
cout<<" ";
cout<<p->needtime;
cout<<" ";
cout<<p->priority;
cout<<" ";
switch(p->process){
case ready:cout<<"ready"<<endl;break;
case working:cout<<"working"<<endl;break;
case finish:cout<<"finish"<<endl;break;
}
p=p->next;
}
cout<<"_______________________________________________________"<<endl;
}
int process_finish(pcb *q){ //判斷進程是否完成
int b=1;
while(b&&q){
b=(b&&q->needtime==0);
q=q->next;
}
return b;
}
void cpuexe(pcb *q){ //進程一次的運行
pcb *t=q;
int c=0;
while(q){
if (q->process!=finish){
q->process=ready;
if(q->needtime==0){
q->process=finish;
}
}
if(c<q->priority&&q->process!=finish){
c=q->priority;
t=q;
}
q=q->next;
}
if(t->needtime!=0){
t->priority-=3;
t->needtime--;
t->process=working;
t->cputime++;
}
}
void priority_cal(){ //優先數程序
pcb * p;
p=get_process();
int cpu=0;
while(!process_finish(p)){
cpu++;
cout<<"cputime:"<<cpu<<endl;
cpuexe(p);
display(p);
}
printf("All Processes Have Finished!");
}
void display_menu(){ //顯示菜單
cout<<"*****************THIS IS THE PRIORITY ALGORITHM******************"<<endl;
cout<<" ONLY DISPALY 3 PROCESSes! "<<endl;
cout<<" 1 : ENTER 2 : EXIT "<<endl;
cout<<"*****************************************************************"<<endl;
}
void main(){
display_menu();
int a;
cin>>a;
switch(a){
case 1:priority_cal();break;
case 2:break;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -