[Previous] [Up] [Next]
Go backward to The C programming language
Go up to Labs
Go forward to Processes -- signals

Processes -- fork() & wait() functions

Write a "C" program that uses the fork() and wait() function. The fork() function "splits" a process. It makes a copy of most of the data that affects the parent process for the child. The wait() function waits for a child process to terminate. This is a mandatory assignment.

Have the parent process fork() off ten (10) sub-processes. If your system does not allow 11 processes at the same time use a suitable number for your system. Make sure that the output of all of the processes is unbuffered. Have each sub-process send out 10 characters. The first process should send out ten "a"s. The second "b"s. The next to last process should send out ten "i"s. The last process needs to send out ten "j"s and ten spaces. Every third time it need to send out a new line as well. The "child" processes should use lower case characters. After each character from each process wait a period of time. You can use sleep(1) to wait one second. If you locate another time delay process with better resolution you may want to use it. Most "UNIX" systems have an internal time keeping process with a timer interrupt at between .02 and .01 seconds. So don't request a delay of less than .02 seconds -- the process may not block.

The parent process needs to wait for all of its children to terminate.

After the last child process has terminated print out the order that the processes terminated. Make a translation from the process id (pid) to the character of that process. This will mean that you print out the characters "A" through "J", but probably not in that order. The parent process should use upper case characters.

For students that want a bit extra to do. Use interprocess communications so that after printing 70 characters on a line send out a new line (instead if just every third "j").

Turn in a printed copy of your program and a copy of its output.

ltaber@pima.edu

[Previous] [Up] [Next]