目录
示例
算法
作业
首页 后端开发 C++ 循环调度的C程序

循环调度的C程序

Sep 25, 2023 pm 05:09 PM
c程序 循环 调度

给定n个进程及其相应的突发时间和时间量,任务是找到平均等待时间和平均周转时间并显示结果。

什么是循环调度?

循环是专为分时系统设计的CPU调度算法。它更像是 FCFS 调度算法,有一个变化,即循环进程受量子时间大小的限制。一个小的时间单位被称为时间量子或时间片。时间量的范围可以是 10 到 100 毫秒。 CPU将就绪队列视为循环队列,以给定的时间片执行进程。它遵循抢占式方法,因为固定时间被分配给进程。它唯一的缺点是上下文切换的开销。

我们需要计算什么?

完成时间是进程完成其执行所需的时间

周转时间是之间的时间间隔流程的提交及其完成。

周转时间 = 流程完成 – 流程提交

等待时间是周转时间和突发时间之间的差值

等待时间 = 周转时间 – 突发时间

示例

我们有 3 个进程 P1、P2 和 P3,它们对应的突发时间为 24、3 和 3

进程 突发时间
P1 24
P2 3
P3 3

由于时间片是4毫秒,进程P1获得前4毫秒,但它需要另外20毫秒来完成其执行,但CPU会在第一个时间片后抢占它, CPU将被分配给下一个进程P2。如表所示,进程 P2 只需 3 毫秒即可完成执行,因此 CPU 将仅分配 3 毫秒的时间量,而不是 4 毫秒。

循环调度的C程序

使用甘特图,计算平均等待时间如下下面 -

平均等待时间 = 17/3 = 5.66 毫秒

算法

Start
Step 1-> In function int turnarroundtime(int processes[], int n, int bt[], int wt[], int tat[])
   Loop For i = 0 and i < n and i++
      Set tat[i] = bt[i] + wt[i]
   return 1
Step 2-> 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 < n and i++
      If rem_bt[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,
   Break
Step 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<n and i++
   Set total_wt = total_wt + wt[i]
   Set total_tat = total_tat + tat[i]
   Print the value i+1, bt[i], wt[i], tat[i]
   Print "Average waiting time = total_wt / n
   Print "Average turnaround time =total_tat / n
Step 4-> 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)
登录后复制

示例

 实例练习

#include <stdio.h>
// Function to calculate turn around time
int 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
// processes
int 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 < n; i++) {
         // If burst time of a process is greater than 0
         // then only need to process further
         if (rem_bt[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 time
int 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</p><p>");
   // 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%d\t\t\t%d\t\t\t%d\t\t\t%d</p><p>",i+1, bt[i], wt[i], tat[i]);
   }
   printf("Average waiting time = %f", (float)total_wt / (float)n);
   printf("</p><p>Average turnaround time = %f</p><p>", (float)total_tat / (float)n);
   return 1;
}
// main function
int main() {
   // process id&#39;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程序的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

C++程序以给定值为参数,找到双曲正弦反函数的值 C++程序以给定值为参数,找到双曲正弦反函数的值 Sep 17, 2023 am 10:49 AM

双曲函数是使用双曲线而不是圆定义的,与普通三角函数相当。它从提供的弧度角返回双曲正弦函数中的比率参数。但要做相反的事,或者换句话说。如果我们想根据双曲正弦值计算角度,我们需要像双曲反正弦运算一样的反双曲三角运算。本课程将演示如何使用C++中的双曲反正弦(asinh)函数,使用双曲正弦值(以弧度为单位)计算角度。双曲反正弦运算遵循以下公式-$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})},其中\:In\:是\:自然对数\:(log_e\:k)

如何在 macOS 中禁用'单击桌面显示”功能 如何在 macOS 中禁用'单击桌面显示”功能 Nov 23, 2023 pm 02:31 PM

默认情况下,macOSSonoma会在您单击桌面壁纸时隐藏所有活动窗口。如果您倾向于在桌面上有一堆需要访问的文件,这将很方便。但是,如果您发现这种行为令人抓狂,则有一种方法可以将其关闭。Apple最新的macOSSonomaMac操作系统有一个新选项,称为“单击壁纸以显示桌面”。默认情况下启用,如果您倾向于打开多个窗口,并且想要访问桌面上的文件或文件夹,而不必最小化或移动窗口,则该选项可能特别有用。启用该功能并单击桌面墙纸后,所有打开的窗口都会暂时被扫到一边,从而直接访问桌面。完成后,您可以再次

C程序使用rename()函数更改文件名 C程序使用rename()函数更改文件名 Sep 21, 2023 pm 10:01 PM

rename函数将文件或目录从旧名称更改为新名称。此操作类似于移动操作。因此,我们也可以使用此rename函数来移动文件。此函数存在于stdio.h库头文件中。rename函数的语法如下:intrename(constchar*oldname,constchar*newname);rename()函数的功能它接受两个参数。一个是oldname,另一个是newname。这两个参数都是指向常量字符的指针,用于定义文件的旧名称和新名称。如果文件重命名成功,则返回零;否则,返回非零整数。在重命名操作期间

C++程序打印字典 C++程序打印字典 Sep 11, 2023 am 10:33 AM

映射是C++中的一种特殊类型的容器,其中每个元素都是一对两个值,即键值和映射值。键值用于索引每个项目,映射值是与键关联的值。无论映射值是否唯一,键始终是唯一的。要在C++中打印映射元素,我们必须使用迭代器。一组项目中的一个元素由迭代器对象指示。迭代器主要与数组和其他类型的容器(例如向量)一起使用,并且它们具有一组特定的操作,可用于识别特定范围内的特定元素。可以增加或减少迭代器来引用范围或容器中存在的不同元素。迭代器指向范围内特定元素的内存位置。使用迭代器在C++中打印地图首先,我们看一下如何定义

写一个C程序,使用strncmp库函数来比较两个字符串 写一个C程序,使用strncmp库函数来比较两个字符串 Sep 09, 2023 pm 01:17 PM

Strncmp是一个预定义的库函数,存在于string.h文件中,它用于比较两个字符串并显示哪个字符串更大。strcmp函数(字符串比较)此函数比较两个字符串。它返回两个字符串中第一个不匹配字符的ASCII差异。语法intstrcmp(string1,string2);如果差异等于零,则string1=string2。如果差异为正,则string1>string2。如果差异为负,则string1<string2。示例strncmp函数此函数用于比较两个字符串的前n个字符。语法strn

C++程序来检查一个字符是否为字母或非字母 C++程序来检查一个字符是否为字母或非字母 Sep 14, 2023 pm 03:37 PM

在解决一些逻辑编程问题时,使用字符串或字符有时非常有用。字符串是字符的集合,字符是1字节数据类型,用于保存ASCII值中的符号。符号可以是英文字母、数字或特殊字符。在本文中,我们将学习如何使用C++检查一个字符是否是英文字母或字母表中的字母。检查isalpha()函数要检查数字是否是字母,我们可以使用ctype.h头文件中的isalpha()函数。这将一个字符作为输入,如果是字母表,则返回true,否则返回false。让我们看看下面的C++实现来了解这个函数的用法。Example的中文翻译为:示

lambda表达式跳出循环 lambda表达式跳出循环 Feb 20, 2024 am 08:47 AM

lambda表达式跳出循环,需要具体代码示例在编程中,循环结构是经常使用的一种重要语法。然而,在特定的情况下,我们可能希望在循环体内满足某个条件时,跳出整个循环,而不是仅仅终止当前的循环迭代。在这个时候,lambda表达式的特性可以帮助我们实现跳出循环的目标。lambda表达式是一种匿名函数的声明方式,它可以在内部定义简单的函数逻辑。它与普通的函数声明不同,

C程序实现欧几里得算法 C程序实现欧几里得算法 Sep 17, 2023 pm 12:41 PM

问题实现欧几里得算法来查找两个整数的最大公约数(GCD)和最小公倍数(LCM),并将结果与​​给定整数一起输出。解决方案实现欧几里得算法求两个整数的最大公约数(GCD)和最小公倍数(LCM)的解决方案如下-求GCD和LCM的逻辑如下-if(firstno*secondno!=0){  gcd=gcd_rec(firstno,secondno);  printf("TheGCDof%dand%dis%d",

See all articles