#include<stdio.h>
#include <stdlib.h>
typedef struct pcb
{
char name[5];
int arrivetime; //到达时间
int runtime; //运行时间
int time;
int finishtime; //完成时间
char state; //状态
struct pcb* next;
}PCB;
PCB *ready = NULL, *p;
void show(PCB* p);
int maxnumber(PCB *p);
void paixu()
{
PCB *first, *second;
int insert = 0;
if ((ready == NULL) || ((p->arrivetime) < (ready->arrivetime)))
{
p->next = ready;
ready = p;
}
else
{
first = ready;
second = first->next;
while (second != NULL)
{
if (p->arrivetime < second->arrivetime)
{
p->next = second;
first->next = p;
insert = 1;
break;
}
else
{
first = first->next;
second = second->next;
}
}
if (insert == 0)
{
first->next = p;
p->next = NULL;
}
}
}
void shuru()
{
int i,j,t1,t2;
printf("\n请输入进程个数:");
scanf("%d",&j);
for(i=1;i<=j;i++)
{ printf("\n第%d个进程\n",i);
p=(PCB*)malloc(sizeof(PCB));
printf("\n输入进程名:");
scanf("%s",p->name);
printf("\n输入进程需运行时间:");
scanf("%d",&t1);
p->runtime=t1;
printf("\n输入进程到达时间:");
scanf("%d",&t2);
p->arrivetime=t2;
p->state='R';
p->next=NULL;
paixu();
}
printf("按到达时间进行排序!\n");
p = ready;
while (p != NULL)
{
show(p);
p = p->next;
}
}
void show(PCB* p) /*建立进程显示函数*/
{
printf("name arrivetime runtime state\n");
printf("%s\t",p->name);
printf("%d\t\t",p->arrivetime);
printf("%d\t",p->runtime);
printf("%c\t\n",p->state);
}
int maxnumber(PCB *p)
{
int max;
max=p->arrivetime;
while(p->next!=NULL)
{
if(max<p->next->arrivetime)
{
max=p->next->arrivetime;
}
p=p->next;
}
return(max);
}
void run()
{
PCB *t,*q,*s;
float sum1=0.0;
int time=0,a,sum=0,n=0;
while(p->next!=NULL)
{
p=p->next;
q=p;
}
printf("进程的调度开始!\n");
p=ready;
s=p;
a=maxnumber(p);
while(p!=NULL)
{
if(p->state=='C')
{
break;
}
while(s!=NULL)
{
n=n+1;
// printf("进程名为%s,调度时间为%d\n",s->name,++a);
s->time=s->runtime;
s=s->next;
}
printf("进程%s运行一次\n",p->name);
p->runtime--;
time=time+1;
p->finishtime=time+a;
if(p->runtime==0)
{
p->state='C';
printf("进程名为%s,其周转时间为 %d\n",p->name,(p->finishtime)-p->arrivetime);
sum+=(p->finishtime)-(p->arrivetime);
printf("进程名为%s的带权周转时间%f\n",p->name,((p->finishtime)-(p->arrivetime))*1.0/(p->time));
sum1+=(time*1.0)/(p->time);
p=p->next;
}
else
{
t=p;
q->next=p;
q=p;
p=t->next;
}
}
printf("平均周转时间为%f\n",sum*1.0/n);
printf("平均带权周转时间%f\n",sum1*1.0/n);
}
void main()
{
shuru();
p = ready;
run() ;
}
#include <stdlib.h>
typedef struct pcb
{
char name[5];
int arrivetime; //到达时间
int runtime; //运行时间
int time;
int finishtime; //完成时间
char state; //状态
struct pcb* next;
}PCB;
PCB *ready = NULL, *p;
void show(PCB* p);
int maxnumber(PCB *p);
void paixu()
{
PCB *first, *second;
int insert = 0;
if ((ready == NULL) || ((p->arrivetime) < (ready->arrivetime)))
{
p->next = ready;
ready = p;
}
else
{
first = ready;
second = first->next;
while (second != NULL)
{
if (p->arrivetime < second->arrivetime)
{
p->next = second;
first->next = p;
insert = 1;
break;
}
else
{
first = first->next;
second = second->next;
}
}
if (insert == 0)
{
first->next = p;
p->next = NULL;
}
}
}
void shuru()
{
int i,j,t1,t2;
printf("\n请输入进程个数:");
scanf("%d",&j);
for(i=1;i<=j;i++)
{ printf("\n第%d个进程\n",i);
p=(PCB*)malloc(sizeof(PCB));
printf("\n输入进程名:");
scanf("%s",p->name);
printf("\n输入进程需运行时间:");
scanf("%d",&t1);
p->runtime=t1;
printf("\n输入进程到达时间:");
scanf("%d",&t2);
p->arrivetime=t2;
p->state='R';
p->next=NULL;
paixu();
}
printf("按到达时间进行排序!\n");
p = ready;
while (p != NULL)
{
show(p);
p = p->next;
}
}
void show(PCB* p) /*建立进程显示函数*/
{
printf("name arrivetime runtime state\n");
printf("%s\t",p->name);
printf("%d\t\t",p->arrivetime);
printf("%d\t",p->runtime);
printf("%c\t\n",p->state);
}
int maxnumber(PCB *p)
{
int max;
max=p->arrivetime;
while(p->next!=NULL)
{
if(max<p->next->arrivetime)
{
max=p->next->arrivetime;
}
p=p->next;
}
return(max);
}
void run()
{
PCB *t,*q,*s;
float sum1=0.0;
int time=0,a,sum=0,n=0;
while(p->next!=NULL)
{
p=p->next;
q=p;
}
printf("进程的调度开始!\n");
p=ready;
s=p;
a=maxnumber(p);
while(p!=NULL)
{
if(p->state=='C')
{
break;
}
while(s!=NULL)
{
n=n+1;
// printf("进程名为%s,调度时间为%d\n",s->name,++a);
s->time=s->runtime;
s=s->next;
}
printf("进程%s运行一次\n",p->name);
p->runtime--;
time=time+1;
p->finishtime=time+a;
if(p->runtime==0)
{
p->state='C';
printf("进程名为%s,其周转时间为 %d\n",p->name,(p->finishtime)-p->arrivetime);
sum+=(p->finishtime)-(p->arrivetime);
printf("进程名为%s的带权周转时间%f\n",p->name,((p->finishtime)-(p->arrivetime))*1.0/(p->time));
sum1+=(time*1.0)/(p->time);
p=p->next;
}
else
{
t=p;
q->next=p;
q=p;
p=t->next;
}
}
printf("平均周转时间为%f\n",sum*1.0/n);
printf("平均带权周转时间%f\n",sum1*1.0/n);
}
void main()
{
shuru();
p = ready;
run() ;
}