0% found this document useful (0 votes)
22 views1 page

Farre 2

Uploaded by

vedalamuparna
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)
22 views1 page

Farre 2

Uploaded by

vedalamuparna
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/ 1

*** Banker algo *** Deadlock detection ***Producer Consumer

void calculate_need(int need[][10], int max[][10], int alloc[][10], int void calculate_need(int need[][10], int max[][10], int alloc[][10], #include <stdio.h>
p, int r) { int p, int r) { #include <stdlib.h>
for (int i = 0; i < p; i++) { for (int i = 0; i < p; i++) { #include <pthread.h>
for (int j = 0; j < r; j++) { for (int j = 0; j < r; j++) { #include <semaphore.h>
need[i][j] = max[i][j] - alloc[i][j]; } } } need[i][j] = max[i][j] - alloc[i][j]; } } } #include <unistd.h>
int is_safe(int processes[], int avail[], int max[][10], int alloc[][10], int detect_deadlock(int processes[], int avail[], int max[][10], int #define BUFFER_SIZE 511
int p, int r) { alloc[][10], int p, int r) { int buffer[BUFFER_SIZE];
int need[10][10]; int need[10][10]; int in = 0;
calculate_need(need, max, alloc, p, r); calculate_need(need, max, alloc, p, r); int out = 0;
int finish[10] = {0}; int finish[10] = {0}; sem_t empty;
int safe_seq[10]; int work[10]; sem_t full;
int work[10]; for (int i = 0; i < r; i++) { pthread_mutex_t mutex;
for (int i = 0; i < r; i++) { work[i] = avail[i]; } void* producer(void* arg) {
work[i] = avail[i]; } int count = 0; for (int i = 0; i < 10; i++) {
int count = 0; while (count < p) { int item = rand() % 100;
while (count < p) { int found = 0; sem_wait(&empty);
int found = 0; for (int i = 0; i < p; i++) { pthread_mutex_lock(&mutex);
for (int i = 0; i < p; i++) { if (finish[i] == 0) { buffer[in] = item;
if (finish[i] == 0) { int j; printf("Produced: %d\n", item);
int j; for (j = 0; j < r; j++) { in = (in + 1) % BUFFER_SIZE;
for (j = 0; j < r; j++) { if (need[i][j] > work[j]) { pthread_mutex_unlock(&mutex);
if (need[i][j] > work[j]) { break; } } sem_post(&full);
break; } } if (j == r) { if (j == r) { sleep(rand() % 2); }
for (int k = 0; k < r; k++) { for (int k = 0; k < r; k++) { return NULL; }
work[k] += alloc[i][k]; } work[k] += alloc[i][k];} void* consumer(void* arg) {
safe_seq[count++] = i; finish[i] = 1; for (int i = 0; i < 10; i++) {
finish[i] = 1; found = 1; sem_wait(&full);
found = 1; } } } count++; } } } pthread_mutex_lock(&mutex);
if (found == 0) { if (found == 0) { int item = buffer[out];
printf("Request cannot be fulfilled\n"); printf("Deadlock detected\n"); printf("Consumed: %d\n", item);
return 0; } } return 1; } } out = (out + 1) % BUFFER_SIZE;
printf("Request can be fulfilled\nSafe Sequence: "); printf("No deadlock detected\n"); pthread_mutex_unlock(&mutex);
for (int i = 0; i < p; i++) { return 0; } sem_post(&empty);
printf("P%d ", safe_seq[i]); } int main() { sleep(rand() % 2); }
printf("\n"); int p, r; return NULL; }
return 1; } printf("Enter number of processes: "); int main() {
int main() scanf("%d", &p); printf("Name: Aakash Manna\nSection: H1\nRoll No.: 02\n");
{ int p, r; printf("Enter number of resources: "); pthread_t prod, cons;
printf("Enter number of processes: "); scanf("%d", &r); sem_init(&empty, 0, BUFFER_SIZE);
scanf("%d", &p); int processes[10], avail[10], max[10][10], alloc[10][10]; sem_init(&full, 0, 0);
printf("Enter number of resources: "); printf("Enter maximum requirement:\n"); pthread_mutex_init(&mutex, NULL);
scanf("%d", &r); for (int i = 0; i < p; i++) { pthread_create(&prod, NULL, producer, NULL);
int processes[10], avail[10], max[10][10], alloc[10][10]; for (int j = 0; j < r; j++) { pthread_create(&cons, NULL, consumer, NULL);
printf("Enter maximum requirement:\n"); scanf("%d", &max[i][j]); } } pthread_join(prod, NULL);
for (int i = 0; i < p; i++) { printf("Enter allocated matrix:\n"); pthread_join(cons, NULL);
for (int j = 0; j < r; j++) { for (int i = 0; i < p; i++) { sem_destroy(&empty);
scanf("%d", &max[i][j]); } } for (int j = 0; j < r; j++) { sem_destroy(&full);
printf("Enter allocated matrix:\n"); scanf("%d", &alloc[i][j]); } } pthread_mutex_destroy(&mutex);
for (int i = 0; i < p; i++) { printf("Enter resource vector:\n"); return 0; }
for (int j = 0; j < r; j++) { for (int i = 0; i < r; i++) {
scanf("%d", &alloc[i][j]); } } scanf("%d", &avail[i]); }
printf("Enter resource vector:\n"); detect_deadlock(processes, avail, max, alloc, p, r);
for (int i = 0; i < r; i++) { return 0; }
scanf("%d", &avail[i]); }
is_safe(processes, avail, max, alloc, p, r);
return 0; }

You might also like