[go: up one dir, main page]

0% found this document useful (0 votes)
13 views7 pages

Deadlock Lab Program

The document presents two algorithms for deadlock detection in operating systems: one for deadlock detection and another using Banker's algorithm. Both algorithms involve user input for process and resource matrices, and they calculate the need matrix to determine if the system is in a safe state or if deadlock occurs. The code includes functions for input, display, and calculation of the necessary matrices and conditions.

Uploaded by

Shashank S
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)
13 views7 pages

Deadlock Lab Program

The document presents two algorithms for deadlock detection in operating systems: one for deadlock detection and another using Banker's algorithm. Both algorithms involve user input for process and resource matrices, and they calculate the need matrix to determine if the system is in a safe state or if deadlock occurs. The code includes functions for input, display, and calculation of the necessary matrices and conditions.

Uploaded by

Shashank S
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/ 7

Ex.No : 7 Design, Develop and Implement an Algorithm for Dead Lock Detection.

#include<stdio.h>

int max[100][100];

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

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

{printf("P%d\t",dead[i]);

}}

else

printf("\nNo Deadlock Occur"); }}

Ex.No 8: Design, Develop and Implement an Algorithm for Deadlock using Banker’s
Algorithm.
#include<stdio.h>

int max[100][100];

int 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("********** Baner's Algo ************\n");

input();

show();
cal();

return 0;

void input()

int i,j;

printf("Enter the no of Processes\t");

scanf("%d",&n);

printf("Enter the no of resources 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 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];

}}

printf("\n");

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("P%d->",i);

if(finish[i]==1) {

i=n;

}}}}}}

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

if(finish[i]==1) {

c1++;

else

{printf("P%d->",i);

}}

if(c1==n)

{printf("\n The system is in safe state");

else

printf("\n Process are in dead lock");

printf("\n System is in unsafe state");

}}

You might also like