3.9 Exercise: Process FansThe exercises in this section expand on the fan structure of Program 3.2 through the development of a simple batch processing facility, called runsim. (Modifications in Section 14.6 lead to a license manager for an application program.) The runsim program takes exactly one command-line argument specifying the maximum number of simultaneous executions. Follow the outline below for implementing runsim. Write a test program called testsim to test the facility. Suggested library functions appear in parentheses.
Write a program called runsim that takes one command-line argument. Check for the appropriate command-line argument and output a usage message if the command line is incorrect. Initialize pr_limit from the command line. The pr_limit variable specifies the maximum number of children allowed to execute at a time. Initialize the pr_count variable to 0. The pr_count variable holds the number of active children. Execute the following main loop until end-of-file is reached on standard input. If pr_count is pr_limit, wait for a child to finish (wait) and decrement pr_count. Read a line from standard input (fgets) of up to MAX_CANON characters and execute a program corresponding to that command line by forking a child (fork, makeargv, execvp). Increment pr_count to track the number of active children. Check to see if any of the children have finished (waitpid with the WNOHANG option). Decrement pr_count for each completed child. After encountering an end-of-file on standard input, wait for all the remaining children to finish (wait) and then exit. Write a test program called testsim that takes two command-line arguments: the sleep time and the repeat factor. The repeat factor is the number of times testsim iterates a loop. In the loop, testim sleeps for the specified sleep time and then outputs a message with its process ID to standard error. Use runsim to run multiple copies of the testsim program. Create a test file called testing.data that contains commands to run. For example, the file might contain the following lines. testsim 5 10 testsim 8 10 testsim 4 10 testsim 13 6 testsim 1 12 Run the program by entering a command such as the following. runsim 2 < testing.data |