给定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 毫秒。
使用甘特图,计算平均等待时间如下下面 -
平均等待时间 = 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'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程序的详细内容。更多信息请关注PHP中文网其他相关文章!

从XML转换到C 并进行数据操作可以通过以下步骤实现:1)使用tinyxml2库解析XML文件,2)将数据映射到C 的数据结构中,3)使用C 标准库如std::vector进行数据操作。通过这些步骤,可以高效地处理和操作从XML转换过来的数据。

C#使用自动垃圾回收机制,而C 采用手动内存管理。1.C#的垃圾回收器自动管理内存,减少内存泄漏风险,但可能导致性能下降。2.C 提供灵活的内存控制,适合需要精细管理的应用,但需谨慎处理以避免内存泄漏。

C 在现代编程中仍然具有重要相关性。1)高性能和硬件直接操作能力使其在游戏开发、嵌入式系统和高性能计算等领域占据首选地位。2)丰富的编程范式和现代特性如智能指针和模板编程增强了其灵活性和效率,尽管学习曲线陡峭,但其强大功能使其在今天的编程生态中依然重要。

C 学习者和开发者可以从StackOverflow、Reddit的r/cpp社区、Coursera和edX的课程、GitHub上的开源项目、专业咨询服务以及CppCon等会议中获得资源和支持。1.StackOverflow提供技术问题的解答;2.Reddit的r/cpp社区分享最新资讯;3.Coursera和edX提供正式的C 课程;4.GitHub上的开源项目如LLVM和Boost提升技能;5.专业咨询服务如JetBrains和Perforce提供技术支持;6.CppCon等会议有助于职业

C#适合需要高开发效率和跨平台支持的项目,而C 适用于需要高性能和底层控制的应用。1)C#简化开发,提供垃圾回收和丰富类库,适合企业级应用。2)C 允许直接内存操作,适用于游戏开发和高性能计算。

C 持续使用的理由包括其高性能、广泛应用和不断演进的特性。1)高效性能:通过直接操作内存和硬件,C 在系统编程和高性能计算中表现出色。2)广泛应用:在游戏开发、嵌入式系统等领域大放异彩。3)不断演进:自1983年发布以来,C 持续增加新特性,保持其竞争力。

C 和XML的未来发展趋势分别为:1)C 将通过C 20和C 23标准引入模块、概念和协程等新特性,提升编程效率和安全性;2)XML将继续在数据交换和配置文件中占据重要地位,但会面临JSON和YAML的挑战,并朝着更简洁和易解析的方向发展,如XMLSchema1.1和XPath3.1的改进。

现代C 设计模式利用C 11及以后的新特性实现,帮助构建更灵活、高效的软件。1)使用lambda表达式和std::function简化观察者模式。2)通过移动语义和完美转发优化性能。3)智能指针确保类型安全和资源管理。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Atom编辑器mac版下载
最流行的的开源编辑器

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

WebStorm Mac版
好用的JavaScript开发工具