#include <stdio.h>
#include <malloc.h>
typedef struct JINCHEN// define struct body
{
Char name[5];
int Tijiaoshijian;
int reachtime;
int runtime;
int beirun;
int accomplishtime;
Char state;
int flag;
struct Jinchen *next;
}PCB;
PCB *finish,*ready;// define the queue One to complete a ready
int time=0;
int t=0;
int num;
void INITPCB ()// process initialization
{
int i,j=0;
PCB *p,*q,*l,*m,*k,*n;
printf (" Please enter the number of processes :");
scanf ("%d", &num);
ready= (PCB *) malloc (sizeof (PCB));
ready->next=null;
Finish= (PCB *) malloc (sizeof (PCB));
finish->next=null;
L= (PCB *) malloc (sizeof (PCB));
l->next=null;
P=l;
for (i=0;i<num;i++)
{
q= (PCB *) malloc (sizeof (PCB));
printf ("\ n Process number %d:\n", i);
printf (" Please enter the process name:");
scanf ("%s", q->name);
q->reachtime=0;
printf (" Please enter the submission time:");
scanf ("%d",& (Q->tijiaoshijian));
printf (" Please enter run time:");
scanf ("%d",& (q->runtime));
q->beirun=q->runtime;
Q->state= ' R ';
q->flag=0;
if (i!=0)// link Ready process
{
q->next=null;
q->next=p->next;
p->next=q;
p=p->next;
}
else//The first process is plugged into a ready queue
{
p->next=q;
p=p->next;
p->next=null;
}
}
p=l->next;
K=ready;
for (i=0;i<num;i++)// Sort by submission time
{
q=l->next;
t=1000;
while (Q!=null)
{
if (q->tijiaoshijian<t&&q->flag==0)
{
t=q->tijiaoshijian;
m= (PCB *) malloc (sizeof (PCB));
m->next=null;
m->accomplishtime=q->accomplishtime;
m->beirun=q->beirun;
m->flag=q->flag;
for (j=0;j<5;j++)
m->name[j]=q->name[j];
m->reachtime=q->reachtime;
m->runtime=q->runtime;
m->state=q->state;
m->tijiaoshijian=q->tijiaoshijian;
n=q;
}
q=q->next;
}
p=p->next;
n->flag=1;
m->flag=1;
k->next=m;
k=k->next;
}
k->next=null;
}
void display ()// function: Calculate and display related outputs
{
PCB *p;
int m,n=0,k=0,l;
p=finish->next;
while (P!=null)
{
printf (" process name :%s", p->name);
m=p->accomplishtime-p->tijiaoshijian;
printf (" turnaround time :%d", m);
l=m/(P->beirun);
printf ("With right turnaround time:%d", l);
printf ("\ n");
N=m+n;
K=l+k;
p=p->next;
}
printf (" average turnaround time :%d\n", n/num);
printf (" average turnaround time with right:%d\n", k/num);
}
void use ()// process call
{
Int J;
PCB *p=ready->next,*k=ready,*q,*n=finish;
while (K->next!=null)
{
p=ready->next;
for (j=0;j<num&&p!=null;j++)
{
time++;
printf ("%d\n\n", ++t);
if (p->reachtime==0)
p->reachtime=time;
if (p->state!= ' C ')
{
printf (" running :%s\n", p->name);
p->runtime--;
if (p->runtime!=0)// process is not finished running, put the queue last
{
Q=p;
while (Q->next!=null)
q=q->next;
ready->next=p->next;
q->next=p;
p=ready->next;
q=q->next;
q->next=null;
}
else// process finished running, put into completion queue
{
P->state= ' C ';
printf ("%s process complete \ n", p->name);
p->accomplishtime=time;
n->next=p;
N=p;
ready->next=p->next;
p->next=null;
}
}
}
}
}
int main ()
{
PCB *p;
INITPCB ();
Use ();
Display ();
p=finish->next;
printf (" run complete !* Description: The completed process has been put into the completion queue, the completed process is not running at the end of the queue ");
}
Experiment two: First come first service process scheduling