#include"stdio.h"
#include"stdlib.h"
void dayin();
typedef struct PCB //定义进程控制块
{
char num[2]; //进程号
char state; //运行状态
int tijiaotime; //提交进程时间
int starttime; //进程开始时间
int finishtime; //结束时间
int needtime; //运行需要时间
float roundtime; //周转时间
float weightroundtime; //带权周转时间
struct PCB *next; //指向下个进程
}pcb;
int time=10000,n; //计时器 (假设当前时间)
float sumroundtime=0,sumweightroundtime=0,avgroundtime,avgweightroundtime; //定义全局平均变量。
pcb *head=NULL,*p,*q; // 进程全局指针。
void run_fcfs(pcb *p1) //进程执行过程
{
time = p1->tijiaotime > time? p1->tijiaotime:time; // 获得真正的当前时间。
p1->starttime=time;
//printf("\n现在时间是%d,开始运行进程%s\n",time,p1->num);
time+=p1->needtime;
p1->state='F';
p1->finishtime=time;
p1->roundtime=p1->finishtime-p1->tijiaotime;
p1->weightroundtime=(float)(p1->finishtime-p1->tijiaotime)/(float)p1->needtime;
sumroundtime+=p1->roundtime;
sumweightroundtime+=p1->weightroundtime;
}
void run_spf(pcb *p1,int t) //模拟进程执行过程
{
p1->starttime=t;
p1->state='W';
p1->finishtime=t+p1->needtime;
p1->roundtime=p1->finishtime-p1->tijiaotime;
p1->weightroundtime=(p1->finishtime-p1->tijiaotime)/(float)p1->needtime;
sumroundtime+=p1->roundtime;
sumweightroundtime+=p1->weightroundtime;
}
void fcfs() //从队首分别取出进程(找到满足条件的进程,并执行。)
{
int i,j,t;
for(j=0;j<n;j++)
{
p=head;
t=10000;
for(i=0;i<n;i++) //找到当前未完成的最早进程
{
if(p->tijiaotime<t && p->state=='W')
{
t=p->tijiaotime;
q=p; //标记当前未完成的进程
}
p=p->next;
}
run_fcfs(q);
}
avgroundtime=sumroundtime/n;
avgweightroundtime=sumweightroundtime/n;
}
/
void spf() //从队首分别取出进程(找到满足条件的最短进程,并执行。)
{
int i,j,t,k,futmin,flag;
t=10000;///
p=head;
for(i=0;i<n;i++) //找到当前未完成的最早到达进程
{
if(p->tijiaotime<t && p->state=='F')
t=p->tijiaotime;//记录范围内。最早到达进程时间。
p=p->next;
} ///
k=0;
while(k<n)
{
flag=0;
futmin=10000;//最小的一个服务时间。(在范围内总是可以找到最小的一个服务时间。)
p=head;
for(i=0;i<n;i++) //找到当前未完成的进程
{
if(p->tijiaotime<=t &&p->state=='F'&& p->needtime<=futmin)
{
futmin=p->needtime;// 每一次都要修改当前最短时间。
q=p; //标记当前未完成的进程
flag=1;
}
p=p->next;
}
if(flag==1)
{
k++;
run_spf(q,t);
t=t+q->needtime; //t为当前时间。用于筛选进程 下一个进程的当前时间。
}
else
{
t=10000;///
p=head;
for(i=0;i<n;i++) //找到当前未完成的进程
{
if(p->tijiaotime<t&&p->state=='F')
{
t=p->tijiaotime;//记录范围内。最早到达进程时间。
}
p=p->next;
}
}
}
avgroundtime=sumroundtime/n;
avgweightroundtime=sumweightroundtime/n;
}
///
void getInfo() //创建进程
{
int num;
printf("\n请输入进程个数:");
scanf("%d",&n);
printf("\n依次输入%d个进程:\n",n);
dayin();
printf("进程名 到达时间 运行时间\n");
for(num=0;num<n;num++)
{
p=(pcb *)malloc(sizeof(pcb));
if(head==NULL) {head=p;q=p;}
dayin();
scanf("%s\t%d\t%d",&p->num,&p->tijiaotime,&p->needtime);
if(p->tijiaotime < time) time=p->tijiaotime; //记录最早时间。
q->next=p;
p->starttime=0;
p->finishtime=0;
p->roundtime=0;
p->weightroundtime=0;
p->next=NULL;
p->state='W';
q=p;
}
}
void display()
{
printf("\n\n进程名\t\t");
p=head;
while(p!=NULL)
{
printf("%s\t",p->num);
p=p->next;
}
printf("平均");
printf("\n到达时间\t");
p=head;
while(p!=NULL)
{
printf("%d\t",p->tijiaotime);
p=p->next;
}
printf("\n运行时间\t");
p=head;
while(p!=NULL)
{
printf("%d\t",p->needtime);
p=p->next;
}
printf("\n开始运行时间\t");
p=head;
while(p!=NULL)
{
printf("%d\t",p->starttime);
p=p->next;
}
p=head;
printf("\n完成时间\t");
while(p!=NULL)
{
printf("%d\t",p->finishtime);
p=p->next;
}
p=head;
printf("\n周转时间\t");
while(p!=NULL)
{
printf("%0.1f\t",p->roundtime);
p=p->next;
}
printf("%0.1f\n",avgroundtime);
p=head;
printf("带权周转时间\t");
while(p!=NULL)
{
// printf("%0.1f\t",p->roundtime);
printf("%0.1f\t",p->weightroundtime);
p=p->next;
}
printf("%0.1f\n",avgweightroundtime);
dayin();
printf("平均周转时间为%0.1f\n",avgroundtime);
printf("平均带权周转时间为%0.1f\n",avgweightroundtime);
dayin();
}
void dayin()
{
printf("---------------------------------------------------------------------------\n");
}
void main() //mian函数
{
dayin();
printf("\t\t先来先服务优先调度算法和短进程优先调度模拟实现\n\n");
dayin();
getInfo();
printf(" \t\t先来先服务进程优先调度模拟\n");
fcfs();
dayin();
//将总时间置为零
sumroundtime=0,sumweightroundtime=0;
display();
printf(" \t\t短进程优先调度模拟\n");
spf();
display();
}