0% found this document useful (0 votes)
3 views31 pages

OS Practical

The document contains multiple C programs demonstrating various CPU scheduling algorithms, including FCFS, SJF, and Round Robin, as well as the Banker's Algorithm for deadlock avoidance, the Producer-Consumer problem using semaphores, and IPC using pipes. Each section includes code snippets, example outputs, and explanations of the algorithms and their implementations. The programs illustrate key concepts in process management and synchronization in operating systems.

Uploaded by

chirag.monujain
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)
3 views31 pages

OS Practical

The document contains multiple C programs demonstrating various CPU scheduling algorithms, including FCFS, SJF, and Round Robin, as well as the Banker's Algorithm for deadlock avoidance, the Producer-Consumer problem using semaphores, and IPC using pipes. Each section includes code snippets, example outputs, and explanations of the algorithms and their implementations. The programs illustrate key concepts in process management and synchronization in operating systems.

Uploaded by

chirag.monujain
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/ 31

1. Write C program to simulate the FCFS CPU Scheduling algorithm.

Ans: #include <stdio.h>

int main() {

int n, i, j, temp;

int at[20], bt[20], ct[20], wt[20], tat[20];

printf("Number of processes: ");

scanf("%d", &n);

for(i = 0; i < n; i++) {

printf("Arrival time P%d: ", i+1);

scanf("%d", &at[i]);

printf("Burst time P%d: ", i+1);

scanf("%d", &bt[i]);

for(i = 0; i < n-1; i++) {

for(j = i+1; j < n; j++) {

if(at[i] > at[j]) {

temp = at[i]; at[i] = at[j]; at[j] = temp;

temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;

int current_time = 0;

float total_wt = 0, total_tat = 0;

for(i = 0; i < n; i++) {

if(current_time < at[i])

current_time = at[i];

ct[i] = current_time + bt[i];

tat[i] = ct[i] - at[i];

wt[i] = tat[i] - bt[i];


current_time = ct[i];

total_wt += wt[i];

total_tat += tat[i];

printf("\nP\tAT\tBT\tCT\tTAT\tWT\n");

for(i = 0; i < n; i++)

printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i+1, at[i], bt[i], ct[i], tat[i], wt[i]);

printf("\nAvg TAT: %.2f\nAvg WT: %.2f\n", total_tat/n, total_wt/n);

printf("\nGantt Chart:\n|");

for(i = 0; i < n; i++)

printf(" P%d |", i+1);

printf("\n0");

for(i = 0; i < n; i++)

printf(" %d", ct[i]);

return 0;

Output

Number of processes: 3

Arrival time P1: 2

Burst time P1: 4

Arrival time P2: 3

Burst time P2: 5

Arrival time P3: 1

Burst time P3: 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

Avg WT: 2.33

Gantt Chart:

| P1 | P2 | P3 |

0 4 8 13

2. Write C program to simulate the SJF CPU Scheduling algorithm.

Ans: #include <stdio.h>

int main() {

int n, i, completed = 0, current_time = 0, min_idx;

int at[20], bt[20], ct[20], wt[20], tat[20], is_completed[20] = {0};

float total_wt = 0, total_tat = 0;

printf("Number of processes: ");

scanf("%d", &n);

for(i = 0; i < n; i++) {

printf("Arrival time P%d: ", i+1);

scanf("%d", &at[i]);

printf("Burst time P%d: ", i+1);

scanf("%d", &bt[i]);

while(completed < n) {

min_idx = -1;

int min_bt = 1e9;

for(i = 0; i < n; i++) {

if(at[i] <= current_time && !is_completed[i] && bt[i] < min_bt) {

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;

tat[min_idx] = ct[min_idx] - at[min_idx];

wt[min_idx] = tat[min_idx] - bt[min_idx];

total_wt += wt[min_idx];

total_tat += tat[min_idx];

is_completed[min_idx] = 1;

completed++;

printf("\nP\tAT\tBT\tCT\tTAT\tWT\n");

for(i = 0; i < n; i++)

printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i+1, at[i], bt[i], ct[i], tat[i], wt[i]);

printf("\nAvg TAT: %.2f\nAvg WT: %.2f\n", total_tat / n, total_wt / n);

printf("\nGantt Chart (process completion order):\n|");

for(i = 0; i < n; i++) {

if(is_completed[i])

printf(" P%d |", i+1);

printf("\n0");

for(i = 0; i < n; i++) {

printf(" %d", ct[i]);

printf("\n");

return 0;

}
Output

Number of processes: 3

Arrival time P1: 1

Burst time P1: 3

Arrival time P2: 3

Burst time P2: 5

Arrival time P3: 2

Burst time P3: 6

P AT BT CT TAT WT

P1 1 3 4 3 0

P2 3 5 9 6 1

P3 2 6 15 13 7

Avg TAT: 7.33

Avg WT: 2.67

Gantt Chart (process completion order):

| P1 | P2 | P3 |

0 4 9 15

3. Write C program to simulate the Round Robin CPU Scheduling algorithm.

Ans: #include <stdio.h>

#define MAX 20

int main() {

int n, tq, time = 0, completed = 0;

int at[MAX], bt[MAX], rt[MAX], ct[MAX], wt[MAX], tat[MAX];

int rq[100], front = 0, rear = -1, in_queue[MAX] = {0};

int gantt[100], gantt_time[100], rq_log[100][MAX], rq_size[100], gc = 0;

printf("Enter number of processes: ");


scanf("%d", &n);

printf("Enter time quantum: ");

scanf("%d", &tq);

for (int i = 0; i < n; i++) {

printf("Arrival time of P%d: ", i + 1);

scanf("%d", &at[i]);

printf("Burst time of P%d: ", i + 1);

scanf("%d", &bt[i]);

rt[i] = bt[i];

for (int i = 0; i < n; i++)

if (at[i] == 0) rq[++rear] = i, in_queue[i] = 1;

while (completed < n) {

if (front > rear) {

time++;

for (int i = 0; i < n; i++)

if (!in_queue[i] && at[i] <= time && rt[i] > 0)

rq[++rear] = i, in_queue[i] = 1;

continue;

int p = rq[front++]; // dequeue

gantt[gc] = p;

gantt_time[gc] = time;

int slice = (rt[p] < tq) ? rt[p] : tq;

time += slice;

rt[p] -= slice;

for (int i = 0; i < n; i++)

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;

tat[p] = ct[p] - at[p];

wt[p] = tat[p] - bt[p];

completed++;

rq_size[gc] = 0;

for (int i = front; i <= rear; i++)

rq_log[gc][rq_size[gc]++] = rq[i];

gc++;

gantt_time[gc] = time;

printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");

float total_tat = 0, total_wt = 0;

for (int i = 0; i < n; i++) {

printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i + 1, at[i], bt[i], ct[i], tat[i], wt[i]);

total_tat += tat[i];

total_wt += wt[i];

printf("\nAvg TAT: %.2f\nAvg WT: %.2f\n", total_tat / n, total_wt / n);

printf("\n--- Gantt Chart ---\n|");

for (int i = 0; i < gc; i++) printf(" P%d |", gantt[i] + 1);

printf("\n%d", gantt_time[0]);

for (int i = 1; i <= gc; i++) printf(" %d", gantt_time[i]);

printf("\n");

printf("\n--- Ready Queue Log ---\n");

for (int i = 0; i < gc; i++) {


printf("After P%d (%d-%d): ", gantt[i] + 1, gantt_time[i], gantt_time[i + 1]);

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

Enter number of processes: 5

Enter time quantum: 2

Arrival time of P1: 0

Burst time of P1: 5

Arrival time of P2: 1

Burst time of P2: 3

Arrival time of P3: 2

Burst time of P3: 1

Arrival time of P4: 3

Burst time of P4: 2

Arrival time of P5: 4

Burst time of P5: 3

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

Avg TAT: 8.60


Avg WT: 5.80

--- Gantt Chart ---

| P1 | P2 | P3 | P1 | P4 | P5 | P2 | P1 | P5 |

0 2 4 5 7 9 11 12 13 14

--- Ready Queue Log ---

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

After P5 (13-14): [empty]

4. Write a C program to simulate Bankers Algorithm for Deadlock Avoidance.

Ans: #include <stdio.h>

#include <stdbool.h>

#define P 5 // Number of processes

#define R 3 // Number of resources

int main() {

int available[R] = {3, 3, 2}; // Available instances of resources

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 need[P][R]; // Need matrix

bool finished[P] = {false};

int safeSequence[P];

int count = 0;

for (int i = 0; i < P; i++) {

for (int j = 0; j < R; j++) {

need[i][j] = max[i][j] - allocation[i][j];

while (count < P) {

bool found = false;

for (int p = 0; p < P; p++) {

if (!finished[p]) {

bool canAllocate = true;

for (int r = 0; r < R; r++) {

if (need[p][r] > available[r]) {

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) {

printf("System is not in a safe state.\n");

return 1;

printf("System is in a safe state.\nSafe sequence is: ");

for (int i = 0; i < P; i++) {

printf("P%d ", safeSequence[i]);

printf("\n");

return 0;

Output

System is in a safe state.

Safe sequence is: P1 P3 P4 P0 P2

5. Write a C program to implement the Producer – Consumer problem using semaphores.

Ans: #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;

pthread_mutex_t mutex;

void* producer(void* arg) {

int item = 0;

while (1) {

item++;

sem_wait(&empty);

pthread_mutex_lock(&mutex);

buffer[in] = item;

printf("Produced: %d\n", item);

in = (in + 1) % SIZE;

pthread_mutex_unlock(&mutex);

sem_post(&full);

sleep(1);

void* consumer(void* arg) {

int item;

while (1) {

sem_wait(&full);

pthread_mutex_lock(&mutex);

item = buffer[out];

printf("Consumed: %d\n", item);

out = (out + 1) % SIZE;

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_create(&p, NULL, producer, NULL);

pthread_create(&c, NULL, consumer, 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.

Ans: #include <stdio.h>

#include <unistd.h>

#include <string.h>

#include <stdlib.h>

int main() {

int fd[2]; // fd[0] = read end, fd[1] = write end

pid_t pid;

char message[] = "Hello from parent!";

char buffer[100];

if (pipe(fd) == -1) {

perror("Pipe failed");

return 1;

pid = fork();

if (pid < 0) {

perror("Fork failed");

return 1;

else if (pid > 0) {

// Parent process

close(fd[0]); // Close unused read end

write(fd[1], message, strlen(message) + 1);

close(fd[1]); // Close write end after writing

} else {

// Child process

close(fd[1]);

read(fd[0], buffer, sizeof(buffer));

printf("Child received: %s\n", buffer);


close(fd[0]);

return 0;

Output

Child received: Hello from parent!

7. Write a C program to illustrate the IPC mechanism using FIFOs.

Ans: #include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/stat.h>

#include <string.h>

#define FIFO_FILE "myfifo"

int main() {

char buffer[100];

int fd;

mkfifo(FIFO_FILE, 0666);

printf("1. Enter 1 to write\n");

printf("2. Enter 2 to read\n");

int choice;

printf("Enter your choice(1 or 2):");

scanf("%d", &choice);

getchar();

if (choice == 1) {

// Writer process

fd = open(FIFO_FILE, O_WRONLY);

printf("Enter message to send: ");

fgets(buffer, sizeof(buffer), stdin);


write(fd, buffer, strlen(buffer) + 1);

close(fd);

printf("Message sent.\n");

} else if (choice == 2) {

// Reader process

fd = open(FIFO_FILE, O_RDONLY);

read(fd, buffer, sizeof(buffer));

printf("Received message: %s\n", buffer);

close(fd);

} else {

printf("Invalid choice.\n");

return 0;

Output

$ ./fifo_ipc

1. Enter 1 to write

2. Enter 2 to read

Enter your choice(1 or 2):1

Enter message to send: Hello FIFO!

Message sent.

8. Write a C program to simulate Paging memory management technique.

Ans: #include <stdio.h>

int main() {

int page_table[20], n_pages, frame_size;

int logical_address, page_number, offset, physical_address;

printf("Enter number of pages: ");

scanf("%d", &n_pages);

printf("Enter frame (page) size: ");


scanf("%d", &frame_size);

printf("Enter page table (frame number for each page):\n");

for (int i = 0; i < n_pages; i++) {

printf("Page %d -> Frame: ", i);

scanf("%d", &page_table[i]);

printf("Enter a logical address (as an integer): ");

scanf("%d", &logical_address);

page_number = logical_address / frame_size;

offset = logical_address % frame_size;

if (page_number >= n_pages) {

printf("Invalid logical address! Page number out of bounds.\n");

return 1;

int frame_number = page_table[page_number];

physical_address = frame_number * frame_size + offset;

printf("Logical Address: [Page: %d, Offset: %d]\n", page_number, offset);

printf("Mapped to Physical Address: %d\n", physical_address);

return 0;

Output

Enter number of pages: 4

Enter frame (page) size: 100

Enter page table (frame number for each page):

Page 0 -> Frame: 5

Page 1 -> Frame: 9

Page 2 -> Frame: 3

Page 3 -> Frame: 7

Enter a logical address (as an integer): 250


Logical Address: [Page: 2, Offset: 50]

Mapped to Physical Address: 350

9. Write a C program to simulate Segmentation memory management technique.

Ans: #include <stdio.h>

int main() {

int segment_count, segment_number, offset;

int base[10], limit[10];

printf("Enter number of segments: ");

scanf("%d", &segment_count);

printf("Enter base and limit for each segment:\n");

for (int i = 0; i < segment_count; i++) {

printf("Segment %d:\n", i);

printf(" Base: ");

scanf("%d", &base[i]);

printf(" Limit: ");

scanf("%d", &limit[i]);

printf("\nEnter Segment Number: ");

scanf("%d", &segment_number);

printf("Enter Offset: ");

scanf("%d", &offset);

if (segment_number >= segment_count) {

printf("Invalid segment number!\n");

return 1;

if (offset >= limit[segment_number]) {

printf("Segmentation fault: Offset exceeds segment limit!\n");

return 1;

}
int physical_address = base[segment_number] + offset;

printf("Logical Address: [Segment: %d, Offset: %d]\n", segment_number, offset);

printf("Mapped to Physical Address: %d\n", physical_address);

return 0;

Output

Enter number of segments: 3

Enter base and limit for each segment:

Segment 0:

Base: 1000

Limit: 400

Segment 1:

Base: 2000

Limit: 300

Segment 2:

Base: 3000

Limit: 500

Enter Segment Number: 1

Enter Offset: 120

Logical Address: [Segment: 1, Offset: 120]

Mapped to Physical Address: 2120

10. Write a C program to simulate the Best Fit contiguous memory allocation technique.

Ans: #include <stdio.h>

int main() {

int blocks[10], processes[10];

int b_count, p_count;

int allocation[10];

printf("Enter number of memory blocks: ");


scanf("%d", &b_count);

printf("Enter size of each memory block:\n");

for (int i = 0; i < b_count; i++) {

printf("Block %d: ", i);

scanf("%d", &blocks[i]);

printf("Enter number of processes: ");

scanf("%d", &p_count);

printf("Enter size of each process:\n");

for (int i = 0; i < p_count; i++) {

printf("Process %d: ", i);

scanf("%d", &processes[i]);

allocation[i] = -1;

for (int i = 0; i < p_count; i++) {

int best_idx = -1;

for (int j = 0; j < b_count; j++) {

if (blocks[j] >= processes[i]) {

if (best_idx == -1 || blocks[j] < blocks[best_idx]) {

best_idx = j;

if (best_idx != -1) {

allocation[i] = best_idx;

blocks[best_idx] -= processes[i];

printf("\nProcess No.\tProcess Size\tBlock No.\n");


for (int i = 0; i < p_count; 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

Enter number of memory blocks: 5

Enter size of each memory block:

Block 0: 100

Block 1: 500

Block 2: 200

Block 3: 300

Block 4: 600

Enter number of processes: 4

Enter size of each process:

Process 0: 212

Process 1: 417

Process 2: 112

Process 3: 426

Process No. Process Size Block No.

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.

Ans: #include <stdio.h>

int main() {

int blocks[10], processes[10];

int b_count, p_count;

int allocation[10];

printf("Enter number of memory blocks: ");

scanf("%d", &b_count);

printf("Enter size of each memory block:\n");

for (int i = 0; i < b_count; i++) {

printf("Block %d: ", i);

scanf("%d", &blocks[i]);

printf("Enter number of processes: ");

scanf("%d", &p_count);

printf("Enter size of each process:\n");

for (int i = 0; i < p_count; i++) {

printf("Process %d: ", i);

scanf("%d", &processes[i]);

allocation[i] = -1;

for (int i = 0; i < p_count; i++) {

for (int j = 0; j < b_count; j++) {

if (blocks[j] >= processes[i]) {

allocation[i] = j;

blocks[j] -= processes[i];

break;

}
}

printf("\nProcess No.\tProcess Size\tBlock No.\n");

for (int i = 0; i < p_count; 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

Enter number of memory blocks: 5

Block 0: 100

Block 1: 500

Block 2: 200

Block 3: 300

Block 4: 600

Enter number of processes: 4

Process 0: 212

Process 1: 417

Process 2: 112

Process 3: 426

Process No. Process Size Block No.

0 212 1

1 417 4
2 112 2

3 426 Not Allocated

12. Write a C program to simulate the concept of Dining-Philosophers problem.

Ans: #include <stdio.h>

#include <pthread.h>

#include <semaphore.h>

#include <unistd.h>

#define N 5

sem_t forks[N];

pthread_t philosophers[N];

int phil_ids[N];

void* dine(void* arg) {

int id = *(int*)arg;

int left = id; // Left fork

int right = (id + 1) % N; // Right fork

while (1) {

printf("Philosopher %d is thinking...\n", id);

sleep(1);

printf("Philosopher %d is hungry.\n", id);

if (id % 2 == 0) {

sem_wait(&forks[left]);

sem_wait(&forks[right]);

} else {

sem_wait(&forks[right]);

sem_wait(&forks[left]);

printf("Philosopher %d is eating.\n", id);

sleep(2);

sem_post(&forks[left]);
sem_post(&forks[right]);

printf("Philosopher %d has finished eating.\n", id);

return NULL;

int main() {

for (int i = 0; i < N; i++)

sem_init(&forks[i], 0, 1);

for (int i = 0; i < N; i++) {

phil_ids[i] = i;

pthread_create(&philosophers[i], NULL, dine, &phil_ids[i]);

for (int i = 0; i < N; 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 0 has finished eating.

Philosopher 2 is thinking...

Philosopher 1 is eating.

...

13. Write a C program to simulate the MVT algorithm.

Ans: #include <stdio.h>

int main() {
int total_memory, used_memory = 0;

int process_size, choice, process_count = 0;

printf("Enter total available memory (in KB): ");

scanf("%d", &total_memory);

while (1) {

printf("\nEnter memory required for process %d (in KB): ", process_count + 1);

scanf("%d", &process_size);

if (process_size <= (total_memory - used_memory)) {

used_memory += process_size;

process_count++;

printf("Process %d allocated %d KB memory.\n", process_count, process_size);

printf("Memory allocated: %d KB | Memory remaining: %d KB\n",

used_memory, total_memory - used_memory);

} else {

printf("Not enough memory to allocate process %d!\n", process_count + 1);

printf("Memory remaining: %d KB\n", total_memory - used_memory);

printf("\nDo you want to continue? (1 = Yes, 0 = No): ");

scanf("%d", &choice);

if (choice == 0) break;

printf("\nTotal Memory: %d KB\n", total_memory);

printf("Used Memory: %d KB\n", used_memory);

printf("Free Memory: %d KB\n", total_memory - used_memory);

printf("Total Processes Allocated: %d\n", process_count);

return 0;

Output

Enter total available memory (in KB): 1000


Enter memory required for process 1 (in KB): 200

Process 1 allocated 200 KB memory.

Memory allocated: 200 KB | Memory remaining: 800 KB

Do you want to continue? (1 = Yes, 0 = No): 1

Enter memory required for process 2 (in KB): 500

Process 2 allocated 500 KB memory.

Memory allocated: 700 KB | Memory remaining: 300 KB

Do you want to continue? (1 = Yes, 0 = No): 1

Enter memory required for process 3 (in KB): 400

Not enough memory to allocate process 3!

Memory remaining: 300 KB

Do you want to continue? (1 = Yes, 0 = No): 0

Total Memory: 1000 KB

Used Memory: 700 KB

Free Memory: 300 KB

Total Processes Allocated: 2

14. Write a C program to implement FIFO page replacement technique.

Ans: #include <stdio.h>

int main() {

int pages[50], frames[10];

int n_pages, n_frames;

int front = 0, page_faults = 0;


int i, j, found;

printf("Enter number of pages: ");

scanf("%d", &n_pages);

printf("Enter the page reference string:\n");

for (i = 0; i < n_pages; i++) {

scanf("%d", &pages[i]);

printf("Enter number of frames: ");

scanf("%d", &n_frames);

for (i = 0; i < n_frames; i++) {

frames[i] = -1;

printf("\nPage\tFrames\n");

for (i = 0; i < n_pages; i++) {

found = 0;

for (j = 0; j < n_frames; j++) {

if (frames[j] == pages[i]) {

found = 1;

break;

if (!found) {

frames[front] = pages[i]; // Replace the oldest page

front = (front + 1) % n_frames;

page_faults++;

printf("%d\t", pages[i]);

for (j = 0; j < n_frames; j++) {

if (frames[j] != -1)

printf("%d ", frames[j]);


else

printf("- ");

printf("\n");

printf("\nTotal Page Faults: %d\n", page_faults);

return 0;

Output

Enter number of pages: 12

Enter the page reference string:

130356336272

Enter number of frames: 3

Page Frames

1 1--

3 13-

0 130

5 530

6 560

3 563

2 263

7 273

Total Page Faults: 8

15. Write a C program to write a C program for implementing sequential file allocation method.

Ans: #include <stdio.h>

int main() {
int memory[50] = {0}; // Simulate 50 disk blocks, 0 = free, 1 = allocated

int n_files, start, length, i, j, flag;

printf("Enter number of files: ");

scanf("%d", &n_files);

for (i = 0; i < n_files; i++) {

printf("\nEnter start block and length of file %d: ", i + 1);

scanf("%d %d", &start, &length);

if (start < 0 || (start + length) > 50) {

printf("Invalid block range. File %d cannot be allocated.\n", i + 1);

continue;

flag = 0;

for (j = start; j < start + length; j++) {

if (memory[j] == 1) {

flag = 1; // Block already allocated

break;

if (flag == 0) {

for (j = start; j < start + length; j++) {

memory[j] = 1;

printf("File %d allocated from block %d to %d\n", i + 1, start, start + length - 1);

} else {

printf("Blocks already allocated. File %d cannot be stored.\n", i + 1);

printf("\nMemory Block Status (0 = free, 1 = allocated):\n");

for (i = 0; i < 50; i++) {


printf("%d ", memory[i]);

if ((i + 1) % 10 == 0)

printf("\n");

return 0;

Output

Enter number of files: 3

Enter start block and length of file 1: 5 6

File 1 allocated from block 5 to 10

Enter start block and length of file 2: 9 4

Blocks already allocated. File 2 cannot be stored.

Enter start block and length of file 3: 15 5

File 3 allocated from block 15 to 19

Memory Block Status (0 = free, 1 = allocated):

0000011111

1000011111

0000000000

0000000000

0000000000

You might also like