fork download
  1. #include<stdio.h>
  2. int max[100][100];
  3. int alloc[100][100];
  4. int need[100][100];
  5. int avail[100];
  6. int n,r;
  7. void input();
  8. void show();
  9. void cal();
  10. int main()
  11. {
  12. int i,j;
  13. printf("********** Deadlock Detection Algo ************\n");
  14. input();
  15. show();
  16. cal();
  17.  
  18. return 0;
  19. }
  20. void input()
  21. {int i,j;
  22. printf("Enter the no of Processes\t");
  23. scanf("%d",&n);
  24. printf("Enter the no of resource instances\t");
  25. scanf("%d",&r);
  26. printf("Enter the Max Matrix\n");
  27. for(i=0;i<n;i++)
  28. {for(j=0;j<r;j++) {
  29. scanf("%d",&max[i][j]);
  30. }}
  31.  
  32. printf("Enter the Allocation Matrix\n");
  33. for(i=0;i<n;i++)
  34. {for(j=0;j<r;j++) {
  35. scanf("%d",&alloc[i][j]);
  36. }}
  37. printf("Enter the available Resources\n");
  38. for(j=0;j<r;j++) {
  39. scanf("%d",&avail[j]);
  40. }}
  41. void show() {
  42. int i,j;
  43. printf("Process\t Allocation\t Max\t Available\t");
  44. for(i=0;i<n;i++) {
  45. printf("\nP%d\t ",i+1);
  46. for(j=0;j<r;j++) {
  47. printf("%d ",alloc[i][j]); }
  48. printf("\t");
  49. for(j=0;j<r;j++)
  50. {printf("%d ",max[i][j]); }
  51. printf("\t");
  52. if(i==0) {
  53. for(j=0;j<r;j++)
  54. printf("%d ",avail[j]);
  55. }}}
  56.  
  57. void cal()
  58. { int finish[100],temp,need[100][100],flag=1,k,c1=0;
  59. int dead[100];
  60. int safe[100];
  61. int i,j;
  62. for(i=0;i<n;i++)
  63.  
  64. {finish[i]=0;
  65. }
  66. //find need matrix
  67. for(i=0;i<n;i++)
  68. {for(j=0;j<r;j++)
  69. {
  70. need[i][j]=max[i][j]-alloc[i][j];
  71. }}
  72. while(flag)
  73. {flag=0;
  74. for(i=0;i<n;i++)
  75. {int c=0;
  76. for(j=0;j<r;j++)
  77. {if((finish[i]==0)&&(need[i][j]<=avail[j]))
  78. {c++;
  79. if(c==r)
  80. {
  81. for(k=0;k<r;k++)
  82. {avail[k]+=alloc[i][j];
  83. finish[i]=1;
  84. flag=1;
  85. }//printf("\nP%d",i);
  86. if(finish[i]==1)
  87. {i=n;
  88. }}}}}}
  89. j=0;
  90. flag=0;
  91. for(i=0;i<n;i++)
  92. {
  93. if(finish[i]==0)
  94. {dead[j]=i;
  95.  
  96. j++;
  97. flag=1;
  98. }}
  99. if(flag==1)
  100. {
  101. printf("\n\nSystem is in Deadlock and the Deadlock process are\n");
  102. for(i=0;i<n;i++)
  103. {printf("P%d\t",dead[i]);
  104. }}
  105. else
  106. {
  107. printf("\nNo Deadlock Occur"); }}
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
********** Deadlock Detection Algo ************
Enter the no of Processes	Enter the no of resource instances	Enter the Max Matrix
Enter the Allocation Matrix
Enter the available Resources
Process	 Allocation	 Max	 Available	
No Deadlock Occur