Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

SOMEONE PLEASE HELP ME. i DONT QUIET UNDERSTAND THIS PROBLEM AND COULD CERTAINLY

ID: 3765222 • Letter: S

Question

SOMEONE PLEASE HELP ME. i DONT QUIET UNDERSTAND THIS PROBLEM AND COULD CERTAINLY USE SOME HELP.

This is to be written in C++. Thanks a bunch.


In class, we discussed various ways in which an Operating System can deal with the
problem of deadlock. One such way was to avoid deadlocks by never getting into a state
that could lead to deadlock. We saw
that the
Banker’s algorithm
could be used to tell us
whether a particular state was safe or unsafe. Being in a safe state guaranteed us that if
every process requested its maximum resources, we could still find a schedule to
complete. In this project, we w
ill simulate multiple processes that each make requests for
resources and later release them. We will have a centralized “Banker” that will approve
or deny resource requests based upon if the resultant state would be safe or not.
How it Will Work
You will
write a program called
bankers
that takes the following command line:
./bankers

n
-
a
where
numprocs
is the number of processes you are simulating and the
available vector
encodes the initial number of resources of
each type that are available for the simulated
processes. For instance,
./bankers

n 2
-
a 5 7 9
means to simulate two processes and a system that initially has 5 copies of resource 1
available, 7 of resource 2, and 9 of resource 3.
Implementation
You wil
l write a t
hreaded program in C using proper
thread library. Have a thread
simulate a process. At thread start, randomly generate a maximum number of resources
that each process will use in the simulation. Make sure that this is less than the initial
availab
le vector.
The body of the threads will look like the following:
while(true)
{
Request some resources less than (max

held)
Sleep a random amount
Release subset of held resources
Sleep a random amount
}
Requesting Resources
You w
ill request resources from the “system” by having your thread call the function:
int request_resources(int pid, int resources[])
Where
pid
is an integer that identifies your simulated process (thread) and
resources
is
an array that indicates how many of ea
ch type you are requesting. This request can be
zero for a particular resource, but should be no more than the maximum you generated
minus the number of that resource you currently hold.
This function will return 0 on success (this is still a safe
state) or
-
1 if the Banker’s
algorithm indicates that this would lead to an unsafe state.
If the request is denied, terminate the thread from those currently running. When only one
process remains, terminate the simulation.
Releasing Resources
You will rel
ease resources from the “system” by having your thread call the function:
void release_resources(int pid, int resources[])
Where
pid
is an integer that identifies your simulated process (thread) and
resources
is
an array that indicates how many of each typ
e you are releasing. This request can be zero
for a particular resource, but should be no more than the maximum number of that
resource you currently hold.
Sleeping
Keep your sleeps under a few seconds just to ensure the simulation doesn’t take
unnecessari
ly long.
Organization
Create three C files:
1.
banker.c
2.
process.c
3.
driver.c
In banker.c, provide an implementation of request_resources() and release_resources()
that uses the Banker’s algorithm to determine if the resulting state wo
uld be safe or
unsafe. Note that many threads may be calling these functions simultaneously, and so you
will need to use synchronization.
In process.c provide a function that simulates a process that can be used as the start
routine of a pthread.
In driver
.c, process commandline arguments, create threads, and keep the simulation
running until there is only one process left.
You can use header files to expose function prototypes as necessary.
Output
For each request or release, output the process id, the resources requested, whether this is
safe or unsafe, and if safe, how many of each resource still remains.

Explanation / Answer

/*PROGRAM TO IMPLEMENT BANKER'S ALGORITHM * --------------------------------------------*/ #include int curr[5][5], maxclaim[5][5], avl[5]; int alloc[5] = {0, 0, 0, 0, 0}; int maxres[5], running[5], safe=0; int count = 0, i, j, exec, r, p, k = 1; int main() { printf(" Enter the number of processes: "); scanf("%d", &p); for (i = 0; i