www.pudn.com > PCB.rar > PCB.c
/* HELLO.C -- Hello, world */
#include "stdio.h"
#define getpcb(type)(type*)malloc(sizeof(type))
#define NULL 0
struct pcb{char name[10];
char state;
int super;
int ntime;
int rtime;
struct pcb *link;
}*ready=NULL,*p;
typedef struct pcb PCB;
main()
{ int len,h=0;char c ;
input();
len=space();
while((len!=0)&&(ready!=NULL))
{c=getchar();
h++;printf("\n the execute number :%d \n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n press any key to continue...");
c=getchar();
}
printf("\n\n the process has been finished. \n");
c=getchar();
}
input()
{int i,num;char k;
clrscr();
printf("\n input process number?");
scanf("%d",&num);
for(i=0;iname);
printf("\n the super of process:");
scanf("%d",&p->super);
printf("\n the time need:");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;
p->state='w';
p->link=NULL;
sort();
}
}
sort()
{
PCB *fir,*sec;
int ins=0;
if((ready==NULL)||((p->super)>(ready->super)))
{p->link=ready;
ready=p;
}
else{fir=ready;
sec=fir->link;
while(sec!=NULL)
{
if((p->super)>(sec->super))
{p->link=sec;
fir->link=p;
sec=NULL;
ins=1;
}
else{fir=fir->link;
sec=sec->link;
}
}
if(ins==0)fir->link=p;
}
}
int space()
{int l=0; PCB *pr=ready;
while(pr!=NULL)
{l++;
pr=pr->link;
}
return(l);
}
check()
{
PCB *pr;
printf("\n ****Now the process running is:%s",p->name);
disp(p);
pr=ready;
printf("\n ****Now the ready queue is :\n");
while(pr!=NULL)
{disp(pr);
pr=pr->link;
}
}
disp(PCB *pr)
{ printf("\nqname\tstate\tsuper\tndtime\truntime\n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->super);
printf("|%d\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
running()
{ (p->rtime)++;
if(p->rtime==p->ntime)
destroy();
else{
(p->super)--;
p->state='w';
sort();
}
}
destroy()
{
printf("\n process [%s]has finished.\n",p->name);
free(p);
}