Question: Please help. 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

Please help.

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.

For this assignment you may involved reading strings and parsing them.

You can ideas create a tool that executes several processes. The exec system call (or one of its variants) is going to be at the heart of your code. It is recommended to use execvp, as it takes a flexible number of parameters.

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. You can use dup2() to make file handle 1 (stdout) go to a file PID.out and handle 2 (stderr) go to a file PID.err for pid PID. You can open the files with flags O_RDWR | O_CREAT | O_APPEND. Remember to set file permissions to be open, such as 0777.

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.

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.

Here is an example run:

$ ./proc_manager cmdfile $ cat 2353.out Starting command 1: child 2353 pid of parent 2234 Finished child 2353 pid of parent 2234 $ cat 2353.err Exited with exitcode = 0 $ cat 2363.out Starting command 2: child 2363 pid of parent 2234 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!