Os Exp 1-10 Print
Os Exp 1-10 Print
Os Exp 1-10 Print
OUTPUT:
Process id is - 15272
I/O SYSTEM CALLS
Exp. No.: 1(b) Date:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
char buffer[100];
int fd = open("input.txt", O_RDONLY); // open the file for reading
if (fd == -1)
{
perror("Error opening file");
exit(EXIT_FAILURE);
}
int nbytes = read(fd, buffer, sizeof(buffer)); // read from the file into
buffer
if (nbytes == -1)
{
perror("Error reading file");
exit(EXIT_FAILURE);
}
return 0;
}
OUTPUT:
Read 20 bytes from file: hello from aanand!!!
Wrote 20 bytes to file
NAMED PIPE -WRITE
Exp. No.: 2-I Date:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FIFO_FILE "myfifo"
int main()
{
int fd;
char data[100];
fd = open(FIFO_FILE, O_WRONLY);
printf("Enter data to be written to the pipe: ");
fgets(data, sizeof(data), stdin);
write(fd, data, sizeof(data));
close(fd);
return 0;
}
OUTPUT:
Enter data to be written to the pipe: Good Morning from NamedPipe.
NAMED PIPE - READ
Exp. No.: 2-II Date:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FIFO_FILE "myfifo"
int main()
{
int fd;
char data[100];
fd = open(FIFO_FILE, O_RDONLY);
read(fd, data, sizeof(data));
printf("Received data: %s", data);
close(fd);
return 0;
}
OUTPUT:
Received data: Good Morning from NamedPipe.
UNNAMED PIPE
Exp. No.: 2-III Date:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int fd[2];
pid_t pid;
char data[100];
if (pipe(fd) == -1)
{
perror("pipe");
exit(1);
}
pid = fork();
if (pid == -1)
{
perror("fork");
exit(1);
}
if (pid == 0)
{
close(fd[1]);
read(fd[0], data, sizeof(data));
printf("Child process received data from parent process: %s", data);
close(fd[0]);
}
else
{
close(fd[0]);
printf("Enter data to be sent to the child process: ");
fgets(data, sizeof(data), stdin);
write(fd[1], data, sizeof(data));
close(fd[1]);
}
return 0;
}
OUTPUT:
Enter data to be sent to the child process: hello from aanand!
Child process received data from parent process: hello from aanand!
FILE PERMISSIONS
Exp. No.: 3 Date:
SOURCE CODE:
#include <sys/stat.h>
#include <stdio.h>
int main() {
int ret;
char *filename = "myfile.txt";
mode_t mode = S_IRUSR | S_IWUSR /*| S_IRGRP | S_IWGRP | S_IROTH */; // Set
file permissions to 664
if (ret == 0) {
printf("File permissions for %s have been set to %o.\n", filename, mode);
} else {
printf("Error setting file permissions for %s.\n", filename);
}
return 0;
}
OUTPUT:
File permissions for myfile.txt have been set to 600.
FCFS (FIRST IN, FIRST OUT) CPU SCHEDULING
Exp. No.: 4(a) Date:
SOURCE CODE:
#include<stdio.h>
int main() {
int n, burst_time[20], waiting_time[20], turnaround_time[20], i;
float avg_waiting_time = 0, avg_turnaround_time = 0;
// Displaying results
printf("\nProcess\t Burst Time\t Waiting Time\t Turnaround Time\n");
for(i = 0; i < n; i++) {
printf("P[%d]\t %d\t\t %d\t\t %d\n", i+1, burst_time[i], waiting_time[i],
turnaround_time[i]);
}
printf("Average Waiting Time: %f\n", avg_waiting_time);
printf("Average Turnaround Time: %f\n", avg_turnaround_time);
return 0;
}
OUTPUT:
Enter the number of processes: 5
int main() {
int n, i, j, burst_time[20], remaining_time[20], waiting_time[20],
turnaround_time[20], process[20], smallest, current_time = 0, completed = 0;
float avg_waiting_time = 0, avg_turnaround_time = 0, total_waiting_time = 0,
total_turnaround_time = 0;
if(remaining_time[j] == 0) {
completed++;
turnaround_time[j] = current_time+1;
waiting_time[j] = turnaround_time[j] - burst_time[j];
total_waiting_time += waiting_time[j];
total_turnaround_time += turnaround_time[j];
}
current_time++;
}
// Display results
printf("\nProcess\t Burst Time\t Waiting Time\t Turnaround Time\n");
for(i = 0; i < n; i++) {
printf("P[%d]\t %d\t\t %d\t\t %d\n", process[i], burst_time[i],
waiting_time[i], turnaround_time[i]);
}
printf("Average Waiting Time: %f\n", avg_waiting_time);
printf("Average Turnaround Time: %f\n", avg_turnaround_time);
return 0;
}
OUTPUT:
Enter the number of processes: 5
int main() {
int n, i, j, burst_time[20], waiting_time[20], turnaround_time[20],
priority[20], process[20], highest;
float avg_waiting_time = 0, avg_turnaround_time = 0, total_waiting_time = 0,
total_turnaround_time = 0;
temp = burst_time[i];
burst_time[i] = burst_time[highest];
burst_time[highest] = temp;
temp = process[i];
process[i] = process[highest];
process[highest] = temp;
}
waiting_time[0] = 0;
// Calculate waiting time and turnaround time for each process
for(i = 1; i < n; i++) {
waiting_time[i] = waiting_time[i-1] + burst_time[i-1];
turnaround_time[i] = waiting_time[i] + burst_time[i];
total_waiting_time += waiting_time[i];
total_turnaround_time += turnaround_time[i];
}
// Calculate average waiting time and average turnaround time
avg_waiting_time = total_waiting_time/n;
avg_turnaround_time = total_turnaround_time/n;
// Display results
printf("\nProcess\t Burst Time\t Priority\t Waiting Time\t Turnaround
Time\n");
for(i = 0; i < n; i++) {
printf("P[%d]\t %d\t\t %d\t\t %d\t\t %d\n", process[i], burst_time[i],
priority[i], waiting_time[i], turnaround_time[i]);
}
printf("Average Waiting Time: %f\n", avg_waiting_time);
printf("Average Turnaround Time: %f\n", avg_turnaround_time);
return 0;
}
OUTPUT:
Enter the number of processes: 5
struct process {
int pid; // process id
int bt; // burst time
int wt; // waiting time
int tat; // turnaround time
int rt; // remaining time
};
avgwt /= n;
avgtat /= n;
int main() {
int n, tq, i;
struct process p[MAX];
roundRobinScheduling(p, n, tq);
return 0;
}
OUTPUT:
Enter the number of processes: 5
Enter time quantum: 25
Enter burst time for each process:
Process[1]: 3
Process[2]: 12
Process[3]: 9
Process[4]: 18
Process[5]: 7
#define BUFFER_SIZE 5
#define MAX_ITEMS 20
int buffer[BUFFER_SIZE];
int in = 0, out = 0, count = 0;
int produce_item() {
static int item_num = 0;
return item_num++;
}
int remove_item() {
int item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
return item;
}
int main() {
pthread_t prod_thread, cons_thread;
pthread_create(&prod_thread, NULL, producer, NULL);
pthread_create(&cons_thread, NULL, consumer, NULL);
pthread_join(prod_thread, NULL);
pthread_join(cons_thread, NULL);
return 0;
}
OUTPUT:
Produced item 0
Produced item 1
Produced item 2
Produced item 3
Produced item 4
Produced item 5
Consumed item 0
Consumed item 1
Consumed item 2
Consumed item 3
Consumed item 4
Consumed item 5
Produced item 6
Produced item 7
Produced item 8
Produced item 9
Produced item 10
Produced item 11
Consumed item 6
Consumed item 7
Consumed item 8
Consumed item 9
Consumed item 10
Consumed item 11
Produced item 12
Produced item 13
Produced item 14
Produced item 15
Produced item 16
DINING PHILOSOPHERS PROBLEM
Exp. No.: 5(b) Date:
SOURCE CODE:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#define N 5
pthread_mutex_t forks[N];
sem_t waiter;
while (1) {
printf("Philosopher %d is thinking...\n", id);
sem_wait(&waiter);
pthread_mutex_lock(&forks[left_fork]);
printf("Philosopher %d picks up fork %d.\n", id, left_fork);
pthread_mutex_lock(&forks[right_fork]);
printf("Philosopher %d picks up fork %d.\n", id, right_fork);
printf("Philosopher %d is eating...\n", id);
pthread_mutex_unlock(&forks[left_fork]);
printf("Philosopher %d puts down fork %d.\n", id, left_fork);
pthread_mutex_unlock(&forks[right_fork]);
printf("Philosopher %d puts down fork %d.\n", id, right_fork);
sem_post(&waiter);
}
}
int main() {
pthread_t philosophers[N];
int ids[N];
sem_init(&waiter, 0, N - 1);
for (int i = 0; i < N; i++) {
ids[i] = i;
pthread_mutex_init(&forks[i], NULL);
pthread_create(&philosophers[i], NULL, philosopher, &ids[i]);
}
for (int i = 0; i < N; i++) {
pthread_join(philosophers[i], NULL);
}
for (int i = 0; i < N; i++) {
pthread_mutex_destroy(&forks[i]);
}
sem_destroy(&waiter);
return 0;
}
OUTPUT:
Philosopher 0 is thinking...
Philosopher 0 picks up fork 0.
Philosopher 0 picks up fork 1.
Philosopher 0 is eating...
Philosopher 0 puts down fork 0.
Philosopher 0 puts down fork 1.
Philosopher 0 is thinking...
Philosopher 0 picks up fork 0.
Philosopher 0 picks up fork 1.
Philosopher 0 is eating...
Philosopher 0 puts down fork 0.
Philosopher 0 puts down fork 1.
Philosopher 0 is thinking...
Philosopher 0 picks up fork 0.
Philosopher 0 picks up fork 1.
Philosopher 0 is eating...
Philosopher 0 puts down fork 0.
Philosopher 0 puts down fork 1.
Philosopher 0 is thinking...
Philosopher 0 picks up fork 0.
Philosopher 0 picks up fork 1.
Philosopher 0 is eating...
Philosopher 0 puts down fork 0.
Philosopher 0 puts down fork 1.
Philosopher 3 is thinking...
Philosopher 3 picks up fork 3.
Philosopher 3 picks up fork 4.
Philosopher 3 is eating...
Philosopher 3 puts down fork 3.
Philosopher 3 puts down fork 4.
Philosopher 3 is thinking...
Philosopher 3 picks up fork 3.
Philosopher 3 picks up fork 4.
Philosopher 3 is eating...
Philosopher 3 puts down fork 3.
Philosopher 3 puts down fork 4.
Philosopher 3 is thinking...
Philosopher 3 picks up fork 3.
Philosopher 3 picks up fork 4.
Philosopher 3 is eating...
Philosopher 3 puts down fork 3.
Philosopher 3 puts down fork 4.
Philosopher 3 is thinking...
Philosopher 3 picks up fork 3.
Philosopher 3 picks up fork 4.
Philosopher 3 is eating...
Philosopher 3 puts down fork 3.
Philosopher 3 puts down fork 4.
Philosopher 2 is thinking...
Philosopher 2 picks up fork 2.
Philosopher 2 picks up fork 3.
Philosopher 2 is eating...
Philosopher 2 puts down fork 2.
Philosopher 2 puts down fork 3.
Philosopher 2 is thinking...
Philosopher 2 picks up fork 2.
Philosopher 2 picks up fork 3.
Philosopher 2 is eating...
Philosopher 2 puts down fork 2.
Philosopher 2 puts down fork 3.
Philosopher 2 is thinking...
Philosopher 2 picks up fork 2.
Philosopher 2 picks up fork 3.
Philosopher 2 is eating...
Philosopher 2 puts down fork 2.
Philosopher 2 puts down fork 3.
Philosopher 2 is thinking...
Philosopher 2 picks up fork 2.
Philosopher 2 picks up fork 3.
Philosopher 2 is eating...
Philosopher 2 puts down fork 2.
Philosopher 2 puts down fork 3.
Philosopher 4 is thinking...
Philosopher 4 picks up fork 4.
BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE
Exp. No.: 6 Date:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESS 10
#define MAX_RESOURCE 10
int main()
{
int i, j, k, process_count, resource_count;
int available[MAX_RESOURCE], max[MAX_PROCESS][MAX_RESOURCE],
allocation[MAX_PROCESS][MAX_RESOURCE], need[MAX_PROCESS][MAX_RESOURCE];
int work[MAX_RESOURCE], finish[MAX_PROCESS], safe_seq[MAX_PROCESS];
if (count == process_count)
{
printf("\nSafe Sequence: ");
for (i = 0; i < process_count; i++)
printf("%d ", safe_seq[i]);
printf("\n");
}
else
{
printf("\nNo Safe Sequence\n");
return 0;
}
count = 0;
while (count < process_count)
{
int found = 0;
for (i = 0; i < process_count; i++)
{
if (finish[i] == 0)
{
int j;
for (j = 0; j < resource_count; j++)
{
if (need[i][j] > work[j])
break;
}
if (j == resource_count)
{
for (k = 0; k < resource_count; k++)
work[k] += allocation[i][k];
finish[i] = 1;
count++;
found = 1;
}
}
}
if (found == 0)
break;
}
if (count == process_count)
printf("\nNo deadlock with new maximum request.\n");
else
printf("\nDeadlock detected with new maximum request.\n");
return 0;
}
OUTPUT:
Enter the number of processes: 5
Enter the number of resources: 3
Enter the number of available resources for each type:
7 8 10
Enter the maximum needs of each process:
Process 0: 3 4 3
Process 1: 2 0 0
Process 2: 6 5 7
Process 3: 7 5 6
Process 4: 2 0 1
Enter the allocated resources for each process:
Process 0: 9 7 3
Process 1: 6 0 1
Process 2: 2 0 8
Process 3: 8 0 0
Process 4: 7 9 3
Safe Sequence: 0 1 2 3 4
int main()
{
int blockSize[MAX_BLOCKS], processSize[MAX_PROCESS];
int m, n;
return 0;
}
OUTPUT:
Enter the number of memory blocks: 5
Enter the size of each memory block:
45
20
50
25
10
Enter the number of processes: 5
Enter the size of each process:
50
19
34
20
9
if (bestIdx != -1)
{
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
int main()
{
int blockSize[MAX_BLOCKS], processSize[MAX_PROCESS];
int m, n;
return 0;
}
OUTPUT:
Enter the size of each memory block:
45
20
50
25
10
Enter the number of processes: 5
Enter the size of each process:
50
19
34
20
9
if (worstIdx != -1)
{
allocation[i] = worstIdx;
blockSize[worstIdx] -= processSize[i];
}
}
int main()
{
int blockSize[MAX_BLOCKS], processSize[MAX_PROCESS];
int m, n;
return 0;
}
OUTPUT:
Enter the number of memory blocks: 5
Enter the size of each memory block:
50
35
78
24
10
Enter the number of processes: 5
Enter the size of each process:
51
30
76
20
9
#define MAX_FRAMES 80
#define MAX_PAGES 80
pageFaults[i] = 1;
}
else
{
pageFaults[i] = 0;
}
}
}
int main()
{
int pages[MAX_PAGES];
int n, frames;
int pageFaults[MAX_PAGES];
fifo(pages, n, frames, pageFaults);
printf("\nPage Faults:\n");
for (int i = 0; i < n; i++)
{
printf("Page %d: %s\n", pages[i], (pageFaults[i] == 1) ? "Fault" : "No
Fault");
}
return 0;
}
OUTPUT:
Enter the number of pages: 12
Enter the page reference string:
2 3 4 2 1 3 7 5 4 3 2 5
Enter the number of frames: 3
Page Faults:
Page 2: Fault
Page 3: Fault
Page 4: Fault
Page 2: No Fault
Page 1: Fault
Page 3: No Fault
Page 7: Fault
Page 5: Fault
Page 4: Fault
Page 3: Fault
Page 2: Fault
Page 5: Fault
LEAST FREQUENTLY USED (LFU) - PAGE REPLACEMENT ALGORITHM
Exp. No.: 8(b) Date:
SOURCE CODE:
#include <stdio.h>
#include <stdbool.h>
#define MAX_FRAMES 70
#define MAX_PAGES 70
typedef struct
{
int pageNumber;
int frequency;
int lastUsed;
} Page;
pageFaults[i] = 1;
}
else
{
pageFaults[i] = 0;
}
}
}
int main()
{
int pages[MAX_PAGES];
int n, frames;
int pageFaults[MAX_PAGES];
lfu(pages, n, frames, pageFaults);
printf("\nPage Faults:\n");
for (int i = 0; i < n; i++)
{
printf("Page %d: %s\n", pages[i], (pageFaults[i] == 1) ? "Fault" : "No
Fault");
}
return 0;
}
OUTPUT:
Enter the number of pages: 12
Enter the page reference string:
2 3 4 2 1 3 7 5 4 3 2 5
Enter the number of frames: 3
Page Faults:
Page 2: Fault
Page 3: Fault
Page 4: Fault
Page 2: No Fault
Page 1: Fault
Page 3: Fault
Page 7: Fault
Page 5: Fault
Page 4: Fault
Page 3: Fault
Page 2: No Fault
Page 5: Fault
LEAST RECENTLY USED (LRU) - PAGE REPLACEMENT ALGORITHM
Exp. No.: 8(c) Date:
SOURCE CODE:
#include <stdio.h>
#include <stdbool.h>
#define MAX_FRAMES 70
#define MAX_PAGES 70
pageFaults[i] = 1;
}
else
{
pageFaults[i] = 0;
}
}
}
int main()
{
int pages[MAX_PAGES];
int n, frames;
int pageFaults[MAX_PAGES];
lru(pages, n, frames, pageFaults);
printf("\nPage Faults:\n");
for (int i = 0; i < n; i++)
{
printf("Page %d: %s\n", pages[i], (pageFaults[i] == 1) ? "Fault" : "No
Fault");
}
return 0;
}
OUTPUT:
Enter the number of pages: 12
Enter the page reference string:
2 3 4 2 1 3 7 5 4 3 2 5
Enter the number of frames: 3
Page Faults:
Page 2: Fault
Page 3: Fault
Page 4: Fault
Page 2: No Fault
Page 1: Fault
Page 3: Fault
Page 7: Fault
Page 5: Fault
Page 4: Fault
Page 3: Fault
Page 2: Fault
Page 5: Fault
OPTIMAL - PAGE REPLACEMENT ALGORITHM
Exp. No.: 8(d) Date:
SOURCE CODE:
#include <stdio.h>
#include <stdbool.h>
// Replace the page that will not be used for the longest
duration
memory[index] = page;
isPresent[index] = true;
}
pageFaults[i] = 1;
}
else
{
pageFaults[i] = 0;
}
}
}
int main()
{
int pages[MAX_PAGES];
int n, frames;
int pageFaults[MAX_PAGES];
optimal(pages, n, frames, pageFaults);
printf("\nPage Faults:\n");
for (int i = 0; i < n; i++)
{
printf("Page %d: %s\n", pages[i], (pageFaults[i] == 1) ? "Fault" : "No
Fault");
}
return 0;
}
OUTPUT:
Enter the number of pages: 12
Enter the page reference string:
2 3 4 2 1 3 7 5 4 3 2 5
Enter the number of frames: 3
Page Faults:
Page 2: Fault
Page 3: Fault
Page 4: Fault
Page 2: No Fault
Page 1: Fault
Page 3: No Fault
Page 7: Fault
Page 5: Fault
Page 4: No Fault
Page 3: No Fault
Page 2: Fault
Page 5: Fault
FIRST COME FIRST SEARCH (FCFS) - DISK SCHEDULING ALGORITHM
Exp. No.: 9(a) Date:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int requests[100];
int n, start;
fcfs(requests, n, start);
return 0;
}
OUTPUT:
Enter the number of requests: 5
Enter the requests:
98
183
37
122
14
Enter the starting position: 53
Sequence of disk access:
53 -> 98 -> 183 -> 37 -> 122 -> 14
Total Seek Time: 469
Average Seek Time: 93.80
SHORTEST SEEK TIME FIRST (SSTF) - DISK SCHEDULING ALGORITHM
Exp. No.: 9(b) Date:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
return closestIndex;
}
sstf(requests, n, start);
return 0;
}
OUTPUT:
Enter the number of requests: 5
Enter the requests:
98
183
37
122
14
Enter the starting position: 53
Sequence of disk access:
53 -> 37 -> 14 -> 98 -> 122 -> 183
Total Seek Time: 208
Average Seek Time: 41.60
SCAN (ELEVATOR)- DISK SCHEDULING ALGORITHM
Exp. No.: 9(c) Date:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void scan(int requests[], int n, int start, int direction, int maxCylinders)
{
int totalSeekTime = 0;
int currentPos = start;
int minCylinder = 0;
int maxCylinder = maxCylinders - 1;
bool visited[100] = {false};
// Change direction and move towards the other end of the disk
direction = -1;
printf("|\n");
// Change direction and move towards the other end of the disk
direction = 1;
printf("|\n");
int main()
{
int requests[100];
int n, start, maxCylinders, direction;
printf("Enter the initial direction (1 for towards the end, -1 for towards
the beginning): ");
scanf("%d", &direction);
OUTPUT:
Enter the number of requests: 6
Enter the requests:
20
89
42
6
57
92
Enter the starting position: 50
Enter the maximum number of cylinders: 200
Enter the initial direction (1 for towards the end, -1 for towards the
beginning): 1
Sequence of disk access:
50 -> 51 -> 52 -> 53 -> 54 -> 55 -> 56 -> 57 -> 58 -> 59 -> 60 -> 61 -> 62 -> 63
-> 64 -> 65 -> 66 -> 67 -> 68 -> 69 -> 70 -> 71 -> 72 -> 73 -> 74 -> 75 -> 76 ->
77 -> 78 -> 79 -> 80 -> 81 -> 82 -> 83 -> 84 -> 85 -> 86 -> 87 -> 88
-> 89 -> 90 -> 91 -> 92 -> 93 -> 94 -> 95 -> 96 -> 97 -> 98 -> 99 -> 100 -> 102 -
> 103 -> 104 -> 105 -> 106 -> 107 -> 108 -> 109 -> 110 -> 111 -> 112 -> |1 -> 0 -
>
Total Seek Time: 174
Average Seek Time: 29.00
CIRCULAR SCAN (CSCAN) - DISK SCHEDULING ALGORITHM
Exp. No.: 9(d) Date:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void cscan(int requests[], int n, int start, int direction, int maxCylinders)
{
int totalSeekTime = 0;
int currentPos = start;
int minCylinder = 0;
int maxCylinder = maxCylinders - 1;
bool visited[100] = {false};
int main()
{
int requests[100];
int n, start, maxCylinders, direction;
printf("Enter the initial direction (1 for towards the end, -1 for towards
the beginning): ");
scanf("%d", &direction);
return 0;
}
OUTPUT:
Enter the number of requests: 6
Enter the requests:
50
30
75
10
100
25
Enter the starting position: 60
Enter the maximum number of cylinders: 200
Enter the initial direction (1 for towards the end, -1 for towards the
beginning): 1
Sequence of disk access:
60 -> 61 -> 62 -> 63 -> 64 -> 65 -> 66 -> 67 -> 68 -> 69 -> 70 -> 71 -> 72 -> 73
-> 74 -> 75 -> 76 -> 77 -> 78 -> 79 -> 80 -> 81 -> 82 -> 83 -> 84 -> 85 -> 86 ->
87 -> 88 -> 89 -> 90 -> 91 -> 92 -> 93 -> 94 -> 95 -> 96 -> 97 -> 98
-> 99 -> 100 -> 102 -> 103 -> 104 -> 105 -> 106 -> 107 -> 108 -> 109 -> 110 ->
111 -> 112 -> 0 ->
Total Seek Time: 164
Average Seek Time: 27.33
CONTIGUOUS ALLOCATION STRATEGIES
Exp. No.: 10(a) Date:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_FILES 20
struct File
{
int fileId;
int startingBlock;
int fileSize;
};
struct Disk
{
int totalBlocks;
bool allocatedBlocks[MAX_FILES];
};
int main()
{
struct Disk disk;
initializeDisk(&disk, 20);
printDiskStatus(&disk);
return 0;
}
OUTPUT:
File 0 allocated successfully.
File 1 allocation failed.
File 2 allocation failed.
File 3 allocated successfully.
File 4 allocation failed.
Disk Status:
Total Blocks: 20
Allocated Blocks:
0: Free
1: Free
2: Free
3: Free
4: Free
5: Free
6: Free
7: Free
8: Free
9: Free
10: Allocated
11: Allocated
12: Allocated
13: Allocated
14: Allocated
15: Free
16: Free
17: Free
18: Free
19: Free
LINKED ALLOCATION STRATEGIES
Exp. No.: 10(b) Date:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_FILES 20
struct File
{
int fileId;
int startingBlock;
int fileSize;
int *blockPointers;
};
struct Disk
{
int totalBlocks;
bool allocatedBlocks[MAX_FILES];
};
int allocatedBlocksCount = 0;
int currentBlock = -1;
for (int i = 0; i < disk->totalBlocks; i++)
{
if (!disk->allocatedBlocks[i])
{
if (currentBlock == -1)
{
file->startingBlock = i;
}
else
{
blockPointers[currentBlock] = i;
}
currentBlock = i;
disk->allocatedBlocks[i] = true;
allocatedBlocksCount++;
if (allocatedBlocksCount == requiredBlocks)
{
break;
}
}
}
if (allocatedBlocksCount == requiredBlocks)
{
blockPointers[currentBlock] = -1;
file->blockPointers = blockPointers;
file->fileId = file->startingBlock;
return true;
}
else
{
printf("Not enough consecutive blocks available on the disk.\n");
free(blockPointers);
return false;
}
}
int main()
{
struct Disk disk;
initializeDisk(&disk, 20);
printDiskStatus(&disk);
return 0;
}
OUTPUT:
File 0 allocated successfully.
File 1 allocated successfully.
Not enough consecutive blocks available on the disk.
File 2 allocation failed.
Not enough consecutive blocks available on the disk.
File 3 allocation failed.
Not enough consecutive blocks available on the disk.
File 4 allocation failed.
Disk Status:
Total Blocks: 20
Allocated Blocks:
0: Allocated
1: Allocated
2: Allocated
3: Allocated
4: Allocated
5: Allocated
6: Allocated
7: Allocated
8: Allocated
9: Allocated
10: Allocated
11: Allocated
12: Allocated
13: Allocated
14: Allocated
15: Allocated
16: Allocated
17: Allocated
18: Allocated
19: Allocated