说明

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

运行过程如下:

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

运行截图

  • FCFS

  • SJF

代码如下

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX_DURANCE 1e6
  4. /*
  5. author: Qin Guoqing;
  6. date:2020年11月17日 17点37分;
  7. description: 模拟实现短作业优先和先来先服务两种调度算法。
  8. */
  9. int count_process; //进程数
  10. int *coming_times; //达到时间
  11. int *serve_times; //服务时间
  12. int *finished_times; //完成时间
  13. int *turnover_times; //周转时间
  14. int *waiting_times; //等待时间
  15. float *turnover_times_weight; //带权周转时间
  16. int method_choosen; //所选调度算法
  17. void input_process_count(); //输入进程个数
  18. void input_coming_time(); //输入进程到达时间
  19. void input_serve_time(); //输入进程服务时间
  20. void print_inputInfo(); //打印输入信息
  21. void choose_method(); //选择调度算法
  22. void inatialize(); //数据初始化
  23. void SJF(); //短作业优先
  24. void FCFS(); //先来先服务
  25. void print_schedulInfo(); //打印调度信息
  26. int main()
  27. {
  28. input_process_count();
  29. input_coming_time();
  30. input_serve_time();
  31. print_inputInfo();
  32. choose_method();
  33. inatialize();
  34. switch (method_choosen)
  35. {
  36. case 1:
  37. FCFS();
  38. break;
  39. default:
  40. SJF();
  41. break;
  42. }
  43. print_schedulInfo();
  44. system("pause");
  45. return 0;
  46. }
  47. void input_process_count()
  48. {
  49. puts("please input the amount of process:");
  50. scanf("%d", &count_process);
  51. }
  52. void input_coming_time()
  53. {
  54. coming_times = (int *)malloc(sizeof(int) * count_process);
  55. puts("please input the coming_time of the processes:");
  56. for (size_t i = 0; i < count_process; i++)
  57. {
  58. scanf("%d", &coming_times[i]);
  59. }
  60. }
  61. void input_serve_time()
  62. {
  63. serve_times = (int *)malloc(sizeof(int) * count_process);
  64. puts("please input the serve_time of the processes:");
  65. for (size_t i = 0; i < count_process; i++)
  66. {
  67. scanf("%d", &serve_times[i]);
  68. }
  69. }
  70. void print_inputInfo()
  71. {
  72. puts("###################################################");
  73. printf("the amount of processes inputed by the user:n = %d\n", count_process);
  74. puts("here are the coming_times of those processes:");
  75. for (int i = 0; i < count_process; i++)
  76. {
  77. printf("%d ", coming_times[i]);
  78. }
  79. printf("\n");
  80. puts("here are the serve_time of those processes:");
  81. for (int i = 0; i < count_process; i++)
  82. {
  83. printf("%d ", serve_times[i]);
  84. }
  85. printf("\n");
  86. }
  87. void choose_method()
  88. {
  89. puts("please choose a schedul method: 1-FCFS,2-SJF:");
  90. scanf("%d", &method_choosen);
  91. }
  92. void FCFS()
  93. {
  94. int current = 0;
  95. int co_coming_times[count_process];
  96. for (size_t i = 0; i < count_process; i++)
  97. {
  98. co_coming_times[i] = coming_times[i];
  99. }
  100. for (size_t j = 0; j < count_process; j++)
  101. {
  102. int earliest = 0, min = co_coming_times[0];
  103. int i;
  104. //先找到当前最先到达的进程,需要使用到达时间的副本来找
  105. for (i = 1; i < count_process; i++)
  106. {
  107. if (co_coming_times[i] < min)
  108. {
  109. min = co_coming_times[i];
  110. earliest = i;
  111. }
  112. }
  113. co_coming_times[earliest] = MAX_DURANCE;
  114. //上一个进程执行完时有可能下个进程还没到达
  115. if (coming_times[earliest] > current)
  116. {
  117. current = coming_times[earliest];
  118. }
  119. //更新其完成时间
  120. finished_times[earliest] = current + serve_times[earliest];
  121. //等待时间
  122. waiting_times[earliest] = current - coming_times[earliest];
  123. //更新当前时间
  124. current += serve_times[earliest];
  125. //更新周转时间
  126. turnover_times[earliest] = serve_times[earliest] + waiting_times[earliest];
  127. //更新带权周转时间
  128. turnover_times_weight[earliest] = (float)turnover_times[earliest] / (float)serve_times[earliest];
  129. }
  130. }
  131. void SJF()
  132. {
  133. int current = 0;
  134. int co_serve_times[count_process], co_coming_times[count_process];
  135. //服务时间的副本
  136. for (size_t i = 0; i < count_process; i++)
  137. {
  138. co_serve_times[i] = serve_times[i];
  139. }
  140. //到达时间的副本
  141. for (size_t i = 0; i < count_process; i++)
  142. {
  143. co_coming_times[i] = coming_times[i];
  144. }
  145. int flag = 0; //标识当前时间下有无已经到达的进程
  146. for (size_t i = 0; i < count_process; i++)
  147. {
  148. int min_p = 0, min = co_serve_times[0], early = co_coming_times[0];
  149. //min_p: 当前调度进程的xiabiao
  150. //min: 当前调度进程的服务时间
  151. //early:当前调度进程的到达时间
  152. for (size_t j = 1; j < count_process; j++)
  153. {
  154. if (co_coming_times[j] <= current && co_serve_times[j] < min)
  155. {
  156. flag = 1;
  157. min = co_serve_times[j];
  158. min_p = j;
  159. }
  160. }
  161. //若当前时间无进程到达,则选择即将最早到达的那个进程
  162. if (flag == 0)
  163. {
  164. for (size_t m = 1; m < count_process; m++)
  165. {
  166. if (co_coming_times[m] < early)
  167. {
  168. early = co_coming_times[m];
  169. min_p = m;
  170. current = early;
  171. }
  172. }
  173. }
  174. co_coming_times[min_p] = MAX_DURANCE;
  175. co_serve_times[min_p] = MAX_DURANCE;
  176. finished_times[min_p] = current + serve_times[min_p];
  177. waiting_times[min_p] = current - coming_times[min_p];
  178. current = finished_times[min_p];
  179. turnover_times[min_p] = waiting_times[min_p] + serve_times[min_p];
  180. turnover_times_weight[min_p] = (float)turnover_times[min_p] / (float)serve_times[min_p];
  181. }
  182. }
  183. void print_schedulInfo()
  184. {
  185. puts("####################################################################################################");
  186. puts("here is the information of the schedul:");
  187. puts("P_name(ID) arrived_time served_time finished_time turnover_time turnover_time_weight");
  188. for (size_t i = 0; i < count_process; i++)
  189. {
  190. 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]);
  191. }
  192. float average_turnover_time, sum_turnover_time = 0; //平均周转时间和总周转时间
  193. float average_turnover_time_weight, sum_turnover_time_weight = 0; //平均带权周转时间和总带权周转时间
  194. for (size_t i = 0; i < count_process; i++)
  195. {
  196. sum_turnover_time += turnover_times[i];
  197. sum_turnover_time_weight += turnover_times_weight[i];
  198. }
  199. average_turnover_time = sum_turnover_time / count_process;
  200. average_turnover_time_weight = sum_turnover_time_weight / count_process;
  201. printf("the average time of turnover time is: %.2f\n", average_turnover_time);
  202. printf("the average time of turnover time with weight is: %.2f\n", average_turnover_time_weight);
  203. puts("####################################################################################################");
  204. }
  205. void inatialize()
  206. {
  207. finished_times = (int *)malloc(sizeof(int) * count_process);
  208. turnover_times = (int *)malloc(sizeof(int) * count_process);
  209. turnover_times_weight = (float *)malloc(sizeof(float) * count_process);
  210. waiting_times = (int *)malloc(sizeof(int) * count_process);
  211. }

C语言模拟实现先来先服务(FCFS)和短作业优先(SJF)调度算法的更多相关文章

  1. 【操作系统】先来先服务和短作业优先算法(C语言实现)

    [操作系统] 先来先服务算法和短作业优先算法实现 介绍: 1.先来先服务 (FCFS: first come first service) 如果早就绪的进程排在就绪队列的前面,迟就绪的进程排在就绪队列 ...

  2. 短作业优先调度算法(SJF)

    假设有n项作业位于就绪队列中,这些作业的提交时间用数组requestTimes按照提交时间的先后顺序存储,对应的作业服务时间(持续时间)用数组durations存储.采用SJF算法,计算n项作业的平均 ...

  3. 语言模拟ATM自动取款机系统

    C语言实验报告       题目名称:C语言模拟ATM自动取款机系统 C语言模拟实现ATM自动取款机功能:输入密码,余额查询,取款,存款,转账,修改密码,退出功能: 代码实现的功能: 账号及密码输入: ...

  4. 关于c语言模拟c++的多态

    关于c++多态,个人认为就是父类调用子类的方法,c++多态的实现主要通过虚函数实现,如果类中含有虚函数,就会出现虚函数表,具体c++多态可以参考<深度探索c++对象模型> c语言模拟多态主 ...

  5. c语言模拟c++的继承和多态

    //C++中的继承与多态 struct A { virtual void fun() //C++中的多态:通过虚函数实现 { cout << "A:fun()" < ...

  6. C语言::模拟实现strlen函数

    题目要求 编写一个C语言程序模拟实现strlen函数. 算法 strlen函数功能是计算字符串中字符的个数.(除\0外) 而字符串本身就是一个字符数组,只不过末尾以\0结束. 因此,我们只需遍历除\0 ...

  7. C语言:模拟密码输入显示星号

    一个安全的程序在用户输入密码时不应该显示密码本身,而应该回显星号或者点号,例如······或******,这在网页.PC软件.ATM机.POS机上经常看到.但是C语言没有提供类似的功能,控制台上只能原 ...

  8. Linux套接子(c语言)模拟http请求、应答

    有关套接子和http请求报文的博客在CSDN有很多比如,点这里查看,这里我就不再做过多赘述了,下面我们直接实战,模拟http请求. 要求:浏览器访问本地的localhost,在浏览器页面打印出 Hel ...

  9. c语言模拟实现oc引用计数

    #include<stdio.h> #include<stdlib.h> //在c中引入 引用计数机制 // 要解决的问题:  1,指向某块动态内存的指针有几个? //    ...

随机推荐

  1. 关于 Deployer 部署结构

    Deployer 部署完成后,在服务器上的结构会是这样子: drwxr-sr-x 5 deployer www-data 4096 Jun 14 09:53 ./ drwxr-sr-x 6 deplo ...

  2. Java线程池的四种创建方式

    Java通过Executors提供四种线程池,分别为:newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程. newFix ...

  3. 模型评价指标:AUC

    参考链接:https://www.iteye.com/blog/lps-683-2387643 问题: AUC是什么 AUC能拿来干什么 AUC如何求解(深入理解AUC) AUC是什么 混淆矩阵(Co ...

  4. better-scroll 与 Vue 结合

    什么是 better-scroll better-scroll 是一个移动端滚动的解决方案,它是基于 iscroll 的重写,它和 iscroll 的主要区别在这里.better-scroll 也很强 ...

  5. drf 权限校验设置与源码分析

    权限校验 权限校验和认证校验必须同时使用,并且权限校验是排在认证校验之后的,这在源码中可以查找到其执行顺序. 权限校验也很重要,认证校验可以确保一个用户登录之后才能对接口做操作,而权限校验可以依据这个 ...

  6. python用scrapy模拟用户登录

    scrapy模拟登录 关注公众号"轻松学编程"了解更多. 注意:模拟登陆时,必须保证settings.py里的COOKIES_ENABLED(Cookies中间件) 处于开启状态 ...

  7. Scrapy加Redis加IP代理池实现音乐爬虫

    音乐爬虫 关注公众号"轻松学编程"了解更多. 目的:爬取歌名,歌手,歌词,歌曲url. 一.创建爬虫项目 创建一个文件夹,进入文件夹,打开cmd窗口,输入: scrapy star ...

  8. 常见特征金字塔网络FPN及变体

    好久没有写文章了(对不起我在划水),最近在看北京的租房(真真贵呀). 预告一下,最近无事,根据个人多年的证券操作策略和自己的浅显的AI时间序列的算法知识,还有自己Javascript的现学现卖,在微信 ...

  9. Android Google官方文档(cn)解析之——Intents and Intent filter

    应用程序核心组件中的三个Activity,service,还有broadcast receiver都是通过一个叫做intent的消息激活的.Intent消息传送是在相同或不同的应用程序中的组件之间后运 ...

  10. GAMES101系列笔记一 图形学概述与线性代数入门

    概述+线性代数 为什么学习图形学? Computer Graphics is AWESOME! 主要涉及内容: 光栅化 曲线和网格 光线追踪 动画与模拟 Differences between CG ...