0% found this document useful (0 votes)
36 views8 pages

Banker C

The document defines variables and data structures to implement a resource allocation algorithm for processes. It initializes arrays to store the current allocation, maximum claim, and available resources for each of 5 processes and 5 resource types. It then prompts the user to input the number of processes and resources, and to populate the claim vector, allocation table, and maximum claim table. Finally, it prints the input tables, calculates available resources, and enters a while loop to continuously allocate resources to the next running process if its needs are less than available resources, updating the running and available arrays, until no processes remain or an unsafe state is reached.

Uploaded by

MoonKnight
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views8 pages

Banker C

The document defines variables and data structures to implement a resource allocation algorithm for processes. It initializes arrays to store the current allocation, maximum claim, and available resources for each of 5 processes and 5 resource types. It then prompts the user to input the number of processes and resources, and to populate the claim vector, allocation table, and maximum claim table. Finally, it prints the input tables, calculates available resources, and enters a while loop to continuously allocate resources to the next running process if its needs are less than available resources, updating the running and available arrays, until no processes remain or an unsafe state is reached.

Uploaded by

MoonKnight
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

#include <stdio.

h>
 
int current[5][5], maximum_claim[5][5], available[5];
int allocation[5] = {0, 0, 0, 0, 0};
int maxres[5], running[5], safe = 0;
int counter = 0, i, j, exec, resources, processes, k = 1;
 
int main()
{
printf("\nEnter number of processes: ");
     scanf("%d", &processes);
 
     for (i = 0; i < processes; i++)
{
         running[i] = 1; // running[2]=1
         counter++; // counter=5
     }
 
     printf("\nEnter number of resources: ");
     scanf("%d", &resources);
 
    

printf("\nEnter Claim Vector:");


     for (i = 0; i < resources; i++)
{
        scanf("%d", &maxres[i]);
//maxres[0]=10 , maxres[1]=5, maxres[2]=7
     }
 
   printf("\nEnter Allocated Resource Table:\n");
     for (i = 0; i < processes; i++)
{
        for(j = 0; j < resources; j++)
{
   scanf("%d", &current[i][j]);
         }
     }
 
     printf("\nEnter Maximum Claim Table:\n");
     for (i = 0; i < processes; i++)
{
         for(j = 0; j < resources; j++)
{
             scanf("%d", &maximum_claim[i][j]);
         }
     }
 

printf("\nThe Claim Vector is: ");


     for (i = 0; i < resources; i++)
{
        printf("\t%d", maxres[i]);
}
 
     printf("\nThe Allocated Resource Table:\n");
     for (i = 0; i < processes; i++)
{
        for (j = 0; j < resources; j++)
{
             printf("\t%d", current[i][j]);
         }
printf("\n");
     }
 
     printf("\nThe Maximum Claim Table:\n");
     for (i = 0; i < processes; i++)
{
         for (j = 0; j < resources; j++)
{
        printf("\t%d", maximum_claim[i][j]);
         }
         printf("\n");
     }
 
     for (i = 0; i < processes; i++)
{
         for (j = 0; j < resources; j++)
{
             allocation[j] += current[i][j];
         }
     }
 
     printf("\nAllocated resources:");
     for (i = 0; i < resources; i++)
{
         printf("\t%d", allocation[i]);
     }
 
     for (i = 0; i < resources; i++)
{
        available[i] = maxres[i] - allocation[i];
}
 
     printf("\nAvailable resources:");
     for (i = 0; i < resources; i++)
{
         printf("\t%d", available[i]);
     }
     printf("\n");
 

     while (counter != 0) counter =0


{
         safe = 0;
         for (i = 0; i < processes; i++)
{
             if (running[i])
//running[0] = 1 , exec=0 not executed
running[1] = 0 , exec=1 executed
running[2] = 1 , exec=1

{
                 exec = 1;
                 for (j = 0; j < resources; j++)
{
           if (maximum_claim[i][j] - current[i][j] > available[j])

need>available
{
                         exec = 0;
                         break;
                     }
                 }
                 if (exec)
{
                     printf("\nProcess%d is executing\n", i + 1);
                     running[i] = 0;
                     counter--;
                     safe = 1;
 
                     for (j = 0; j < resources; j++)
{
                         available[j] += current[i][j];
current=allocated
                     }
                break;
                 }
             }
         }
         if (!safe)
{
             printf("\nThe processes are in unsafe state.\n");
             break;
         }
else
{
             printf("\nThe process is in safe state");
             printf("\nAvailable vector:");
 
             for (i = 0; i < resources; i++)
{
                 printf("\t%d", available[i]);
             }
 
        printf("\n");
         }
     }
     return 0;
}

Available: A vector of length m. It shows


number of available resources of each type. If
Available[i] = k, then k instances of resource
Ri are available.

Max: An n×m matrix that contain maximum


demand of each process. If Max[i,j] = k, then
process Pi can request maximum k instances
of resource type Rj.

Allocation: An n×m matrix that contain


number of resources of each type currently
allocated to each process. If Allocation[i,j] =
k, then Pi is currently allocated k instances of
resource type Rj.

Need: An n×m matrix that shows the


remaining resource need of each process. If
Need[i,j] = k, then process Pi may need k
more instances of resource type Rj to
complete the task.

Maximum Resources :10 5 7


Allocated Resources:7 2 5
Available: 3 3 2

need<available
available+allocated

You might also like