×

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

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 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.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 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.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

Flow chart of While loop in C

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

3 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.