Kunalexp 5
Kunalexp 5
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.
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:
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 = [(10- 1) + (1 - 1) + (17- 2) + (5-3)]/ 4 = 26/4 = 6.5 milliseconds.
Gantt’s Chart :
#define MAX_PROCESSES 10
struct Process {
int id;
int arrivalTime;
int burstTime;
int remainingTime;
int completionTime;
int turnAroundTime;
int waitingTime;
};
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,
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:
struct Process {
int id;
int arrivalTime;
int burstTime;
int remainingTime;
int completionTime;
int turnAroundTime;
int waitingTime;
};
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);
}
sortProcessesByArrivalTime(processes, n);
roundRobin(processes, n, quantum);
return 0;
}
OUTPUT: