OS Practical
OS Practical
int main() {
int n, i, j, temp;
scanf("%d", &n);
scanf("%d", &at[i]);
scanf("%d", &bt[i]);
int current_time = 0;
current_time = at[i];
total_wt += wt[i];
total_tat += tat[i];
printf("\nP\tAT\tBT\tCT\tTAT\tWT\n");
printf("\nGantt Chart:\n|");
printf("\n0");
return 0;
Output
Number of processes: 3
P AT BT CT TAT WT
P1 1 3 4 3 0
P2 2 4 8 6 2
P3 3 5 13 10 5
Avg TAT: 6.33
Gantt Chart:
| P1 | P2 | P3 |
0 4 8 13
int main() {
scanf("%d", &n);
scanf("%d", &at[i]);
scanf("%d", &bt[i]);
while(completed < n) {
min_idx = -1;
min_bt = bt[i];
min_idx = i;
if(min_idx == -1) {
current_time++; // CPU idle
} else {
current_time += bt[min_idx];
ct[min_idx] = current_time;
total_wt += wt[min_idx];
total_tat += tat[min_idx];
is_completed[min_idx] = 1;
completed++;
printf("\nP\tAT\tBT\tCT\tTAT\tWT\n");
if(is_completed[i])
printf("\n0");
printf("\n");
return 0;
}
Output
Number of processes: 3
P AT BT CT TAT WT
P1 1 3 4 3 0
P2 3 5 9 6 1
P3 2 6 15 13 7
| P1 | P2 | P3 |
0 4 9 15
#define MAX 20
int main() {
scanf("%d", &tq);
scanf("%d", &at[i]);
scanf("%d", &bt[i]);
rt[i] = bt[i];
time++;
rq[++rear] = i, in_queue[i] = 1;
continue;
gantt[gc] = p;
gantt_time[gc] = time;
time += slice;
rt[p] -= slice;
if (!in_queue[i] && at[i] > gantt_time[gc] && at[i] <= time && rt[i] > 0)
rq[++rear] = i, in_queue[i] = 1;
if (rt[p] > 0)
rq[++rear] = p;
else {
ct[p] = time;
completed++;
rq_size[gc] = 0;
rq_log[gc][rq_size[gc]++] = rq[i];
gc++;
gantt_time[gc] = time;
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
total_tat += tat[i];
total_wt += wt[i];
for (int i = 0; i < gc; i++) printf(" P%d |", gantt[i] + 1);
printf("\n%d", gantt_time[0]);
printf("\n");
if (rq_size[i] == 0) printf("[empty]");
else for (int j = 0; j < rq_size[i]; j++) printf("P%d ", rq_log[i][j] + 1);
printf("\n");
return 0;
Output
Process AT BT CT TAT WT
P1 0 5 13 13 8
P2 1 3 12 11 8
P3 2 1 5 3 2
P4 3 2 9 6 4
P5 4 3 14 10 7
| P1 | P2 | P3 | P1 | P4 | P5 | P2 | P1 | P5 |
0 2 4 5 7 9 11 12 13 14
After P1 (0-2): P2 P3 P1
After P2 (2-4): P3 P1 P4 P5 P2
After P3 (4-5): P1 P4 P5 P2
After P1 (5-7): P4 P5 P2 P1
After P4 (7-9): P5 P2 P1
After P5 (9-11): P2 P1 P5
After P2 (11-12): P1 P5
After P1 (12-13): P5
#include <stdbool.h>
int main() {
int max[P][R] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}
};
int allocation[P][R] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}
};
int safeSequence[P];
int count = 0;
if (!finished[p]) {
canAllocate = false;
break;
if (canAllocate) {
for (int r = 0; r < R; r++) {
available[r] += allocation[p][r];
safeSequence[count++] = p;
finished[p] = true;
found = true;
if (!found) {
return 1;
printf("\n");
return 0;
Output
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define SIZE 5
pthread_mutex_t mutex;
int item = 0;
while (1) {
item++;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer[in] = item;
in = (in + 1) % SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&full);
sleep(1);
int item;
while (1) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
item = buffer[out];
pthread_mutex_unlock(&mutex);
sem_post(&empty);
sleep(2);
int main() {
pthread_t p, c;
sem_init(&empty, 0, SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);
pthread_join(p, NULL);
pthread_join(c, NULL);
return 0;
Output
Produced: 1
Consumed: 1
Produced: 2
Produced: 3
Consumed: 2
Produced: 4
Consumed: 3
Produced: 5
Produced: 6
Consumed: 4
Produced: 7
Consumed: 5
Consumed: 6
...
6. Write a C program to illustrate the IPC mechanism using Pipes.
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main() {
pid_t pid;
char buffer[100];
if (pipe(fd) == -1) {
perror("Pipe failed");
return 1;
pid = fork();
if (pid < 0) {
perror("Fork failed");
return 1;
// Parent process
} else {
// Child process
close(fd[1]);
return 0;
Output
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
int main() {
char buffer[100];
int fd;
mkfifo(FIFO_FILE, 0666);
int choice;
scanf("%d", &choice);
getchar();
if (choice == 1) {
// Writer process
fd = open(FIFO_FILE, O_WRONLY);
close(fd);
printf("Message sent.\n");
} else if (choice == 2) {
// Reader process
fd = open(FIFO_FILE, O_RDONLY);
close(fd);
} else {
printf("Invalid choice.\n");
return 0;
Output
$ ./fifo_ipc
1. Enter 1 to write
2. Enter 2 to read
Message sent.
int main() {
scanf("%d", &n_pages);
scanf("%d", &page_table[i]);
scanf("%d", &logical_address);
return 1;
return 0;
Output
int main() {
scanf("%d", &segment_count);
scanf("%d", &base[i]);
scanf("%d", &limit[i]);
scanf("%d", &segment_number);
scanf("%d", &offset);
return 1;
return 1;
}
int physical_address = base[segment_number] + offset;
return 0;
Output
Segment 0:
Base: 1000
Limit: 400
Segment 1:
Base: 2000
Limit: 300
Segment 2:
Base: 3000
Limit: 500
10. Write a C program to simulate the Best Fit contiguous memory allocation technique.
int main() {
int allocation[10];
scanf("%d", &blocks[i]);
scanf("%d", &p_count);
scanf("%d", &processes[i]);
allocation[i] = -1;
best_idx = j;
if (best_idx != -1) {
allocation[i] = best_idx;
blocks[best_idx] -= processes[i];
printf("%d\t\t%d\t\t", i, processes[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i]);
else
printf("Not Allocated\n");
return 0;
Output
Block 0: 100
Block 1: 500
Block 2: 200
Block 3: 300
Block 4: 600
Process 0: 212
Process 1: 417
Process 2: 112
Process 3: 426
0 212 3
1 417 1
2 112 2
3 426 4
11. Write a C program to simulate the First Fit contiguous memory allocation technique.
int main() {
int allocation[10];
scanf("%d", &b_count);
scanf("%d", &blocks[i]);
scanf("%d", &p_count);
scanf("%d", &processes[i]);
allocation[i] = -1;
allocation[i] = j;
blocks[j] -= processes[i];
break;
}
}
printf("%d\t\t%d\t\t", i, processes[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i]);
else
printf("Not Allocated\n");
return 0;
Output
Block 0: 100
Block 1: 500
Block 2: 200
Block 3: 300
Block 4: 600
Process 0: 212
Process 1: 417
Process 2: 112
Process 3: 426
0 212 1
1 417 4
2 112 2
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define N 5
sem_t forks[N];
pthread_t philosophers[N];
int phil_ids[N];
int id = *(int*)arg;
while (1) {
sleep(1);
if (id % 2 == 0) {
sem_wait(&forks[left]);
sem_wait(&forks[right]);
} else {
sem_wait(&forks[right]);
sem_wait(&forks[left]);
sleep(2);
sem_post(&forks[left]);
sem_post(&forks[right]);
return NULL;
int main() {
sem_init(&forks[i], 0, 1);
phil_ids[i] = i;
pthread_join(philosophers[i], NULL);
return 0;
Output
Philosopher 0 is thinking...
Philosopher 1 is thinking...
Philosopher 0 is hungry.
Philosopher 0 is eating.
Philosopher 1 is hungry.
Philosopher 2 is thinking...
Philosopher 1 is eating.
...
int main() {
int total_memory, used_memory = 0;
scanf("%d", &total_memory);
while (1) {
printf("\nEnter memory required for process %d (in KB): ", process_count + 1);
scanf("%d", &process_size);
used_memory += process_size;
process_count++;
} else {
scanf("%d", &choice);
if (choice == 0) break;
return 0;
Output
int main() {
scanf("%d", &n_pages);
scanf("%d", &pages[i]);
scanf("%d", &n_frames);
frames[i] = -1;
printf("\nPage\tFrames\n");
found = 0;
if (frames[j] == pages[i]) {
found = 1;
break;
if (!found) {
page_faults++;
printf("%d\t", pages[i]);
if (frames[j] != -1)
printf("- ");
printf("\n");
return 0;
Output
130356336272
Page Frames
1 1--
3 13-
0 130
5 530
6 560
3 563
2 263
7 273
15. Write a C program to write a C program for implementing sequential file allocation method.
int main() {
int memory[50] = {0}; // Simulate 50 disk blocks, 0 = free, 1 = allocated
scanf("%d", &n_files);
continue;
flag = 0;
if (memory[j] == 1) {
break;
if (flag == 0) {
memory[j] = 1;
} else {
if ((i + 1) % 10 == 0)
printf("\n");
return 0;
Output
0000011111
1000011111
0000000000
0000000000
0000000000