0% found this document useful (0 votes)
6 views9 pages

Kunalexp 5

The document outlines an experiment for an Operating System Lab focused on demonstrating preemptive scheduling algorithms, specifically Shortest Remaining Time First (SRTF) and Round Robin (RR). It includes theoretical explanations, example calculations for waiting and turnaround times, and C programming implementations for both algorithms. The conclusion emphasizes the study and implementation of these scheduling concepts.

Uploaded by

motwanikunal63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Kunalexp 5

The document outlines an experiment for an Operating System Lab focused on demonstrating preemptive scheduling algorithms, specifically Shortest Remaining Time First (SRTF) and Round Robin (RR). It includes theoretical explanations, example calculations for waiting and turnaround times, and C programming implementations for both algorithms. The conclusion emphasizes the study and implementation of these scheduling concepts.

Uploaded by

motwanikunal63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

----------------------------------------------------------------------------------------------------------------

Course Code: CSL403 Name: Operating System Lab

Class: SE Semester: IV

EXPERIMENT NO 5
Aim: Write a program to demonstrate the concept of preemptive scheduling algorithms. a.
Shortest Remaining Time First(SRTF)
b. Round Robin (RR)

Theory:

Process scheduling is an essential part of a Multiprogramming operating systems. Such operating systems
allow more than one process to be loaded into the executable memory at a time and the loaded process
shares the CPU using time multiplexing.

Preemptive scheduling: here CPU can release the processes even in the middle of the execution. CPU
received a signal from process p2. OS compares the priorities of p1 ,p2. If p1>p2, CPU continues the
execution of p1. If p1 >p2, CPU continues the execution of p1.

A. Shortest Remaining Time First(SRTF):

The choice arises when a new process arrives at the ready queue while a previous process is still executing.
The next CPU burst of the newly arrived process may be shorter than what is left of the currently executing
process. A preemptive SJF algorithm will preempt the currently executing process, whereas a
nonpreemptive SJF algorithm will allow the currently running process to finish its CPU burst. Preemptive
SJF scheduling is sometimes called shortest-remaining-time-first scheduling.

Example:

Gantt chart:

Name: Kunal Motwani Roll No.: 72


Process P1 is started at time 0, since it is the only process in the queue. Process P2 arrives at time 1. The
remaining time for process P1 (7 milliseconds) is larger than the time required by process P2 (4
milliseconds), so process P1 is preempted, and process P2 is scheduled.

Waiting time:
Process Waiting Time
P1 (0-0)+(10-1)= 9
P2 (1-1)=0
P3 (17-2)=15
P4 (5-3)=2

The average waiting time:

The average waiting time = [(10- 1) + (1 - 1) + (17- 2) + (5-3)]/ 4 = 26/4 = 6.5 milliseconds.

B. Round Robin (RR) scheduling:


It is designed especially for time sharing systems. Here CPU switches between the processes. When the
time quantum expired, the CPU switched to another job. A small unit of time, called a time quantum or
time slice. A time quantum is generally from 10 to 100 ms. The time quantum is generally depending
on OS. Here ready queue is a circular queue. CPU scheduler picks the first process from ready queue,
sets timer to interrupt after one time quantum and dispatches the process.

PROCESS BURST TIME


P1 30
P2 6
P3 8

Gantt’s Chart :

AVERAGE WAITING TIME :

Waiting time for P1 => 0+(15-5)+(24-20) => 0+10+4 = 14


Waiting time for P2 => 5+(20-10) => 5+10 = 15 Waiting time for P3
=> 10+(21-15) => 10+6 = 16 Average waiting time => (14+15+16)/3 =
15 ms.
Name: Kunal Motwani Roll No.: 72
AVERAGE TURN AROUND TIME :

Turn around time = waiting time + burst Time


Turn around time for P1 => 14+30 =44
Turn around time for P2 => 15+6 = 21
Turn around time for P3 => 16+8 = 24
Average turn around time =>( 44+21+24 )/3 = 29.66 ms

Program for SRTN:


#include<stdio.h>

#define MAX_PROCESSES 10

struct Process {
int id;
int arrivalTime;
int burstTime;
int remainingTime;
int completionTime;
int turnAroundTime;
int waitingTime;
};

void sortProcessesByArrivalTime(struct Process processes[], int n) {


for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (processes[j].arrivalTime > processes[j+1].arrivalTime) {
struct Process temp = processes[j];
processes[j] = processes[j+1];
processes[j+1] = temp;
}
}
}
}

void sjfPreemptive(struct Process processes[], int n) {


int currentTime = 0;
int completed = 0;
int prevProcess = -1;
float totalWaitingTime = 0, totalTurnaroundTime = 0;

Name: Kunal Motwani Roll No.: 72


int isCompleted[n];
for (int i = 0; i < n; i++) {
processes[i].remainingTime = processes[i].burstTime;

isCompleted[i] = 0;
}
while (completed < n) {
int idx = -1;
int minRemainingTime = 9999;
for (int i = 0; i < n; i++) {
if (processes[i].arrivalTime <= currentTime && !isCompleted[i] &&
processes[i].remainingTime < minRemainingTime) {
minRemainingTime = processes[i].remainingTime;
idx = i;
}
}
if (idx == -1) {
currentTime++;
continue;
}
processes[idx].remainingTime--;
currentTime++;
if (processes[idx].remainingTime == 0) {
isCompleted[idx] = 1;
completed++;
processes[idx].completionTime = currentTime;
processes[idx].turnAroundTime = processes[idx].completionTime -
processes[idx].arrivalTime;
processes[idx].waitingTime = processes[idx].turnAroundTime -
processes[idx].burstTime;

totalWaitingTime += processes[idx].waitingTime;
totalTurnaroundTime += processes[idx].turnAroundTime;
}
}
printf("PID\t\tAT\t\tBT\t\tCT\t\tTAT\t\tWT\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
processes[i].id, processes[i].arrivalTime, processes[i].burstTime,
processes[i].completionTime, processes[i].turnAroundTime,

Name: Kunal Motwani Roll No.: 72


processes[i].waitingTime);
}

printf("\nAverage Turnaround Time: %.2f\n", totalTurnaroundTime / n);


printf("Average Waiting Time: %.2f\n", totalWaitingTime / n);
}

int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
for (int i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i+1);
processes[i].id = i+1;
scanf("%d %d", &processes[i].arrivalTime, &processes[i].burstTime);
}
sortProcessesByArrivalTime(processes, n);
sjfPreemptive(processes, n);
return 0;
}

OUTPUT:

Program for RR:


#include<stdio.h>

Name: Kunal Motwani Roll No.: 72


#define MAX_PROCESSES 10

struct Process {
int id;
int arrivalTime;
int burstTime;

int remainingTime;
int completionTime;
int turnAroundTime;
int waitingTime;
};

void sortProcessesByArrivalTime(struct Process processes[], int n) {


for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (processes[j].arrivalTime > processes[j+1].arrivalTime) {
struct Process temp = processes[j];
processes[j] = processes[j+1];
processes[j+1] = temp;
}
}
}
}

void roundRobin(struct Process processes[], int n, int quantum) {


int currentTime = 0;
int completed = 0;
float totalWaitingTime = 0, totalTurnaroundTime = 0;
int isCompleted[n];
for (int i = 0; i < n; i++) {
processes[i].remainingTime = processes[i].burstTime;
isCompleted[i] = 0;
}

while (completed < n) {


int allCompleted = 1;

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

Name: Kunal Motwani Roll No.: 72


if (processes[i].remainingTime > 0) {
allCompleted = 0;
if (processes[i].arrivalTime <= currentTime) {
if (processes[i].remainingTime > quantum) {
processes[i].remainingTime -= quantum;
currentTime += quantum;
} else {
currentTime += processes[i].remainingTime;
processes[i].remainingTime = 0;
isCompleted[i] = 1;

completed++;

processes[i].completionTime = currentTime;
processes[i].turnAroundTime = processes[i].completionTime
- processes[i].arrivalTime;
processes[i].waitingTime = processes[i].turnAroundTime -
processes[i].burstTime;

totalWaitingTime += processes[i].waitingTime;
totalTurnaroundTime += processes[i].turnAroundTime;
}
}
}
}

if (allCompleted) break;
}

printf("PID\t\tAT\t\tBT\t\tCT\t\tTAT\t\tWT\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
processes[i].id, processes[i].arrivalTime, processes[i].burstTime,
processes[i].completionTime, processes[i].turnAroundTime,
processes[i].waitingTime);
}

printf("\nAverage Turnaround Time: %.2f\n", totalTurnaroundTime / n);


printf("Average Waiting Time: %.2f\n", totalWaitingTime / n);
}

Name: Kunal Motwani Roll No.: 72


int main() {
int n, quantum;

printf("Enter the number of processes: ");


scanf("%d", &n);

printf("Enter the time quantum for Round Robin scheduling: ");


scanf("%d", &quantum);

struct Process processes[n];

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


printf("Enter arrival time and burst time for process %d: ", i+1);
processes[i].id = i+1;
scanf("%d %d", &processes[i].arrivalTime, &processes[i].burstTime);
}

sortProcessesByArrivalTime(processes, n);
roundRobin(processes, n, quantum);

return 0;
}

OUTPUT:

Name: Kunal Motwani Roll No.: 72


Conclusion: Hence, we have studied and implemented programs to demonstrate the concept of preemptive
scheduling algorithms.

Name: Kunal Motwani Roll No.: 72

You might also like