Preemptive
Preemptive
Faculty – Vallidevi K
Lab 6
a) Round-robin scheduling
b) SRTF
Code:
(a) Round Robin Scheduling
#include <stdio.h>
#include <stdlib.h>
// Keep traversing processes in round robin manner until all of them are not done.
while (1) {
int done = 1;
// If burst time is smaller than or equal to quantum. Last cycle for this process
else {
// Increase the value of t i.e. shows how much time a process has been processed
t = t + rem_bt[i];
// As the process gets fully executed make its remaining burst time = 0
rem_bt[i] = 0;
}
}
}
// Driver code
int main() {
// Process id's
int processes[] = {1, 2, 3};
int n = sizeof processes / sizeof processes[0];
// Time quantum
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}
Output:
(b) SRTF
Code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct Process {
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
};
if (check == 0) {
t++;
continue;
}
// Update minimum
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;
if (wt[shortest] < 0)
wt[shortest] = 0;
}
// Increment time
t++;
}
}
// Driver code
int main() {
struct Process proc[] = {{1, 6, 1}, {2, 8, 1}, {3, 7, 2}, {4, 3, 3}};
int n = sizeof proc / sizeof proc[0];
findavgTime(proc, n);
return 0;
}
Output:
Result:
Implemented given pre-emptive scheduling algorithms and calculated the completion time,
turnaround time and waiting time for each of them.