×

Round Robin Scheduling in C

Round Robin Scheduling in C

Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms used in various operating systems to process networks and scheduling. It is more similar to FCFS (First Come First Serve) scheduling algorithm, but the only difference is that round-robin scheduling processes are bounded with a quantum time size. A small unit of time is called time quantum or time slice. Time slice or time quantum can range from 10 to 100 milliseconds. The chief idea is to allocate the CPU to all the processes running in the same order for the same amount of time.

It is a pre-emptive scheduling algorithm famous for CPU scheduling and used in numerous operating systems. Pre-emptive scheduling is a technique that divides the time slots of the CPU to a particular process. Preemptive scheduling is a better approach than other algorithms like shortest job first, considering that there will be a guarantee that all the processes will be completed at the cost of overall performance.

  1. Completion time (CT): it is the time required by the process to complete the execution.
  2. Turnaround time (TAT): it is the difference between the completion time (CT) and the arrival time (AT).

            Turnaround time (TAT) = completion time (CT) - Arrival Time (AT)

  • Waiting time (WT): total time between requesting action and acquiring the resource.

            Waiting time (WT) = Turnaround time (TAT) - Burst Time (BT)

  • Response time: it is the time at which the system response to a process

Algorithm:

  • Processes are arranged in a first come, first serve order as we do in a queue.
  • Each process that is in a queue is allocated a quantum value.
  • The first process is executed until the end of the provided quantum value. After the quantum value is ended, an interrupt is generated, and the state is saved.
  • The CPU moves to the following process present in the queue, and the same method repeatedly follows until all the processes are completed, first-come, and no more processes are left.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 #include <math.h>
 int main()
 {
 int i, limit, total = 0, x, counter = 0, time_quantum;
 int wt = 0, tat = 0, a1[10], bt[10], temp[10];
 float average_wt, average_tat;
 printf("Enter the total number of processes: \n");
 scanf("%d", &limit);
 x = limit;
 for(i = 0; i < limit; i++)
 {
 printf("Enter details of the processes %d: \n", i + 1);
 printf("Arrival Time: \t");
 scanf("%d", &at[i]);
 printf("Burst Time:t");
 scanf("%d", &bt[i]);
 temp[i] = bt[i];
 }
 printf("Enter time quantum or time slice: \t");
 scanf("%d", &time_quantum);
 printf("\n Process ID\t\t  burst time\t turn around time\t waiting time\n”);
 for(total = 0, i = 0; x != 0;)
 {
 if(temp[i] <= time_quantum && temp[i] > 0)
 {
 total = total + temp[i];
 temp[i] = 0;
 counter = 1;
 }
 else if(temp[i] > 0)
 {
 temp[i] = temp[i] - time_quantum;
 total = total + time_quantum;
 }
 if(temp[i] == 0 && counter == 1)
 {
 x--;
 printf("\n Process: %d\t\t %d\t %d\t %d\n", i + 1, bt[i], total - at[i], total - at[i] - bt[i]);
 wt = wt + total - at[i] - bt[i];
 tat = tat + total - at[i];
 counter = 0;
 }
 if(i == limit - 1)
 {
 i = 0;
 }
 else if(at[i + 1] <= total)
 {
 i++;
 }
 else
 {
 i = 0;
 }
 }
 average_wt = wt * 1.0 / limit;
 average_tat = tat * 1.0 / limit;
 printf("\n\n Average Waiting Time:\t %f", average_wt);
 printf("\n Average Turnaround Time:\t %f \n", average_tat);
 return 0;
 } 

Output:

 Enter the total number of processes: 4
 Enter the details of process 1:
 Arrival time: 0
 Burst time: 4
 Enter the details of process 2:
 Arrival time: 1
 Burst time: 7
 Enter the details of process 3:
 Arrival time: 2
 Burst time: 5
 Enter the details of process 4:
 Arrival time: 3
 Burst time: 6
 Enter the time quantum: 3
 Process ID             burst time          turn around time           waiting time
 Process 1                      4                           13                                  9
 Process 2                      5                           16                                 11
 Process 3                      6                           18                                 12
 Process 4                      7                           21                                 14
 Average waiting time: 11.500000000
 Average turnaround time: 17.00000000 

Advantages of using Round Robin Scheduling:

  • Each and every process attain the same priority as it has fixed quantum.
  • No starvation or stagnation will be allowed, and hence no process will be left unattended.
  • Average response time will be improved; that is, better execution will take place.
  • CPU will be best utilized in this case, and extra resources will not be required.
  • Threads having the same priority are scheduled perfectly in this method as CPU will be equally shared between all the processes.

Disadvantages of Round Robin Scheduling:

  • Every process has to wait until its turn arrives. This leads to a higher average waiting time.
  • It is overhead of context switching.
  • Decreased rate of completion as processes will not wholly be executed leads to low throughput.
  • Length of quantum time has a significant effect on waiting time.
  • Lack of priority might be a problem as processes require immediate attention.

 


Related Topics

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.