CIS 307: Hints for Homework 5: Using Threads
As in homework 3 you must have three processes: store manager, rand_proc1
and rand_proc2. The store manager maintains the store and the other processes
send read/update requests and receive responses from the store manager.
Unlike homework 3 rand_proc1 and rand_proc2 now send read/update requests
over separate pipes C_BOX1 and C_BOX2 respectively. The store manager sends
responses to rand_proc1 and rand_proc2 over the pipes RAND_BOX1 and RAND_BOX2
respectively. There are three major departures from homework 3. They are:
- Now the store manager has to wait for requests on separate pipes. This
problem must be solved by the use of the select system service (Stevens
p.396 and hints in UnixIII on the course home page). Basically you have to
tell the kernel through the use of this service which file descriptors
you are interested in and how long you wish to wait for either reading
or writing to the files corresponding to those descriptors. On return
from the service the kernel will give you the count of the descriptors
which are ready and by way of a macro FD_ISSET you can find out which
descriptors are actually ready. Then the appropriate operation may be
performed (in this case the reading of the request from the pipes
C_BOX1 and C_BOX2).
- In homework 3 you made the store manager put the requests on a priority
queue and the actual operations were performed by a signal handler after
an arbitrary delay (to simulate slow operations). In this program for
each request received the store manager creates a thread which deals
with that request and then dies after its completion. The operations
performed by the thread are coded in a routine which is passed as an
argument to the function (pthread_create) which creates the thread. At
the time of creation of the thread you also have to pass a pointer to a
structure which contains all the data needed by that routine to complete
the requested operation. For hints on threads refer to UnixIII on the
course home page.
- Since threads run concurrently and since they access the same data
structure - the store, you must guarantee mutual exclusion. In this
program, you have to use mutexes, one separate mutex for each entry
in the store. Again the source for hints is UnixIII.
The following are the major steps in the program:
MAIN
Create the pipes C_BOX1, C_BOX2, RAND_BOX1 and RAND_BOX2.
Fork STORE_MANAGER, RAND_PROC1 and RAND_PROC2.
Loop
Get user choice for displaying information(1 or 2).
If choice = 1 signal RAND_PROC1
If choice = 2 signal RAND_PROC2
If choice = 3 terminate STORE_MANAGER, RAND_RPOC1, RAND_PROC2
and EXIT
Forever
STORE_MANAGER
Initialize STORE.
Create N mutex's (N = # of entries in the store).
Loop
Wait for request from RAND_BOX1 and RAND_BOX2 (using select).
(Look at the while loop loop in sample program 3 in the hints
on select in UNIXIII)
Identify the sender. (Use FD_ISSET)
Read the request message from the appropriate pipe.
Create a thread and pass this message to the thread.
Forever
RAND_PROC
Loop
Generate request.
Send request on the appropriate pipe to STORE_MANAGER.
Log request.
Wait for response on the appropriate pipe.
Log response.
Wait for random period of time.
Forever.
THREAD
Lock file for access to the log file for STORE_MANAGER.
(May use a separate mutex for exclusive access to log file)
Log the request for the STORE_MANAGER.
Unlock file for access to the log file.
Identify the store entry N which will be accessed.
Lock mutex N.
Perform READ/UPDATE request.
Unlock mutex N.
Lock file for access to the log file.
Log the response for the STORE_MANAGER.
Unlock file for access to the log file.
Send the response on the appropriate pipe.
Pradeep Pappachan
ppappach@thunder.ocis.temple.edu