0% found this document useful (0 votes)
29 views6 pages

Preemptive

Preemptive-Scheduling Algorithm using C

Uploaded by

Utkarsh Varman
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)
29 views6 pages

Preemptive

Preemptive-Scheduling Algorithm using C

Uploaded by

Utkarsh Varman
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/ 6

Operating Systems Lab (BCSE303P)

Faculty – Vallidevi K
Lab 6

Name: Utkarsh Varman


Register No. 21BLC1030
Experiment – 6:
Implement the following Preemptive scheduling algorithms using C programming
Calculate the Completion Time, Turn Around Time and Waiting time.
Hint:
1. Completion Time: Time at which the process completes its execution.
2. Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time =
Completion Time – Arrival Time
3. Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time.

a) Round-robin scheduling
b) SRTF

Code:
(a) Round Robin Scheduling
#include <stdio.h>
#include <stdlib.h>

// Function to find the waiting time for all processes


void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum) {
// Make a copy of burst times bt[] to store remaining burst times.
int rem_bt[n];
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];

int t = 0; // Current time

// Keep traversing processes in round robin manner until all of them are not done.
while (1) {
int done = 1;

// Traverse all processes one by one repeatedly


for (int i = 0; i < n; i++) {
// If burst time of a process is greater than 0 then only need to process further
if (rem_bt[i] > 0) {
done = 0; // There is a pending process

if (rem_bt[i] > quantum) {


// Increase the value of t i.e. shows how much time a process has been processed
t += quantum;
// Decrease the burst_time of current process by quantum
rem_bt[i] -= quantum;
}

// 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];

// Waiting time is current time minus time used by this process


wt[i] = t - bt[i];

// As the process gets fully executed make its remaining burst time = 0
rem_bt[i] = 0;
}
}
}

// If all processes are done


if (done == 1)
break;
}
}

// Function to calculate turn around time


void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
// Calculating turnaround time by adding bt[i] + wt[i]
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
}

// Function to calculate average time


void findavgTime(int processes[], int n, int bt[], int quantum) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;

// Function to find waiting time of all processes


findWaitingTime(processes, n, bt, wt, quantum);

// Function to find turn around time for all processes


findTurnAroundTime(processes, n, bt, wt, tat);

// Display processes along with all details


printf("Processes Burst time Waiting time Turn around time\n");

// Calculate total waiting time and total turn around time


for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf("%d\t\t%d\t\t%d\t\t%d\n", i+1, bt[i], wt[i], tat[i]);
}
printf("Average waiting time = %f\n", (float)total_wt / (float)n);
printf("Average turn around time = %f\n", (float)total_tat / (float)n);
}

// Driver code
int main() {
// Process id's
int processes[] = {1, 2, 3};
int n = sizeof processes / sizeof processes[0];

// Burst time of all processes


int burst_time[] = {10, 5, 8};

// Time quantum
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}

Output:

Average Waiting Time: 12.0s


Average Turn Around Time: 19.66s
Average Completion Time: 6s

(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
};

// Function to find the waiting time for all processes


void findWaitingTime(struct Process proc[], int n, int wt[]) {
int rt[n]; // Remaining time of all processes
for (int i = 0; i < n; i++)
rt[i] = proc[i].bt;

int complete = 0, t = 0, minm = INT_MAX;


int shortest = 0, finish_time;
int check = 0;

// Process until all processes gets completed


while (complete != n) {
// Find process with minimum remaining time among the processes that arrives till the current
time
for (int j = 0; j < n; j++) {
if ((proc[j].art <= t) && (rt[j] < minm) && rt[j] > 0) {
minm = rt[j];
shortest = j;
check = 1;
}
}

if (check == 0) {
t++;
continue;
}

// Reduce remaining time by one


rt[shortest]--;

// Update minimum
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;

// If a process gets completely executed


if (rt[shortest] == 0) {
// Increment complete
complete++;
check = 0;

// Find finish time of current process


finish_time = t + 1;
// Calculate waiting time
wt[shortest] = finish_time - proc[shortest].bt - proc[shortest].art;

if (wt[shortest] < 0)
wt[shortest] = 0;
}
// Increment time
t++;
}
}

// Function to calculate turn around time


void findTurnAroundTime(struct Process proc[], int n, int wt[], int tat[]) {
// Calculating turnaround time by adding proc[i].bt + wt[i]
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}

// Function to calculate average time


void findavgTime(struct Process proc[], int n) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;

// Function to find waiting time of all processes


findWaitingTime(proc, n, wt);

// Function to find turn around time for all processes


findTurnAroundTime(proc, n, wt, tat);

// Display processes along with all details


printf("Processes Burst time Waiting time Turn around time\n");

// Calculate total waiting time and total turn around time


for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf("%d\t\t%d\t\t%d\t\t\t%d\n", proc[i].pid, proc[i].bt, wt[i], tat[i]);
}

printf("Average waiting time = %f\n",(float)total_wt / (float)n);


printf("Average turn around time = %f\n",(float)total_tat / (float)n);
}

// 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:

Average Waiting Time: 6.75s


Average Turn Around Time: 12.75s
Average Completion Time: 8.5s

Result:
Implemented given pre-emptive scheduling algorithms and calculated the completion time,
turnaround time and waiting time for each of them.

You might also like