NBCA-208P — Operating System Lab Practical File
NBCA-208P
OPERATING SYSTEM LAB
LTP:0 0 3
PRACTICAL FILE
Name _______________________________
Roll No. _______________________________
Semester / Section _______________________________
Submitted To _______________________________
Page 1 of 18
NBCA-208P — Operating System Lab Practical File
INDEX
[Link]. Name of the Program Date Page No. Signature
WAP to implement First Come First Serve (FCFS) CPU
1
Scheduling Algorithm in C.
WAP to implement Shortest Job First (SJF) CPU
2
Scheduling Algorithm in C.
WAP to implement Shortest Remaining Time First (SRTF)
3
CPU Scheduling Algorithm in C.
WAP to implement PRIORITY CPU Scheduling
4
Algorithm in C.
WAP to implement ROUND ROBIN CPU Scheduling
5
Algorithm in C.
6 WAP to implement BANKER'S Algorithm in C.
WAP to implement FIFO Page Replacement Algorithm in
7
C.
WAP to implement LRU Page Replacement Algorithm in
8
C.
Page 2 of 18
NBCA-208P — Operating System Lab Practical File
Experiment No. 1
Aim: WAP to implement First Come First Serve (FCFS) CPU Scheduling Algorithm in C.
Program:
#include <stdio.h>
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[20], wt[20], tat[20];
float awt = 0, atat = 0;
printf("Enter burst time for each process:\n");
for (int i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &bt[i]);
}
wt[0] = 0;
for (int i = 1; i < n; i++)
wt[i] = wt[i - 1] + bt[i - 1];
printf("\nProcess\tBT\tWT\tTAT\n");
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
awt += wt[i];
atat += tat[i];
printf("P%d\t%d\t%d\t%d\n", i + 1, bt[i], wt[i], tat[i]);
}
printf("\nAverage Waiting Time = %.2f\n", awt / n);
printf("Average Turnaround Time = %.2f\n", atat / n);
return 0;
}
Sample Output:
Enter number of processes: 3
Enter burst time for each process:
P1: 5
P2: 3
P3: 8
Process BT WT TAT
P1 5 0 5
P2 3 5 8
P3 8 8 16
Page 3 of 18
NBCA-208P — Operating System Lab Practical File
Average Waiting Time = 4.33
Average Turnaround Time = 9.67
Result: The C program to implement First Come First Serve (FCFS) CPU Scheduling was executed
successfully and the output was verified.
Page 4 of 18
NBCA-208P — Operating System Lab Practical File
Experiment No. 2
Aim: WAP to implement Shortest Job First (SJF) CPU Scheduling Algorithm in C.
Program:
#include <stdio.h>
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[20], wt[20], tat[20], done[20], p[20];
float awt = 0, atat = 0;
printf("Enter burst time for each process:\n");
for (int i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &bt[i]);
done[i] = 0;
p[i] = i + 1;
}
int completed = 0, time = 0;
while (completed != n) {
int min_idx = -1, min_bt = 9999;
for (int i = 0; i < n; i++) {
if (!done[i] && bt[i] < min_bt) {
min_bt = bt[i];
min_idx = i;
}
}
wt[min_idx] = time;
time += bt[min_idx];
tat[min_idx] = time;
done[min_idx] = 1;
completed++;
}
printf("\nProcess\tBT\tWT\tTAT\n");
for (int i = 0; i < n; i++) {
awt += wt[i];
atat += tat[i];
printf("P%d\t%d\t%d\t%d\n", p[i], bt[i], wt[i], tat[i]);
}
printf("\nAverage Waiting Time = %.2f\n", awt / n);
printf("Average Turnaround Time = %.2f\n", atat / n);
return 0;
Page 5 of 18
NBCA-208P — Operating System Lab Practical File
Sample Output:
Enter number of processes: 3
Enter burst time for each process:
P1: 5
P2: 3
P3: 8
Process BT WT TAT
P1 5 3 8
P2 3 0 3
P3 8 8 16
Average Waiting Time = 3.67
Average Turnaround Time = 9.00
Result: The C program to implement Shortest Job First (SJF) CPU Scheduling was executed
successfully and the output was verified.
Page 6 of 18
NBCA-208P — Operating System Lab Practical File
Experiment No. 3
Aim: WAP to implement Shortest Remaining Time First (SRTF) CPU Scheduling Algorithm in C.
Program:
#include <stdio.h>
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[20], art[20], rt[20], wt[20], tat[20];
int completion[20], is_completed[20] = {0};
float awt = 0, atat = 0;
printf("Enter arrival time and burst time of each process:\n");
for (int i = 0; i < n; i++) {
printf("P%d Arrival Time: ", i + 1);
scanf("%d", &art[i]);
printf("P%d Burst Time: ", i + 1);
scanf("%d", &bt[i]);
rt[i] = bt[i];
}
int complete = 0, time = 0, min_idx;
while (complete != n) {
int min_rt = 9999;
min_idx = -1;
for (int i = 0; i < n; i++) {
if (art[i] <= time && !is_completed[i] && rt[i] < min_rt &&
rt[i] > 0) {
min_rt = rt[i];
min_idx = i;
}
}
if (min_idx == -1) {
time++;
continue;
}
rt[min_idx]--;
time++;
if (rt[min_idx] == 0) {
complete++;
is_completed[min_idx] = 1;
completion[min_idx] = time;
tat[min_idx] = completion[min_idx] - art[min_idx];
Page 7 of 18
NBCA-208P — Operating System Lab Practical File
wt[min_idx] = tat[min_idx] - bt[min_idx];
}
}
printf("\nProcess\tBT\tAT\tWT\tTAT\n");
for (int i = 0; i < n; i++) {
awt += wt[i];
atat += tat[i];
printf("P%d\t%d\t%d\t%d\t%d\n", i + 1, bt[i], art[i], wt[i],
tat[i]);
}
printf("\nAverage Waiting Time = %.2f\n", awt / n);
printf("Average Turnaround Time = %.2f\n", atat / n);
return 0;
}
Sample Output:
Enter number of processes: 3
Enter arrival time and burst time of each process:
P1 Arrival Time: 0
P1 Burst Time: 8
P2 Arrival Time: 1
P2 Burst Time: 4
P3 Arrival Time: 2
P3 Burst Time: 9
Process BT AT WT TAT
P1 8 0 4 12
P2 4 1 0 4
P3 9 2 10 19
Average Waiting Time = 4.67
Average Turnaround Time = 11.67
Result: The C program to implement Shortest Remaining Time First (SRTF) CPU Scheduling was
executed successfully and the output was verified.
Page 8 of 18
NBCA-208P — Operating System Lab Practical File
Experiment No. 4
Aim: WAP to implement PRIORITY CPU Scheduling Algorithm in C.
Program:
#include <stdio.h>
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[20], pr[20], p[20], wt[20], tat[20];
float awt = 0, atat = 0;
printf("Enter burst time and priority (lower number = higher priority):\
n");
for (int i = 0; i < n; i++) {
printf("P%d Burst Time: ", i + 1);
scanf("%d", &bt[i]);
printf("P%d Priority: ", i + 1);
scanf("%d", &pr[i]);
p[i] = i + 1;
}
/* Sort processes by priority (ascending) */
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (pr[j] > pr[j + 1]) {
int t = pr[j]; pr[j] = pr[j + 1]; pr[j + 1] = t;
t = bt[j]; bt[j] = bt[j + 1]; bt[j + 1] = t;
t = p[j]; p[j] = p[j + 1]; p[j + 1] = t;
}
}
}
wt[0] = 0;
for (int i = 1; i < n; i++)
wt[i] = wt[i - 1] + bt[i - 1];
printf("\nProcess\tBT\tPriority\tWT\tTAT\n");
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
awt += wt[i];
atat += tat[i];
printf("P%d\t%d\t%d\t\t%d\t%d\n", p[i], bt[i], pr[i], wt[i],
tat[i]);
}
printf("\nAverage Waiting Time = %.2f\n", awt / n);
printf("Average Turnaround Time = %.2f\n", atat / n);
Page 9 of 18
NBCA-208P — Operating System Lab Practical File
return 0;
}
Sample Output:
Enter number of processes: 3
Enter burst time and priority (lower number = higher priority):
P1 Burst Time: 10
P1 Priority: 3
P2 Burst Time: 1
P2 Priority: 1
P3 Burst Time: 2
P3 Priority: 4
Process BT Priority WT TAT
P2 1 1 0 1
P1 10 3 1 11
P3 2 4 11 13
Average Waiting Time = 4.00
Average Turnaround Time = 8.33
Result: The C program to implement Priority CPU Scheduling was executed successfully and the
output was verified.
Page 10 of 18
NBCA-208P — Operating System Lab Practical File
Experiment No. 5
Aim: WAP to implement ROUND ROBIN CPU Scheduling Algorithm in C.
Program:
#include <stdio.h>
int main() {
int n, tq;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[20], rt[20], wt[20], tat[20];
printf("Enter burst time for each process:\n");
for (int i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &bt[i]);
rt[i] = bt[i];
}
printf("Enter time quantum: ");
scanf("%d", &tq);
int time = 0;
float awt = 0, atat = 0;
while (1) {
int done = 1;
for (int i = 0; i < n; i++) {
if (rt[i] > 0) {
done = 0;
if (rt[i] > tq) {
time += tq;
rt[i] -= tq;
} else {
time += rt[i];
wt[i] = time - bt[i];
rt[i] = 0;
}
}
}
if (done == 1)
break;
}
printf("\nProcess\tBT\tWT\tTAT\n");
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
awt += wt[i];
atat += tat[i];
Page 11 of 18
NBCA-208P — Operating System Lab Practical File
printf("P%d\t%d\t%d\t%d\n", i + 1, bt[i], wt[i], tat[i]);
}
printf("\nAverage Waiting Time = %.2f\n", awt / n);
printf("Average Turnaround Time = %.2f\n", atat / n);
return 0;
}
Sample Output:
Enter number of processes: 3
Enter burst time for each process:
P1: 10
P2: 5
P3: 8
Enter time quantum: 2
Process BT WT TAT
P1 10 13 23
P2 5 10 15
P3 8 13 21
Average Waiting Time = 12.00
Average Turnaround Time = 19.67
Result: The C program to implement Round Robin CPU Scheduling was executed successfully and
the output was verified.
Page 12 of 18
NBCA-208P — Operating System Lab Practical File
Experiment No. 6
Aim: WAP to implement BANKER'S Algorithm in C.
Program:
#include <stdio.h>
int main() {
int n, m;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter number of resource types: ");
scanf("%d", &m);
int alloc[20][20], max[20][20], need[20][20], avail[20];
printf("Enter allocation matrix:\n");
for (int i = 0; i < n; i++) {
printf("P%d: ", i);
for (int j = 0; j < m; j++)
scanf("%d", &alloc[i][j]);
}
printf("Enter maximum matrix:\n");
for (int i = 0; i < n; i++) {
printf("P%d: ", i);
for (int j = 0; j < m; j++)
scanf("%d", &max[i][j]);
}
printf("Enter available resources:\n");
for (int j = 0; j < m; j++)
scanf("%d", &avail[j]);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
int finish[20] = {0}, safeSeq[20];
int work[20];
for (int j = 0; j < m; j++)
work[j] = avail[j];
int count = 0;
while (count < n) {
int found = 0;
for (int i = 0; i < n; i++) {
if (!finish[i]) {
int j;
for (j = 0; j < m; j++) {
if (need[i][j] > work[j])
Page 13 of 18
NBCA-208P — Operating System Lab Practical File
break;
}
if (j == m) {
for (int k = 0; k < m; k++)
work[k] += alloc[i][k];
safeSeq[count++] = i;
finish[i] = 1;
found = 1;
}
}
}
if (!found) {
printf("\nSystem is NOT in a safe state.\n");
return 0;
}
}
printf("\nSystem is in a SAFE state.\nSafe sequence: ");
for (int i = 0; i < n; i++)
printf("P%d ", safeSeq[i]);
printf("\n");
return 0;
}
Sample Output:
Enter number of processes: 5
Enter number of resource types: 3
Enter allocation matrix:
P0: 0 1 0
P1: 2 0 0
P2: 3 0 2
P3: 2 1 1
P4: 0 0 2
Enter maximum matrix:
P0: 7 5 3
P1: 3 2 2
P2: 9 0 2
P3: 2 2 2
P4: 4 3 3
Enter available resources: 3 3 2
System is in a SAFE state.
Safe sequence: P1 P3 P4 P0 P2
Result: The C program to implement Banker's Algorithm was executed successfully and the output
was verified.
Page 14 of 18
NBCA-208P — Operating System Lab Practical File
Experiment No. 7
Aim: WAP to implement FIFO Page Replacement Algorithm in C.
Program:
#include <stdio.h>
int main() {
int n, frames;
printf("Enter number of pages: ");
scanf("%d", &n);
int pages[50];
printf("Enter the page reference string:\n");
for (int i = 0; i < n; i++)
scanf("%d", &pages[i]);
printf("Enter number of frames: ");
scanf("%d", &frames);
int frame[10];
for (int i = 0; i < frames; i++)
frame[i] = -1;
int faults = 0, front = 0;
printf("\nPage\tFrames\n");
for (int i = 0; i < n; i++) {
int found = 0;
for (int j = 0; j < frames; j++) {
if (frame[j] == pages[i]) {
found = 1;
break;
}
}
if (!found) {
frame[front] = pages[i];
front = (front + 1) % frames;
faults++;
}
printf("%d\t", pages[i]);
for (int j = 0; j < frames; j++) {
if (frame[j] != -1)
printf("%d ", frame[j]);
}
printf("\n");
}
printf("\nTotal Page Faults = %d\n", faults);
Page 15 of 18
NBCA-208P — Operating System Lab Practical File
return 0;
}
Sample Output:
Enter number of pages: 9
Enter the page reference string:
7 0 1 2 0 3 0 4 2
Enter number of frames: 3
Page Frames
7 7
0 7 0
1 7 0 1
2 2 0 1
0 2 0 1
3 2 3 1
0 2 3 0
4 4 3 0
2 4 2 0
Total Page Faults = 8
Result: The C program to implement FIFO Page Replacement Algorithm was executed successfully
and the output was verified.
Page 16 of 18
NBCA-208P — Operating System Lab Practical File
Experiment No. 8
Aim: WAP to implement LRU Page Replacement Algorithm in C.
Program:
#include <stdio.h>
int main() {
int n, frames;
printf("Enter number of pages: ");
scanf("%d", &n);
int pages[50];
printf("Enter the page reference string:\n");
for (int i = 0; i < n; i++)
scanf("%d", &pages[i]);
printf("Enter number of frames: ");
scanf("%d", &frames);
int frame[10], time_used[10];
for (int i = 0; i < frames; i++) {
frame[i] = -1;
time_used[i] = 0;
}
int faults = 0;
printf("\nPage\tFrames\n");
for (int i = 0; i < n; i++) {
int found = 0, idx = -1;
for (int j = 0; j < frames; j++) {
if (frame[j] == pages[i]) {
found = 1;
idx = j;
break;
}
}
if (found) {
time_used[idx] = i;
} else {
int free_idx = -1;
for (int j = 0; j < frames; j++) {
if (frame[j] == -1) {
free_idx = j;
break;
}
}
Page 17 of 18
NBCA-208P — Operating System Lab Practical File
if (free_idx != -1) {
frame[free_idx] = pages[i];
time_used[free_idx] = i;
} else {
int lru_idx = 0;
for (int j = 1; j < frames; j++) {
if (time_used[j] < time_used[lru_idx])
lru_idx = j;
}
frame[lru_idx] = pages[i];
time_used[lru_idx] = i;
}
faults++;
}
printf("%d\t", pages[i]);
for (int j = 0; j < frames; j++) {
if (frame[j] != -1)
printf("%d ", frame[j]);
}
printf("\n");
}
printf("\nTotal Page Faults = %d\n", faults);
return 0;
}
Sample Output:
Enter number of pages: 9
Enter the page reference string:
7 0 1 2 0 3 0 4 2
Enter number of frames: 3
Page Frames
7 7
0 7 0
1 7 0 1
2 2 0 1
0 2 0 1
3 2 0 3
0 2 0 3
4 4 0 3
2 4 0 2
Total Page Faults = 7
Result: The C program to implement LRU Page Replacement Algorithm was executed successfully
and the output was verified.
Page 18 of 18