0% found this document useful (0 votes)
9 views5 pages

Osy P11

The document contains practical implementations of the Producer-Consumer problem and the Banker's algorithm in C. It includes code for a producer and consumer using semaphores and mutexes, as well as the Banker's algorithm for resource allocation safety checks. The document also outlines the necessary input for the Banker's algorithm and the corresponding output for both programs.
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)
9 views5 pages

Osy P11

The document contains practical implementations of the Producer-Consumer problem and the Banker's algorithm in C. It includes code for a producer and consumer using semaphores and mutexes, as well as the Banker's algorithm for resource allocation safety checks. The document also outlines the necessary input for the Banker's algorithm and the corresponding output for both programs.
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/ 5

BTCOL406 Operating Systems

Practical No. 11
Aim Implementation of Producer-Consumer problem, Bankers algorithm.

1. Consumer problem Code


----------------------------------------( Program Codes )------------------------------------
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

#define BUFFER_SIZE 5

int buffer[BUFFER_SIZE];
int in = 0, out = 0;

sem_t empty, full;


pthread_mutex_t mutex;

void* producer(void* arg) {


int item,i;
for (i = 0; i < 10; i++) {
item = rand() % 100; // Produce a random item
sem_wait(&empty); // Decrement empty count
pthread_mutex_lock(&mutex); // Lock the buffer

buffer[in] = item;
printf("Producer produced: %d at %d\n", item, in);
in = (in + 1) % BUFFER_SIZE;

pthread_mutex_unlock(&mutex); // Unlock the buffer


sem_post(&full); // Increment full count
sleep(1); // Simulate time to produce
}
return NULL;
}

void* consumer(void* arg) {


int item,i;
for ( i = 0; i < 10; i++) {
sem_wait(&full); // Decrement full count
pthread_mutex_lock(&mutex); // Lock the buffer

item = buffer[out];
printf("Consumer consumed: %d from %d\n", item, out);
out = (out + 1) % BUFFER_SIZE;

Practical N0. 11 Page 1 of 5


BTCOL406 Operating Systems

pthread_mutex_unlock(&mutex); // Unlock the buffer


sem_post(&empty); // Increment empty count
sleep(1); // Simulate time to consume
}
return NULL;
}

int main() {
pthread_t prod, cons;
// Initialize semaphores and mutex
sem_init(&empty, 0, BUFFER_SIZE); // Initially, buffer is empty
sem_init(&full, 0, 0); // Initially, no full slots
pthread_mutex_init(&mutex, NULL);
// Create producer and consumer threads
pthread_create(&prod, NULL, producer, NULL);
pthread_create(&cons, NULL, consumer, NULL);
// Wait for threads to complete
pthread_join(prod, NULL);
pthread_join(cons, NULL);
// Cleanup
pthread_mutex_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);

return 0;
}
------------------------------------( Program Output )---------------------------------------

Practical N0. 11 Page 2 of 5


BTCOL406 Operating Systems

2. Bankers algorithm Code


----------------------------------------( Program Codes )------------------------------------
#include <stdio.h>
#define MAX 10

int n, m, i, j, p;
int allocation[MAX][MAX], max[MAX][MAX], need[MAX][MAX], available[MAX];
int finished[MAX], safeSequence[MAX];

void calculateNeed() {
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - allocation[i][j];
}

int isSafe() {
int work[MAX], count = 0, found;
for (i = 0; i < m; i++)
work[i] = available[i];

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


finished[i] = 0;

while (count < n) {


found = 0;
for (p = 0; p < n; p++) {
if (!finished[p]) {
for (j = 0; j < m; j++)
if (need[p][j] > work[j])
break;

if (j == m) { // All needs are met


for (j = 0; j < m; j++)
work[j] += allocation[p][j];
safeSequence[count++] = p;
finished[p] = 1;
found = 1;
}
}
}
if (!found) {
printf("System is not in a safe state!\n");
return 0;
}
}

Practical N0. 11 Page 3 of 5


BTCOL406 Operating Systems

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


for (i = 0; i < n; i++)
printf("P%d ", safeSequence[i]);
printf("\n");
return 1;
}

int main() {
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the number of resource types: ");
scanf("%d", &m);

printf("Enter the Allocation matrix:\n");


for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &allocation[i][j]);

printf("Enter the Max matrix:\n");


for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &max[i][j]);

printf("Enter the Available resources:\n");


for (i = 0; i < m; i++)
scanf("%d", &available[i]);

calculateNeed();

if (!isSafe()) {
printf("Unsafe state detected, allocation aborted!\n");
}

return 0;
}

Practical N0. 11 Page 4 of 5


BTCOL406 Operating Systems

------------------------------------( Program Output )---------------------------------------

Practical N0. 11 Page 5 of 5

You might also like