Operating System Lab Programs in C
// 1. FCFS CPU Scheduling
#include <stdio.h>
void fcfs() {
int n, i;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[n], wt[n], tat[n];
printf("Enter burst time for each process:\n");
for(i = 0; i < n; i++) {
printf("P%d: ", i+1);
scanf("%d", &bt[i]);
}
wt[0] = 0;
for(i = 1; i < n; i++) {
wt[i] = wt[i-1] + bt[i-1];
}
for(i = 0; i < n; i++) {
tat[i] = wt[i] + bt[i];
}
printf("\nProcess\tBT\tWT\tTAT\n");
for(i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\n", i+1, bt[i], wt[i], tat[i]);
}
}
// 2. SJF CPU Scheduling
void sjf() {
int n, i, j;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[n], wt[n], tat[n], p[n], temp;
for(i = 0; i < n; i++) {
printf("Enter burst time for P%d: ", i+1);
scanf("%d", &bt[i]);
p[i] = i;
}
for(i = 0; i < n-1; i++) {
for(j = i+1; j < n; j++) {
if(bt[i] > bt[j]) {
temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;
temp = p[i]; p[i] = p[j]; p[j] = temp;
}
}
}
wt[0] = 0;
for(i = 1; i < n; i++) {
wt[i] = wt[i-1] + bt[i-1];
}
for(i = 0; i < n; i++) {
tat[i] = wt[i] + bt[i];
}
printf("\nProcess\tBT\tWT\tTAT\n");
for(i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\n", p[i]+1, bt[i], wt[i], tat[i]);
}
}
// 3. Round Robin Scheduling
void round_robin() {
int n, tq, i, time = 0;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[n], rem_bt[n], wt[n], tat[n];
printf("Enter burst time for each process:\n");
for(i = 0; i < n; i++) {
printf("P%d: ", i+1);
scanf("%d", &bt[i]);
rem_bt[i] = bt[i];
wt[i] = 0;
}
printf("Enter time quantum: ");
scanf("%d", &tq);
int done;
do {
done = 1;
for(i = 0; i < n; i++) {
if(rem_bt[i] > 0) {
done = 0;
if(rem_bt[i] > tq) {
time += tq;
rem_bt[i] -= tq;
} else {
time += rem_bt[i];
wt[i] = time - bt[i];
rem_bt[i] = 0;
}
}
}
} while(!done);
for(i = 0; i < n; i++) {
tat[i] = wt[i] + bt[i];
}
printf("\nProcess\tBT\tWT\tTAT\n");
for(i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\n", i+1, bt[i], wt[i], tat[i]);
}
}
// 4. Banker's Algorithm for Deadlock Avoidance
#include <stdbool.h>
void bankers() {
int n, m, i, j, k;
printf("Enter number of processes and resources: ");
scanf("%d%d", &n, &m);
int alloc[n][m], max[n][m], avail[m], need[n][m], finish[n], ans[n], ind = 0;
printf("Enter allocation matrix:\n");
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
scanf("%d", &alloc[i][j]);
printf("Enter max matrix:\n");
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
scanf("%d", &max[i][j]);
printf("Enter available resources:\n");
for(i = 0; i < m; i++)
scanf("%d", &avail[i]);
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
for(i = 0; i < n; i++) finish[i] = 0;
for(k = 0; k < n; k++) {
for(i = 0; i < n; i++) {
if(finish[i] == 0) {
bool flag = true;
for(j = 0; j < m; j++)
if(need[i][j] > avail[j]) {
flag = false; break;
}
if(flag) {
for(j = 0; j < m; j++) avail[j] += alloc[i][j];
ans[ind++] = i; finish[i] = 1;
}
}
}
}
bool safe = true;
for(i = 0; i < n; i++) if(!finish[i]) safe = false;
if(safe) {
printf("Safe sequence: ");
for(i = 0; i < n; i++) printf("P%d ", ans[i]);
printf("\n");
} else printf("System is not in safe state\n");
}