You will develop a proc_manager program that reads in the commands to execute from an input file one-by-one. You will read one command and its parameters per each line. Then the proc_manager program will start up the process with the exec command and wait for it to complete, while the command will write its stdout and stderr to log files.
You will run each command in a child process using exec (or one of its variants) and the parent process will wait for each exec to complete. For each child process there will be log messages written to output and error files, which are named after its PID for the executed command.For example, process pid #156 will have logs written to 156.out and 156.err. Upon start, the string "Starting command INDEX: child PID pid of parent PPID" (replace INDEX, PID, PPID) will be logged to the corresponding output file 156.out. You can retrieve PID and PPID through the return value of fork() in the parent, or through getpid() or getppid(). Either the parent or child can open and write this to the log file for each child process. Upon finish of an exec (either a successful finish or termination via a signal), the parent process should write to the output file PID.out the string "Finished child PID pid of parent PPID" (replace PID, PPID).
Each time a child process finishes, the message "Exited with exitcode = X" should be written to the error file of the process PID.err. X is the PID process's exit code. If the process was killed with a signal, then "Killed with signal S" should be written to the PID.err error file. S is the signal number that killed the process. This information is gathered using the status parameter of the wait() system call ***. If the program cannot be started (exec fails), you may use perror("name of command") **** to write a descriptive error message and command name to stderr (to the command's error file). If any child processes encounter an invalid command (exec fails) they should have an exit code of 2. Remember the exit code of 0 indicates success, while exit codes other than 0 indicate some failure that was detected.
Here is an example run:
<ls output> Finished child 2363 pid of parent 2234 $ cat 2363.err Exited with exitcode = 0 $ cat 2377.out Starting command 3: child 2377 pid of parent 2234 Finished child 2377 pid of parent 2234 $ cat 2377.err Killed with signal 15 ... ...
$ cat cmdfile <-- This is not part of the file! It is just the read-file command
proc_manager runs until there are no more processes running. In this example the ls, pwd and wc commands finish right after they are started. The sleep commands run for several seconds, so proc_manager will detect and print to PID.err if they were killed with a kill signal; to kill them you may do "pkill sleep" in another terminal. If a parent process has no child process then wait(..) returns immediately "-1", thus you can continue until wait(..) returns -1.