Skip to content Skip to sidebar Skip to footer

C Program For Round Robin Scheduling With Gantt Chart

Round Robin Scheduling

Introduction

Scheduling is an essential part of operating systems, and Round Robin Scheduling is one of the most widely used scheduling algorithms. It is a preemptive scheduling algorithm that schedules processes in a circular queue. The processes are assigned a fixed time slice, and the CPU switches to the next process when the time slice is over.

Gantt Chart is a graphical representation of the execution of processes over time. It is a bar chart that shows the start and end time of each process. It is used to visualize the scheduling algorithm and to monitor the system's performance.

Round Robin Scheduling Algorithm

The Round Robin Scheduling algorithm is a preemptive scheduling algorithm that schedules processes in a circular queue. Each process is assigned a fixed time slice called a quantum or time slice. When the time slice is over, the CPU switches to the next process in the queue.

If a process completes its execution within the time slice, it is removed from the queue. If a process still has some work left after the time slice, it is moved to the end of the queue. This ensures that all processes get an equal chance to execute and prevents starvation.

The Round Robin Scheduling algorithm is simple and easy to implement. It is widely used in time-sharing systems, where multiple users share the same resources.

Gantt Chart

Gantt Chart

A Gantt Chart is a graphical representation of the execution of processes over time. It is a bar chart that shows the start and end time of each process. It is used to visualize the scheduling algorithm and to monitor the system's performance.

The Gantt Chart consists of a horizontal axis representing time and a vertical axis representing processes. Each process is represented by a horizontal bar that shows its start time and end time. The length of the bar represents the duration of the process.

The Gantt Chart is a powerful tool for analyzing the performance of the scheduling algorithm. It can be used to identify bottlenecks and to optimize the system's performance.

C Program for Round Robin Scheduling with Gantt Chart

Here is a C program for implementing Round Robin Scheduling with Gantt Chart.

#include #include #define MAX 100struct process {int pid;int burst_time;int remaining_time;int arrival_time;int waiting_time;int turnaround_time;int start_time;int end_time;};void round_robin_scheduling(struct process p[], int n, int quantum);void print_gantt_chart(struct process p[], int n);int main() {int n, quantum;struct process p[MAX];printf("Enter the number of processes: ");scanf("%d", &n);printf("Enter the quantum: ");scanf("%d", &quantum);printf("Enter the arrival time and burst time of each process:\n");for (int i = 0; i < n; i++) {printf("Process %d: ", i + 1);p[i].pid = i + 1;scanf("%d %d", &p[i].arrival_time, &p[i].burst_time);p[i].remaining_time = p[i].burst_time;}round_robin_scheduling(p, n, quantum);printf("\nGantt Chart:\n");print_gantt_chart(p, n);return 0;}void round_robin_scheduling(struct process p[], int n, int quantum) {int time = 0;int completed = 0;int queue[MAX];int front = 0, rear = -1;for (int i = 0; i < n; i++) {p[i].waiting_time = 0;p[i].turnaround_time = 0;p[i].start_time = -1;p[i].end_time = -1;}do {for (int i = 0; i < n; i++) {if (p[i].arrival_time == time) {rear++;queue[rear] = i;}}if (rear == -1) {printf("Idle %d-%d\n", time, time + quantum);time += quantum;} else {int current = queue[front];front++;if (p[current].start_time == -1)p[current].start_time = time;if (p[current].remaining_time <= quantum) {time += p[current].remaining_time;p[current].remaining_time = 0;p[current].end_time = time;completed++;p[current].turnaround_time = p[current].end_time - p[current].arrival_time;p[current].waiting_time = p[current].turnaround_time - p[current].burst_time;} else {time += quantum;p[current].remaining_time -= quantum;printf("P%d %d-%d\n", p[current].pid, time - quantum, time);rear++;queue[rear] = current;}}} while (completed != n);}void print_gantt_chart(struct process p[], int n) {int time = 0;printf(" ");for (int i = 0; i < n; i++) {for (int j = 0; j < p[i].burst_time; j++)printf("--");printf(" ");}printf("\n|");while (1) {int done = 1;for (int i = 0; i < n; i++) {if (p[i].start_time <= time && p[i].remaining_time > 0) {done = 0;printf("P%d", p[i].pid);for (int j = 0; j < 2; j++) {for (int k = 0; k < p[i].burst_time; k++)printf(" ");printf("|");}p[i].remaining_time--;if (p[i].remaining_time == 0)p[i].end_time = time + 1;} else {for (int j = 0; j < 2 * p[i].burst_time + 1; j++)printf(" ");printf("|");}}printf("\n");time++;if (done == 1)break;printf(" ");for (int i = 0; i < n; i++) {if (p[i].start_time <= time && p[i].remaining_time > 0)printf("--");else {for (int j = 0; j < 2 * p[i].burst_time; j++)printf(" ");}printf(" ");}printf("\n|");}printf("\n");printf(" ");for (int i = 0; i < n; i++) {for (int j = 0; j < p[i].burst_time; j++)printf("--");printf(" ");}printf("\n");printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");for (int i = 0; i < n; i++) {printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].arrival_time, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time);}}

Related video of C Program For Round Robin Scheduling With Gantt Chart