循环调度的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月8日 07:37:35

相关推荐

  • 使用冒泡排序算法对给定的数字列表进行升序排序的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
  • C程序使用结构体存储库存系统

    结构是不同数据类型变量的集合,以单一名称分组在一起。 结构的特征 C 语言中结构的特征编程语言如下 – 可以通过使用赋值将不同数据类型的所有结构元素的内容复制到其类型的另一个结构变量 为了处理复杂的数据类型,最好在另一个结构中创建结构,这称为嵌套结构。 可以将整个结构、结构的各个元素和结…

    2025年12月17日
    000
  • C程序打印“偶数”或“奇数”,不使用条件语句

    在本节中,我们将看到如何在不使用任何条件语句(如,>=,==)的情况下检查一个数是奇数还是偶数。 我们可以通过使用条件语句轻松地检查奇数还是偶数。我们可以将数字除以2,然后检查余数是否为0。如果为0,则是偶数。否则,我们可以将数字与1进行AND运算。如果答案为0,则是偶数,否则为奇数。 这里不…

    2025年12月17日
    000
  • C++程序来检查一个字符是否为字母或非字母

    在解决一些逻辑编程问题时,使用字符串或字符有时非常有用。字符串是字符的集合,字符是 1 字节数据类型,用于保存 ASCII 值中的符号。符号可以是英文字母、数字或特殊字符。在本文中,我们将学习如何使用 C++ 检查一个字符是否是英文字母或字母表中的字母。 检查 isalpha() 函数 要检查数字是…

    2025年12月17日
    000
  • C程序乘以两个浮点数?

    Float是“浮点数”的缩写。按照定义,它是编译器内置的基本数据类型,用于定义具有浮动小数点的数值。浮点类型变量是可以保存实数的变量,例如4320.0、-3.33或0.01226。浮点数名称中的浮点部分指的是小数点可以“浮动”,即可以支持小数点前后可变数量的数字。 浮点数 类别 类型 最小大小 典型…

    2025年12月17日
    000
  • 寻找给定列表的中位数的C程序

    如果列表中的元素按顺序排列,则将列表中的元素分成两部分且两边元素数量相等的中间值称为中位数。 元素个数为奇数只有一个中间值;而;偶数个项目有两个中间值。 因此,偶数个项目的中位数被指定为两个中间值的平均值。 算法 请参考下面给出的算法来计算中位数。 步骤 1 – 将项目读入数组,同时保留…

    2025年12月17日
    000
  • 将以下内容翻译为中文:使用递归在C程序中将二进制转换为格雷码

    二进制数是只有两位 0 和 1 的数字。 格雷码是一种特殊类型的二进制数,其属性是代码的两个连续数字 em> 的差异不能超过一位。格雷码的这一特性使其在 K-map、纠错、通信等方面更加有用。 这使得二进制到格雷码的转换成为必要。那么,让我们看一下将二进制转换为格雷码的算法使用递归。 示例 让…

    2025年12月17日
    000

发表回复

登录后才能评论
关注微信