You are on page 1of 10

COURSE CODE – CSE316

COURSE NAME: OPERATING SYSTEM


CA3

SUBMITTED TO: SUBMITTED BY:


MR. BISHAL SAHA DIKSHA VERMA
12218932
K21PK (64)
INTRODUCTION TO THE QUESTION

The Round Robin scheduling algorithm is a pre-emptive scheduling algorithm that is widely
used in computer operating systems. It is a cyclic algorithm that assigns a fixed time
quantum to each process in the ready queue. When the CPU executes a process for the
specified time quantum, it preempts the process and switches to the next process in the
ready queue.
In this question, the CPU executes process P1 for the first 2 units of time, then switches to
process P2, then P3, and finally P4. After executing P4 for 2 units of time, the CPU returns to
process P1 and the cycle starts again. This ensures that each process gets a fair share of the
CPU time, and no process is allowed to monopolize the CPU.
However, if a process completes execution before its time quantum expires, the CPU does
not need to preempt it, and the next process in the ready queue is executed immediately.
This reduces the waiting time for the processes in the queue and improves the overall
performance of the system.

A) Design and implement the simulation program using C programming language.


#include<stdio.h>
#include<stdbool.h>

// Process structure
struct Process {
int pid; // Process ID
int arrival; // Arrival Time
int burst; // Burst Time
int waiting; // Waiting Time
int turnaround; // Turnaround Time
int remaining; // Remaining Time
};
void calculateTime(struct Process p[], int n, int quantum)
{
int i, time = 0, remaining = n;
bool flag = false;
int wt_total = 0, tat_total = 0;

// Calculate the waiting time and turnaround time of each process


while (remaining != 0) {
for (i = 0; i < n; i++) {
if (p[i].remaining > 0) {
flag = true;

// If remaining time is greater than quantum, execute for quantum time and then
context switch
if (p[i].remaining > quantum) {
time += quantum;
p[i].remaining -= quantum;
}

// If remaining time is less than or equal to quantum, execute for remaining time
and then complete the process
else {
time += p[i].remaining;
p[i].waiting = time - p[i].arrival - p[i].burst;
p[i].remaining = 0;
remaining--;
p[i].turnaround = time - p[i].arrival;
}
}
}

// If all processes have been executed once, start over from the first process
if (flag == false) {
time++;
}
flag = false;
}

// Calculate the total waiting and turnaround time of all processes


for (i = 0; i < n; i++) {
wt_total += p[i].waiting;
tat_total += p[i].turnaround;
}

// Calculate the average waiting and turnaround time of all processes


float avg_wt = (float)wt_total / n;
float avg_tat = (float)tat_total / n;

// Display the results


printf("Process ID\t Arrival Time\t Burst Time\t Waiting Time\t Turnaround Time\n");
for (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, p[i].burst, p[i].waiting,
p[i].turnaround);
}
printf("Average waiting time = %f\n", avg_wt);
printf("Average turnaround time = %f\n", avg_tat);
}

// Main function
int main()
{
int n, quantum;
printf("Enter the number of processes: ");
scanf("%d", &n);

// Create an array of n processes


struct Process p[n];
int i;
for (i = 0; i < n; i++) {
printf("Enter the arrival time and burst time of process %d: ", i+1);
scanf("%d %d", &p[i].arrival, &p[i].burst);
p[i].pid = i+1;
p[i].remaining = p[i].burst;
}
printf("Enter the time quantum: ");
scanf("%d", &quantum);

B) Generate a set of "processes" with random arrival times and CPU burst times using a
random number generator.
Process Arrival Time CPU Burst Time
P1 2 4
P2 4 3
P3 0 5
P4 3 2

Arrival time for each process represents the time at which the process enters the CPU
scheduling queue. In this question, the processes are not already in the queue at time 0, so
the CPU will be idle until the first process, P3, arrives at time 0. After that, the CPU will
execute the processes according to the Round Robin scheduling algorithm with a time
quantum of 2 units of time.
C) Implement the Round Robin Scheduling algorithm in the simulation program. D) Have
the simulation program run for a set amount of time (e.g. 100-time units) and record the
average waiting time and response time for each process.

#include <stdio.h>
#define MAX_PROCESS 4
#define TIME_QUANTUM 2

// Process structure
struct Process {
int pid;
int burst_time;
int remaining_time;
int arrival_time;
int completion_time;
};

// Function to calculate waiting time and turnaround time


void calculateTimes(struct Process processes[], int n, int total_time) {
int i;
float avg_waiting_time = 0, avg_turnaround_time = 0;

printf("Process\tArrival Time\tBurst Time\tCompletion Time\tWaiting Time\tTurnaround


Time\n");

for (i = 0; i < n; i++) {


int waiting_time = processes[i].completion_time - processes[i].burst_time -
processes[i].arrival_time;
int turnaround_time = processes[i].completion_time - processes[i].arrival_time;

avg_waiting_time += waiting_time;
avg_turnaround_time += turnaround_time;

printf("P%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].pid,
processes[i].arrival_time, processes[i].burst_time,
processes[i].completion_time, waiting_time, turnaround_time);
}

avg_waiting_time /= n;
avg_turnaround_time /= n;

printf("\nAverage Waiting Time: %.2f\n", avg_waiting_time);


printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);
}

int main() {
struct Process processes[MAX_PROCESS] = {
{1, 4, 4, 0, 0},
{2, 5, 5, 0, 0},
{3, 2, 2, 0, 0},
{4, 1, 1, 0, 0}
};

int n = MAX_PROCESS;
int time = 0, i = 0;
int completed = 0;

while (completed < n) {


struct Process *current_process = &processes[i];

// Execute the process for the time quantum or until completion


if (current_process->remaining_time <= TIME_QUANTUM) {
time += current_process->remaining_time;
current_process->remaining_time = 0;
} else {
time += TIME_QUANTUM;
current_process->remaining_time -= TIME_QUANTUM;
}

// Update the completion time if the process has completed


if (current_process->remaining_time == 0 && current_process->completion_time == 0)
{
current_process->completion_time = time;
completed++;
}

// Move to the next process in the queue


i = (i + 1) % n;

// Check if all processes have arrived


int arrived = 1;
for (int j = 0; j < n; j++) {
if (processes[j].arrival_time <= time && processes[j].completion_time == 0) {
arrived = 0;
break;
}
}

// If all processes have arrived but none can be executed, increment time
if (arrived) {
int flag = 1;
for (int j = 0; j < n; j++) {
if (processes[j].remaining_time > 0) {
flag = 0;
break;
}
}
if (flag) {
time++;
}
}
}

// Calculate waiting time and turnaround time


calculateTimes(processes, n, time);

return 0;
}

E) Compare the results of the simulation with the ideal scenario of a perfect scheduler.

In the ideal scenario of a perfect scheduler, each process would receive an equal amount of
CPU time and the waiting time and turnaround time for each process would be minimal.
However, in Round Robin scheduling with a fixed time quantum, the waiting time and
turnaround time for each process can vary depending on their execution time.

Using Round Robin scheduling with a time quantum of 2 units of time, the execution order
of the processes would be as follows:
P1 executes for 2 units of time, then is preempted and placed at the end of the queue.
P2 executes for 2 units of time, then is preempted and placed at the end of the queue.
P3 executes for 2 units of time, then is preempted and placed at the end of the queue.
P4 executes for 2 units of time, then completes.
P1 executes for 2 units of time, then completes.
P3 executes for 2 units of time, then completes.
P2 executes for 1 unit of time, then completes.
The waiting time and turnaround time for each process can be calculated as follows:

Process Waiting Time Turnaround Time


P1 2 units 7 units
P2 0 units 3 units
P3 6 units 10 units
P4 0 units 2 units
As we can see, the waiting time and turnaround time for each process are not optimal,
especially for P1 and P3, which have longer execution times. In the ideal scenario of a
perfect scheduler, each process would have received an equal amount of CPU time, and the
waiting time and turnaround time for each process would have been minimized. Therefore,
while Round Robin scheduling ensures fairness in CPU time allocation, it may not always
result in optimal waiting time and turnaround time for each process.

You might also like