作业调度之先来先服务算法C语言实现

代码如下

  1. /*
  2.     @author WellsLiu
  3.     @url liuyanzhao.com
  4. */
  5. #include"stdio.h"
  6. #include"stdlib.h"
  7. typedef struct PCB      //定义进程控制块
  8. {   char name[10];   //进程名
  9.     char state;   //运行状态
  10.     int  ArriveTime;  //到达时间
  11.     int  StartTime;  //进程开始时间
  12.     int  FinishTime;  //进程结束时间
  13.     int  ServiceTime;   //服务时间
  14.     float WholeTime;//周转时间
  15.     float WeightWholeTime;//带权周转时间
  16.     double AverageWT_FCFS; //平均周转时间
  17.     double AverageWWT_FCFS;//带权平均周转时间
  18.     struct PCB *next;  //指向下个进程
  19. }pcb;
  20.     double x=0,y=0;
  21.     int i;
  22.         int time;   //计时器
  23.         int n;    //进程个数
  24.     pcb *head=NULL,*p,*q; //进程链表指针
  25. void run_FCFS(pcb *p1) //运行未完成的进程
  26. {
  27.     time = p1->ArriveTime > time? p1->ArriveTime:time;
  28.     p1->StartTime=time;
  29. //  printf("\n时刻:%d,  当前开始运行作业%s\n\n",time,p1->name);
  30.     time+=p1->ServiceTime;
  31.     p1->state='T';
  32.     p1->FinishTime=time;
  33.     p1->WholeTime=p1->FinishTime-p1->ArriveTime;
  34.     p1->WeightWholeTime=p1->WholeTime/p1->ServiceTime;
  35.         x+=p1->WholeTime;
  36.         y+=p1->WeightWholeTime;
  37.         p1->AverageWT_FCFS=p1->WholeTime/n;
  38.     p1->AverageWWT_FCFS=p1->WeightWholeTime/n;
  39.     printf("%4s  %6d  %10d  %10d  %8d  %10.1f  %10.2f   %12.2f %14.2f\n",p1->name,p1->ArriveTime,p1->StartTime,p1->ServiceTime,p1->FinishTime,p1->WholeTime,p1->WeightWholeTime,p1->AverageWT_FCFS,p1->AverageWWT_FCFS);
  40. }
  41. void FCFS() //找到当前未完成的进程
  42. {
  43.       int i;
  44.       p=head;
  45.       printf("\n============================================================================================================\n");
  46.       printf("作业名   到达时间  开始时间    服务时间  完成时间   周转时间   带权周转时间   平均周转时间   平均带权周转时间 \n");
  47.       printf("------------------------------------------------------------------------------------------------------------\n");
  48.       for(i=0;i<n;i++)
  49.       {
  50.            if(p->state=='F')
  51.            {
  52.                 q=p;    //标记当前未完成的进程
  53.                 run_FCFS(q);
  54.            }
  55.            p=p->next;
  56.       }
  57.       printf("\n============================================================================================================\n");
  58. }
  59. void getInfo()          //获得进程信息并创建进程
  60. {
  61.     int num;
  62.     printf("\n进程个数:");
  63.     scanf("%d",&n);
  64.     for(num=0;num<n;num++)
  65.     {
  66.         p=(pcb *)malloc(sizeof(pcb));
  67.         printf("依次输入:进程名 到达时间 服务时间\n");
  68.         scanf("%s\t%d\t%d",&p->name,&p->ArriveTime,&p->ServiceTime);
  69.         if(head==NULL)
  70.         {head=p;q=p;time=p->ArriveTime;}
  71.         if(p->ArriveTime < time) time=p->ArriveTime;
  72.         q->next=p;
  73.         p->StartTime=0;
  74.         p->FinishTime=0;
  75.         p->WholeTime=0;
  76.         p->WeightWholeTime=0;
  77.         p->next=NULL;
  78.         p->state='F';
  79.         q=p;
  80.     }
  81. }
  82. void main()
  83. {   printf("先来先服务FCFS算法模拟\n");
  84.     getInfo();
  85.     p=head;
  86.     FCFS();
  87. }

运行如图

作业调度之先来先服务算法C语言实现

 

本文链接:https://liuyanzhao.com/4694.html

  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
言曌

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: