OS Lab Manual
OS Lab Manual
OS Lab Manual
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <sys/wait.h>
6
7 int main() {
8 pid_t pid;
9
10 pid = fork();
11
12 if (pid == -1) {
13 // Error handling for fork failure
14 perror("fork");
15 return 1;
16 } else if (pid == 0) {
17 // Child process
18 printf("Child process\n");
19 // Use exec() to execute another program
20 execl("/bin/ls", "ls", "-l", NULL);
21 } else {
22 // Parent process
23 printf("Parent process\n");
24 // Wait for the child process to finish
25 wait(NULL);
26 printf("Child process finished\n");
27 }
28
29 return 0;
30 }
28 // Display processes along with their waiting time and turnaround time
29 printf("\nProcess Burst Time Waiting"
30 " Time Turn-Around Time\n");
31 int total_wt = 0, total_tat = 0;
32 for (int i = 0; i < n; i++) {
33 total_wt = total_wt + wt[i];
34 total_tat = total_tat + tat[i];
35 printf(" %d ", (i + 1));
36 printf(" %d ", bt[i]);
37 printf(" %d", wt[i]);
38 printf(" %d\n", tat[i]);
39 }
40
61 temp = processes[j];
62 processes[j] = processes[j+1];
63 processes[j+1] = temp;
64 }
65
66 findAverageTime(processes, n, bt);
67 }
68
84
91 // If remaining burst time is less than or equal to quantum, finish the process
92 if (bt[i] <= quantum) {
93 time += bt[i];
94 wt[i] = time - bt[i];
95 bt[i] = 0;
96 } else {
97 // Reduce the burst time by the quantum time
98 time += quantum;
99 bt[i] -= quantum;
100 }
101 }
102 }
103
113 // Display processes along with their waiting time and turnaround time
114 printf("\nProcess Burst Time Waiting"
115 " Time Turn-Around Time\n");
116 int total_wt = 0, total_tat = 0;
117 for (int i = 0; i < n; i++) {
118 total_wt = total_wt + wt[i];
119 total_tat = total_tat + tat[i];
120 printf(" %d ", (i + 1));
121 printf(" %d ", bt[i]);
122 printf(" %d", wt[i]);
123 printf(" %d\n", tat[i]);
124 }
125
128 }
129
176
177
183 }
184 /* FCFS Scheduling - Expected Output for FCFS
185 Enter the number of processes: 3
186 Enter the burst times for each process:
187 Burst time for Process 1: 5
188 Burst time for Process 2: 1
189 Burst time for Process 3: 2
190
7 #define BUFFER_SIZE 5
8
9 int buffer[BUFFER_SIZE];
10 int in = 0, out = 0;
11
17 while (1) {
18 sem_wait(&empty);
19 sem_wait(&mutex);
20
21 // Produce item
22 buffer[in] = item;
23 printf("Produced item %d\n", item);
24 in = (in + 1) % BUFFER_SIZE;
25 item++;
26
27 sem_post(&mutex);
28 sem_post(&full);
29
33 pthread_exit(NULL);
34 }
35
39 while (1) {
40 sem_wait(&full);
41 sem_wait(&mutex);
42
43 // Consume item
44 item = buffer[out];
45 printf("Consumed item %d\n", item);
46 out = (out + 1) % BUFFER_SIZE;
47
48 sem_post(&mutex);
49 sem_post(&empty);
50
54 pthread_exit(NULL);
55 }
56
57 int main() {
58 pthread_t producer_thread, consumer_thread;
59
60 // Initialize semaphores
61 sem_init(&mutex, 0, 1);
62 sem_init(&empty, 0, BUFFER_SIZE);
63 sem_init(&full, 0, 0);
64
69 // Join threads
70 pthread_join(producer_thread, NULL);
71 pthread_join(consumer_thread, NULL);
72
73 // Destroy semaphores
74 sem_destroy(&mutex);
75 sem_destroy(&empty);
76 sem_destroy(&full);
77
78 return 0;
79 }
80
81 In this program:
82
83 mutex semaphore is used to provide mutual exclusion while accessing the buffer.
84 empty semaphore is used to track the number of empty slots in the buffer.
85 full semaphore is used to track the number of filled slots in the buffer.
86 The producer waits on empty semaphore before producing an item and signals full
13 int main() {
14 int fd;
15 char message[BUFFER_SIZE];
16
20 // Writer process
21 if (fork() == 0) {
22 // Open the FIFO for writing
23 fd = open(FIFO_NAME, O_WRONLY);
24 if (fd == -1) {
25 perror("open");
26 exit(EXIT_FAILURE);
27 }
28
40 else {
41 // Open the FIFO for reading
42 fd = open(FIFO_NAME, O_RDONLY);
43 if (fd == -1) {
44 perror("open");
45 exit(EXIT_FAILURE);
46 }
47
59 return 0;
60 }
61
62 In this program:
63
6 int available[MAX_RESOURCES];
7 int maximum[MAX_PROCESSES][MAX_RESOURCES];
8 int allocation[MAX_PROCESSES][MAX_RESOURCES];
9 int need[MAX_PROCESSES][MAX_RESOURCES];
10 int work[MAX_RESOURCES];
11 int finish[MAX_PROCESSES];
12
15 // Function declarations
16 void initialize();
17 void input();
18 void display();
19 int isSafe();
20 void requestResource(int processNumber);
21 void releaseResource(int processNumber);
22
23 int main() {
24 initialize();
25 input();
26 display();
27
28 if (isSafe()) {
29 printf("\nSystem is in safe state.\n");
30
36 releaseResource(processNumber);
37 display();
38 } else {
39 printf("\nSystem is in unsafe state.\n");
40 }
41
42 return 0;
43 }
44
45 void initialize() {
46 printf("Enter the number of processes: ");
47 scanf("%d", &nProcesses);
48
65 void input() {
66 printf("Enter the allocation matrix:\n");
67 for (int i = 0; i < nProcesses; ++i) {
68 for (int j = 0; j < nResources; ++j) {
69 scanf("%d", &allocation[i][j]);
70 need[i][j] = maximum[i][j] - allocation[i][j];
71 }
72 finish[i] = 0;
73 }
74 }
75
76 void display() {
77 printf("\nAvailable Resources:\n");
78 for (int j = 0; j < nResources; ++j) {
79 printf("%d ", available[j]);
80 }
81
82 printf("\n\nMaximum Resources:\n");
83 for (int i = 0; i < nProcesses; ++i) {
84 for (int j = 0; j < nResources; ++j) {
85 printf("%d ", maximum[i][j]);
86 }
87 printf("\n");
88 }
89
90 printf("\nAllocation Matrix:\n");
91 for (int i = 0; i < nProcesses; ++i) {
92 for (int j = 0; j < nResources; ++j) {
93 printf("%d ", allocation[i][j]);
94 }
95 printf("\n");
96 }
97 }
98
99 int isSafe() {
100 for (int j = 0; j < nResources; ++j) {
101 work[j] = available[j];
102 }
103
131 }
132
219 1
220 1
221
3 #include <stdio.h>
4 #define MAX_MEMORY_SIZE 1000
5 #define MAX_PROCESSES 10
6
7 typedef struct {
8 int pid; // Process ID
9 int size; // Process size
10 int allocated; // Allocation status (1 for allocated, 0 for unallocated)
11 } Process;
12
25 if (worstFitIndex != -1) {
26 // Allocate memory to the process
27 processes[i].allocated = 1;
28 memory[worstFitIndex] -= processes[i].size;
29 printf("Process %d allocated to memory block %d using Worst Fit\n",
,→ processes[i].pid, worstFitIndex);
30 } else {
31 printf("Process %d cannot be allocated using Worst Fit\n",
,→ processes[i].pid);
32 }
33 }
34 }
35
,→ numMemoryBlocks) {
37 for (int i = 0; i < numProcesses; i++) {
38 int bestFitIndex = -1;
39
48 if (bestFitIndex != -1) {
49 // Allocate memory to the process
50 processes[i].allocated = 1;
51 memory[bestFitIndex] -= processes[i].size;
52 printf("Process %d allocated to memory block %d using Best Fit\n",
,→ processes[i].pid, bestFitIndex);
53 } else {
54 printf("Process %d cannot be allocated using Best Fit\n",
,→ processes[i].pid);
55 }
56 }
57 }
58
71 if (!processes[i].allocated) {
72 printf("Process %d cannot be allocated using First Fit\n",
,→ processes[i].pid);
73 }
74 }
75 }
76
77 int main() {
78 int memory[MAX_MEMORY_SIZE];
79 Process processes[MAX_PROCESSES];
80
112
119 }
120 for (int i = 0; i < numProcesses; i++) {
121 processes[i].allocated = 0;
122 }
123
127 return 0;
128 }
129
14 printf("Page\tFrame\n");
15
19 if (!isPresent[pages[i]]) {
20 pageFaults++;
21 if (rear < frames - 1) {
22 rear++;
23 } else {
24 rear = 0;
25 }
26 frame[rear] = pages[i];
27 isPresent[pages[i]] = true;
28 }
29
43 int pageFaults = 0;
44 int frame[MAX_FRAMES];
45 bool isPresent[MAX_PAGES] = {false};
46 int leastUsed[MAX_FRAMES] = {0};
47 int count = 0;
48
49 printf("Page\tFrame\n");
50
54 if (!isPresent[pages[i]]) {
55 pageFaults++;
56 if (count < frames) {
57 frame[count] = pages[i];
58 isPresent[pages[i]] = true;
59 leastUsed[count] = i;
60 count++;
61 } else {
62 int index = 0;
63 for (int j = 1; j < frames; j++) {
64 if (leastUsed[j] < leastUsed[index]) {
65 index = j;
66 }
67 }
68 isPresent[frame[index]] = false;
69 frame[index] = pages[i];
70 isPresent[pages[i]] = true;
71 leastUsed[index] = i;
72 }
73 } else {
74 for (int j = 0; j < frames; j++) {
75 if (frame[j] == pages[i]) {
76 leastUsed[j] = i;
77 break;
78 }
79 }
80 }
81
87 }
88 }
89 printf("\n");
90 }
91 printf("Total Page Faults: %d\n", pageFaults);
92 }
93
94 int main() {
95 int pages[MAX_PAGES];
96 int n, frames;
97 char choice;
98
127 return 0;
128 }
129
61 int main() {
62 int n, head, i;
63 char direction;
64 int *requests;
65
82 printf("\nSCAN Schedule:\n");
83 SCAN(requests, n, head, direction);
84
85 free(requests);
86
87 return 0;
88 }
89
90 //Expected Output
91 Enter the number of requests: 10
92 Enter the requests: 10
93 20
94 30
95 40
96 50
97 60
98 70
99 80
100 90
101 1
102 Enter the initial position of the head: 0
103 Enter the direction (l for left, r for right): l
104
19 // Global variables
20 File* files[MAX_BLOCKS] = {NULL}; // Array to hold pointers to file blocks
21 int lastBlock = 0; // Variable to track the last allocated block
22
43 newFile->blocks = NULL;
44 newFile->next = NULL;
45
87 files[fileId] = NULL;
88 printf("File with ID %d deleted\n", fileId);
89 }
90
151 return 0;
152 }
153
175
31 singleLevelDirectory.files[singleLevelDirectory.numFiles].size = size;
32 singleLevelDirectory.numFiles++;
33 printf("File ’%s’ created successfully in single-level directory\n",
,→ filename);
34 } else {
35 printf("Cannot create file. Single-level directory is full.\n");
36 }
37 }
38
,→ filename);
72 twoLevelDirectory[i].files[twoLevelDirectory[i].numFiles].size =
,→ size;
73 twoLevelDirectory[i].numFiles++;
74 printf("File ’%s’ created successfully in directory ’%s’ of
,→ two-level directory\n", filename, dirname);
75 return;
76 } else {
77 printf("Cannot create file. Directory ’%s’ is full.\n", dirname);
78 return;
79 }
80 }
81 }
82 printf("Directory ’%s’ not found in two-level directory\n", dirname);
83 }
84