0% found this document useful (0 votes)
10 views21 pages

Exno5 Os

The document outlines two algorithms for managing deadlocks in operating systems: deadlock avoidance and deadlock detection. The deadlock avoidance algorithm simulates resource allocation to ensure the system remains in a safe state, while the deadlock detection algorithm identifies processes involved in a deadlock situation. Both algorithms include detailed steps and sample code for implementation.

Uploaded by

vishal.cs23
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)
10 views21 pages

Exno5 Os

The document outlines two algorithms for managing deadlocks in operating systems: deadlock avoidance and deadlock detection. The deadlock avoidance algorithm simulates resource allocation to ensure the system remains in a safe state, while the deadlock detection algorithm identifies processes involved in a deadlock situation. Both algorithms include detailed steps and sample code for implementation.

Uploaded by

vishal.cs23
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/ 21

EX.

NO: 5A DEADLOCK AVOIDANCE

DATE:

AIM:

To Simulate Algorithm for Deadlock avoidance

ALGORITHM:

Step 1: Start the Program

Step 2: Get the values of resources and processes.

Step 3: Get the avail value.

Step 4: After allocation find the need value.

Step 5: Check whether its possible to allocate. If possible it is safe state

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()

int pno,rno,i,j,prc,count,t,total; count=0;

clrscr();

printf("\n Enter number of process:");

scanf("%d",&pno);

printf("\n Enter number of resources:");

scanf("%d",&rno);

for(i=1;i< =pno;i++)

flag[i]=0;

printf("\n Enter total numbers 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]);
pno;i++)

printf("\n for process %d:",i);


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

printf("\n available resources:\n");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("\n Allocated matrix Max need");


for(i=1;i<= pno;i++)

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

Allocated matrix Max need

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.

Step 3: Get the avail value..


Step 4: After allocation find the need value. Step 5: Check whether its possible to allocate.
Step 6: If it is possible then the system is in safe state. Step 7: Stop the execution

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

Enter the allocation matrix3 3 3


203
124
Enter the available resources1 2 0

Process allocation max available


P1 333 368 120
P2 203 433
P3 124 344
System is in deadlock and deadlock process areP1 P2 P3

RESULT:
Thus the program to implement the deadlock detection was executed successfully.
{

You might also like