Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

CSE316:-

OPERATING SYSTEMS

LOVELY PROFESSIONAL UNIVERSITY, JALANDHAR,


PUNJAB, INDIA.

NAME: ASHUTOSH CHAUHAN


SUBMITTED TO: Ms.MANBIR KAUR
REGISTRATION NUMBER: 12110812
ROLL NUMBER: RK21MPB39

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.

A)Design and implement the simulation program using the C


programming language.

#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("Process\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting


Time\n");
for (int i = 0; i < n; i++) {

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
}

// print the generated processes


printf("Process\tArrival Time\tBurst Time\n");
for (int i = 0; i < n; i++) {
printf("%s\t\t%d\t\t%d\n", processes[i].name, processes[i].arrival_time,
processes[i].burst_time);
}

return 0;
}
Result:-

C) Implement the Round Robin Scheduling algorithm in the simulation


program.

#include <stdio.h>
#include <stdlib.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);

// Read the arrival time and burst time of each process


for (i = 0; i < n; i++) {
process[i].pid = i+1;
printf("Enter the arrival time and burst time of process %d: ", i+1);
scanf("%d%d", &process[i].arrival_time, &process[i].burst_time);
process[i].waiting_time = 0;
process[i].response_time = -1;
remaining++;
}

// Implement the Round Robin Scheduling algorithm


while (remaining > 0) {
for (i = 0; i < n; i++) {
if (completed[i] == 1) {
continue;
}
if (process[i].response_time == -1) {
process[i].response_time = current_time - process[i].arrival_time;
}
if (process[i].burst_time <= quantum) {
current_time += process[i].burst_time;
process[i].waiting_time = current_time - process[i].arrival_time -
process[i].burst_time;
total_waiting_time += process[i].waiting_time;
total_response_time += process[i].response_time;
process[i].burst_time = 0;
completed[i] = 1;
remaining--;
printf("Process %d: waiting time = %d, response time = %d\n",
process[i].pid, process[i].waiting_time, process[i].response_time);
} else {
current_time += quantum;
process[i].burst_time -= 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;

// Set the seed for the random number generator


srand(time(NULL));

// 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++;
}

// Implement the Round Robin Scheduling algorithm


while (remaining > 0) {
for (i = 0; i < n; i++) {
if (completed[i] == 1) {
continue;
}
if (process[i].response_time == -1) {
process[i].response_time = current_time - process[i].arrival_time;
}
if (process[i].burst_time <= quantum) {
current_time += process[i].burst_time;
process[i].waiting_time = current_time - process[i].arrival_time -
process[i].burst_time;
total_waiting_time += process[i].waiting_time;
total_response_time += process[i].response_time;
process[i].burst_time = 0;
completed[i] = 1;
remaining--;
printf("Process %d: waiting time = %d, response time = %d\n", process[i].pid,
process[i].waiting_time, process[i].response_time);
} else {
current_time += quantum;
process[i].burst_time -= 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:-

E) Compare the results of the simulation with the ideal scenario of a


perfect scheduler(write at least 500 words at the end of the code and
comment on it).
In the ideal scenario of a perfect scheduler, all processes would be scheduled in such
a way that each process is executed for an equal amount of time, without any context
switches or overhead. This would result in an optimal utilization of the CPU resources
and the completion of all processes in the shortest possible time.

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.

You might also like