Exno5 Os
Exno5 Os
DATE:
AIM:
ALGORITHM:
Step 6: If the new request comes then check that the system is in safety or not if weallow the request. Step 7:
Stop the execution
PROGRAM:
#include<stdio.h>
void main()
clrscr();
scanf("%d",&pno);
scanf("%d",&rno);
for(i=1;i< =pno;i++)
flag[i]=0;
for(i=1;i<= pno;i++)
scanf("%d",&max[i][j]);
pno;i++)
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("\n"); for(j=1;j<=
rno;j++)
{
}
printf("|");
for(j=1;j<= rno;j++)
{
}
prc=0;
printf("%4 d",need[i][ j]);
}
for(i=1;i<= pno;i++)
{
if(flag[i]==0)
{
prc=i;
for(j=1;j<= rno;j++)
{
if(work[j]< need[i][j])
{
prc=0; break;
}
}
}
if(prc!=0) break;
}
if(prc!=0)
{
printf("\n Process %d completed",i);count++; printf("\n Available matrix:");for(j=1;j<= rno;j++)
{
work[j]+=allocated[prc][j]; allocated[prc][j]=0; max[prc][j]=0; flag[prc]=1;
printf(" %d",work[j]);
}
}
}while(count!=pno&&prc!=0); if(count==pno)
printf("\nThe system is in a safe state!!");
else
getch();
}
printf("\nThe system is in an unsafe state!!");
}
}
OUTPUT
Enter number of process:5 Enter number of resources:3
Enter total numbers of each resources:10 5 7Enter Max resources for each process:
for process 1: 7 5 3
for process 2: 3 2 2
for process 3: 9 0 2
for process 4: 2 2 2
for process 5: 4 3 3
Enter allocated resources for each process:for process 1: 0 1 0
for process 2: 3 0 2
for process 3: 3 0 2
for process 4: 2 1 1
for process 5: 0 0 2 available resources:
2 3 0
0 1 0| 7 5 3| 7 4 3
3 0 2| 3 2 2| 0 2 0
3 0 2| 9 0 2| 6 0 0
2 1 1| 2 2 2| 0 1 1
0 0 2| 4 3 3| 4 3 1
Process 2 completed
Available matrix: 5 3 2 Allocated matrix
Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
2 1 1| 2 2 2| 0 1 1
0 0 2| 4 3 3| 4 3 1
Process 4 completed
Available matrix: 7 4 3 Allocated matrix
Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 1 completed
Available matrix: 7 5 3 Allocated matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
0 0 0| 0 0 0| 0 00
0 0 2| 4 3 3| 4 31
Process 3 completed
Available matrix: 10 5 5 Allocated matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 5 completed
Available matrix: 10 57
The system is in a safe state!!
RESULT:
Thus the program to implement the deadlock avoidance was executed and verified.
EX.NO:5B DEADLOCK DETECTION ALGORITHM
DATE:
AIM:
To Simulate Algorithm for Deadlock detection
ALGORITHM:
Step 1: Start the Program
Step 2: Get the values of resources and processes.
PROGRAM
#include<stdio.h> #include<conio.h> int max[100][100]; i nt alloc[100][100]; int need[100][100]; int
avail[100];
int n,r;
void input(); void show(); void cal(); int main()
{
int i,j;
printf("********** Deadlock Detection Algo ************\n"); input();show(); cal();
getch(); return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");scanf("%d",&n); printf("Enter the no of resource
instances\t");scanf("%d",&r); printf("Enter the Max Matrix\n");for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n"); for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n"); for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t"); for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1); for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t"); if(i==0)
{
for(j=0;j<r;j++) printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0; int dead[100];int safe[100]; int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(flag)
{
flag=0; for(i=0;i<n;i++)
{
int c=0; for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j]; finish[i]=1;flag=1;
}
//printf("\nP%d",i); if(finish[i]==1)
{
i=n;
}
}
}
}
}
} j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i; j++;
flag=1;
}
}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n");for(i=0;i<n;i++)
{
}
}
else
{
}
}
printf("P%d\t",dead[i]);
OUTPUT:
Enter the no. Of processes 3
Enter the no of resources instances 3Enter the max matrix
368
433
344
RESULT:
Thus the program to implement the deadlock detection was executed successfully.
{