CS 1550 – Project 2: Banker’s Algorithm

Due: Sunday, June 22, 2014 by 11:59pm

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 the pthread 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.

Create a Makefile that builds the bankers executable from your source files.

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.

File Backups

One of the major contributions the university provides for the AFS filesystem is nightly backups. However, the /u/OSLab/ partition is not part of AFS space. Thus, any files you modify under your personal directory in /u/OSLab/ are not backed up. If there is a catastrophic disk failure, all of your work will be irrecoverably lost. As such, it is my recommendation that you:

Backup all the files you change under /u/OSLab to your ~/private/ directory frequently!

Loss of work not backed up is not grounds for an extension. You have been warned.

Requirements and Submission

You need to submit:

 

Make a tar.gz file as in the first assignment, named USERNAME-project2.tar.gz

Copy it to ~jrmst106/submit/1550 by the deadline for credit.