Demonstrating Deadlock with Resource Allocation

#include<stdio.h>
int main()
{
	int found,flag,l,p[5][10],tp,c[5][10],i,j,k=1,m[10],r[10],a[10],temp[10],sum=0,amp;
	
	printf("enter total no of processes:\n");
	scanf("%d",&tp);
	
	printf("enter clain matrix:\n");
	for(i=1;i<=4;i++)
	for(j=1;j<=5;j++)
	{
		scanf("%d",&c[i][j]);
	}
	
	printf("enter allocation matrix:\n");
	for(i=1;i<=4;i++)
		for(j=1;j<=5;j++)
		{
			scanf("%d",&p[i][j]);
		}
	
		printf("enter resource vector:\n");
	for(i=1;i<=5;i++)
	{
		scanf("%d",&r[i]);
	}
	
	printf("enter availability vector:\n");
	for(i=1;i<=5;i++)
	{
		scanf("%d",&a[i]);
		temp[i]=a[i];
	}
	
	for(i=1;i<=4;i++)
	{
		sum=0;
		for(j=1;j<=5;j++)
		{
			sum+=p[i][j];
		}
		if(sum==0)
		{
			m[k]=i;
			k++;
		}
	}
	
	for(i=1;i<=4;i++)
	{
		for(l=1;l<k;l++)
			if(i!=m[l])
			{
				flag=1;
				for(j=1;j<=5;j++)
					if(c[i][j]>temp[j])
					{
						flag=0;
						break;
					}
			}
		if(flag==1)
		{
			m[k]=i;
			k++;
			for(j=1;j<=5;j++)
				temp[j]+=p[i][j];
		}
	}
	
	printf("deadlock causing processes are:");
	for(j=1;j<=tp;j++)
	{
		found=0;
		for(i=1;i<k;i++)
		{
			if(j==m[i])
			found=1;
		}
		if(found==0)
			printf("%d\t",j);
	}
	return 0;
}

/*

enter total no. of processes : 
4 
enter claim matrix : 
0
1
0
0
1
0
0
1
0
1
0
0
0
0
1
1
0
1
0
1
enter allocation matrix : 
1
0
1
1
0
1
1
0
0
0
0
0
0
1 
0
0
0
0
0
0
enter resource vector : 
2
1
1
2
1
enter the availability vector : 
0
0
0
0
1 
deadlock causing processes are : 
1
2

*/

One thought on “Demonstrating Deadlock with Resource Allocation”

  1. Please see my answer to this question . Bottom line whenever two threads need to acquire two different resources, and do so in different orders then you can get deadlocks.

Leave a Reply

Your email address will not be published. Required fields are marked *