0% found this document useful (0 votes)
46 views

Banker 'S Algorithm

The document describes a lab task to implement the Banker's Algorithm for deadlock avoidance in an operating system. It includes code to define matrices for maximum claims, allocated resources, and availability as well as functions to check for safe states and execute processes. The code tracks available resources, determines which processes can execute safely, and outputs status updates until all processes have completed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Banker 'S Algorithm

The document describes a lab task to implement the Banker's Algorithm for deadlock avoidance in an operating system. It includes code to define matrices for maximum claims, allocated resources, and availability as well as functions to check for safe states and execute processes. The code tracks available resources, determines which processes can execute safely, and outputs status updates until all processes have completed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

SOFTWARE ENGINEERING DEPARTMENT (SED)

Lab #15

SUBMITTED TO:

Ma’am Rabia Arshad

SUBMITTED BY:

Muhammad Yasir Hassan

ROLL NUMBER:

19-SE-31

SECTION:

Alpha (α)

DATE:

26-01-2022
Lab Task
Task 1:
Implement Banker ’s Algorithm.
#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, k = 1, exec, resources, processes;
int main()
{
printf("\n Enter Number of Processes: ");
scanf("%d", &processes);
for (i = 0; i < processes; i++)
{
running[i] = 1;
counter++;
}
printf("\n Enter Number of Resources: ");
scanf("%d", &resources);
printf("\n Enter the Values of Max Matrix:\n\n");
for (i = 0; i < processes; i++)
{
for(j = 0; j < resources; j++)
{
printf("\t");
scanf("%d", &maximum_claim[i][j]);
}
printf("\n");
}
printf("\n Enter the Values of Allocated Resource Matrix:\n\n");
for (i = 0; i < processes; i++)
{
for(j = 0; j < resources; j++)
{
printf("\t");
scanf("%d", &current[i][j]);
}
printf("\n");
}
printf("\n Enter the Maximum Instances of Each Resources: ");
for (i = 0; i < resources; i++)
{
scanf("%d", &maxres[i]);
}
printf("\n-------------- Need Matrix ------------\n");
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
printf("\t%d", maximum_claim[i][j]);
}
printf("\n");
}

printf("\n--------------- The Available Vector ------------\n ");


for (i = 0; i < resources; i++)
{
printf("\t%d", maxres[i]);
}
printf("\n------------ Allocated Matrix (Current Status) ----------\n");
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
printf("\t%d", current[i][j]);
}
printf("\n");
}
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
allocation[j] += current[i][j];
}
}
printf("\n Allocated 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("\n Available Resources: ");
for (i = 0; i < resources; i++)
{
printf("\t%d", available[i]);
}
printf("\n");
while (counter != 0)
{
safe = 0;
for (i = 0; i < processes; i++)
{
if (running[i])
{
exec = 1;
for (j = 0; j < resources; j++)
{
if (maximum_claim[i][j] - current[i][j] > available[j])
{
exec = 0;
break;
}
}
if (exec)
{
printf("\n Process %d is Executing \n", i + 1);
running[i] = 0;
counter--;
safe = 1;
for (j = 0; j < resources; j++)
{
available[j] += current[i][j];
}
break;
}
}
}
if (!safe)
{
printf("\n The Processes are in Unsafe State \n");
break;
}
else
{
printf("\n The Process is in Safe State ");
printf("\n Available Vector ");

for (i = 0; i < resources; i++)


{
printf("\t%d", available[i]);
}
printf("\n");
}
}
return 0;
}

You might also like