?? vb.cpp
字號(hào):
#define N 20
#include <stdio.h>
#include <conio.h>
typedef struct pcb
{
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;
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ù)*/
FILE *f;
/*定義建立就緒隊(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;
else
return 1;
}
PCB *p1,*p2,*p3;
p1=head_run.next;
p2=&head_run;
while(p1!=NULL)
{
p2=p1;
p1=p2->next;
}
p1=p2;
p3=head_input.next;
p2=&head_input;
while(p3!=NULL)
{
if(((unsigned long)p3->arrivetime <=current)&&(p3->state ==R))
{
printf("Time slice is %8d(time%4d);Process%s start,\n",current,(current+500)/1000,p3->pname );
fprintf(f,"Time slice is %8d(time%4d);Process %s stare ,\n",current,(current+500)/1000,p3->pname );
p2->next =p3->next ;
p3->next =p1->next ;
p1->next =p3;
p3=p2;
}
p2=p3;
p3=p3->next ;
}
return 1;
}
int runprocess() /*定義運(yùn)行進(jìn)程函數(shù)*/
{
PCB *p1,*p2;
if (head_run.next ==NULL)
{
current++;
return 1;
}
else
{
p1=head_run.next ;
p2=&head_run;
while (p1!=NULL)
{
p1->runtime --;
current++;
if(p1->runtime <=0)
{
printf("Time slice is %8d time %4d;Process %s end.\n",current,(current+500)/1000,p1->pname );
fprintf(f,"Time slice is %8d time %4d;Process %s end.\n",current,(current+500)/1000,p1->pname );
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;
printf("How many processes do you want to run: ");
fprintf(f,"How many processes do you want to run:");
scanf("%d",&num);
fprintf(f,"%d\n",&num);
pcb_input=new PCB;
p1=pcb_input;
for (int i=0;i<num;i++)
{
printf("No. %3d process input pname: ",i+1);
fprintf(f,"No. %3d process input pname: ",i+1);
scanf("%s",p1->pname );
fprintf(f,"%s\n",p1->pname );
printf(" runtime: ");
fprintf(f," runtime: ");
scanf("%d",&(p1->runtime ));
fprintf(f,"%d\n",&(p1->runtime ));
printf(" arrivetime: ");
fprintf(f," arrivetime: ");
scanf("%d",&(p1->arrivetime ));
fprintf(f,"%d\n",&(p1->arrivetime ));
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;
}
/*------定義建立進(jìn)程函數(shù)------*/
void inputprocess1()
{
PCB *p1,*p2;
int num; /*要建立的進(jìn)程數(shù)*/
unsigned long max=0;
printf("How many processes do you want to run: ");
fprintf(f,"How many processes do you want to run:");
scanf("%d",&num);
fprintf(f,"%d\n",&num);
pcb_input=new PCB;
p1=pcb_input;
for (int i=0;i<num;i++)
{
printf("No. %3d process input pname: ",i+1);
fprintf(f,"No. %3d process input pname: ",i+1);
scanf("%s",p1->pname );
fprintf(f,"%s\n",p1->pname );
printf(" runtime: ");
fprintf(f," runtime: ");
scanf("%d",&(p1->runtime ));
fprintf(f,"%d\n",&(p1->runtime ));
printf(" arrivetime: ");
fprintf(f," arrivetime: ");
scanf("%d",&(p1->arrivetime ));
fprintf(f,"%d\n",&(p1->arrivetime ));
p1->runtime =(p1->runtime )*1000;
p1->arrivetime =(p1->arrivetime )*1000;
p1->state =R;
if(i!=num-1)
{
p1->next =new PCB;
p1=p1->next ;
}
else
{
p1->next =pcb_input;
}
}
p1=pcb_input;
while (p1->next !=pcb_input)
{
printf("process name is %s\n",p1->pname );
fprintf(f,"process name is %s\n",p1->pname );
p1=p1->next ;
}
printf("process name is %s\n",p1->pname );
fprintf(f,"process name is %s\n",p1->pname );
}
void runprocess1() /*定義運(yùn)行進(jìn)程函數(shù)*/
{
pcb *pre,*cur;
if(pcb_input==NULL)
return ;
else
{
cur=pcb_input;
pre=cur->next ;
while(pre->next !=cur) //find the last node in the list
{
pre=pre->next ;
}
while ((cur->runtime >=0)||(cur->next !=cur))
{
if(current<(unsigned long)cur->arrivetime )
{
pre=cur;
cur=cur->next ;
}
else
{
if(current==(unsigned long)cur->arrivetime )
{
printf("Time slice is %8d(time %4d);Process %s start,\n",current,(current+500)/1000,cur->pname );
fprintf(f,"Time slice is %8d(time %4d);Process %s start,\n",current,(current+500)/1000,cur->pname );
}
cur->runtime --;
if(cur->runtime <0)//means the job have ended;
{
printf("Time slice is %8d(time %4d);Process %s start,\n",current,(current+500)/1000,cur->pname );
fprintf(f,"Time slice is %8d(time %4d);Process %s start,\n",current,(current+500)/1000,cur->pname );
if(cur==cur->next ) //delete the last job then break;
{
delete cur;
cur=NULL;
return;
}
else
{
pre->next =cur->next ;
pcb *tmp=cur;
delete tmp;
cur=pre->next ;
}
}
else
{
cur->runtime --;
pre=cur;
cur=cur->next ;
}
}
current++;
}
}
}
//To DO set one cycle link to solve the problem
void main()
{
f=fopen("result.txt","w");
printf("\ntime 1=1000 time slice\n");
fprintf(f,"\ntime 1=1000 time slice\n");
current=0;
inputprocess();
readyprocess();
//inputprocess();
//runprocess();
getch();
fclose(f);
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -