Professional Documents
Culture Documents
Os Ca3
Os Ca3
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.
// 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;
// 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;
}
// Main function
int main()
{
int n, quantum;
printf("Enter the number of processes: ");
scanf("%d", &n);
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;
};
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;
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;
// 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++;
}
}
}
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: