Overview

When the program begins, it forks into two processes:

The child process will interact with the user via stdin and stdout. It first prompts the user for a secret number between 0 and 1023 (inclusive). The parent process will try to guess the user’s number by randomly picking a number between 0 and 1023 and displaying it to stdout. The user will then enter either “hi” or “lo”, depending if the parent’s guess is either too high or too low, respectively, compared to the secret that the child process knows (which it input from the user). The child will then send either SIGUSR1 or SIGUSR2 to the parent process to let it know if its guess is too high or too low, respectively. The parent then guesses a new number and everything repeats until the parent guesses the secret number. At this point, the user will enter “correct” and the child will send a SIGINT signal to the parent to indicate it has guessed correctly. The parent then displays the number of guesses it needed to guess the correct number and then sends a SIGINT signal to the child. When the child receives the SIGINT, it kills the parent process and then terminates itself. Each time a process sends a signal, it also prints a string to stderr with the format senderPID # signal-name # receiverPID where senderPID is the pid of the sending process, receiverPID is the pid of the receiving process, and signal-name is the name of the signal being sent.

Tutorial