csenotes12

Matrix Question

Apr 9th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. #include<conio.h>
  2. #include<stdio.h>
  3. int main()
  4. {
  5.  int n;    //n number of process
  6.  int r;      // number of resources
  7.  int i,j,k,cnt,cntt;
  8.  int avail[10],p[10];
  9.  int need[10][10],alloc[10][10],max[10][10];
  10.  
  11.  printf("\nEnter number of process :");
  12.  scanf("%d",&n);
  13.  printf("\n Enter  resources available : ");
  14.  scanf("%d",&r);
  15.  printf("\nEnter insatnces for resources :\n");
  16.  for(i=0;i<r;i++)
  17.  {  printf("R%d ",i+1);
  18.   scanf("%d",&avail[i]);
  19.   }
  20.  printf("\n Enter allocation matrix  \n");
  21.  for(i=0;i<n;i++)
  22.  {
  23.  printf("p%d",i+1);          p[i]=0;
  24.  for(j=0;j<r;j++)
  25.  {
  26.   scanf("%d",&alloc[i][j]);
  27.  }
  28. }
  29.   printf("\n Enter MAX matrix  \n");
  30.  
  31.  for(i=0;i<n;i++)
  32.  {
  33.  printf("p%d",i+1);
  34.  for(j=0;j<r;j++)
  35.  {
  36.   scanf("%d",&max[i][j]);
  37.  }
  38.  }
  39.  
  40.  for(i=0;i<n;i++)
  41.  {
  42.  printf("\np%d\t",i+1) ;
  43.  for(j=0;j<r;j++)
  44.  {
  45.   need[i][j]=max[i][j]-alloc[i][j];
  46.   printf("\t%d",need[i][j]);
  47.   }
  48.  }
  49.  k=0;     cntt=0;
  50.  printf("\n\n");
  51.  while(k<15)
  52.  {
  53.  for(i=0;i<n;i++)
  54.  {  cnt=0;
  55.  for(j=0;j<r;j++)
  56.  {
  57.   if(p[i]==1) break;
  58.   if(need[i][j]<=avail[j])
  59.   {
  60.   cnt++;
  61.   }
  62.   if(cnt==r)
  63.   {
  64.   for(j=0;j<r;j++)
  65.   {
  66.   avail[j]+=alloc[i][j];
  67.   }
  68.   printf("p%d\t",i+1);  p[i]=1;     cntt++;
  69.   }
  70.  }
  71.  } k++;
  72.  }
  73.  if(cntt<n-1)
  74.  {
  75.  printf("\n deadlock ");
  76.  }
  77.       getch();
  78.  }
Add Comment
Please, Sign In to add comment