fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int main()
  4. {
  5. int n,r,i,j,k,avail[10],alloc[10][10],max[10][10],need[10][10],c=0,c1=0,finish[10],flag=1;
  6. printf("enter the number of processes\n");
  7. scanf("%d",&n);
  8. printf("enter the number of resources\n");
  9. scanf("%d",&r);
  10. printf("enter the allocation matrix\n");
  11. for(i=0;i<n;i++)
  12. {
  13. for(j=0;j<r;j++)
  14. {
  15. scanf("%d",&alloc[i][j]);
  16. }
  17. }
  18. printf("enter the max matrix\n");
  19. for(i=0;i<n;i++)
  20. {
  21. for(j=0;j<r;j++)
  22. {
  23. scanf("%d",&max[i][j]);
  24. }
  25. }
  26. printf("enter the available matrix\n");
  27. for(j=0;j<r;j++)
  28. {
  29. scanf("%d",&avail[j]);
  30. }
  31. printf("need matrix is:\n");
  32. for(i=0;i<n;i++)
  33. {
  34. printf("\n");
  35. for(j=0;j<r;j++)
  36. {
  37. need[i][j]=max[i][j]-alloc[i][j];
  38. printf("%d",need[i][j]);
  39. }
  40. }
  41. for(i=0;i<n;i++)
  42. {
  43. finish[i]=0;
  44. }
  45. while(flag)
  46. {
  47. flag=0;
  48. for(i=0;i<n;i++)
  49. {
  50. c=0;
  51. for(j=0;j<r;j++)
  52. {
  53. if((finish[i]==0)&&(need[i][j]<=avail[j]))
  54. {
  55. c++;
  56. if(c==r)
  57. {
  58. for(k=0;k<r;k++)
  59. {
  60. avail[k]=avail[k]+alloc[i][k];
  61. }
  62. flag=1;
  63. finish[i]=1;
  64. printf("p%d->",i);
  65. i=n;
  66.  
  67. }
  68. }
  69. }
  70. }
  71. }
  72. for(i=0;i<n;i++)
  73. {
  74. if(finish[i]==1)
  75. {
  76. c1++;
  77. }
  78. else
  79. {
  80. printf("p%d->",i);
  81. }
  82. }
  83. if(c1==n)
  84. {
  85. printf("\nsystem is in safe state\n");
  86. }
  87. else
  88. {
  89. printf("\nsystem is not in safe state\n");
  90. }
  91.  
  92.  
  93. }
Success #stdin #stdout 0s 5448KB
stdin
5
4
0 0 1 2
1 0 0 0
1 3 5 4
0 6 3 2
0 0 1 4
0 0 1 2
1 7 5 0
2 3 5 6
0 6 5 2
0 6 5 6
1 5 2 0
stdout
enter the number of processes
enter the number of resources
enter the allocation matrix
enter the max matrix
enter the available matrix
need matrix is:

0000
0750
1002
0020
0642p0->p2->p1->p3->p4->
system is in safe state