when the grandchildren, not the children, call execvp.

Team-FLY

14.6 Exercise: License Manager

The exercises in this section are along the lines of the runsim program developed in the exercises of Section 3.9. In those exercises, runsim reads a command from standard input and forks a child that calls execvp to execute the command. That runsim program takes a single command-line argument specifying the number of child processes allowed to execute simultaneously . It also keeps a count of the children and uses wait to block when it reaches the limit.

In these exercises, runsim again reads a command from standard input and forks a child. The child in turn forks a grandchild that calls execvp . The child waits for the grandchild to complete and then exits. Figure 14.1 shows the structure of runsim when three such pairs are executing. This program uses semaphores to control the number of simultaneous executions.

Figure 14.1. The structure of runsim when the grandchildren, not the children, call execvp .

graphics/14fig01.gif

14.6.1 License object

Implement a license object based on a named semaphore generated from the pathname /tmp.license.uid , where uid is the process user ID. The license should have the following public functions.

 int getlicense(void); 

blocks until a license is available.

 int returnlicense(void); 

increments the number of available licenses.

 int initlicense(void); 

performs any needed initialization of the license object.

 int addtolicense(int n); 

adds a certain number of licenses to the number available.

 int removelicenses(int n); 

decrements the number of licenses by the specified number.

14.6.2 The runsim main program

Write a runsim program that runs up to n processes at a time. Start the runsim program by typing the following command.

 runsim n 

Implement runsim as follows .

  1. Check for the correct number of command-line arguments and output a usage message if incorrect.

  2. Perform the following in a loop until end-of-file on standard input.

    1. Read a command from standard input of up to MAX_CANON characters .

    2. Request a license from the license object.

    3. Fork a child that calls docommand and then exits. Pass the input string to docommand .

    4. Check to see if any of the children have finished ( waitpid with the WNOHANG option).

The docommand function has the following prototype.

 void docommand(char *cline); 

Implement docommand as follows.

  1. Fork a child (a grandchild of the original). This grandchild calls makeargv on cline and calls execvp on the resulting argument array.

  2. Wait for this child and then return the license to the license object.

  3. Exit.

Test the program as in Section 3.9. Improve the error messages to make them more readable. Write a test program that takes two command-line arguments: the sleep time and the repeat factor. The test program simply repeats a loop for the specified number of times. In the loop, the test program sleeps and then outputs a message with its process ID to standard error. After completing the specified number of iterations, the program exits. Use runsim to run multiple copies of the test program.

Try executing several copies of runsim concurrently. Since they all use the same semaphore, the number of grandchildren processes should still be bounded by n .

14.6.3 Extensions to the license manager

Modify the license object so that it supports multiple types of licenses, each type identified by a numerical key. Test the program under conditions similar to those described in the previous section.

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net