Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*Please write in C coding language* Also it can be single threaded. For this project, you will write a multithreaded program that implements the bankers

*Please write in C coding language* Also it can be single threaded.

For this project, you will write a multithreaded program that implements the

bankers algorithm discussed in Section 7.5.3. Several customers request and

release resources from the bank. The banker will grant a request only if it leaves

the system in a safe state. A request that leaves the system in an unsafe state

will be denied. This programming assignment combines three separate topics:

(1) multithreading, (2) preventing race conditions, and (3) deadlock avoidance.

The Banker

The banker will consider requests from n customers for m resources types. as

outlined in Section 7.5.3. The banker will keep track of the resources using the

following data structures:

/* these may be any values >= 0 */

#define NUMBER OF CUSTOMERS 5

#define NUMBER OF RESOURCES 3

/* the available amount of each resource */

int available[NUMBER OF RESOURCES];

/*the maximum demand of each customer */

int maximum[NUMBER OF CUSTOMERS][NUMBER OF RESOURCES];

/* the amount currently allocated to each customer */

int allocation[NUMBER OF CUSTOMERS][NUMBER OF RESOURCES];

/* the remaining need for each customer */

int need[NUMBER OF CUSTOMERS][NUMBER OF RESOURCES];

The Customers

Create n customer threads that request and release resources from the bank.

The customers will continually loop, requesting and then releasing random

numbers of resources. The customers requests for resources will be bounded

by their respective values in the need array. The banker will grant a request if

it satisies the safety algorithm outlined in Section 7.5.3.1. If a request does not

leave the system in a safe state, the banker will deny it. Function prototypes

for requesting and releasing resources are as follows:

int request resources(int customer num, int request[]);

int release resources(int customer num, int release[]);

These two functions should return 0 if successful (the request has been

granted) and 1 if unsuccessful. Multiple threads (customers) will concurrently access shared data through these two functions. Therefore, access must be

controlled through mutex locks to prevent race conditions. Both the Pthreads

and Windows

APIs provide mutex locks. The use of Pthreads mutex locks is

covered in Section 5.9.4; mutex locks for Windows systems are described in the

project entitled Producer-Consumer Problem at the end of Chapter 5. Implementation

You should invoke your program by passing the number of resources of each

type on the command line. For example, if there were three resource types,

with ten instances of the first type, five of the second type, and seven of the

third type, you would invoke your program follows:

./a.out 10 5 7

The available array would be initialized to these values. You may initialize the

maximum array (which holds the maximum demand of each customer) using

any method you find convenient.

image text in transcribed

image text in transcribed

image text in transcribed

7.5.3 Banker's Algorithm allocation system with multiple instances of each resounce type. The deadlock avoidance algorithm that we describe next is applicable to such a system but scheme. This algorithm is comsmonly known as the banker's algorithm. The name was chosen because the algorithm could be edin a banking sysbemh to ensure that the bank never Figure 7.8 An unsafe state in a resource-allocation graph allocated its available cash in such a way that it could no longer satisfy the needs of all its castomers When a new process enters the system, it mast declare the maximum number of instances of each nsource type that it may need. This namber may not exceed the total number of resources in the system. When a user reqaests a set of resources, the system must determine whether the allocation of these resources will leave the system in a safe state, If it will, the resources are allocated; otherwise, the process mast wait until some other process releases enough resources Several data structures must be maintained to algorithm. These data structures encode the state of the resource-allocation system. We need the following data structures, where n is the number of processes in the system and m is the namber of resource types: Available. Avector of indicates the umber of availableresources equals k, then instances of resource type R are available . Max An x m matrix defines the maximum demand of each pricess. If MariIA equals k, then process P may request at most k instances of resource type R Allocation. An " m matrix defines the number of resources of each type currently allocated to each process. If Allocation(411 equalsk, then process P is currently allocated k instances of resource type R * * Need. An " matrix indicates the remaining resource need of each process. If Need ul equals k, then process P may need k more instances of resource type Ry to complete its task Note that Needlll equals Max These data structures vary over time in both size and value, To simplify the of the banker's algoeithm, we next establis some notation Lt X and Y be vectors of length n. We say that Xs Y if and only if Xil Yli] for all i : 1, 2 . For example, if X: (1,732) and Y (Q321), then X. In addition, Y X iIY and Y X. We can treat each rove in the matrioes Allocation and Need as vectors and refer to them as Allocation, and Need The vector Allocation specifies the resources currently allocabed to process P, the vector Nend, specises the additional resounces that process P may still request to complete its task We can now present the algorithm for finding out whether or not a system is in a safe state. This algorithm can be described as follows 1 Let work and Finish be vectors of length m and st, respectively. Initialize Work "Anailable and Finish(f]-false for i-0,1,-N-1. 2 Find an index i such that both a. FiniskM=false b. Need, s Work

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions