C语言模拟实现先来先服务(FCFS)和短作业优先调度算法(SJF)

说明

该并非实现真正的处理机调度,只是通过算法模拟这两种调度算法的过程。
运行过程如下:

  1. 输入进程个数
  2. 输入各个进程的到达事件
  3. 输入各个进程的要求服务事件
  4. 选择一种调度算法
  5. 程序给出调度结果:各进程的完成时间、周转时间、带权周转时间。

运行截图

  • FCFS
    在这里插入图片描述
  • SJF
    在这里插入图片描述

代码如下

#include <stdio.h>
#include <stdlib.h>

#define MAX_DURANCE 1e6

/*
author: Qin Guoqing;

date:2020年11月17日 17点37分;

description: 模拟实现短作业优先和先来先服务两种调度算法。

*/

int count_process;            //进程数
int *coming_times;            //达到时间
int *serve_times;             //服务时间
int *finished_times;          //完成时间
int *turnover_times;          //周转时间
int *waiting_times;           //等待时间
float *turnover_times_weight; //带权周转时间
int method_choosen;           //所选调度算法

void input_process_count(); //输入进程个数
void input_coming_time();   //输入进程到达时间
void input_serve_time();    //输入进程服务时间
void print_inputInfo();     //打印输入信息
void choose_method();       //选择调度算法
void inatialize();          //数据初始化
void SJF();                 //短作业优先
void FCFS();                //先来先服务
void print_schedulInfo();   //打印调度信息
int main()
{
  input_process_count();
  input_coming_time();
  input_serve_time();
  print_inputInfo();
  choose_method();
  inatialize();
  switch (method_choosen)
  {
  case 1:
    FCFS();
    break;

  default:
    SJF();
    break;
  }
  print_schedulInfo();
  system("pause");
  return 0;
}

void input_process_count()
{
  puts("please input the amount of process:");
  scanf("%d", &count_process);
}

void input_coming_time()
{
  coming_times = (int *)malloc(sizeof(int) * count_process);
  puts("please input the coming_time of the processes:");
  for (size_t i = 0; i < count_process; i++)
  {
    scanf("%d", &coming_times[i]);
  }
}

void input_serve_time()
{
  serve_times = (int *)malloc(sizeof(int) * count_process);
  puts("please input the serve_time of the processes:");
  for (size_t i = 0; i < count_process; i++)
  {
    scanf("%d", &serve_times[i]);
  }
}
void print_inputInfo()
{
  puts("###################################################");
  printf("the amount of processes inputed by the user:n = %d\n", count_process);
  puts("here are the coming_times of those processes:");
  for (int i = 0; i < count_process; i++)
  {
    printf("%d ", coming_times[i]);
  }
  printf("\n");
  puts("here are the serve_time of those processes:");
  for (int i = 0; i < count_process; i++)
  {
    printf("%d ", serve_times[i]);
  }
  printf("\n");
}

void choose_method()
{
  puts("please choose a schedul method: 1-FCFS,2-SJF:");
  scanf("%d", &method_choosen);
}

void FCFS()
{
  int current = 0;
  int co_coming_times[count_process];
  for (size_t i = 0; i < count_process; i++)
  {
    co_coming_times[i] = coming_times[i];
  }
  for (size_t j = 0; j < count_process; j++)
  {
    int earliest = 0, min = co_coming_times[0];
    int i;
    //先找到当前最先到达的进程,需要使用到达时间的副本来找
    for (i = 1; i < count_process; i++)
    {
      if (co_coming_times[i] < min)
      {
        min = co_coming_times[i];
        earliest = i;
      }
    }
    co_coming_times[earliest] = MAX_DURANCE;
    //上一个进程执行完时有可能下个进程还没到达
    if (coming_times[earliest] > current)
    {
      current = coming_times[earliest];
    }
    //更新其完成时间
    finished_times[earliest] = current + serve_times[earliest];
    //等待时间
    waiting_times[earliest] = current - coming_times[earliest];
    //更新当前时间
    current += serve_times[earliest];
    //更新周转时间
    turnover_times[earliest] = serve_times[earliest] + waiting_times[earliest];
    //更新带权周转时间
    turnover_times_weight[earliest] = (float)turnover_times[earliest] / (float)serve_times[earliest];
  }
}

void SJF()
{
  int current = 0;
  int co_serve_times[count_process], co_coming_times[count_process];
  //服务时间的副本
  for (size_t i = 0; i < count_process; i++)
  {
    co_serve_times[i] = serve_times[i];
  }
  //到达时间的副本
  for (size_t i = 0; i < count_process; i++)
  {
    co_coming_times[i] = coming_times[i];
  }
  int flag = 0; //标识当前时间下有无已经到达的进程
  for (size_t i = 0; i < count_process; i++)
  {
    int min_p = 0, min = co_serve_times[0], early = co_coming_times[0];
    //min_p: 当前调度进程的xiabiao
    //min: 当前调度进程的服务时间
    //early:当前调度进程的到达时间
    for (size_t j = 1; j < count_process; j++)
    {
      if (co_coming_times[j] <= current && co_serve_times[j] < min)
      {
        flag = 1;
        min = co_serve_times[j];
        min_p = j;
      }
    }
    //若当前时间无进程到达,则选择即将最早到达的那个进程
    if (flag == 0)
    {
      for (size_t m = 1; m < count_process; m++)
      {
        if (co_coming_times[m] < early)
        {
          early = co_coming_times[m];
          min_p = m;
          current = early;
        }
      }
    }
    co_coming_times[min_p] = MAX_DURANCE;
    co_serve_times[min_p] = MAX_DURANCE;
    finished_times[min_p] = current + serve_times[min_p];
    waiting_times[min_p] = current - coming_times[min_p];
    current = finished_times[min_p];

    turnover_times[min_p] = waiting_times[min_p] + serve_times[min_p];
    turnover_times_weight[min_p] = (float)turnover_times[min_p] / (float)serve_times[min_p];
  }
}

void print_schedulInfo()
{
  puts("####################################################################################################");
  puts("here is the information of the schedul:");
  puts("P_name(ID)    arrived_time    served_time    finished_time    turnover_time    turnover_time_weight");
  for (size_t i = 0; i < count_process; i++)
  {
    printf("%10d    %12d    %11d    %14d    %13d    %20f\n", i, coming_times[i], serve_times[i], finished_times[i], turnover_times[i], turnover_times_weight[i]);
  }
  float average_turnover_time, sum_turnover_time = 0;               //平均周转时间和总周转时间
  float average_turnover_time_weight, sum_turnover_time_weight = 0; //平均带权周转时间和总带权周转时间
  for (size_t i = 0; i < count_process; i++)
  {
    sum_turnover_time += turnover_times[i];
    sum_turnover_time_weight += turnover_times_weight[i];
  }
  average_turnover_time = sum_turnover_time / count_process;
  average_turnover_time_weight = sum_turnover_time_weight / count_process;
  printf("the average time of turnover time is: %.2f\n", average_turnover_time);
  printf("the average time of turnover time with weight is: %.2f\n", average_turnover_time_weight);
  puts("####################################################################################################");
}

void inatialize()
{
  finished_times = (int *)malloc(sizeof(int) * count_process);
  turnover_times = (int *)malloc(sizeof(int) * count_process);
  turnover_times_weight = (float *)malloc(sizeof(float) * count_process);
  waiting_times = (int *)malloc(sizeof(int) * count_process);
}
  • 9
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
先来先服务调度算法FCFS)是一种最简单的调度算法,也是最容易实现的一种算法。该算法按照作业提交的先后顺序进行度,即先提交的作业先执行,后提交的作业后执行。该算法的优点是简单易懂,缺点是平均等待时间较长,不适用于长作业作业优先调度算法SJF)是一种根据作业长度来进行度的算法。该算法优先作业长度较作业,以期望达到最优的平均等待时间。该算法的优点是平均等待时间较,缺点是无法预测作业的长度,可能会导致长作业等待时间过长。 以下是用C语言编码实现先来先服务调度算法作业优先调度算法的示例代码: ```c // 先来先服务调度算法 #include <stdio.h> int main() { int n, i, j, wt[20], bt[20], tat[20], avwt = 0, avtat = 0; printf("Enter total number of processes(maximum 20): "); scanf("%d", &n); printf("\nEnter Process Burst Time\n"); for(i = 0; i < n; i++) { printf("P[%d]: ", i + 1); scanf("%d", &bt[i]); } wt[0] = 0; for(i = 1; i < n; i++) { wt[i] = 0; for(j = 0; j < i; j++) wt[i] += bt[j]; } printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time"); for(i = 0; i < n; i++) { tat[i] = bt[i] + wt[i]; avwt += wt[i]; avtat += tat[i]; printf("\nP[%d]\t\t%d\t\t%d\t\t%d", i + 1, bt[i], wt[i], tat[i]); } avwt /= i; avtat /= i; printf("\n\nAverage Waiting Time: %d", avwt); printf("\nAverage Turnaround Time: %d", avtat); return 0; } // 作业优先调度算法 #include <stdio.h> int main() { int n, i, j, pos, temp, wt[20], bt[20], tat[20], avwt = 0, avtat = 0; printf("Enter total number of processes(maximum 20): "); scanf("%d", &n); printf("\nEnter Process Burst Time\n"); for(i = 0; i < n; i++) { printf("P[%d]: ", i + 1); scanf("%d", &bt[i]); } for(i = 0; i < n; i++) { pos = i; for(j = i + 1; j < n; j++) { if(bt[j] < bt[pos]) pos = j; } temp = bt[i]; bt[i] = bt[pos]; bt[pos] = temp; } wt[0] = 0; for(i = 1; i < n; i++) { wt[i] = 0; for(j = 0; j < i; j++) wt[i] += bt[j]; } printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time"); for(i = 0; i < n; i++) { tat[i] = bt[i] + wt[i]; avwt += wt[i]; avtat += tat[i]; printf("\nP[%d]\t\t%d\t\t%d\t\t%d", i + 1, bt[i], wt[i], tat[i]); } avwt /= i; avtat /= i; printf("\n\nAverage Waiting Time: %d", avwt); printf("\nAverage Turnaround Time: %d", avtat); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值