fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(){
  4. int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
  5. int p, r, i, j, process, count;
  6. count = 0;
  7. printf("Enter the no of processes: ");
  8. scanf("%d", &p);
  9. for(i=0; i<p; i++)
  10. completed[i] = 0;
  11. printf("\n\nEnter the no of resources: ");
  12. scanf("%d", &r);
  13. printf("\n\nEnter the Max Matrix for each process: ");
  14. for(i=0; i<p; i++){
  15. printf("\nFor process %d: ", i+1);
  16. for(j=0; j <r; j++)
  17. scanf("%d", &Max[i][j]);
  18. }
  19. printf("\n\nEnter the allocation for each process: ");
  20. for(i=0; i<p; i++){
  21. printf("\nFor process %d: ",i+1);
  22. for(j=0;j<r; j++)
  23. scanf("%d", &alloc[i][j]);
  24. }
  25. printf("\n\nEnter the Available Resources: ");
  26. for(i=0; i<r; i++)
  27. scanf("%d", &avail[i]);
  28. for(i=0; i<p; i++)
  29. for(j=0; j<r; j++)
  30. need[i][j] = Max[i][j]-alloc[i][j];
  31. do{
  32. printf("\n Max matrix:\tAllocation matrix:\n");
  33. for(i=0; i<p; i++){
  34. for(j=0;j<r; j++)
  35. printf("%d ", Max[i][j]);
  36. printf("\t\t");
  37. for(j=0;j<r; j++)
  38. printf("%d ", alloc[i][j]);
  39. printf("\n");
  40. }
  41. process = -1;
  42. for(i=0; i<p; i++){
  43. if(completed[i]==0){//if not completed
  44. process = i;
  45. for(j=0;j<r; j++){
  46. if(avail[j] < need[i][j]){
  47. process = -1;
  48. break;
  49. }
  50. }
  51. }
  52. if(process != -1)
  53. break;
  54. }
  55. if(process != -1){
  56. printf("\nProcess %d runs to completion!", process + 1);
  57. safeSequence[count] = process + 1;
  58. count++;
  59. for(j=0;j<r; j++){
  60. avail[j] += alloc[process][j];
  61. alloc[process][j] = 0;
  62. Max[process][j] = 0;
  63. }
  64. completed[process] = 1;
  65. }
  66. }while(count != p && process != -1);
  67. if(count == p){
  68. printf("\nThe system is in a safe state!!\n");
  69. printf("Safe Sequence: <");
  70. for(i=0; i<p; i++)
  71. printf("%d ", safeSequence[i]);
  72. printf(">\n");
  73. }
  74. else
  75. printf("\nThe system is in an unsafe state!!");
  76. }
Success #stdin #stdout 0.01s 5276KB
stdin
5
3
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
3 3 2
stdout
Enter the no of processes: 

Enter the no of resources: 

Enter the Max Matrix for each process: 
For process 1: 
For process 2: 
For process 3: 
For process 4: 
For process 5: 

Enter the allocation for each process: 
For process 1: 
For process 2: 
For process 3: 
For process 4: 
For process 5: 

Enter the Available Resources: 
 Max matrix:	Allocation matrix:
7 5 3 		0 1 0 
3 2 2 		2 0 0 
9 0 2 		3 0 2 
2 2 2 		2 1 1 
4 3 3 		0 0 2 

Process 2 runs to completion!
 Max matrix:	Allocation matrix:
7 5 3 		0 1 0 
0 0 0 		0 0 0 
9 0 2 		3 0 2 
2 2 2 		2 1 1 
4 3 3 		0 0 2 

Process 4 runs to completion!
 Max matrix:	Allocation matrix:
7 5 3 		0 1 0 
0 0 0 		0 0 0 
9 0 2 		3 0 2 
0 0 0 		0 0 0 
4 3 3 		0 0 2 

Process 1 runs to completion!
 Max matrix:	Allocation matrix:
0 0 0 		0 0 0 
0 0 0 		0 0 0 
9 0 2 		3 0 2 
0 0 0 		0 0 0 
4 3 3 		0 0 2 

Process 3 runs to completion!
 Max matrix:	Allocation matrix:
0 0 0 		0 0 0 
0 0 0 		0 0 0 
0 0 0 		0 0 0 
0 0 0 		0 0 0 
4 3 3 		0 0 2 

Process 5 runs to completion!
The system is in a safe state!!
Safe Sequence: <2 4 1 3 5 >