• 0

Problem With Deadlock Avoidance C Code


Question

Problem:Deadlock Avoidance

Inputs: No. Of Process ; Request matrix ; allocation matrix ;resource vector

Need Matrix Is Computed By REQUEST- ALLOCATION MATRIX

After that REsource Vector is taken

let me explain my problem

>> no of process 3

FOR NEED MATRIX :

1 0 0

2 1 0

7 1 1

REQUEST MATRIX :

3 1 1

4 2 1

9 2 3

ALLOCATION MATRIX

2 1 1

2 1 1

2 1 2

THE CODEWORKS FINE ,ACTUALLY THE NEED MATRIX ie REQ- ALLO MATRIX IS COMPUTED THE RESOURCE ARRAY IS TAKEN FOR EG 1 1 1

THEN 1 IS COMPARED TO NEED MATRIX 1ST ELEMENT IN TOP ROW IF LESSER THAN IT GOES FOR THE CHECKING OF NEXT RESOURCE WITH THE NEXT ELEMENT IN THE MATRIX IN RIGHTWARD DIRECTION ,, SUPPOSE ALL THE RESOURCE VECTORS ARE CHECKED WITH NEED MATRIX THEN RESOURCE VECTOR IS ADDED WITH THAT CORRESPONDING ROW OF ALLOCATION ELEMENTS,BUT IF RESOUCE VECTOR IS LESSER THAN NEED MATRIX THEN IT SIMPLY SKIPS AND GOES FOR THE NEXT LINE CHECKING I N MY EG

1: IS 1>=1 YES SO NEXT ELEMENT OF RESOURCE VEC.. IS 1>=0 YES SO IS 1>=0 YES

SO NEW RESOURCE VECTOR IS 1+2 1+1 1+1

AND THIS GOES ON IF AT LAST RES VEC.. IS 1+2+2 1+1+1 1+1+1 WHICH IS 5 3 3

SINCE 5<7 SO PROCESS 3 IS UNSAFE AND REST ARE SAFE

CODE WORKS FINE IF THE ABOVE VALUES ARE GIVEN BUT NOT IF

REQUEST MATRIX IS

2 1 0

1 0 0

7 1 1

OR

2 1 0

7 1 1

1 0 0

PROGRAM :

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10][10],b[10][10],n[10][10],i,j,x,y,succ,done;

int z[10],flag[10]={0},r,p;

printf("enter the number of resources you want ti have ");

scanf("%d",&r);

printf("\n enter the reqest matrix \n ");

for(x=1;x<=r;x++)

{

for(y=1;y<=r;y++)

{

scanf("%d",&b[x][y]);

}

}

printf("\n enter the allocation matrix");

for(x=1;x<=r;x++)

{

for(y=1;y<=r;y++)

{

scanf("%d",&a[x][y]);

}

}

for(x=1;x<=r;x++)

{

for(y=1;y<=r;y++)

{

n[x][y]=b[x][y]-a[x][y];

printf(" %d",n[x][y]);

}

printf("\n");

}

printf("\n Enter the resource vector\n");

for(x=1;x<=r;x++)

{

scanf("%d",&z[x]);

}

done=r;

for(x=1;x<=r && done>=0;x++)

{

for(y=1;y<=r;y++)

{

if(z[y]>=n[x][y])

{

flag[x]=1;

if(y==r)

{

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

{

z=z+a[x];

}

flag[x]=1;

done--;

x=1;

}

}

else

{

flag[x]=0;

if(y==r)

{

done--;

break;

}

else

{

break;

}

}

}

}

for(x=1;x<=r;x++)

{

if(flag[x]==1)

{

printf("\nprocess [%d] in safe state",x);

}

else if(flag[x]==0)

{

printf("\nprocess [%d] unsafe",x);

}

}

getch();

}

WAITING FOR EXPERTS ANSWERS PLS HELP

Edited by pradyot
Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

I guess ill tackle this, took me a while to try to understand what your trying to do.

for anyone that wants to help, hes referring to bankers algorithm

for(x=1;x&lt;=r;x++)
{
for(y=1;y&lt;=r;y++)
{
scanf("%d",&amp;b[x][y]);
}
}

is it intentional to skip b[0][0]?

for a 3x3 matrix

- 0 1 2

0 a b c

1 d e f

2 g h i

so when u declare a[10] it has indexes 0-9

when u write to a[10], it writes to something else.

fixing that error makes your code

seems to work for me so far.

also why is main void

if something were to happen in main, its best to return values to describe the error; so its good practice to do int main()

Link to comment
Share on other sites

  • 0

thank u for responding i made the changes but looks like the program is running only for one time in loop of x

and x does not gets reset to 0

code:

#include<stdio.h>

#include<conio.h>

int main()

{

int a[10][10],b[10][10],n[10][10],i,j,x,y,succ,done;

int z[10],flag[10]={0},r,p;

printf("enter the number of resources you want ti have ");

scanf("%d",&r);

printf("\n enter the reqest matrix \n ");

for(x=0;x<r;x++)

{

for(y=0;y<r;y++)

{

scanf("%d",&b[x][y]);

}

}

printf("\n enter the allocation matrix");

for(x=0;x<r;x++)

{

for(y=0;y<r;y++)

{

scanf("%d",&a[x][y]);

}

}

for(x=0;x<r;x++)

{

for(y=0;y<r;y++)

{

n[x][y]=b[x][y]-a[x][y];

printf(" %d",n[x][y]);

}

printf("\n");

}

printf("\n Enter the resource vector\n");

for(x=0;x<r;x++)

{

scanf("%d",&z[x]);

}

done=r;

for(x=0;x<r && done>0;x++)

{

for(y=0;y<r;y++)

{

if(z[y]>=n[x][y])

{

flag[x]=1;

if(y==r)

{

for(i=0;i<r;i++)

{

z=z+a[x];

a[x]=0;

n[x]=0;

}

flag[x]=1;

--done;

x=0;

break;

}

}

else

{

flag[x]=0;

if(y==r)

{

--done;

}

else

{

break;

}

}

}

}

for(x=0;x<r;x++)

{

if(flag[x]==1)

{

printf("\nprocess [%d] in safe state",x);

}

else if(flag[x]==0)

{

printf("\nprocess [%d] unsafe",x);

}

}

getch();

}

pls solve!!

Link to comment
Share on other sites

  • 0

Pradyot, please format your code using the tags. Just pasting your code in doesn't help us because to help you we first have to format your code properly.

if( blah )
{
	// do this
	//
	//
}else{
	// do that
	//
	//
}

rather than

if( blah )

{

// do this

//

//

}else{

// do that

//

//

}

While this is a trivial example, formatting all of your code correctly really helps us to help you. It also helps you to right better code so hopefully you'll notice bugs more, too.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.