OPERATING SYSTEM LAB PROGRAMS
1. FCFS (First Come First Serve) Scheduling
Program (C Language)
#include <stdio.h>
int main() {
int n, i;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[n], wt[n], tat[n];
printf("Enter burst times:\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]);
return 0;
}
1. FCFS Scheduling – Expected Input & Output
INPUT
Enter number of processes: 4
Enter burst times:
P1: 5
P2: 3
P3: 8
P4: 6
OUTPUT
Process BT WT TAT
P1 5 0 5
P2 3 5 8
P3 8 8 16
P4 6 16 22
2. SJF (Shortest Job First) Scheduling – Non-preemptive
Program (C Language)
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[n], p[n], wt[n], tat[n];
for(i = 0; i < n; i++) {
printf("Enter burst time of P%d: ", i+1);
scanf("%d", &bt[i]);
p[i] = i + 1;
}
for(i = 0; i < n; i++) {
for(j = i+1; j < n; j++) {
if(bt[i] > bt[j]) {
int 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], bt[i], wt[i], tat[i]);
return 0;
}
2. SJF Scheduling – Expected Input & Output
INPUT
Enter number of processes: 4
Enter burst time of P1: 6
Enter burst time of P2: 2
Enter burst time of P3: 8
Enter burst time of P4: 3
OUTPUT
Process BT WT TAT
P2 2 0 2
P4 3 2 5
P1 6 5 11
P3 8 11 19
3. Priority Scheduling (Non-preemptive)
Program (C Language)
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[n], pr[n], p[n], wt[n], tat[n];
for(i = 0; i < n; i++) {
printf("Enter burst time and priority of P%d: ", i+1);
scanf("%d %d", &bt[i], &pr[i]);
p[i] = i + 1;
}
for(i = 0; i < n; i++) {
for(j = i+1; j < n; j++) {
if(pr[i] > pr[j]) {
int temp = pr[i]; pr[i] = pr[j]; pr[j] = temp;
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\tPri\tWT\tTAT\n");
for(i = 0; i < n; i++)
printf("P%d\t%d\t%d\t%d\t%d\n", p[i], bt[i], pr[i], wt[i], tat[i]);
return 0;
}
3. Priority Scheduling – Expected Input & Output
INPUT
(Here smaller number = higher priority)
Enter number of processes: 4
Enter burst time and priority of P1: 5 2
Enter burst time and priority of P2: 3 1
Enter burst time and priority of P3: 8 4
Enter burst time and priority of P4: 6 3
OUTPUT
Process BT Pri WT TAT
P2 3 1 0 3
P1 5 2 3 8
P4 6 3 8 14
P3 8 4 14 22
4. Round Robin Scheduling
Program (C Language)
#include <stdio.h>
int main() {
int n, i, qt;
printf("Enter number of processes: ");
scanf("%d", &n);
int bt[n], rem[n], wt[n], tat[n];
for(i = 0; i < n; i++) {
printf("Enter burst time of P%d: ", i+1);
scanf("%d", &bt[i]);
rem[i] = bt[i];
wt[i] = 0;
}
printf("Enter time quantum: ");
scanf("%d", &qt);
int t = 0, done;
do {
done = 1;
for(i = 0; i < n; i++) {
if(rem[i] > 0) {
done = 0;
if(rem[i] > qt) {
t += qt;
rem[i] -= qt;
} else {
t += rem[i];
wt[i] = t - bt[i];
rem[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]);
return 0;
}
4. Round Robin Scheduling – Expected Input & Output
INPUT
Enter number of processes: 4
Enter burst time of P1: 8
Enter burst time of P2: 4
Enter burst time of P3: 9
Enter burst time of P4: 5
Enter time quantum: 3
OUTPUT (may slightly vary by implementation)
Process BT WT TAT
P1 8 17 25
P2 4 6 10
P3 9 22 31
P4 5 14 19
5. Banker's Algorithm (Deadlock Avoidance)
Program (C Language)
#include <stdio.h>
int main() {
int n, r, i, j, k;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter number of resources: ");
scanf("%d", &r);
int alloc[n][r], max[n][r], avail[r], need[n][r];
printf("Enter allocation matrix:\n");
for(i = 0; i < n; i++)
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
printf("Enter max matrix:\n");
for(i = 0; i < n; i++)
for(j = 0; j < r; j++)
scanf("%d", &max[i][j]);
printf("Enter available resources:\n");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < n; i++)
for(j = 0; j < r; j++)
need[i][j] = max[i][j] - alloc[i][j];
int finish[n], safe[n], idx = 0;
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) {
int flag = 0;
for(j = 0; j < r; j++) {
if(need[i][j] > avail[j]) {
flag = 1;
break;
}
}
if(!flag) {
for(j = 0; j < r; j++)
avail[j] += alloc[i][j];
safe[idx++] = i;
finish[i] = 1;
}
}
}
}
int safeFlag = 1;
for(i = 0; i < n; i++)
if(finish[i] == 0)
safeFlag = 0;
if(safeFlag) {
printf("\nSafe Sequence: ");
for(i = 0; i < n; i++)
printf("P%d ", safe[i]);
printf("\n");
} else {
printf("System is not in a safe state.\n");
}
return 0;
}
5. Banker's Algorithm – Expected Input & Output
INPUT
(Number of processes: 5, Resources: 3)
Enter number of processes: 5
Enter number of resources: 3
Enter allocation matrix:
010
200
302
211
002
Enter max matrix:
753
322
902
222
433
Enter available resources:
332
OUTPUT
Safe Sequence: P1 P3 P4 P0 P2
6. Write a Device Driver for Any Device or Peripheral (Simulated Keyboard Driver)
A full OS-level driver is too large, so colleges usually accept a simulated driver showing initialization, read, and write operations.
Program: Simulated Keyboard Device Driver
#include <stdio.h>
#include <string.h>
char keyboard_buffer[100];
void device_init() {
strcpy(keyboard_buffer, "");
printf("Keyboard Driver Initialized...\n");
}
void device_write(char c[]) {
strcpy(keyboard_buffer, c);
printf("Driver WRITE: Data written to buffer.\n");
}
void device_read() {
printf("Driver READ: %s\n", keyboard_buffer);
}
int main() {
device_init();
char data[100];
printf("Enter data to send to keyboard device: ");
scanf("%s", data);
device_write(data);
printf("Reading data from device...\n");
device_read();
return 0;
}
Expected Input
hello
Expected Output
Keyboard Driver Initialized...
Enter data to send to keyboard device: hello
Driver WRITE: Data written to buffer.
Reading data from device...
Driver READ: hello
7. Disk Scheduling Algorithm (SCAN Example)
Here we implement SCAN (Elevator Algorithm).
Program: SCAN Disk Scheduling
#include <stdio.h>
#include <stdlib.h>
int main() {
int queue[20], head, n, i, j, temp;
printf("Enter number of disk requests: ");
scanf("%d", &n);
printf("Enter disk requests: ");
for(i = 0; i < n; i++)
scanf("%d", &queue[i]);
printf("Enter initial head position: ");
scanf("%d", &head);
queue[n] = head;
n++;
for(i = 0; i < n; i++)
for(j = i+1; j < n; j++)
if(queue[i] > queue[j]) {
temp = queue[i];
queue[i] = queue[j];
queue[j] = temp;
}
int pos = 0;
for(i = 0; i < n; i++)
if(queue[i] == head) pos = i;
int seek = 0;
printf("Order of service: ");
for(i = pos; i < n; i++) {
printf("%d ", queue[i]);
if(i != pos)
seek += abs(queue[i] - queue[i-1]);
}
seek += abs(queue[n-1] - queue[pos-1]);
printf("%d ", queue[pos-1]);
for(i = pos-1; i > 0; i--) {
printf("%d ", queue[i-1]);
seek += abs(queue[i] - queue[i-1]);
}
printf("\nTotal Seek Time = %d\n", seek);
}
Expected Input
5
55 58 39 18 90
50
Expected Output
Order of service: 55 58 90 39 18
Total Seek Time = 142
8. Dining Philosopher Problem
Program: Dining Philosopher Using Semaphores (POSIX)
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
sem_t forks[5];
void* philosopher(void* num) {
int id = *(int*)num;
printf("Philosopher %d is thinking...\n", id);
sleep(1);
sem_wait(&forks[id]);
sem_wait(&forks[(id+1)%5]);
printf("Philosopher %d is eating...\n", id);
sleep(2);
sem_post(&forks[id]);
sem_post(&forks[(id+1)%5]);
printf("Philosopher %d finished eating.\n", id);
}
int main() {
pthread_t ph[5];
int ids[5];
for(int i=0;i<5;i++)
sem_init(&forks[i],0,1);
for(int i=0;i<5;i++){
ids[i]=i;
pthread_create(&ph[i], NULL, philosopher, &ids[i]);
}
for(int i=0;i<5;i++)
pthread_join(ph[i],NULL);
return 0;
}
Expected Output
Philosopher 0 is thinking...
Philosopher 1 is thinking...
Philosopher 2 is thinking...
Philosopher 3 is thinking...
Philosopher 4 is thinking...
Philosopher 2 is eating...
Philosopher 0 is eating...
Philosopher 2 finished eating.
Philosopher 4 is eating...
...
(Output order varies due to threads.)
9. Producer–Consumer Problem Using Semaphores
Program
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define SIZE 5
int buffer[SIZE], in=0, out=0;
sem_t empty, full, mutex;
void* producer(void* p) {
int item;
while(1) {
item = rand() % 100;
sem_wait(&empty);
sem_wait(&mutex);
buffer[in] = item;
printf("Produced: %d\n", item);
in = (in + 1) % SIZE;
sem_post(&mutex);
sem_post(&full);
sleep(1);
}
}
void* consumer(void* c) {
while(1) {
sem_wait(&full);
sem_wait(&mutex);
int item = buffer[out];
printf("Consumed: %d\n", item);
out = (out + 1) % SIZE;
sem_post(&mutex);
sem_post(&empty);
sleep(1);
}
}
int main() {
pthread_t p, c;
sem_init(&empty,0,SIZE);
sem_init(&full,0,0);
sem_init(&mutex,0,1);
pthread_create(&p,NULL,producer,NULL);
pthread_create(&c,NULL,consumer,NULL);
pthread_join(p,NULL);
pthread_join(c,NULL);
return 0;
}
Expected Output
Produced: 32
Consumed: 32
Produced: 87
Consumed: 87
Produced: 14
Consumed: 14
...
10. LRU Page Replacement Algorithm
Program: LRU
#include <stdio.h>
int main() {
int pages[20], frame[10], n, f, i, j, k, page_fault = 0;
printf("Enter number of pages: ");
scanf("%d", &n);
printf("Enter page reference string: ");
for(i=0;i<n;i++)
scanf("%d",&pages[i]);
printf("Enter number of frames: ");
scanf("%d",&f);
for(i=0;i<f;i++) frame[i] = -1;
printf("Page\tFrames\n");
for(i=0;i<n;i++) {
int hit = 0;
for(j=0;j<f;j++)
if(frame[j] == pages[i]) hit = 1;
if(hit == 0) {
int least = n, pos = -1;
for(j=0;j<f;j++) {
int found = 0;
for(k=i-1;k>=0;k--) {
if(frame[j] == pages[k]) {
found = 1;
if(k < least) {
least = k;
pos = j;
}
break;
}
}
if(found == 0) { pos = j; break; }
}
frame[pos] = pages[i];
page_fault++;
}
printf("%d\t", pages[i]);
for(j=0;j<f;j++) printf("%d ", frame[j]);
printf("\n");
}
printf("Total Page Faults: %d\n", page_fault);
return 0;
}
Expected Input
8
70120304
3
Expected Output
Page Frames
7 7 -1 -1
0 7 0 -1
1 701
2 201
0 201
3 203
0 203
4 403
Total Page Faults: 6