Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int main() {
- int alloc[10][10];
- int request[10][10],avail[10],r[10],w[10];
- static int mark[20];
- int i,j,np,nr;
- // open the input file and read the content of
- // this program expects that all the input is stored in defined manner
- // inside input.txt file
- FILE* file = fopen ("in.txt", "r");
- // read all the conetents
- fscanf (file, "%d", &np);
- fscanf (file, "%d", &nr);
- for(i=0; i<nr; i++) {
- fscanf (file, "%d", &r[i]);
- }
- // read the allocation matrix
- for(i=0; i<np; i++)
- for(j=0; j<nr; j++)
- fscanf (file, "%d", &alloc[i][j]);
- // read the request matrix
- for(i=0; i<np; i++) for(j=0; j<nr; j++)
- fscanf (file, "%d", &request[i][j]);
- // solution starts here
- printf("\n");
- //marking processes with zero allocation
- for(i=0; i<np; i++) {
- int count=0;
- for(j=0; j<nr; j++) {
- if(alloc[i][j]==0)
- count++;
- else
- break;
- }
- if(count==nr)
- mark[i]=1;
- }
- for(j=0; j<nr; j++)
- w[j]=avail[j];
- //mark processes with request less than or equal to W
- for(i=0; i<np; i++) {
- int canbeprocessed=0;
- if(mark[i]!=1) {
- for(j=0; j<nr; j++) {
- if(request[i][j]<=w[j])
- canbeprocessed=1;
- else {
- canbeprocessed=0;
- break;
- }
- }
- if(canbeprocessed) {
- mark[i]=1;
- for(j=0; j<nr; j++) w[j]+=alloc[i][j];
- }
- }
- }
- printf("printing the contents read from file: \n");
- printf("Numer of processes: %d",np);
- printf("\n");
- printf("Numer of resources: %d",nr);
- printf("\n");
- printf("Availbaility vector\n");
- for(i=0; i<nr; i++) {
- printf("%d ",r[i]);
- }
- printf("\n");
- printf("Allocation Matrix\n");
- for(i=0; i<np; i++) {
- for(j=0; j<nr; j++)
- printf("%d ", alloc[i][j]);
- printf("\n");
- }
- printf("\n");
- printf("Request Matrix\n");
- for(i=0; i<np; i++) {
- for(j=0; j<nr; j++)
- printf("%d ", request[i][j]);
- printf("\n");
- }
- //checking for unmarked processes
- int deadlock=0;
- for(i=0; i<np; i++)
- if(mark[i]!=1)
- deadlock=1;
- if(deadlock)
- printf("\nThere exist a Deadlock");
- else printf("\n There does not exists no Deadlock");
- //fclose (file);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement