The goal of the lab is to implement a multi-threaded request scheduler
that is similar to how the popular Apache web server schedules request.
You do not need to have a knowledge of Apache to complete the lab - simply
use the problem specification below.
Assume that request scheduling involves a Master thread and N Slave threads. The master thread "listens" for requests and inserts it into a request queue. Slave threads wait for requests to arrive into the request queue and upon the arrival of a new request, any idle slave thread that is waiting for a request may take the request and process it.
Your goal is to implement this technique for dispatching requests using a bounded buffer producer-consumer framework. Assume that the request queue is a circular buffer of size N (i.e., an array of size N). The method involves a single producer, which is the master thread, and N consumers, which are the slave threads.
The master thread will sleep for a random short duration and produce a request. Each request has a sequentially increasing request ID and a randomly chosen request length (assign each new request a random length between 1 and M seconds). The master thread then inserts the request into the queue and goes back to sleep for a random short duration before it produces another request. Of course, if the request queue, which is a bounded buffer, is full, the master thread must wait before it can insert the request into the queue.
Each slave thread can be idle or busy. When a slave thread is idle, it acts as a consumer waiting for a new request in the request queue. After it consumes a request from the queue, the slave thread will be busy for a duration that is equal to the request length for that request. The busy state of a slave thread should be emulated by making the thread sleep for that duration. Upon completing the request, the slave thread goes back to idle thread and attempts to consume a pending request from the request queue. If the queue is empty, the slave thread must wait, like a consumer in the producer consumer problem.
In part 1 of this lab, you will implement the above problem using Java and Monitors.
Implement the bounded buffer producers and consumers using concepts discussed in class.
Run your program with a single producer (Master thread) and N consumers (slave threads).
N should be a configurable parameter that you specify as input to your program. You can also
specify other inputs such as M, the max duration of a request and parameter that indicate how
long a producer should sleep before producing the next request.
The master and slave threads should generate and consume requests as discussed above.
Have the master and slave threads print useful messages that indicate their actions.
For example,
the producer should print messages such as
Producer: produced request ID n, length t seconds at time CURRENT-TIME
Producer: sleeping for X seconds
The consumer should print messages such as
Consumer i: assigned request ID n, processing request for the next t seconds, current time is CURRENT-TIME
Consumer i: completed request ID n at time CURRENT-TIME
The second part of the lab involves solving the same problem in C++ (use of C is allowed if you prefer it over C++) using the pthreads library. Since C and C++ do not support monitors, you should use semaphores to implement your solution. Like before, your code should implement a single producer (master) thread and N consumer (slave) threads. The master and slave threads should print instructive messages like in part 1 to indicate their current actions.
Helpful references IF you are not familiar with pthreads, be sure to review this reference on Pthreds programming. A brief tutorial is also available.
There are many tutorials on Java threads and syncronization such as here. Oracle provides a concurrency tutorial here
A percent of your lab grade will come from your participation in this project as a member of your group.
What you need to turn in (each person individually):
Include in your zip file a copy of your assessment of the division of labor in the group in the format shown below. For a 2 person group, if you give yourself a 50% and your partner gives you a 50%, you will get the full credit for group participation. If you give your partner a 40% and your partner gives himself or herself a 40%, he or she will get fewer points for group participation. And so on...
You should be very careful about using code snippets you find on the Internet. In general your code should be your own. It is OK to read tutorials on the web and use these concepts in your assignment. Blind use of code from web is strictly disallowed. Feel free to check with us if you have questions on this policy. And be sure to document any Internet sources/ tutorials you have used to complete the assignment in your README file.