Oslab 5
Oslab 5
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];
f[i].flag = 0;
}
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++;
g = 1;
}
}
}
if (g == 0) {
printf("\nREQUEST NOT GRANTED - DEADLOCK OCCURRED");
printf("\nSYSTEM IS IN UNSAFE STATE");
return;
}
}
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 allocation: 1 2 2
Enter Max: 3 3 2
Enter allocation: 1 0 3
Enter Max: 6 1 3
Enter allocation: 1 2 1
Enter Max: 3 3 3
Enter allocation: 0 2 1
Enter Max: 4 2 2
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