Team-FLY |
14.6 Exercise: License ManagerThe 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 .
14.6.1 License objectImplement 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);
int returnlicense(void);
int initlicense(void);
int addtolicense(int n);
int removelicenses(int n);
14.6.2 The runsim main programWrite 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 .
The docommand function has the following prototype. void docommand(char *cline); Implement docommand as follows.
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 managerModify 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 |