先来先服务调度算法模拟进程

源代码:

#include<stdio.h>

#include<stdlib.h>

//定义进程控制块
typedef struct PCB
{
char         ID[3]; //进程号
char         name[10];         //进程名
char         state; //运行状态
int arrivetime; //到达时间
int starttime;         //进程开始时间
int finishtime;         //进程结束时间
int servicetime;         //服务时间
float turnaroundtime;         //周转时间
float weightedturnaroundtime; //带权周转时间
struct PCB *next; //指向下个进程
}pcb;

int time; //计时器
int n; //进程个数
char s;

pcb *head=NULL,*p,*q; //进程链表指针

void run_fcfs(pcb *p1) //运行未完成的进程
{
time=p1->arrivetime>time?p1->arrivetime:time;
p1->starttime=time;
printf("\n现在时间是%d,开始运行作业%s\n",time,p1->name);
time+=p1->servicetime;
p1->state='T';
p1->finishtime=time;
p1->turnaroundtime=p1->finishtime-p1->arrivetime;
p1->weightedturnaroundtime=p1->turnaroundtime/p1->servicetime;
printf("ID 到达时间 开始时间 服务时间 完成时间 周转时间 带权周转时间 \n");
printf("%s%6d%10d%10d%8d%10.1f%10.2f\n",p1->ID,p1->arrivetime,
p1->starttime,p1->servicetime,p1->finishtime,p1->turnaroundtime,p1->weightedturnaroundtime);
}

void fcfs() //找到当前未完成的进程
{
int i,j;
p=head;
for(i=0;i<n;i++)
{
if(p->state=='F')
{
q=p; //标记当前未完成的进程
run_fcfs(q);
}
p=p->next;
}
}

void getInfo() //获取进程信息并创建进程
{
int num;
printf("\n作业个数:");
scanf("%d",&n);
for(num=0;num<n;num++)
{
p=(pcb *)malloc(sizeof(pcb));
printf("依次输入: \nID 进程名 到达时间 服务时间\n");
scanf("%s\t%s\t%d\t%d",&p->ID,&p->name,&p->arrivetime,&p->servicetime);
if(head==NULL) {head=p;q=p;time=p->arrivetime;}
if(p->arrivetime<time) time=p->arrivetime;
q->next=p;
p->starttime=0;
p->finishtime=0;
p->turnaroundtime=0;
p->weightedturnaroundtime=0;
p->next=NULL;
p->state='F';
q=p;
}
}

void main()
{
printf("先来先服务模拟进程调度算法");
getInfo();
p=head;
fcfs();

}

测试结果:





猜你喜欢

转载自blog.csdn.net/weixin_38694789/article/details/80386067