Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by Admin ( 9 years ago )
OS LAB

1. Write a program to avoid deadlock using Banker’s algorithm.(Safety algorithm)


#include<stdio> 
int  main() 
{ 
int allocated[15][15], max[15][15], need[15][15], avail[15], tres[15], work[15], flag[15]; 
int pno,rno, i , j, prc, count, t, total; 
count=0; 

printf("Enter number of process:"); 
scanf("%d", &pno); 
printf("Enter number of resources:"); 
scanf("%d",&rno); 
for(i=1;i<=pno;i++) 
{ flag[i]=0; } 
printf("Enter total number of each resources:"); 
for(i=1;i<=rno;i++) 
scanf("%d", &tres[i]); 

printf("\n Enter max resources for each process:"); 
for(i=1;i<=pno;i++) 
{ 
printf("\n for process %d:",i); 

for(j=1;j<=rno;j++) 
scanf("%d", &max[i][j]); 
} 

printf("Enter allocated resources for each process:"); 
for(i=1;i<=pno;i++) 
{ 
printf("\n for process %d:",i); 
for(j=1;j<=rno;j++) 
scanf("%d", &allocated[i][j]); 
} 

printf("\n available resources:"); 
for(j=1;j<=rno;j++) 
{ 
avail[j]=0; 
total=0; 
for(i=1;i<=pno;i++) 
{ 
total+=allocated[i][j]; 
} 
avail[j]=tres[j]-total; 
work[j]=avail[j]; 
printf(" %d \t", work[j]);
} 

do 
{ 
for(i=1;i<=pno;i++) 
{ for (j=1;j<=rno;j++) 
{ 
need[i][j]=max[i][j]-allocated[i][j]; 
} 
} 

printf("Allocated matrix     Max      Need"); 
for(i=1;i<=pno;i++) 
{ printf("\n"); 
for(j=1;j<=rno;j++) 


{ printf("%d", allocated[i][j]); 
} 
printf("|"); 
for(j=1;j<=rno;j++) 
{ 
printf("M", max[i][j]); 
} 
printf("|"); 
for(j=1;j<=rno;j++) 
{ 
printf("M",need[i][j]); 
} 
} 

prc=0; 
for(i=1;i<=pno;i++) 
{ if( flag[i]==0) 
{ prc=1; 
for(j=1;j<=rno;j++) 
{ if( work[j]< need prc=0; if(prc!=0) if(prc!=0) for(j=1;j work[j]+=allocated[prc][j]; allocated[prc][j]=0; max[prc][j]=0; flag[prc]=1; count!=pno prc!=0); xss=removed>
using namespace std;

const int P = 5;

const int R = 3;

// Function to find the need of each process
void calculateNeed(int need[P][R], int maxm[P][R],
int allot[P][R])
{
for (int i = 0 ; i < P xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed> work[j])
break;
if (j == R)
{
for (int k = 0 ; k < R xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed> 
#include<stdlib> 
int main() 
{ 
int max[10][10],need[10][10],alloc[10][10],avail[10],complete[10],safeseq[10]; 
int p,r,i,j,process,count; 
count=0; 
printf("Enter no. of processes:"); 
scanf("%d",&p); 
for(i=0;i<p complete[i]=0; for(i=0;i for(j=0;j for(i=0;i for(j=0;j for(i=0;i for(i=0;i for(j=0;j need[i][j]=max[i][j]-alloc[i][j]; for(j=0;j for(j=0;j process=-1; for(i=0;i xss=removed process=i; for(j=0;j process=-1; if(process!=-1) if(process!=-1) safeseq[count]=process+1; for(j=0;j avail[j]+=alloc[process][j]; alloc[process][i]=0; max[process][j]=0; complete[process]=1; while(count!=p process!=-1); xss=removed for(i=0;i>\n"); 
} 
else 
{ 
printf("\nThe system is in an unsafe state!!"); 
} 
}












OUTPUT:

 

Revise this Paste

Your Name: Code Language: