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

Oslab 5

The document contains a C program that implements the Banker's algorithm for resource allocation and deadlock avoidance in operating systems. It allows the user to input the number of processes, resources, their allocation, maximum claims, and available resources, and then checks if a new resource request can be granted without leading to a deadlock. Additionally, it includes a producer-consumer problem implementation demonstrating synchronization between producer and consumer processes using a simple buffer management system.
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)
4 views5 pages

Oslab 5

The document contains a C program that implements the Banker's algorithm for resource allocation and deadlock avoidance in operating systems. It allows the user to input the number of processes, resources, their allocation, maximum claims, and available resources, and then checks if a new resource request can be granted without leading to a deadlock. Additionally, it includes a producer-consumer problem implementation demonstrating synchronization between producer and consumer processes using a simple buffer management system.
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

#include <stdio.

h>

struct file {
int all[10];
int max[10];
int need[10];
int flag;
};

void main() {
struct file f[10];
int i, j, k, p, n, r, cnt = 0, id;
int avail[10], seq[10], newr[10];

printf("Enter number of processes: ");


scanf("%d", &n);
printf("Enter number of resources: ");
scanf("%d", &r);

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


printf("Enter details for P%d\n", i);
printf("Enter allocation: ");
for (j = 0; j < r; j++)
scanf("%d", &f[i].all[j]);

printf("Enter Max: ");


for (j = 0; j < r; j++)
scanf("%d", &f[i].max[j]);

f[i].flag = 0;
}

printf("\nEnter Available Resources: ");


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

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


for (j = 0; j < r; j++) {
f[i].need[j] = f[i].max[j] - f[i].all[j];
if (f[i].need[j] < 0) {
printf("\nError: Process P%d has been allocated more than its
maximum claim!", i);
return;
}
}
}

printf("\nEnter New Request Details\n");


printf("Enter process ID (0 to %d): ", n - 1);
scanf("%d", &id);

if (id < 0 || id >= n) {


printf("\nError: Invalid process ID!\n");
return;
}

printf("Enter Request for Resources: ");


for (i = 0; i < r; i++) {
scanf("%d", &newr[i]);

if (newr[i] > f[id].need[i]) {


printf("\nError: Process P%d requested more than its maximum
need!\n", id);
return;
}

if (newr[i] > avail[i]) {


printf("\nError: Not enough available resources for P%d's
request!\n", id);
return;
}
}

for (i = 0; i < r; i++) {


f[id].all[i] += newr[i];
avail[i] -= newr[i];
f[id].need[i] -= newr[i]; // Update need matrix
}

cnt = 0;
int fl = 0, g;

while (cnt != n) {
g = 0;
for (j = 0; j < n; j++) {
if (f[j].flag == 0) {
int canExecute = 1;
for (p = 0; p < r; p++) {
if (avail[p] < f[j].need[p]) {
canExecute = 0;
break;
}
}

if (canExecute) {
printf("\nP%d is visited", j);
seq[fl++] = j;
f[j].flag = 1;
for (k = 0; k < r; k++)
avail[k] += f[j].all[k];
cnt++;

printf("\nAvailable resources after P%d execution: (", j);


for (k = 0; k < r; k++)
printf("%d ", avail[k]);
printf(")");

g = 1;
}
}
}

if (g == 0) {
printf("\nREQUEST NOT GRANTED - DEADLOCK OCCURRED");
printf("\nSYSTEM IS IN UNSAFE STATE");
return;
}
}

printf("\nSYSTEM IS IN SAFE STATE");


printf("\nThe Safe Sequence is: (");
for (i = 0; i < fl; i++)
printf("P%d ", seq[i]);
printf(")\n");

printf("\nProcess\t\tAllocation\tMax\t\tNeed\n");
for (i = 0; i < n; i++) {
printf("P%d\t\t", i);
for (j = 0; j < r; j++)
printf("%d ", f[i].all[j]);
printf("\t\t");
for (j = 0; j < r; j++)
printf("%d ", f[i].max[j]);
printf("\t\t");
for (j = 0; j < r; j++)
printf("%d ", f[i].need[j]);
printf("\n");
}
}
Enter number of processes: 4

Enter number of resources: 3

Enter details for P0

Enter allocation: 1 2 2

Enter Max: 3 3 2

Enter details for P1

Enter allocation: 1 0 3

Enter Max: 6 1 3

Enter details for P2

Enter allocation: 1 2 1

Enter Max: 3 3 3

Enter details for P3

Enter allocation: 0 2 1

Enter Max: 4 2 2

Enter Available Resources: 2 1 0

Enter New Request Details

Enter process ID (0 to 3): 2

Enter Request for Resources: 1 0 1

Bankers algo
#include<stdio.h>
#include<stdlib.h>
int mutex = 1;
int empty = 5;
int full = 0;
int x = 0;
void producer(){
--mutex;
--empty;
++full;
x++;
printf("item %d was produced",x);
++mutex;
}
void consumer(){
--mutex;
++empty;
--full;
x--;
printf("Item %d was consumed",x);
++mutex;
}
void main(){
int ch;
do{
printf("\n1)producer\n2)consumer\n3)exit\n");
scanf("%d",&ch);
switch(ch){
case 1:
if(mutex==1&&empty!=0){
producer();
}else{
printf("buffer is full");
}
break;
case 2:
if(mutex==1&&full!=0){
consumer();
}else{
printf("buffer is empty");
}
break;
case 3: exit(0);
break;
default: printf("Invalid choice");
}
}while(ch!=3);
} producer consumer

You might also like