循环调度的C程序

we are given with the n processes with their corresponding burst time and time quantum and the task is to find the average waiting time and average turnaround time and display the result.

What is Round Robin Scheduling?

Round robin is a CPU scheduling algorithm that is designed especially for time sharing systems. It is more like a FCFS scheduling algorithm with one change that in Round Robin processes are bounded with a quantum time size. A small unit of time is known as Time Quantum or Time Slice. Time quantum can range from 10 to 100 milliseconds. CPU treat ready queue as a circular queue for executing the processes with given time slice. It follows preemptive approach because fixed time are allocated to processes. The only disadvantage of it is overhead of context switching.

What we need to calculate?

Completion Time is the time required by the process to complete its execution

Turnaround Time is the time interval between the submission of a process and its completion.

Turnaround Time = completion of a process – submission of a process

Waiting Time is the difference between turnaround time and burst time

Waiting Time = turnaround time – burst time

Example

We are given with 3 processes P1, P2 and P3 with their corresponding burst time as 24, 3 and 3

Process Burst Time

P124P23P33

Since the time quantum is of 4 milliseconds, process P1 gets the first 4 milliseconds but it requires another 20 millisecond to complete its execution but CPU will preempt it after the first time quantum and CPU will be allocated to the next process P2. As shown in the table, Process P2 requires only 3 milliseconds to complete its execution so CPU will be allocated for time quantum of 3 milliseconds only instead of 4 milliseconds.

循环调度的C程序

Using the Gantt chart, Average waiting time is calculated as given below −

Average waiting time = 17/3 = 5.66 milliseconds

Algorithm

StartStep 1-> In function int turnarroundtime(int processes[], int n, int bt[], int wt[], int tat[])   Loop For i = 0 and i  In function int waitingtime(int processes[], int n, int bt[], int wt[], int quantum)Declare rem_bt[n]   Loop For i = 0 and i < n and i++      Set rem_bt[i] = bt[i]      Set t = 0   Loop While (1)      Set done = true   Loop For i = 0 and i  0 then,         Set done = false      If rem_bt[i] > quantum then,         Set t = t + quantum         Set rem_bt[i] = rem_bt[i] - quantum      Else         Set t = t + rem_bt[i]         Set wt[i] = t - bt[i]         Set rem_bt[i] = 0      If done == true then,   BreakStep 3->In function int findavgTime(int processes[], int n, int bt[], int quantum)   Declare and initialize wt[n], tat[n], total_wt = 0, total_tat = 0   Call function waitingtime(processes, n, bt, wt, quantum)   Call function turnarroundtime(processes, n, bt, wt, tat)   Print "Processes Burst Time Waiting Time turnaround time "   Loop For i=0 and i In function int main()   Delcare and initialize processes[] = { 1, 2, 3}   Declare and initialize n = sizeof processes / sizeof processes[0]   Declare and initialize burst_time[] = {8, 6, 12}   Set quantum = 2   Call function findavgTime(processes, n, burst_time, quantum)

Example

 实例演示

#include // Function to calculate turn around timeint turnarroundtime(int processes[], int n,int bt[], int wt[], int tat[]) {   // calculating turnaround time by adding   // bt[i] + wt[i]   for (int i = 0; i < n ; i++)   tat[i] = bt[i] + wt[i];   return 1;}// Function to find the waiting time for all// processesint waitingtime(int processes[], int n,int bt[], int wt[], int quantum) {   // Make a copy of burst times bt[] to store remaining   // burst times.   int rem_bt[n];   for (int i = 0 ; i < n ; i++)   rem_bt[i] = bt[i];   int t = 0; // Current time   // Keep traversing processes in round robin manner   // until all of them are not done.   while (1) {      bool done = true;      // Traverse all processes one by one repeatedly      for (int i = 0 ; i  0) {            done = false; // There is a pending process            if (rem_bt[i] > quantum) {               // Increase the value of t i.e. shows               // how much time a process has been processed               t += quantum;               // Decrease the burst_time of current process               // by quantum               rem_bt[i] -= quantum;            }            // If burst time is smaller than or equal to            // quantum. Last cycle for this process            else {               // Increase the value of t i.e. shows               // how much time a process has been processed               t = t + rem_bt[i];               // Waiting time is current time minus time               // used by this process               wt[i] = t - bt[i];               // As the process gets fully executed               // make its remaining burst time = 0               rem_bt[i] = 0;            }         }      }      // If all processes are done      if (done == true)         break;   }   return 1;}// Function to calculate average timeint findavgTime(int processes[], int n, int bt[],int quantum) {   int wt[n], tat[n], total_wt = 0, total_tat = 0;   // Function to find waiting time of all processes   waitingtime(processes, n, bt, wt, quantum);   // Function to find turn around time for all processes   turnarroundtime(processes, n, bt, wt, tat);   // Display processes along with all details   printf("Processes Burst Time Waiting Time turnaround time

"); // Calculate total waiting time and total turn // around time for (int i=0; i<n; i++) { total_wt = total_wt + wt[i]; total_tat = total_tat + tat[i]; printf("t%dttt%dttt%dttt%d

",i+1, bt[i], wt[i], tat[i]); } printf("Average waiting time = %f", (float)total_wt / (float)n); printf("

Average turnaround time = %f

", (float)total_tat / (float)n); return 1;}// main functionint main() { // process id's int processes[] = { 1, 2, 3}; int n = sizeof processes / sizeof processes[0]; // Burst time of all processes int burst_time[] = {8, 6, 12}; // Time quantum int quantum = 2; findavgTime(processes, n, burst_time, quantum); return 0;}

输出

循环调度的C程序

以上就是循环调度的C程序的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1445648.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 22:43:10
下一篇 2025年12月17日 22:43:23

相关推荐

  • async函数在循环中的注意事项

    在循环中使用async函数需注意避免并发陷阱、控制执行顺序、处理数据竞争和错误。1. 并发执行可能导致结果不可预测,如数据竞争;2. 顺序执行可通过for…of或reduce实现,确保前一个任务完成后再执行下一个;3. 控制并发数量可使用并发池技术,限制同时运行的任务数;4. 错误处理应…

    2025年12月20日 好文分享
    000
  • C++初学者如何避免编写出无限循环或死循环

    答案是明确循环终止条件并确保循环变量正确更新。编写循环时需设定清晰退出路径,避免因未更新变量或条件判断错误导致无限循环,使用调试输出或计数器辅助验证循环正常结束。 编写C++程序时,初学者很容易因为逻辑不清晰或控制条件设置不当而陷入无限循环。要避免这种情况,关键在于理解循环机制,并养成良好的编程习惯…

    2025年12月18日
    000
  • 范围for循环背后机制 基于迭代器的语法糖实现

    范围for循环是c++++11引入的语法糖,其本质是编译器将for (auto& elem : container)转换为基于std::begin和std::end的迭代器循环,通过引入__range临时变量、获取迭代器并执行传统循环结构来实现,该机制避免了手动编写繁琐的迭代器代码,同时保持…

    2025年12月18日
    000
  • while和do-while有什么区别?while先判断后执行,do-while先执行后判断

    while和do-while的关键区别在于执行顺序:1. while是先判断条件再执行循环体,若条件不满足则可能完全不执行;2. do-while则是先执行循环体再判断条件,即使条件不满足也会至少执行一次。例如,当初始条件为假时,while循环不会运行,而do-while循环会执行一次。适用场景上,…

    2025年12月18日 好文分享
    000
  • C程序将一个文件的内容复制到另一个文件中

    C文件I/O − 创建、打开、读取、写入和关闭文件 C文件管理 文件可用于存储大量持久数据。像许多其他语言一样,’C’提供以下文件管理函数: 创建文件打开文件读取文件向文件写入关闭文件 以下是’C’中最重要的文件管理函数: 函数 目的 fopen ()…

    2025年12月17日
    000
  • C程序按字母顺序排序姓名

    用户必须输入姓名的数量,并且这些姓名需要使用strcpy()函数按字母顺序排序。 字符数组(或字符集合)被称为字符串。 声明 以下是数组的声明: char stringname [size]; 例如,char string[50]; 长度为50个字符的字符串。 初始化 使用单个字符常量 char s…

    2025年12月17日
    000
  • 使用冒泡排序算法对给定的数字列表进行升序排序的C程序

    在 C 编程语言中,冒泡排序是最简单的排序技术,也称为交换排序。 冒泡排序过程 将第一个元素与列表中的其余元素进行比较,如果它们不按顺序进行交换(交换)。 对列表中的其他元素重复相同的操作列表,直到所有元素都已排序。 算法 下面给出的是一种算法,通过使用冒泡排序技术 – 第 1 步 &#…

    2025年12月17日
    000
  • C程序打印带有当前时间的数字时钟

    在本节中,我们将了解如何使用 C 语言制作数字时钟。要处理时间,我们可以使用 time.h 头文件。该头文件有一些函数签名,用于处理日期和时间相关问题。 time.h 的四个重要组成部分如下 size_t 这个 size_t 基本上是无符号整数类型。这是sizeof()的结果。 clock_t用于存…

    2025年12月17日
    000
  • C程序使用rename()函数更改文件名

    rename函数将文件或目录从旧名称更改为新名称。此操作类似于移动操作。因此,我们也可以使用此rename函数来移动文件。 此函数存在于stdio.h库头文件中。 rename函数的语法如下: int rename(const char * oldname, const char * newname…

    2025年12月17日
    000
  • C程序示例,演示fork()和pipe()函数

    在本题中,我们将演示fork()和pipe()。在这里,我们将为 Linux 创建一个 C 程序,该程序将连接两个字符串,使用 2 个进程,其中一个进程将获取输入并将其发送给其他进程,其他进程将字符串与预定义的字符串连接起来并返回连接后的字符串。 第一让回顾一下fork()和pipe() fork(…

    2025年12月17日
    000
  • 不会在按下Ctrl+Z时暂停的C程序

    在编程中,当程序出现故障并在终端编译器中以异常方式运行时,程序员有权利显式停止程序的运行。要显式停止程序,用户必须知道需要按下的正确键盘快捷键。 为了终止代码块的执行,有两种类型的键盘快捷键被使用。 Ctrl+c – 用于停止程序的执行,它需要一些时间来完成输入/输出操作,然后暂停执行。…

    2025年12月17日
    000
  • C程序检查日期是否有效

    给定的日期格式为日期、月份和年份(整数)。任务是确定该日期是否可行。 有效日期范围应为 1/1/1800 – 31/12/9999,超出这些日期的日期无效。 这些日期不仅包含年份范围,还包含与日历日期相关的所有约束。 约束是 – 日期不能是小于 1 且大于 31月份不能小于 1 且大于 …

    2025年12月17日
    000
  • 递归插入排序的C程序

    插入排序是一种排序算法,它是一种基于就地比较的算法。 该算法的工作原理是将元素放置在已排序子数组中的位置,即元素之前的子数组是排序子数组。 算法 Step1 – 从 1 到 n-1 循环并执行 – Step2 .1 – 选择位置 i 处的元素,array[i]。 …

    2025年12月17日
    000
  • 六边形图案的C程序

    我们被给定一个整数’n’,任务是生成六边形图案并显示最终输出。 示例 Input-: n=5Output-: Input-: n = 4Output-: Approach we are using in the given program is as follows − In…

    2025年12月17日
    000
  • 一个使用C程序的谜题

    这里我们将看到一道 C 谜题。假设我们有两个数字 48 和 96。我们必须将第一个数字添加到第二个数字之后。所以最终的结果将是9648。但是我们不能使用任何逻辑、算术、字符串相关的操作,也不能使用任何预定义的函数。那么我们怎样才能做到这一点呢? 这很简单。我们可以通过在 C 中使用 Token Pa…

    2025年12月17日
    000
  • C程序实现欧几里得算法

    问题 实现欧几里得算法来查找两个整数的最大公约数 (GCD) 和最小公倍数 (LCM),并将结果与​​给定整数一起输出。 解决方案 实现欧几里得算法求两个整数的最大公约数 (GCD) 和最小公倍数 (LCM) 的解决方案如下 – 求 GCD 和 LCM 的逻辑如下 – if(…

    2025年12月17日
    000
  • 用动态链表存储汽车信息的C程序

    链接列表使用动态内存分配,即它们相应地增长和收缩。它是节点的集合。 节点有两部分,如下所示 – 数据链接 链表的类型 C 语言中链表的类型如下 – 单链表/单链表列表双链表循环单链表循环双链表 算法 参考下面给出的算法,使用动态链表存储汽车信息。 步骤 1 – 声…

    2025年12月17日
    000
  • C程序以PGM格式写入图像

    pgm 是便携式灰度地图。如果我们想在 c 中将二维数组存储为 png、jpeg 或任何其他图像格式的图像,则在写入文件之前,我们必须做大量工作以某种指定的格式对数据进行编码。 Netpbm 格式提供了一种简单且便携的解决方案。 Netpbm是一个开源的图形程序包,基本上使用在linux或Unix平…

    2025年12月17日
    000
  • Rabin-Karp算法的C程序用于模式搜索

    C 中的模式匹配– 我们必须查找一个字符串是否存在于另一个字符串中,例如,字符串“algorithm”存在于字符串“naive algorithm”中。如果是找到,然后显示它的位置(即它所在的位置)。我们倾向于创建一个接收 2 个字符数组的函数,如果匹配则返回位置否则返回-1。 Inpu…

    2025年12月17日
    000
  • C++程序以给定值为参数,找到双曲正弦反函数的值

    双曲函数是使用双曲线而不是圆定义的,与普通三角函数相当。它从提供的弧度角返回双曲正弦函数中的比率参数。但要做相反的事,或者换句话说。如果我们想根据双曲正弦值计算角度,我们需要像双曲反正弦运算一样的反双曲三角运算。 本课程将演示如何使用 C++ 中的双曲反正弦 (asinh) 函数,使用双曲正弦值(以…

    2025年12月17日
    000

发表回复

登录后才能评论
关注微信