《Operating Systems》-Experimental instruction-Experiment 3 FCFS algorithm
《Operating Systems》-Experimental instruction-Experiment 3 FCFS algorithm
/**********函数声明**********/
//先到先服务算法
void fcfs()
{
if(now->t_arrive>TIME)
{
printf("[Time: %d]\t No process running \n",TIME);
return;
}
if(now->state=='N')
{
now->state='R';
now->t_start=TIME;
printf("[Time: %d]\t Process:%s First running \n",TIME,now->name);
}
else if(now->state=='R')
{
(now->t_run)++;
if(now->t_run>=now->t_service)
{
now->state='F';
now->t_finish=TIME;
printf("[Time: %d]\t Process:%s Task accomplished \n",TIME,now->name);
now=now->next;
if(now!=NULL) fcfs();
}
else printf("[Time: %d]\t Process : %s Running, Running time: %d\n",TIME,now-
>name,now->t_run);
}
}
void result()
{
pcb *p=head;
printf("\n=========Running results=========\n\n");
printf("ProcessName Priority ArrivalTime StartTime FinishingTime ServiceTime
TurnaroundTime TurnaroundTimeWithRights\n");
while(p!=NULL)
{
printf(" %s\t %d\t %d\t %d\t %d\t %d\t %d\t %.2f\n",p-
>name,p->priority,p->t_arrive,
p->t_start,p->t_finish,p->t_service,p->t_finish-p->t_arrive,
1.0*(p->t_finish-p->t_arrive)/p->t_service);
p=p->next;
}
}
void timer()
{
fcfs();
if(now==NULL) return;
TIME++;
Sleep(DELAY);
timer();
}
void init()
{
pcb *p,*q;
unsigned short i;
printf("Job scheduling algorithm: First come, first served (FCFS)\n ");
//printf("\n1:2: Short process first (SPF)\n3:High priority ratio 4:Time slice circular
scheduling\n");
//printf("Input simulation type: [ ]\b\b");
//scanf("%c",&TYPE);
printf("Input number of processes:\n");
scanf("%d",&NUM);
for(i=0; i<NUM; i++)
{
p=(pcb *)malloc(sizeof(pcb));
printf("[No %d] Input: Name Priority ArrivalTime ServiceTime\n",i+1);
scanf("%s\t%d\t%d\t %d",&p->name,&p->priority,&p->t_arrive,&p->t_service);
if(head==NULL)
{
head=p;
q=p;
}
q->next=p;
p->t_start=0;
p->t_finish=0;
p->t_run=0;
p->t_wait=0;
p->next=NULL;
p->state='N';
q=p;
}
}
//按到达时间冒泡排序
pcb* sort_pcb(pcb *h_head)
{
pcb *p,*p1,*p2,*p3;
pcb h, t;
if (h_head == NULL) return NULL;
h.next=h_head;
p=&h;
while (p->next!=NULL)
{
p=p->next;
}
p=p->next=&t;
while (p!=h.next)
{
p3=&h;
p1=p3->next;
p2=p1->next;
while (p2!=p)
{
if ((p1->t_arrive)>(p2->t_arrive))
{
p1->next=p2->next;
p2->next=p1;
p3->next=p2;
p3=p2;
p2=p1->next;
}
else
{
p3=p1;
p1=p2;
p2=p2->next;
}
}
p=p1;
}
while (p->next!=&t)
{
p=p->next;
}
p->next=NULL;
return h.next;
}
//void main()
int main()
{
init();
system("CLS");
head=sort_pcb(head);
now=head;
printf("The processes are running in simulation……\n");
timer();
result();
//return 0;
}