说明

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

运行过程如下:

  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);
}

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. Java 中的 Lambda 表达式

    Lambda表达式 Lambda 表达式是 JDK1.8 的一个新特性,又称特殊的匿名内部类,可以取代大部分的匿名内部类,语法更简洁,可以写出更优雅的 Java 代码,可以极大地优化代码结构. Lam ...

  2. 安装 WSL2、Ubuntu 及 docker(详细步骤)

    本文链接:https://www.cnblogs.com/tujia/p/13438639.html 一.更新Windows版本 WSL 2 随着 Windows build 19041 而推出,能更 ...

  3. C#基础访问修饰符概述

    前言: 在编写面向对象语言时我们时长离不开相关类型和成员的相关访问性,而访问性的关键则是取决于访问修饰符的声明,其作用是用于指定类型或成员的可访问性. 访问修饰符的六种可访问性级别: public:共 ...

  4. Linux下的django项目02

    3.创建user模型 3.1 创建用户模型user 第一步 django-admin startproject syl 第二 在syl下创建apps文件包并标记根源 cd 到apps下并进行以下步骤 ...

  5. Learn day6 模块pickle\json\random\os\zipfile\面对对象(类的封装 操作 __init__)

    1.模块 1.1 pickle模块 # ### pickle 序列化模块 import pickle """ 序列化: 把不能够直接存储的数据变得可存储 反序列化: 把数 ...

  6. error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools“

    python3 是用 VC++ 14 编译的, python27 是 VC++ 9 编译的, 安装 python3 的包需要编译的也是要 VC++ 14 以上支持的. 可以下载安装这个: 链接:htt ...

  7. 11张图和源码带你解析Spring Bean的生命周期,建议收藏~!

    在网上已经有跟多Bean的生命周期的博客,但是很多都是基于比较老的版本了,最近把整个流程画成了一个流程图.待会儿使用流程图,说明以及代码的形式来说明整个声明周期的流程.注意因为代码比较多,这里的流程图 ...

  8. SQL2005中清空操作日志的语句(SQL2008有所不同)

    方法一(我常用的): backup transaction 库名 with no_log go DBCC SHRINKDATABASE(库名) go 在VS中调用语句: string sb = &qu ...

  9. spring处理静态资源方式

    1. <mvc:default-servlet-handler/>default-servlet-handler在SpringMVC上下文定义一个org.springframework.w ...

  10. StringUtil字符串工具-功能扩展

    对java的String类的一些小补充. public class StringUtil { /** 将一个字符串的首字母大写然后返回. @param str 要处理的目标字符串. @return S ...