Rno 39
Rno 39
OPERATING SYSTEMS
Q3 In Round Robin Scheduling, each process is assigned a fixed time slot in the CPU
scheduling queue, known as a time quantum. If a process does not complete within
its assigned time quantum, the CPU context switches to the next process in the queue.
This ensures that each process gets a fair share of the CPU time and prevents any
process from monopolizing the CPU.
Suppose there are 4 processes: P1, P2, P3, and P4. The time quantum for the
scheduling algorithm is 2 units of time.
#include<stdio.h>
#include<stdbool.h>
struct process {
char name[5];
int arrival_time;
int burst_time;
int remaining_time;
int completion_time;
int turnaround_time;
int waiting_time;
bool completed;
};
int main() {
int n = 4; // number of processes
int time_quantum = 2; // time quantum for Round Robin Scheduling
struct process processes[n];
// initialize the processes with their respective arrival and burst times
strcpy(processes[0].name, "P1");
processes[0].arrival_time = 0;
processes[0].burst_time = 5;
processes[0].remaining_time = processes[0].burst_time;
processes[0].completed = false;
strcpy(processes[1].name, "P2");
processes[1].arrival_time = 1;
processes[1].burst_time = 3;
processes[1].remaining_time = processes[1].burst_time;
processes[1].completed = false;
strcpy(processes[2].name, "P3");
processes[2].arrival_time = 2;
processes[2].burst_time = 4;
processes[2].remaining_time = processes[2].burst_time;
processes[2].completed = false;
strcpy(processes[3].name, "P4");
processes[3].arrival_time = 3;
processes[3].burst_time = 2;
processes[3].remaining_time = processes[3].burst_time;
processes[3].completed = false;
int time = 0;
int remaining_processes = n;
while (remaining_processes > 0) {
for (int i = 0; i < n; i++) {
if (!processes[i].completed && processes[i].arrival_time <= time) {
printf("Process %s is executing at time %d\n", processes[i].name, time);
if (processes[i].remaining_time <= time_quantum) {
time += processes[i].remaining_time;
processes[i].completion_time = time;
processes[i].turnaround_time = processes[i].completion_time -
processes[i].arrival_time;
processes[i].waiting_time = processes[i].turnaround_time -
processes[i].burst_time;
processes[i].completed = true;
remaining_processes--;
} else {
time += time_quantum;
processes[i].remaining_time -= time_quantum;
}
}
}
}
printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t\t%d\n",processes[i].name,processes[i].arrival_time,
processes[i].burst_time,processes[i].completion_time,processes[i].turnaround_time,
processes[i].waiting_time);
}
return 0;
}
Result:-
B)Generate a set of "processes" with random arrival times and CPU burst
times using a random
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct process {
char name[5];
int arrival_time;
int burst_time;
};
int main() {
int n = 4; // number of processes
int time_quantum = 2; // time quantum for Round Robin Scheduling
struct process processes[n];
srand(time(0)); // seed the random number generator with the current time
// generate random arrival times and CPU burst times for each process
for (int i = 0; i < n; i++) {
sprintf(processes[i].name, "P%d", i+1);
processes[i].arrival_time = rand() % 10; // generate a random arrival time
between 0 and 9
processes[i].burst_time = (rand() % 10) + 1; // generate a random burst time
between 1 and 10
}
return 0;
}
Result:-
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, quantum, i, j;
struct Process process[100];
int current_time = 0, total_waiting_time = 0, total_response_time = 0;
int completed[100] = {0}, remaining = 0;
// Read the number of processes and time quantum from the user
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the time quantum: ");
scanf("%d", &quantum);
// Calculate the average waiting time and response time of all the processes
float avg_waiting_time = (float) total_waiting_time / n;
float avg_response_time = (float) total_response_time / n;
printf("Average waiting time = %.2f\n", avg_waiting_time);
printf("Average response time = %.2f\n", avg_response_time);
return 0;
}
Result:-
D) Have the simulation program run for a set amount of time and record
the average waiting time and response time for each process.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Define the structure of the process
struct Process {
int pid;
int arrival_time;
int burst_time;
int waiting_time;
int response_time;
};
int main() {
int n, quantum, i, j;
struct Process process[100];
int current_time = 0, total_waiting_time = 0, total_response_time = 0;
int completed[100] = {0}, remaining = 0;
// Read the number of processes and time quantum from the user
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the time quantum: ");
scanf("%d", &quantum);
// Generate the arrival time and burst time of each process using a random number generator
for (i = 0; i < n; i++) {
process[i].pid = i+1;
process[i].arrival_time = rand() % 10;
process[i].burst_time = rand() % 10 + 1;
process[i].waiting_time = 0;
process[i].response_time = -1;
remaining++;
}
// Calculate the average waiting time and response time of all the processes
float avg_waiting_time = (float) total_waiting_time / n;
float avg_response_time = (float) total_response_time / n;
printf("Average waiting time = %.2f\n", avg_waiting_time);
printf("Average response time = %.2f\n", avg_response_time);
return 0;
}
Result:-
To compare the results of the simulation with the ideal scenario of a perfect
scheduler, we can analyze the turnaround time and waiting time of each process.
Turnaround time is the total time taken by a process from arrival to completion,
while waiting time is the time a process spends waiting in the ready queue. Lower
values of these metrics indicate better performance of the scheduling algorithm.
In a perfect scheduler, the waiting time for each process would be zero, as each
process would be executed for an equal amount of time without any interruptions.
However, in our simulation, we can see that the waiting time for each process is non-
zero, indicating that each process had to wait in the ready queue before it was
executed. This waiting time is a result of the context switches that occur when the
time quantum expires, and the CPU context switches to the next process in the
queue.
Similarly, in a perfect scheduler, the turnaround time for each process would be
equal to the sum of the arrival time and CPU burst time, as each process would be
executed for its entire CPU burst time without any interruptions. However, in our
simulation, we can see that the turnaround time for each process is higher than the
sum of the arrival time and CPU burst time, indicating that each process had to wait
in the ready queue and also experienced overhead due to context switches.
Overall, while Round Robin Scheduling provides a fair allocation of CPU time to each
process, it results in some overhead due to context switches and less efficient
utilization of CPU resources compared to a perfect scheduler. However, in practical
scenarios, Round Robin Scheduling is a commonly used scheduling algorithm that
strikes a balance between fairness and efficiency.