re: 進(jìn)程調(diào)度:先來先服務(wù)算法、時(shí)間片輪轉(zhuǎn)調(diào)度算法
2009-04-20 10:37 |
#include<conio.h>
#include<iostream.h>
#include<fstream.h>
#define N 20
typedef struct pcb/*進(jìn)程控制塊定義*/
{
char pname[N]; /*進(jìn)程名*/
int runtime; /*運(yùn)行時(shí)間*/
int arrivetime; /*到達(dá)時(shí)間*/
char state; /*進(jìn)程狀態(tài)*/
struct pcb *next; /*鏈接指針*/
}PCB;
PCB head_input;//就緒隊(duì)列
PCB head_run;
PCB * pcb_input;
static char R='r',C='c';
unsigned long current; /*記錄系統(tǒng)當(dāng)前時(shí)間的變量*/
void inputprocess(); /*建立進(jìn)程函數(shù)*/
int readyprocess(); /*建立就緒隊(duì)列函數(shù)*/
int readydata(); /*判斷進(jìn)程是否就緒函數(shù)*/
int runprocess(); /*運(yùn)行進(jìn)程函數(shù)*/
ofstream fout;
/*定義建立就緒隊(duì)列函數(shù)*/
int readyprocess()
{
while(1)
{
if(readydata()==0)/*判斷是否就緒函數(shù)*/
return 1;
else
runprocess();/*運(yùn)行進(jìn)程函數(shù)*/
}
}
/*定義判斷就緒隊(duì)列是否有進(jìn)程函數(shù)*/
int readydata()
{
if(head_input.next==NULL)
{
if(head_run.next==NULL)
return 0;//就緒隊(duì)列和運(yùn)行隊(duì)列都沒有進(jìn)程時(shí),返回0,最終程序結(jié)束
else
return 1;//就緒隊(duì)列為空,運(yùn)行隊(duì)列非空,返回上級循環(huán),繼續(xù)運(yùn)行
}
PCB *p1,*p2,*p3;
p1=head_run.next;
p2=&head_run;
while(p1!=NULL)
{
p2=p1;
p1=p2->next;
}
p1=p2;//p1指向run隊(duì)列的最后一個(gè)進(jìn)程
p3=head_input.next;//p3指向就緒隊(duì)列的第一個(gè)進(jìn)程
p2=&head_input;
while(p3!=NULL)
{//遍歷就緒隊(duì)列
if(((unsigned long)p3->arrivetime<=current)&&(p3->state==R))
{//若符合條件(p3所指進(jìn)程的時(shí)間)
cout<<"Time slice is "<<current<<" (time "<<(current+500)/1000<<"); Process "<<p3->pname<<" start,\n";
fout<<"Time slice is "<<current<<" (time "<<(current+500)/1000<<"); Process "<<p3->pname<<" start,\n";
p2->next=p3->next;
//將p3所指進(jìn)程,放到p1所指進(jìn)程之后
p3->next=p1->next;
p1->next=p3;
p3=p2;
}
p2=p3;
p3=p3->next;
}
return 1;
}
/*定義運(yùn)行進(jìn)程函數(shù)*/
int runprocess()
{
PCB *p1,*p2;
if(head_run.next==NULL)
{//若運(yùn)行隊(duì)列為空,時(shí)間+1,跳出函數(shù)
current++;
return 1;
}
else
{
p1=head_run.next;//p1指向運(yùn)行隊(duì)列的第一個(gè)進(jìn)程
p2=&head_run;
while(p1!=NULL)
{//遍歷運(yùn)行隊(duì)列
p1->runtime--;//p1所指進(jìn)程運(yùn)行時(shí)間-1
current++;//時(shí)間+1
if(p1->runtime<=0)
{//p1所指進(jìn)程運(yùn)行時(shí)間=0,輸出并刪除
cout<<"Time slice is "<<current<<" (time "<<(current+500)/1000<<"); Process "<<p1->pname<<" end. \n";
fout<<"Time slice is "<<current<<" (time "<<(current+500)/1000<<"); Process "<<p1->pname<<" end. \n";
p1->state=C;
p2->next=p1->next;
delete p1;
p1=NULL;
}
else
{
p2=p1;
p1=p2->next;
}
}
return 1;
}
}
/*定義建立進(jìn)程函數(shù)*/
void inputprocess()
{
PCB *p1,*p2;
int num; /*要建立的進(jìn)程數(shù)*/
unsigned long max=0;
cout<<"How many processes do you want to run:";
fout<<"How mant processes do you want to run:";
cin>>num;
fout<<num<<endl;
p1=&head_input;
p2=p1;
p1->next=new PCB;
p1=p1->next;
for(int i=0;i<num;i++)
{
cout<<"No."<<i+1<<" process input pname:";
fout<<"No."<<i+1<<" process input pname:";
cin>>p1->pname;
fout<<p1->pname<<endl;
cout<<" runtime:";
fout<<" runtime:";
cin>>p1->runtime;
fout<<p1->runtime<<endl;
cout<<" arrivetime:";
fout<<" arrivetime:";
cin>>p1->arrivetime;
fout<<p1->arrivetime<<endl;
p1->runtime=(p1->runtime)*1000;
p1->arrivetime=(p1->arrivetime)*1000;
p1->state=R;
if((unsigned long)(p1->arrivetime)>max)
max=p1->arrivetime;
p1->next=new PCB;
p2=p1;
p1=p1->next;
}
delete p1;
p1=NULL;
p2->next=NULL;
}
void main()
{
fout.open("result.txt");
cout<<"\ntime 1=1000 time slice\n";
fout<<"\ntime 1=1000 time slice\n";
current=0;
inputprocess();
readyprocess();
cout<<flush;
getch();
fout.close();
}
回復(fù) 更多評論