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. }
  79. if(c1==n)
  80. {
  81. printf("\nsystem is in safe state\n");
  82. }
  83. else
  84. {
  85. printf("\nsystem is not in safe state\n");
  86. }
  87.  
  88.  
  89. }
  90.  
  91.  
  92.  
Success #stdin #stdout 0s 5444KB
stdin
5
4
0012
1000
1354
0632
0014
0012
1750
2356
0652
0656
1520
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:

34458672444003444598524768
3424524905388-1750-2356
-652-656-15200
-1601157816-27364-1762360
-10-19791280p2->p3->p0->p1->p4->
system is in safe state