PCS 511(S) Operating System & Computer Networks Lab
Problem Statement 9: Process Scheduling
Some operating systems allow more than one process to be loaded into the executable memory at
a time. These loaded processes must share the CPU in an efficient manner. The act of determining
which of these loaded processes should be assigned to CPU and removal of currently running
process from CPU is known as process scheduling. Given a list of processes, their CPU burst
times and arrival times, print the Gantt Chart for the following given scheduling policies. Also find
average waiting time and average turnaround time required for complete execution of these
processes. A
a. FCFS – First Come First Served: process which arrives first will get the CPU first.
b. SJF NP – Shortest Job First Non-Preemptive: process which needs CPU for least amount
will get the CPU first. Here non-preemptive means currently running process leaves CPU
voluntarily after completing its execution.
c. SJF P – Shortest Job First Preemptive – Here preemptive means operating system decides
when to move currently running process.
Manual 9.a
#include<stdio.h>
// Swap function for sorting
void swap(float *xp, float *yp) {
float temp = *xp;
*xp = *yp;
*yp = temp;
}
// Sort according to Arrival Time
void sortArray(float bt[ ], float at[ ], int p[ ], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (at[j] > at[j + 1]) {
swap(&at[j], &at[j + 1]);
swap(&bt[j], &bt[j + 1]);
int temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}}}}
int main() {
int n;
printf("Enter total number of processes: ");
scanf("%d", &n);
float bt[n], at[n], wt[n], ct[n], tat[n];
int p[n];
float sum_tat = 0, sum_wt = 0;
// Input
printf("\nEnter Arrival Times:\n");
for (int i = 0; i < n; i++) {
scanf("%f", &at[i]);
p[i] = i + 1;
}
printf("\nEnter Burst Times:\n");
for (int i = 0; i < n; i++)
scanf("%f", &bt[i]);
// Sort processes by arrival time
sortArray(bt, at, p, n);
// Calculate CT, TAT, WT
ct[0] = at[0] + bt[0];
tat[0] = ct[0] - at[0];
wt[0] = tat[0] - bt[0];
sum_tat += tat[0];
sum_wt += wt[0];
for (int i = 1; i < n; i++) {
if (at[i] > ct[i - 1])
ct[i] = at[i] + bt[i];
else
ct[i] = ct[i - 1] + bt[i];
tat[i] = ct[i] - at[i];
wt[i] = tat[i] - bt[i];
sum_tat += tat[i];
sum_wt += wt[i];
}
// Print Table
printf("Process\tAT\tBT\tCT\tTAT\tWT\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%.0f\t%.0f\t%.0f\t%.0f\t%.0f\n", p[i], at[i], bt[i], ct[i], tat[i], wt[i]);
}
// Print Averages
printf("Average TAT = %.2f\n", sum_tat / n);
printf("Average WT = %.2f\n", sum_wt / n);
return 0;
}
Manual 9.b
#include <stdio.h>
#define max 30
int main() {
int n, i, j, t = 0, min, p[max], bt[max], at[max], wt[max], tat[max], ct[max], completed[max];
float awt = 0, atat = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the process numbers: ");
for (i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
printf("Enter the Burst Times: ");
for (i = 0; i < n; i++) {
scanf("%d", &bt[i]);
}
printf("Enter the Arrival Times: ");
for (i = 0; i < n; i++) {
scanf("%d", &at[i]);
completed[i] = 0;
}
int count = 0;
printf("\nGantt Chart: ");
while (count < n) {
min = -1;
for (i = 0; i < n; i++) {
if (!completed[i] && at[i] <= t) {
if (min == -1 || bt[i] < bt[min] ||
(bt[i] == bt[min] && at[i] < at[min]) ||
(bt[i] == bt[min] && at[i] == at[min] && p[i] < p[min])) {
min = i;
}
}
}
if (min == -1) {
t++;
continue;
}
t += bt[min];
ct[min] = t;
tat[min] = ct[min] - at[min];
wt[min] = tat[min] - bt[min];
awt += wt[min];
atat += tat[min];
completed[min] = 1;
count++;
printf("P%d ", p[min]);
}
printf("\n\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\n", p[i], at[i], bt[i], ct[i], tat[i], wt[i]);
}
printf("\nAverage Waiting Time = %.2f", awt / n);
printf("\nAverage Turnaround Time = %.2f\n", atat / n);
return 0;
}
Manual 8.c
#include<stdio.h>
#include<stdbool.h>
struct process_struct {
float at; // Arrival Time
float bt; // Burst Time
float ct; // Completion Time
float wt; // Waiting Time
float tat; // Turnaround Time
float rt; // Response Time
float start_time;
int process_num;
} ps[100];
int main() {
int n;
float bt_remaining[100];
bool is_completed[100] = {false};
int current_time = 0;
int completed = 0;
printf("Enter total number of processes: ");
scanf("%d", &n);
float sum_tat = 0, sum_wt = 0, sum_rt = 0;
printf("\nEnter Process Numbers: ");
for (int i = 0; i < n; i++) {
scanf("%d", &ps[i].process_num);
}
printf("\nEnter Arrival Times: ");
for (int i = 0; i < n; i++) {
scanf("%f", &ps[i].at);
}
printf("\nEnter Burst Times: ");
for (int i = 0; i < n; i++) {
scanf("%f", &ps[i].bt);
bt_remaining[i] = ps[i].bt;
}
while (completed != n) {
int min_index = -1;
int minimum = 10000000;
for (int i = 0; i < n; i++) {
if (ps[i].at <= current_time && is_completed[i] == false) {
if (bt_remaining[i] < minimum) {
minimum = bt_remaining[i];
min_index = i;
} else if (bt_remaining[i] == minimum) {
if (ps[i].at < ps[min_index].at) {
minimum = bt_remaining[i];
min_index = i;
}}}}
if (min_index == -1) {
current_time++;
} else {
if (bt_remaining[min_index] == ps[min_index].bt)
ps[min_index].start_time = current_time;
bt_remaining[min_index] -= 1;
current_time++;
if (bt_remaining[min_index] == 0) {
ps[min_index].ct = current_time;
ps[min_index].tat = ps[min_index].ct - ps[min_index].at;
ps[min_index].wt = ps[min_index].tat - ps[min_index].bt;
ps[min_index].rt = ps[min_index].start_time - ps[min_index].at;
sum_tat += ps[min_index].tat;
sum_wt += ps[min_index].wt;
sum_rt += ps[min_index].rt;
completed++;
is_completed[min_index] = true;
}}
}printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\tRT\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n",
ps[i].process_num,
ps[i].at,
ps[i].bt,
ps[i].ct,
ps[i].tat,
ps[i].wt,
ps[i].rt);
}
printf("\nAverage Waiting Time = %.2f", sum_wt / n);
printf("\nAverage Response Time = %.2f", sum_rt / n);
printf("\nAverage Turnaround Time = %.2f\n", sum_tat / n);
return 0;
}
+++++++