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 |
---|---|
P1 | 24 |
P2 | 3 |
P3 | 3 |
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.
Using the Gantt chart, Average waiting time is calculated as given below −
Average waiting time = 17/3 = 5.66 milliseconds
Algorithm
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)
Example
实例演示
#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; }
输出
The above is the detailed content of C program for loop scheduling. For more information, please follow other related articles on the PHP Chinese website!

XML is used in C because it provides a convenient way to structure data, especially in configuration files, data storage and network communications. 1) Select the appropriate library, such as TinyXML, pugixml, RapidXML, and decide according to project needs. 2) Understand two ways of XML parsing and generation: DOM is suitable for frequent access and modification, and SAX is suitable for large files or streaming data. 3) When optimizing performance, TinyXML is suitable for small files, pugixml performs well in memory and speed, and RapidXML is excellent in processing large files.

The main differences between C# and C are memory management, polymorphism implementation and performance optimization. 1) C# uses a garbage collector to automatically manage memory, while C needs to be managed manually. 2) C# realizes polymorphism through interfaces and virtual methods, and C uses virtual functions and pure virtual functions. 3) The performance optimization of C# depends on structure and parallel programming, while C is implemented through inline functions and multithreading.

The DOM and SAX methods can be used to parse XML data in C. 1) DOM parsing loads XML into memory, suitable for small files, but may take up a lot of memory. 2) SAX parsing is event-driven and is suitable for large files, but cannot be accessed randomly. Choosing the right method and optimizing the code can improve efficiency.

C is widely used in the fields of game development, embedded systems, financial transactions and scientific computing, due to its high performance and flexibility. 1) In game development, C is used for efficient graphics rendering and real-time computing. 2) In embedded systems, C's memory management and hardware control capabilities make it the first choice. 3) In the field of financial transactions, C's high performance meets the needs of real-time computing. 4) In scientific computing, C's efficient algorithm implementation and data processing capabilities are fully reflected.

C is not dead, but has flourished in many key areas: 1) game development, 2) system programming, 3) high-performance computing, 4) browsers and network applications, C is still the mainstream choice, showing its strong vitality and application scenarios.

The main differences between C# and C are syntax, memory management and performance: 1) C# syntax is modern, supports lambda and LINQ, and C retains C features and supports templates. 2) C# automatically manages memory, C needs to be managed manually. 3) C performance is better than C#, but C# performance is also being optimized.

You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.

Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
