OS Lab Exercise Final
OS Lab Exercise Final
SEMAPHORE
}
PROGRAM:
void consumer() {
#include <stdio.h>
mutex = wait(mutex);
#include <stdlib.h>
full = wait(full);
#define SIZE 3 // Buffer size
int item = buffer[out];
int buffer[SIZE];
printf("Consumer consumed item %d
int in = 0, out = 0; from index %d\n", item, out);
int mutex = 1, full = 0, empty = SIZE; out = (out + 1) % SIZE;
// Semaphore-like operations empty = signal(empty);
int wait(int s) { mutex = signal(mutex);
return (--s); }
} int main() {
int signal(int s) { int choice;
return (++s); while (1) {
} printf("\n--- MENU ---\n");
void producer() { printf("1. PRODUCE\n");
int item; printf("2. CONSUME\n");
printf("Enter the item to produce: "); printf("3. EXIT\n");
scanf("%d", &item); printf("Enter your choice: ");
mutex = wait(mutex); scanf("%d", &choice);
empty = wait(empty); switch (choice) {
buffer[in] = item; case 1:
printf("Producer produced item %d at if ((mutex == 1) && (empty != 0))
index %d\n", item, in);
producer();
in = (in + 1) % SIZE;
else
printf("Buffer is FULL. Cannot
full = signal(full); produce.\n");
break; ENTER YOUR CHOICE: 1
case 2: Enter the item produced by the producer:
20
if ((mutex == 1) && (full != 0))
Producer produces the item 20 at index 1
consumer();
else
ENTER YOUR CHOICE: 1
printf("Buffer is EMPTY.
Cannot consume.\n"); Enter the item produced by the producer:
30
break;
Producer produces the item 30 at index 2
case 3:
printf("Exiting...\n");
ENTER YOUR CHOICE: 1
exit(0);
BUFFER IS FULL
default:
printf("Invalid choice. Try again.\
n"); ENTER YOUR CHOICE: 2
} Consumer consumes item 10 from index 0
}
ENTER YOUR CHOICE: 2
return 0; Consumer consumes item 20 from index 1
}
OUTPUT ENTER YOUR CHOICE: 2
1. PRODUCER Consumer consumes item 30 from index 2
2. CONSUMER
3. EXIT ENTER YOUR CHOICE: 2
ENTER YOUR CHOICE: 1 BUFFER IS EMPTY
Enter the item produced by the producer:
10
ENTER YOUR CHOICE: 1
Producer produces the item 10 at index 0
Enter the item produced by the producer:
40
Producer produces the item 40 at index 0
ENTER YOUR CHOICE: 1
Enter the item produced by the producer:
50
Producer produces the item 50 at index 1
ENTER YOUR CHOICE: 2
Consumer consumes item 40 from index 0
EXNO-7 DEADLOCK AVOIDANCE
USING BANKERS ALGORITHM
for (i = 0; i < n; i++) {
#include <stdio.h>
printf("P%d: ", i);
#include <stdlib.h>
for (j = 0; j < m; j++) {
int available[20];
scanf("%d", &max[i][j]);
int max[20][20];
}
int allocation[20][20];
}
int need[20][20];
// Input Available resources
int flag[20];
printf("\nEnter Available Resources:\n");
int main() {
for (i = 0; i < m; i++) {
int n, m, i, j, k, processCount = 0;
printf("Resource %d: ", i);
int safe, request[20], processNo, choice;
scanf("%d", &available[i]);
printf("Enter the number of processes: ");
}
scanf("%d", &n);
// Calculate Need Matrix
printf("Enter the number of resources: ");
printf("\nNeed Matrix:\n");
scanf("%d", &m);
for (i = 0; i < n; i++) {
// Input Allocation Matrix
for (j = 0; j < m; j++) {
printf("\nEnter the Allocation Matrix:\n");
need[i][j] = max[i][j] - allocation[i][j];
for (i = 0; i < n; i++) {
printf("%d ", need[i][j]);
printf("P%d: ", i);
}
for (j = 0; j < m; j++) {
printf("\n");
scanf("%d", &allocation[i][j]);
}
}
// Safety Check
}
printf("\nChecking system for safe state...\n");
// Input Max Matrix
int finish[20] = {0}, work[20];
printf("\nEnter the Max Matrix:\n");
for (i = 0; i < m; i++) {
work[i] = available[i]; }
} // Request Logic
} break;
} }
if (valid == -1) { if (j == m) {
}
if (count == n) {
finish[i] = 0; } else {
} Need Matrix
} P0: 7 4 3
P1: 1 2 2
return 0; P2: 6 0 0
Allocation Matrix:
P0: 0 1 0
P1: 2 0 0
P2: 3 0 2
Max Matrix:
P0: 7 5 3
P1: 3 2 2
P2: 9 0 2
Available Resources:
Resource 0: 3
Resource 1: 3
EXNO-8 DEADLOCK AVOIDANCE // Input request matrix
ALGORITHM
printf("Enter the Request matrix (%d x %d):\n",
np, nr);
PROGRAM:
for (i = 0; i < np; i++) {
#include <stdio.h> for (j = 0; j < nr; j++) {
int main() { scanf("%d", &request[i][j]);
int alloc[10][10], request[10][10], avail[10], }
total[10], work[10];
}
int mark[20] = {0}; // initialized to 0
// Calculate Available = Total - Allocated
int i, j, np, nr;
for (j = 0; j < nr; j++) {
printf("Enter the number of processes: ");
avail[j] = total[j];
scanf("%d", &np);
for (i = 0; i < np; i++) {
printf("Enter the number of resources: ");
avail[j] -= alloc[i][j];
scanf("%d", &nr);
}
// Input total resources
}
for (i = 0; i < nr; i++) {
} work[j] = avail[j];
printf("Enter the Allocation matrix (%d x %d):\ // Mark processes with 0 allocation as completed
n", np, nr);
for (i = 0; i < np; i++) {
for (i = 0; i < np; i++) {
int zeroAlloc = 1;
for (j = 0; j < nr; j++) {
for (j = 0; j < nr; j++) {
scanf("%d", &alloc[i][j]);
if (alloc[i][j] != 0) {
}
zeroAlloc = 0;
}
break;
} } while (done);
} }
mark[i] = 1;
done = 1; return 0;
} }
} OUTPUT
} Number of processes: 3
Number of resources: 2
Allocation matrix:
10
21
31
Request matrix:
10
11
11
❌ Deadlock detected.
P1 P2
err = pthread_create(&tid[i], NULL,
&createthread, NULL);
EXNO-9
THREADING if (err != 0)
else
#include <stdio.h>
printf("\nThread %d created successfully\
#include <string.h> n", i + 1);
#include <pthread.h> }
#include <stdlib.h> // Join the threads to wait for them to finish
#include <unistd.h> for (int i = 0; i < 2; i++) {
pthread_t tid[2]; // Only 2 threads needed pthread_join(tid[i], NULL);
void* createthread(void* arg) { }
pthread_t id = pthread_self(); printf("\nAll threads completed.\n");
if (pthread_equal(id, tid[0])) { return 0;
printf("\nFirst thread processing...\n"); }
} else if (pthread_equal(id, tid[1])) { OUTPUT
printf("\nSecond thread processing...\n"); Thread 1 created successfully
} Thread 2 created successfully
// Simulate work
int main(void) {
All threads completed.
int err;
int offset, pa, limit, p; // Calculate page number and offset from logical
address
printf("Enter the number of pages: ");
p = la / size;
scanf("%d", &n);
offset = la % size;
printf("Enter the size of each page (in bytes): ");
// Find frame number corresponding to page p
scanf("%d", &size);
int frame = -1;
limit = n * size - 1;
for (i = 0; i < n; i++) {
printf("Enter the logical address (0 to %d): ",
limit); if (pno[i] == p) {
OUTPUT
Page 0: 0
Page 1: 1
Page 2: 2
Page Table:
Page No Frame No
0 5
1 8
2 2
Page Number: 1
Offset: 70
} else
scanf("%d", &pno); }
Process 1: 212 }
1 212 2 }
} else {
printf("\n%d\t\t%d\t\tNot Allocated", i,
p[i]);
return 0;
OUTPUT
Block 1: 100
Block 2: 500
Block 3: 200
Block 4: 300
Block 5: 600
Process 1: 212
Process 2: 417
Process 3: 112
Process 4: 426
if(temp >= 0 && temp > greatest) {
EXNO-11(C) WORST FIT parray[i] = j;
greatest = temp;
PROGRAM: }
#include <stdio.h> }
}
int main() {
int fragment[20], b[20], p[20], i, j, nb, if(parray[i] != 0) {
np, temp, greatest; fragment[i] = greatest;
static int barray[20], parray[20]; barray[parray[i]] = 1; // mark block
as used
printf("\n\t\t MEMORY }
MANAGEMENT SCHEME - WORST }
FIT\n");
printf("\nProcess No.\tProcess Size\
printf("\nEnter the number of blocks: "); tBlock No.\tBlock Size\tFragment");
scanf("%d", &nb); for(i = 1; i <= np; i++) {
if(parray[i] != 0) {
printf("Enter the number of processes: "); printf("\n%d\t\t%d\t\t%d\t\t%d\t\t
scanf("%d", &np); %d",
i, p[i], parray[i], b[parray[i]],
printf("\nEnter the size of each block:\ fragment[i]);
n"); } else {
for(i = 1; i <= nb; i++) { printf("\n%d\t\t%d\t\tNot
printf("Block no %d: ", i); Allocated", i, p[i]);
scanf("%d", &b[i]); }
} }
2 417 2 500 83