AVINASHommi

BANK

Nov 14th, 2012
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7];
  6. int p,r,i,j,prc,count;
  7. clrscr();
  8. count=0;
  9. for(i=1;i<=7;i++)
  10. comp[i]=0;
  11. printf("Enter the no of processes:\n");
  12. scanf("%d",&p);
  13. printf("Enter the no of resources:\n");
  14. scanf("%d",&r);
  15. printf("Enter the claim for each process:");
  16. for(i=1;i<=p;i++)
  17. {
  18. printf("\nFor process %d:",i);
  19. for(j=1;j<=r;j++)
  20. {
  21. scanf("%d",&clm[i][j]);
  22. }
  23. }
  24. printf("Enter the allocation for each process:\n");
  25. for(i=1;i<=p;i++)
  26. {
  27. //printf("\nFor process ",i);
  28. for(j=1;j<=r;j++)
  29. {
  30. scanf("%d",&alloc[i][j]);
  31. }
  32. }
  33. printf("Enter total no of each resource:");
  34. for(j=1;j<=r;j++)
  35. scanf("%d",&rsrc[j]);
  36. for(j=1;j<=r;j++)
  37. {
  38. int total=0;
  39. avail[j]=0;
  40. for(i=1;i<=p;i++)
  41. {total+=alloc[i][j];}
  42. avail[j]=rsrc[j]-total;
  43. }
  44. do
  45. {
  46. for(i=1;i<=p;i++)
  47. {
  48. for(j=1;j<=r;j++)
  49. {
  50. req[i][j]=clm[i][j]-alloc[i][j];
  51. }
  52. }
  53. printf("\n\nAvailable resorces is:");
  54. for(j=1;j<=r;j++)
  55. { printf(" ",avail[j]); }
  56. printf("\nClaim matrix:\t\tAllocation matrix:\n");
  57. for(i=1;i<=p;i++)
  58. {
  59. for(j=1;j<=r;j++)
  60. {
  61. printf("%d",clm[i][j]);
  62. }
  63. printf("\t\t\t");
  64. for(j=1;j<=r;j++)
  65. {
  66. printf("%d",alloc[i][j]);
  67. }
  68. printf("\n");
  69. }
  70. prc=0;
  71. for(i=1;i<=p;i++)
  72. {
  73. if(comp[i]==0)//if not completed
  74. {
  75. prc=i;
  76. for(j=1;j<=r;j++)
  77. {
  78. if(avail[j]<req[i][j])
  79. {
  80. prc=0;
  81. break;
  82. }
  83. }
  84. }
  85. if(prc!=0)
  86. break;
  87. }
  88. if(prc!=0)
  89. {
  90. printf("\nProcess ",prc,"runs to completion!");
  91. count++;
  92. for(j=1;j<=r;j++)
  93. {
  94. avail[j]+=alloc[prc][j];
  95. alloc[prc][j]=0;
  96. clm[prc][j]=0;
  97. comp[prc]=1;
  98. }
  99. }
  100. }
  101. while(count!=p&&prc!=0);
  102. if(count==p)
  103. printf("\nThe system is in a safe state!!");
  104. else
  105. printf("\nThe system is in an unsafe state!!");
  106. getch();
  107. }
Add Comment
Please, Sign In to add comment