CIS 307: Homework 4

Using Unix System Services (shared memory, file locks). Given March 26, due April 5

This homework is a continuation of Homework 3. It will have a similar behavior, but changed implementation. Now we will not have the STORE_MANAGER process. In addition the main program menu will have an extra option allowing the main program to list the current content of the store.

The processes RAND_PROC1, RAND_PROC2 and the main program now share a segment of their virtual memories. This shared segment will contain all the data structures previously managed by the STORE_MANAGER process. This brings to the problem greater efficiency [since no pipes are needed and communication is at memory speed], a number of complications [shared memory and locks], and a great simplification [no need for agenda and pipes].

Some complications are due to the fact that the process STORE_MANAGER does not exist anymore. Thus the requests from RAND_PROC1 and RAND_PROC2 are not serialized by the STORE_MANAGER and you have to use appropriate locks within the code executed by RAND_PROC1 and RAND_PROC2. Since there are no pipes, all the commands (STORE_READ and STORE_UPDATE) will be carried out as function calls from RAND_PROC1 and RAND_PROC2, with the appropriate parameters, and will return the appropriate values when the operation is completed.
As side effect of each call, a record will be written to the CHILDprocid.DAT file with the following information:

To create critical regions use file locking. Use a separate lock for each entry of the store (use a single file with the different locks represented by different records in the file) See the example on Read and Write locks to see how to protect read and write operations.

Other complications arise since we are using shared memory.
To share memory among processes use the shmget, shmat, shmdt operations. shmget creates a segment. It should be invoked with as key IPC_PRIVATE. It should NOT be invoked in the main program. Instead the two processes RAND_PROC1 and RAND_PROC2 should behave symmetrically. They should agree on the name of a file and, atomically, they should check on the existence of the file. If not there, they should create the file, create the segment, and store its id in the file. If there, they should read the segment id from the file. The main program will acquire the segment id by reading the file [if the file is not there, the main program will loop test-sleep until the file becomes available].
Once a process has the id of the segment, the process attaches to it with shmat. When done, just before exiting the program, be sure to use shmdt to delete the segment.
Often people forget to delete memory segments. Use at the shell level the command ipcs to determine what shared resources are being used in the system and ipcrm to remove them if no longer needed.

There is a great simplification in this homework with respect to homework 3. Now the work on the store is done directly by the clients. Thus the fact that we are dealing with slow operations does not require us to manage in the store-manager an agenda (a priority queue). Instead the clients themselves start the operations, sleep, wake up at completion, and continue.

The interaction between the main program and the children processes using signals remains unchanged from homework 3. But now the main program has an extra menu entry to print the content of the whole store. During the time that store printing is going on no store entry can be updated [but entries can be read].

ingargiola@cis.temple.edu