0% found this document useful (0 votes)
49 views10 pages

Lab No 14

The document describes a lab report on simulating the Banker's Algorithm for deadlock avoidance. It includes: 1) An overview of the Banker's Algorithm and its key data structures and steps to test for safety. 2) C source code implementing the Banker's Algorithm that takes input on processes and resources, checks for safety, and outputs whether the system is in a safe state. 3) A description of the Resource Allocation Algorithm and its steps to allocate resources if the system is safe, with accompanying C source code. 4) The lab task is to simulate implementing the Banker's Algorithm.

Uploaded by

M Ahmed
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)
49 views10 pages

Lab No 14

The document describes a lab report on simulating the Banker's Algorithm for deadlock avoidance. It includes: 1) An overview of the Banker's Algorithm and its key data structures and steps to test for safety. 2) C source code implementing the Banker's Algorithm that takes input on processes and resources, checks for safety, and outputs whether the system is in a safe state. 3) A description of the Resource Allocation Algorithm and its steps to allocate resources if the system is safe, with accompanying C source code. 4) The lab task is to simulate implementing the Banker's Algorithm.

Uploaded by

M Ahmed
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/ 10

Preston University, Kohat

Islamabad Campus

Department of Computer Science

OPERATING SYSTEMS

Lab Report #______________

Submitted by

Name: ________________________________

Reg. No: _______________________________

BS Computer Science (BSCS)

Submitted to:

Instructor’s Name: ________________________

Lab Date: ___________________ Marks & Signature

Submission Date: ________________

91
LAB NO 14: SIMULATE BANKER’S ALGORITHM FOR
DEADLOCK AVOIDANCE

Duration 3 Hours

OS /Tool/Language C compiler

Objective(s) To Simulate Banker’s Algorithm for Deadlock Avoidance

Banker’s Algorithm

The Banker’s Algorithm is a resource allocation that tests the safety by simulating the
allocation of predetermined maximum possible amounts of all resources, and then makes "s-
state" check to test for possible deadlock conditions for all other pending activities, before
deciding whether allocation should be allowed to continue.

When a new process enters the system, it declares the maximum number of instances that are
needed. This number cannot exceed the total number of resources in the system. If the process
can be accommodated based upon the needs of the system, then resources are allocated,
otherwise the process must wait. The algorithm is actually made up of two separate algorithms:
the safety algorithm and the resource allocation algorithm.

Data Structures for the Banker’s Algorithm

§ n-Number of process, m-number of resource types.


§ Available: Available[j]=k, k – instance of resource type Rj is available.
§ Max: If max[i, j]=k, Pi may request at most k instances resource Rj.
§ Allocation: If Allocation [i, j]=k, Pi allocated to k instances of resource Rj
§ Need: If Need[I, j]=k, Pi may need k more instances of resource type Rj,
Need[I, j]=Max[I, j]- Allocation[I, j];

Steps for Banker’s (Safety) Algorithm:

1. Work and Finish be the vector of length m and n respectively, Work=Available and
Finish[i] =False.

92
2. Find an i such that both
Finish[i] =False
Need<=Work If no such I exists go to step 4.
3. work= work + Allocation, Finish[i] =True;
4. if Finish[1]=True for all I, then the system is in safe state.

Banker’s (Safety) Algorithm

1. Start the program.

2. Get the values of resources and processes.

3. Get the avail value.

4. After allocation find the need value.

5. Check whether its possible to allocate.

6. If it is possible then the system is in safe state.

7. Else system is not in safety state

8. Stop the process.

Banker’s (Safety) Algorithm Source Code

#include<stdio.h>

void main() {

char job[10][10];

int time[10],avail,tem[10],temp[10];

int safe[10];

int ind=1,i,j,q,n,t;

printf("Enter no of jobs: ");

scanf("%d",&n);

for(i=0;i<n;i++) {

printf("Enter name and time: ");

scanf("%s%d",&job[i],&time[i]);

93
}

printf("Enter the available resources:");

scanf("%d",&avail);

for(i=0;i<n;i++) {

temp[i]=time[i];

tem[i]=i;

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

for(j=i+1;j<n;j++) {

if(temp[i]>temp[j]) {

t=temp[i];

temp[i]=temp[j];

temp[j]=t;

t=tem[i];

tem[i]=tem[j];

tem[j]=t;

for(i=0;i<n;i++) {

q=tem[i];

if(time[q]<=avail) {

safe[ind]=tem[i];

avail=avail-tem[q];

printf("%s",job[safe[ind]]);

ind++;

94
}

else {

printf("No safe sequence\n");

printf("Safe sequence is:");

for(i=1;i<ind; i++)

printf("%s %d\n",job[safe[i]],time[safe[i]]);

getch();

Input/Output:

Enter no of jobs: 4

Enter name and time: A 1

Enter name and time: B 4

Enter name and time: C 2

Enter name and time: D 3

Enter the available resources: 20

Safe sequence is: A 1, C 2, D 3, B 4.

Resource request algorithm

Let Request i be request vector for the process Pi, If request i=[j]=k, then process Pi wants k
instances of resource type Rj.

1. if Request<=Need I go to step 2. Otherwise raise an error condition.


2. if Request<=Available go to step 3. Otherwise Pi must since the resources are
available.
3. Have the system pretend to have allocated the requested resources to process Pi by
modifying the state as follows;

95
Available=Available-Request I;
Allocation I=Allocation +Request I;
Need i=Need i- Request I;

If the resulting resource allocation state is safe, the transaction is completed and process Pi is
allocated its resources. However if the state is unsafe, the Pi must wait for Request i and the
old resource-allocation state is restored.

Resource allocation algorithm

1. Start the program.


2. Get the values of resources and processes.
3. Get the avail value.
4. After allocation find the need value.
5. Check whether its possible to allocate.
6. If it is possible then the system is in safe state.
7. Else system is not in safety state.
8. If the new request comes then check that the system is in safety.
9. or not if we allow the request.
10. Stop the program.
11. end

Resource allocation algorithm Source code:

#include<stdio.h>

#include<string.h>

void main() {

int alloc[10][10],max[10][10];

int avail[10],work[10],total[10];

int i,j,k,n,need[10][10];

int m;

int count=0,c=0;

char finish[10];

96
printf("Enter the no. of processes and resources:");

scanf("%d%d",&n,&m);

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

finish[i]='n';

printf("Enter the claim matrix:\n");

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

for(j=0;j<m;j++)

scanf("%d",&max[i][j]);

printf("Enter the allocation matrix:\n");

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

for(j=0;j<m;j++)

scanf("%d",&alloc[i][j]);

printf("Resource vector:");

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

scanf("%d",&total[i]);

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

avail[i]=0;

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

for(j=0;j<m;j++)

avail[j]+=alloc[i][j];

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

work[i]=avail[i];

for(j=0;j<m;j++)

work[j]=total[j]-work[j];

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

97
for(j=0;j<m;j++)

need[i][j]=max[i][j]-alloc[i][j];

A:

for(i=0;i<n;i++) {

c=0;

for(j=0;j<m;j++)

if((need[i][j]<=work[j])&&(finish[i]=='n')) c++;

if(c==m) {

printf("All the resources can be allocated to Process %d", i+1);

printf("\n\nAvailable resources are:");

for(k=0;k<m;k++) {

work[k]+=alloc[i][k];

printf("%4d",work[k]);

printf("\n"); finish[i]='y';

printf("\nProcess %d executed?:%c \n",i+1,finish[i]);

count++;

if(count!=n)

goto A;

else

printf("\n System is in safe mode");

printf("\n The given state is safe state");

getch();

98
}

Intput

Enter the no. of processes and resources: 4 3

Enter the claim matrix:

322

613

314

422

Enter the allocation matrix:

100

612

211

002

Resource vector:9 3 6

Output

All the resources can be allocated to Process 2

Available resources are: 6 2 3

Process 2 executed?:y

All the resources can be allocated to Process 3

Available resources are: 8 3 4

Process 3 executed?:y

All the resources can be allocated to Process 4

Available resources are: 8 3 6

Process 4 executed?:y

All the resources can be allocated to Process 1

99
Available resources are: 9 3 6

Process 1 executed?:y

System is in safe mode

The given state is safe state

Lab Task(s)

§ To simulate the implementation of Banker’s Algorithm.

100

You might also like