IT, Web
nclude all the c++ library names that have been used in the project along with comments for each syntax lines
MAT 375 Project #3
Due date: Nov. 24
th, 2015
How to submit? : Submit the hardcopy of your source code and sample result
Project Description
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 will 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 <numprocs> -a <available vector>
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 will write a threaded 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
available 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 will 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 each 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 release 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 type 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
unnecessarily 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 would 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.
You should not copy from others or let other students use your code. Violation to this policy
will result in automatic fail. (I might ask you to explain your code as well.)