Skip to main content

Assignment No:1

Assignment No:1

 Slot 1 

I) Add the following functionalities in your program 

a) Accept Available 

b) Display Allocation, Max 

c) Display the contents of need matrix 

d) Display Available

CODE:

👇

  1. #include<stdio.h>
  2. #include<ctype.h>
  3.  int  avail[] = {3,3,2} ;
  4.       int max[][10] = { {7,5,3} ,{3,2,2},{9,0,2} ,{2,2,2},{4,3,3}};
  5.       int alloc[][10] = { {0,1,0} ,{2,0,0},{3,0,2} ,{2,1,1},{0,0,2}};
  6.       
  7.       int need[10][10] ;
  8.           int m = 3 , n = 5 , i,j;
  9.           void  get_allocation_matrix();

  10. void main()
  11.    {
  12.     get_allocation_matrix();
  13.    }
  14.       void get_allocation_matrix()
  15.       {
  16.     
  17.      printf("\n Number of Resources of matrix have %d", m);
  18.       printf("\n Number of Processes of matrix have %d", n);
  19.      printf("\n Number of Available resources are\t ");
  20.           for(j=0;j<m;j++)
  21.           {
  22.             printf("%d \t ",avail[j]) ;
  23.             }
  24.         printf("\n Number of Maximum need process can have \n ");
  25.         for(i=0;i<n;i++)
  26.           {
  27.                  for (j=0;j<m;j++)
  28.                    {
  29.                      printf("%d\t",max[i][j]);
  30.                     }
  31.                         printf("\n");
  32.            } 
  33.              printf("\n Number of Allocation of resources to process are \n ");
  34.         for(i=0;i<n;i++)
  35.           {
  36.                  for (j=0;j<m;j++)
  37.                    {
  38.                      printf("%d\t",alloc[i][j]);
  39.                     }
  40.                         printf("\n");
  41.            } 
  42.        printf("\n Number of need of resources process can have \n ");
  43.       for(i=0;i<n;i++)
  44.           {
  45.                  for (j=0;j<m;j++)
  46.                    {
  47.                      need[i][j] = max[i][j] - alloc[i][j] ;
  48.                      printf("%d\t",need[i][j]);
  49.                     }
  50.                         printf("\n");
  51.            }
  52.         }
OUTPUT:








Slot 2
 I) Modify above program so as to include the following: a) Accept Request for a process b) Resource request algorithm c) Safety algorithm Consider a system with ‘n’ processes and ‘m’ resource types. Accept number of instances for every resource type. For each process accept the allocation and maximum requirement matrices. Write a program to display the contents of need matrix and to check if the given request of a process can be granted immediately or not.
CODE:
👇
  1. #define true 1
  2. #define false 0
  3. #include<stdlib.h>
  4. #include<stdio.h>
  5. #include<ctype.h>
  6.  int  avail[] = {1,5,2,0} ,work[10] ,req[10];
  7.       int max[][10] = { {0,0,1,2} ,{1,7,5,0},{2,3,5,6} ,{0,6,5,2},{0,6,5,2}};
  8.       int alloc[][10] = { {0,0,1,2} ,{1,0,0,0},{1,3,5,4} ,{0,6,3,2},{0,0,1,4}};
  9.       int safeseq[5] , ssi=-1 ;
  10.       int need[10][10] , finish[10] ;
  11.           int m = 4 , n = 5 , i,j ;
  12.           void  get_allocation_matrix();
  13.          int  safe_state(); 
  14.          void get_alloc_request();
  15.           int need_lte_work(int i);
  16.           void print_safes();
  17.           int req_lte_need(int i);
  18.             int req_lte_avail();
  19. void main()
  20.    {
  21.     get_allocation_matrix();
  22.        if(safe_state())
  23.        {
  24.          printf("\n System is in safe state \n");
  25.          print_safes();
  26.        }
  27.     else{
  28.         printf(" \n system is not in safe state \n ");
  29.      }
  30.      get_alloc_request();
  31.      if(safe_state())
  32.        {
  33.          printf("\n System is in safe state \n");
  34.          print_safes();
  35.        }
  36.     else{
  37.         printf(" \n system is not in safe state \n ");
  38.      }
  39.    }
  40.    
  41.       void get_allocation_matrix()
  42.       {
  43.      printf("\n Number of Resources of matrix have %d", m);
  44.       printf("\n Number of Processes of matrix have %d", n);
  45.      printf("\n Number of Available resources are\t ");
  46.           for(j=0;j<m;j++)
  47.           {
  48.             printf("%d \t ",avail[j]) ;
  49.             }
  50.         printf("\n Number of Maximum need process can have \n ");
  51.         for(i=0;i<n;i++)
  52.           {
  53.                  for (j=0;j<m;j++)
  54.                    {
  55.                      printf("%d\t",max[i][j]);
  56.                     }
  57.                         printf("\n");
  58.            }
  59.           
  60.              printf("\n Number of Allocation of resources to process are \n ");
  61.         for(i=0;i<n;i++)
  62.           {
  63.                  for (j=0;j<m;j++)
  64.                    {
  65.                      printf("%d\t",alloc[i][j]);
  66.                     }
  67.                         printf("\n");
  68.            }  
  69.        printf("\n Number of need of resources process can have \n ");
  70.       for(i=0;i<n;i++)
  71.           {
  72.                  for (j=0;j<m;j++)
  73.                    {
  74.                      need[i][j] = max[i][j] - alloc[i][j] ;
  75.                      printf("%d\t",need[i][j]);
  76.                     }
  77.                         printf("\n");
  78.            }
  79.         }
  80.         
  81.         int  safe_state()
  82.       {
  83.            for(j=0;j<m ; j++)
  84.            work[j] = avail[j] ;
  85.            for(i=0;i<n;i++)
  86.            finish[i] = false ;
  87.         printf ("\n check whether process is in safe state \n ");   
  88.            do
  89.            {
  90.            int found=false;
  91.                     for(i=0;i<n;i++)
  92.                     {
  93.                     if( finish[i] == false && need_lte_work(i))
  94.                        {
  95.                          printf("\n selected process %d ", i);
  96.                           finish[i]=true;
  97.                             for(j=0;j<m;j++)
  98.                                 work[j] = work[j] + alloc[i][j] ;
  99.                                   safeseq[++ssi] = i;
  100.                                   found = true ;
  101.                                    break ;
  102.                                    }
  103.                                 }
  104.                     if(found == false)  
  105.                         {
  106.                            for(i=0;i<n;i++)
  107.                               if(finish[i]==false)
  108.                                   return (false);   // unsafe state
  109.                                return (true);
  110.                                }
  111.                     } while (1) ;
  112.                     
  113.                        } 
  114.       
  115.       int need_lte_work(int i)
  116.          {
  117.            for(j=0;j<m;j++)
  118.               {
  119.                  if(need[i][j] > work[j] )
  120.                   return (false);
  121.                   }
  122.               return(true);
  123.            }
  124.            
  125.  void print_safes()
  126.   {
  127.                  printf("\n Safe Sequence  is \n ");          
  128.                      for ( i = 0 ; i < ssi ; i++)
  129.                          printf(" %d\t" , safeseq[i]);
  130.                          printf(" \n ");      
  131.   }  
  132.   
  133.  void get_alloc_request()
  134.   {
  135.      printf(" Enter the process who want to request \n ");
  136.      scanf ("%d", &i);
  137.      printf("Enter Request \n ");
  138.      for(j=0;j<m;j++)
  139.         scanf("%d",&req[j]);
  140.         
  141.       if(!req_lte_need(i))
  142.       {
  143.         printf("request of process is more than need of process \n ");
  144.         exit(0);
  145.       }
  146.       if(!req_lte_avail())
  147.       {
  148.         printf("Request of process is more than available of process \n process %d has to be wait",i);
  149.         exit(0);
  150.       }
  151.      
  152.      for(j=0;j<m;j++) 
  153.      {
  154.       alloc[i][j] -= req[j];
  155.       need[i][j] -= req[j];
  156.       avail[j] -= req[j] ;
  157.       }
  158.   }
  159.   
  160.   int req_lte_need(int i)
  161.       {
  162.          for(j=0;j<n;j++)
  163.             {
  164.                if( req[j] > need[i][j])
  165.                   return 0;
  166.                }
  167.              return 1;
  168.       }
  169.    
  170.     int req_lte_avail()
  171.       {
  172.          for(j=0;j<n;j++)
  173.             {
  174.                if( req[j] > avail[j])
  175.                   return 0;
  176.                }
  177.              return 1;
  178.       }

Comments