Os Program
Os Program
#include <stdio.h>
struct Process {
int pid;
int burst_time;
int remaining_time;
int waiting_time;
int turnaround_time;
};
int main() {
scanf("%d", &n);
p[i].pid = i + 1;
scanf("%d", &p[i].burst_time);
p[i].remaining_time = p[i].burst_time;
p[i].waiting_time = 0;
p[i].turnaround_time = 0;
scanf("%d", &quantum);
int done;
do {
done = 1;
if (p[i].remaining_time > 0) {
done = 0;
time += quantum;
p[i].remaining_time -= quantum;
} else {
time += p[i].remaining_time;
p[i].remaining_time = 0;
} while (!done);
total_wt += p[i].waiting_time;
total_tat += p[i].turnaround_time;
printf("\nProcess\tBurst\tWaiting\tTurnaround\n");
return 0;
P1 10 13 23
P2 5 9 14
P3 8 12 20
SJF PROGRAM
#include <stdio.h>
int main() {
int n, i, j;
scanf("%d", &bt[i]);
p[i] = i + 1;
temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
// Calculate waiting time
wt[0] = 0;
avg_wt += wt[i];
avg_tat += tat[i];
// Print results
printf("\nProcess\tBurst\tWaiting\tTurnaround\n");
return 0;
}
[user@localhost ~]$ vi sjf.c
P2 3 0 3
P1 5 3 8
P3 8 8 16
FCFS PROGRAM
#include <stdio.h>
int main() {
int n, i;
int bt[20], wt[20], tat[20];
scanf("%d", &n);
scanf("%d", &bt[i]);
avg_wt += wt[i];
avg_tat += tat[i];
printf("\nProcess\tBurst\tWaiting\tTurnaround\n");
return 0;
P1 5 0 5
P2 3 5 8
P3 8 8 16
Average Waiting Time = 4.33
#include <stdio.h>
int main() {
int n, i, j;
scanf("%d", &n);
p[i] = i + 1;
scanf("%d", &bt[i]);
scanf("%d", &priority[i]);
// Swap priority
temp = priority[i];
priority[i] = priority[j];
priority[j] = temp;
temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
// Swap process ID
temp = p[i];
p[i] = p[j];
p[j] = temp;
wt[0] = 0;
avg_wt += wt[i];
avg_tat += tat[i];
printf("\nProcess\tBurst\tPriority\tWaiting\tTurnaround\n");
return 0;
P2 3 1 0 3
P1 5 2 3 8
P3 8 3 8 16
SEQUENTIAL ALLOCATION:
#include <stdio.h>
int main() {
f[i] = 0;
X:
f[j] = 1;
} else {
break;
if (j == (st + len))
scanf("%d", &c);
if (c == 1)
goto X;
else
exit(0);
OUTPUT:
4-> Allocated
5-> Allocated
6-> Allocated
7-> Allocated
8 -> Allocated
9-> Allocated
10-> Allocated
11 -> Allocated
12 -> Allocated
13 -> Allocated
// File: indexed_allocation.c
#include <stdio.h>
#include <stdlib.h>
int main() {
f[i] = 0;
scanf("%d", &indexBlock);
if (f[indexBlock] == 0) {
f[indexBlock] = 1;
scanf("%d", &n);
printf("Enter blocks to be allocated:\n");
scanf("%d", &b[i]);
if (f[b[i]] == 0)
count++;
if (count == n) {
f[b[i]] = 1;
} else {
} else {
return 0;
}
Output:
10
11
12
Index Block: 5
// File: linked_allocation.c
#include <stdio.h>
int main() {
if (files[i] == 0) {
files[i] = 1;
} else {
flag = 1;
break;
if (flag == 0) {
} else {
return 0;
Output: