?? 2.txt
字號:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define null 0
FILE * f;
int flag=1;
/*保存現場部分*/
typedef struct sce{
int midresult;
int times;
}scen;
/*進程控制塊*/
typedef struct pcb_type{
char name[20];
int pid;
int (*fp)();
scen scene;
int priority;
int status;/*4為未結束,5為結束*/
struct pcb_type *next;
}pcb;
/*按優(yōu)先級入隊列*/
pcb *inqueue(pcb *head,pcb *apcb)
{ pcb *p=head;
while(p->next!=null)
{ if(p->next->priority<apcb->priority)
break;
p=p->next;
}/*while*/
if(p->next==null)
p->next=apcb;
else
{
apcb->next=p->next;
p->next=apcb;
}
return head;
}/*inqueue*/
/*出隊列*/
pcb *outqueue(pcb *head)
{
pcb *p=head->next;
head->next=p->next;
p->next=null;
return p;
}/*outqueue*/
/*判斷隊列是否為空*/
int checkqueue(pcb *head)
{
if(head->next==null)
return 0;
else return 1;
}
float Random()
{
srand(time(0));
return random(1000)/1000;
}
/*進程一*/
int process1(int *midresult,int *times)
{ int allTheTimes=3;
int temp=*midresult;
int n=0;
int i=0;
for(;i<allTheTimes-(*times);i++)
{ temp++;
n++;
if(Random()>0.33)/*模擬時間片*/
{
*midresult=temp;
*times=*times+n;
return 4;
}
}
*midresult=temp;
return 5;
}
/*進程二*/
int process2(int *midresult,int *times)
{
int allTheTimes=4;
int temp=*midresult;
int n=0;
int i=0;
for(;i<allTheTimes-(*times);i++)
{
temp++;
n++;
if(Random()<0.33||Random()>0.66)
{
*midresult=temp;
*times=*times+n;
return 4;
}
}
*midresult=temp;
return 5;
}
/*進程三*/
int process3(int *midresult,int *times)
{
int allTheTimes=5;
int temp=*midresult;
int n=0;
int i=0;
for(;i<allTheTimes-(*times);i++)
{
temp++;
n++;
if(Random()<0.66)
{
*midresult=temp;
*times=*times+n;
return 4;
}
}
*midresult=temp;
return 5;
}
/*調度器*/
void schedule(pcb *head1,pcb *head2)
{ pcb *p,*r;
pcb *temp_head;
/*檢驗就緒隊列是否為空*/
if(!checkqueue(head1))
{
/*空則交換兩個優(yōu)先級隊列*/
temp_head=head1;
head1=head2;
head2=temp_head;
/*如果仍為空,則退出*/
if(!checkqueue(head1))
{
fprintf(f,"All processes are complete!");
flag=0;
return;
}
}
/*以下為對隊列的現狀的顯示*/
r=head1;
fprintf(f,"\nhead1 ");
while(r->next!=null)
{ r=r->next;
fprintf(f,"[%s]\t",r->name);
}
fprintf(f,"null\n");
r=head2;
fprintf(f,"head2 ");
while(r->next!=null)
{
r=r->next;
fprintf(f,"[%s]\t",r->name);
}
fprintf(f,"null\n");
/*以上為對隊列的現狀的顯示*/
/*不為空,從高優(yōu)先級隊列隊頭取一個PCB*/
p=outqueue(head1);
/*檢查進程的狀態(tài),恢復現場 */
if(p->status!=5)
{
fprintf(f,"%s",p->name);
fprintf(f," is using CPU\t");
p->status=p->fp(&(p->scene.midresult),&(p->scene.times));
}
/*進程結束,打印結果*/
if(p->status==5)
{ fprintf(f,"%s",p->name);
fprintf(f," is complete,the result is ");
fprintf(f,"%d\n",p->scene.midresult);
/*釋放進程控制塊*/
free(p);
/*結束本次調度*/
return;
}
/*進程未結束,入隊列*/
fprintf(f,"\n%s",p->name);
fprintf(f," is in queue2");
inqueue(head2,p);
/*以下為對隊列的現狀的顯示*/
r=head1;
fprintf(f,"\nhead1 ");
while(r->next!=null)
{ r=r->next;
fprintf(f,"[%s]\t",r->name);
}
fprintf(f,"null\n");
r=head2;
fprintf(f,"head2 ");
while(r->next!=null)
{
r=r->next;
fprintf(f,"[%s]\t",r->name);
}
fprintf(f,"null\n");
/*以上為對隊列的現狀的顯示*/
}/*schedule*/
/*初始化進程控制塊*/
void init_process(pcb *apcb)
{
apcb->scene.midresult=0;
apcb->scene.times=0;
/*Set name*/
printf("Please give a name for the process:");
fflush(stdin);
gets(apcb->name);
/*Set priority*/
printf("Please give ");
printf("%s",apcb->name);
printf(" a priority: ");
fflush(stdin);
scanf("%d",&apcb->priority);
/*Set status*/
apcb->status=4;
apcb->next=null;
}/*init_process*/
/*主函數*/
void main()
{ pcb *r;
pcb *head1=(pcb *)malloc(sizeof(pcb));
pcb *head2=(pcb *)malloc(sizeof(pcb));
pcb *p1=(pcb *)malloc(sizeof(pcb));
pcb *p2=(pcb *)malloc(sizeof(pcb));
pcb *p3=(pcb *)malloc(sizeof(pcb));
f=fopen("a.txt","w");
head1->next=null;
head2->next=null;
p1->fp=process1;
p2->fp=process2;
p3->fp=process3;
init_process(p1);
init_process(p2);
init_process(p3);
head1=inqueue(head1,p1);
head1=inqueue(head1,p2);
head1=inqueue(head1,p3);
while(flag)
{
/*以下為對隊列的現狀的顯示(在schedule函數外)*/
fprintf(f,"/*out side*/");
r=head1;
fprintf(f,"\nhead1 ");
while(r->next!=null)
{ r=r->next;
fprintf(f,"[%s]\t",r->name);
}
fprintf(f,"null\n");
r=head2;
fprintf(f,"head2 ");
while(r->next!=null)
{
r=r->next;
fprintf(f,"[%s]\t",r->name);
}
fprintf(f,"null\n");
fprintf(f,"/*eof*/");
/*以上為對隊列的現狀的顯示(在schedule函數外)*/
schedule(head1,head2);/*調度*/
}
fclose(f);
getch();
}/*main*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -