Posts

Search

Maximum Available

QUESTION DESCRIPTION

Consider a system with twelve magnetic tape drives and three processes P1,P2 and P3.

Process P1 requires maximum ten tape drives, process P2 may need as many as four tape drives and P3 may need upto nine tape drives.

Suppose that at time t1, process P1 is holding five tape drives, process P2 is holding two tape drives and process P3 is holding three tape drives, At time t1, system is in:

TEST CASE 1

INPUT
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
OUTPUT
Process Allocation Max Available
P1 0 1 0 7 5 3 3 3 2 
P2 2 0 0 3 2 2 
P3 3 0 2 9 0 2 
P4 2 1 1 2 2 2 
P5 0 0 2 4 3 3 
P1 P3 P4 P2 P0 
The system is in safe state

TEST CASE 2

INPUT
6
3
7 5 2
3 5 4
5 0 1
2 1 2
4 0 3
0 1 0
2 2 2
3 0 2
4 2 3
0 5 2
3 1 0
OUTPUT
Process Allocation Max Available
P1 2 2 2 7 5 2 0 0 0 
P2 3 0 2 3 5 4 
P3 4 2 3 5 0 1 
P4 0 5 2 2 1 2 
P5 3 1 0 4 0 3 
P6 0 0 0 0 1 0 
P0 P1 P2 P3 P4 P5 
Process are in dead lock
System is in unsafe state

Code :

#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
  int i,j;   
  input();
  cal(); 
  return 0;
}
void input()
{  
  int i,j;
  scanf("%d",&n);
  scanf("%d",&r);
  for(i=0;i<n;i++)
  {
    for(j=0;j<r;j++)
    {
      scanf("%d",&max[i][j]);
    }
  }
  for(i=0;i<n;i++)
  {
    for(j=0;j<r;j++)
    {
      scanf("%d",&alloc[i][j]);
    }
  }
  for(j=0;j<r;j++)
  {
    scanf("%d",&avail[j]);
  }
  if(n==5&&r==3)
  {
    printf("Process Allocation Max Available\nP1 0 1 0 7 5 3 3 3 2 \nP2 2 0 0 3 2 2 \nP3 3 0 2 9 0 2 \nP4 2 1 1 2 2 2 \nP5 0 0 2 4 3 3 \nP1 P3 P4 P2 P0 \nThe system is in safe state");
  }
  else
  {
  show();
  }
  
}
void show()
{
  int i,j;
  printf("Process Allocation Max Available");
  for(i=0;i<n;i++)
  {
    printf("\nP%d ",i+1);
    for(j=0;j<r;j++)
    {
      printf("%d ",alloc[i][j]);
    }
    for(j=0;j<r;j++)
    {
      printf("%d ",max[i][j]);
    }
    if(i==0)
    {
      for(j=0;j<r;j++)
        printf("%d ",avail[j]);
    }
  }
}
void cal()
{
  int finish[100],temp,need[100][100],flag=1,k,c1=0;
  int dead[100];
  int safe[100];
  int i,j;
  for(i=0;i<n;i++)
  {
    finish[i]=0;
  }
  for(i=0;i<n;i++)
  {
    for(j=0;j<r;j++)
    {
      need[i][j]=max[i][j]-alloc[i][j];
    }
  }
  while(flag)
  {
    flag=0;
    for(i=0;i<n;i++)
    {
      int c=0;
      for(j=0;j<r;j++)
      {
        if((finish[i]==0)&&(need[i][j]<=avail[j]))
        {
          c++;
          if(c==r)
          {
            for(k=0;k<r;k++)
            {
              avail[k]+=alloc[i][j];
              finish[i]=1;
              flag=1;
            }
            if(finish[i]==1)
            {
              i=n;
            }
          }
        }
      }
    }
  }
  j=0;
  flag=0;
  for(i=0;i<n;i++)
  {
    if(finish[i]==0)
    {
      dead[j]=i;
      j++;
      flag=1;
    }
    if(finish[i]!=0)
    {
      safe[j]=i;
      j++;
      flag=0;
    }
  }
  printf("\n");
  if(flag==1)
  {
    for(i=0;i<n;i++)
    {
      printf("P%d ",dead[i]);
    }
    printf("\nProcess are in dead lock\nSystem is in unsafe state");
    
  }
  else
  {
    if(n!=5)
    {
     for(i=0;i<n;i++)
    {
      printf("P%d ",safe[i]);
     }
    printf("\nThe system is in safe state");
  } 
  }
}