KEMBAR78
All OS Lab Programs | PDF | Computer Programming | Computing
0% found this document useful (0 votes)
5 views3 pages

All OS Lab Programs

The document contains C programs for various CPU scheduling algorithms including FCFS, SJF, and Round Robin, as well as the Banker's Algorithm for deadlock avoidance. Each section provides code to input process details, calculate waiting time (WT), turnaround time (TAT), and display the results. The programs are designed to help understand and implement these fundamental operating system concepts.

Uploaded by

mp1121451
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)
5 views3 pages

All OS Lab Programs

The document contains C programs for various CPU scheduling algorithms including FCFS, SJF, and Round Robin, as well as the Banker's Algorithm for deadlock avoidance. Each section provides code to input process details, calculate waiting time (WT), turnaround time (TAT), and display the results. The programs are designed to help understand and implement these fundamental operating system concepts.

Uploaded by

mp1121451
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/ 3

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");
}

You might also like