Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define P 5
- #define R 4
- int main(){
- int allocated[P][R] = {{1,2,1,1},{1,2,1,1},{1,2,0,1},{2,2,1,1},{1,1,1,1}};;
- int needed[P][R] = {{1,2,0,2},{1,2,2,3},{1,1,0,0},{2,2,0,1},{2,2,1,2}};
- int done[P] = {0, 0, 0, 0, 0};
- int available[R] = {2, 1, 0, 0};
- int completed = 0, count, i, j; //i represents process, j represents resource
- while(1){
- for(i = 0; i < P; i++){
- if(done[i])continue;
- count = 0;
- for(j = 0; j < R; j++)
- if(available[j] >= needed[i][j]) count++;
- else break;
- if(count == R){
- printf("%d is executing.\n", i+1);
- done[i] = 1;
- printf("The free array is:-\n");
- for(j = 0; j < R; j++){
- available[j] += allocated[i][j];
- needed[i][j] = 0;
- allocated[i][j] = 0;
- printf("%d ", available[j]);
- }
- printf("\n\n");
- break;
- }
- }
- if(count != R){
- printf("Unsafe!");
- break;
- }
- count=0;
- for(i=0; i < P; i++)
- if(done[i] == 1) count++;
- if(count==P) return 0;
- }
- }
Add Comment
Please, Sign In to add comment