3/15/22, 6:41 PM Assignment 3 https://sjsu.instructure.com/courses/1471966/assignments/ XXXXXXXXXX/4 Assignment 3 Due Monday by 11:59pm Points 100 Submitting a file upload (Turnitin enabled) File...

see pdf file


3/15/22, 6:41 PM Assignment 3 https://sjsu.instructure.com/courses/1471966/assignments/6061714 1/4 Assignment 3 Due Monday by 11:59pm Points 100 Submitting a file upload (Turnitin enabled) File Types zip Available until Apr 4 at 11:59pm Start Assignment Objectives: Learn about how processes execute commands, keep logs, track exit codes and signals, and duplicate file descriptors. Background: When a UNIX-like system starts up, it runs init. Nowadays this is a program called systemd on UNIX-like systems. On Mac the similar system manager is called launchd. It runs under PID of 1 and is an ancestor of all other processes. You can see the process with command "ps aux". If a process is left as an orphan (its parent dies), it gets reassigned as a child of PID 1. These service programs (init, systemd or launchd) are running in the background; on UNIX-like OSs these are commonly referred to as daemons and generally have names ending with the letter “d.” These programs ensure that things start up properly and stay running. If a process crashes systemd (or launchd) can detect this and start it back up. You will develop a proc_manager program that reads in the commands to execute from an input file. You will read one command and its parameters per each line. Then the proc_manager program will start up all the processes with the exec command and wait for them to complete, while each command will write its stdout and stderr to log files. For this assignment you may use ideas from your C implementation for the worksheets (zyBooks), which involved reading strings and parsing them. You can also use some of your code ideas from assignment #2 to 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. Some template code for using exec is provided under folder code/a3. 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. See the dup2 examples in the connect2.c code file to see how to open the files and use dup2 in the child. You can open the files with flags O_RDWR | O_CREAT | O_APPEND. Remember to set file permissions to be open, such as 0777. https://sjsu.instructure.com/courses/1471966/files/folder/code/a3 https://sjsu.instructure.com/courses/1471966/files/folder/code/ipc?preview=61628922 3/15/22, 6:41 PM Assignment 3 https://sjsu.instructure.com/courses/1471966/assignments/6061714 2/4 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(); INDEX is the linecount of the command in the input file. 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. This information is gathered using the status parameter of the wait() system call . If the program cannot be started (exec fails), write a descriptive error message and command name to stderr (to the command's error file); for this purpose you may use fprintf(stderr...) or perror("name of command") . 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. 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. 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 ... ... Where in this example cmdfile contains 6 commands: $ cat cmdfile.txt <-- this line is not part of the file! it is just the "cat" print-file command sleep 5 ls -latr sleep 3 pwd sleep 1 wc /etc/passwd 1 2 3 https://sjsu.instructure.com/courses/1471966/files/folder/code/a3 3/15/22, 6:41 pm assignment 3 https://sjsu.instructure.com/courses/1471966/assignments/6061714 3/4 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. grading overall requirements are satisfied (such as spawning of processes) 20% code compiles without errors or warnings (use gcc -wall -werror) 5% correctly uses fork() 10% correctly uses wait() (or a variant of wait) 10% correct use of file handles (in parent, and children with dup2) 5% a version of exec is used correctly in child processes 10% the exit and signal codes and spawning messages are printed ok from parent 10% all processes run in parallel and are spawned correctly 5% an output and error file is created for each command 10% code is commented and indented (use of white space) 10% zip file contains proc_manager.c 5% submission upload a zip file called proj3.zip that contains your source files that are needed to compile and run on virtualbox (including proc_manager.c). 4 sample code to parse error code and signal of a completed process is in the slides! see this: https://docs.google.com/presentation/d/1tfajhe88j3ylpwa9cz5dhcansy1gia-yiueueijej0q/edit? folder=1huyg3ez13ynehils7ky31jl4diphu4sp#slide=id.g88a604c4df_1_0 (https://docs.google.com/presentation/d/1tfajhe88j3ylpwa9cz5dhcansy1gia-yiueueijej0q/edit? folder=1huyg3ez13ynehils7ky31jl4diphu4sp#slide=id.g88a604c4df_1_0) 1 https://docs.google.com/presentation/d/1tfajhe88j3ylpwa9cz5dhcansy1gia-yiueueijej0q/edit?folder=1huyg3ez13ynehils7ky31jl4diphu4sp#slide=id.g88a604c4df_1_0 3/15/22, 6:41 pm assignment 3 https://sjsu.instructure.com/courses/1471966/assignments/6061714 4/4 https://www.tutorialspoint.com/c_standard_library/c_function_perror.htm (https://www.tutorialspoint.com/c_standard_library/c_function_perror.htm) 2 in the example above, "killed with signal 15" was produced because in a different terminal (since the parent process is associated with the original terminal) the user typed "pkill sleep" (or pkill ... another cmd) or "kill -15 pid" or "kill - sigterm pid" or "kill pid". you can use "ps aux" to find the pid of the child process. so the only thing that goes to the terminal output is whatever the parent prints, if anything. the child output goes to the corresponding .out file; the "exited ..." or "killed ..." for a child go to the corresponding .err file. 3 same as in assignment 2, you want your processes to get spawned in parallel and the parent waits for any process to finish. thus you don't have a bottleneck of a quick process waiting behind a long process to finish. 4 https://www.tutorialspoint.com/c_standard_library/c_function_perror.htm this="" line="" is="" not="" part="" of="" the="" file!="" it="" is="" just="" the="" "cat"="" print-file="" command="" sleep="" 5="" ls="" -latr="" sleep="" 3="" pwd="" sleep="" 1="" wc="" etc/passwd="" 1="" 2="" 3="" https://sjsu.instructure.com/courses/1471966/files/folder/code/a3="" 3/15/22,="" 6:41="" pm="" assignment="" 3="" https://sjsu.instructure.com/courses/1471966/assignments/6061714="" 3/4="" 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.="" grading="" overall="" requirements="" are="" satisfied="" (such="" as="" spawning="" of="" processes)="" 20%="" code="" compiles="" without="" errors="" or="" warnings="" (use="" gcc="" -wall="" -werror)="" 5%="" correctly="" uses="" fork()="" 10%="" correctly="" uses="" wait()="" (or="" a="" variant="" of="" wait)="" 10%="" correct="" use="" of="" file="" handles="" (in="" parent,="" and="" children="" with="" dup2)="" 5%="" a="" version="" of="" exec="" is="" used="" correctly="" in="" child="" processes="" 10%="" the="" exit="" and="" signal="" codes="" and="" spawning="" messages="" are="" printed="" ok="" from="" parent="" 10%="" all="" processes="" run="" in="" parallel="" and="" are="" spawned="" correctly="" 5%="" an="" output="" and="" error="" file="" is="" created="" for="" each="" command="" 10%="" code="" is="" commented="" and="" indented="" (use="" of="" white="" space)="" 10%="" zip="" file="" contains="" proc_manager.c="" 5%="" submission="" upload="" a="" zip="" file="" called="" proj3.zip="" that="" contains="" your="" source="" files="" that="" are="" needed="" to="" compile="" and="" run="" on="" virtualbox="" (including="" proc_manager.c).="" 4="" sample="" code="" to="" parse="" error="" code="" and="" signal="" of="" a="" completed="" process="" is="" in="" the="" slides!="" see="" this:="" https://docs.google.com/presentation/d/1tfajhe88j3ylpwa9cz5dhcansy1gia-yiueueijej0q/edit?="" folder="1HuyG3ez13YNEHILs7ky31jL4dIPhU4Sp#slide=id.g88a604c4df_1_0" (https://docs.google.com/presentation/d/1tfajhe88j3ylpwa9cz5dhcansy1gia-yiueueijej0q/edit?="" folder="1HuyG3ez13YNEHILs7ky31jL4dIPhU4Sp#slide=id.g88a604c4df_1_0)" 1="" https://docs.google.com/presentation/d/1tfajhe88j3ylpwa9cz5dhcansy1gia-yiueueijej0q/edit?folder="1HuyG3ez13YNEHILs7ky31jL4dIPhU4Sp#slide=id.g88a604c4df_1_0" 3/15/22,="" 6:41="" pm="" assignment="" 3="" https://sjsu.instructure.com/courses/1471966/assignments/6061714="" 4/4="" https://www.tutorialspoint.com/c_standard_library/c_function_perror.htm="" (https://www.tutorialspoint.com/c_standard_library/c_function_perror.htm)="" 2="" in="" the="" example="" above,="" "killed="" with="" signal="" 15"="" was="" produced="" because="" in="" a="" different="" terminal="" (since="" the="" parent="" process="" is="" associated="" with="" the="" original="" terminal)="" the="" user="" typed="" "pkill="" sleep"="" (or="" pkill="" ...="" another="" cmd)="" or="" "kill="" -15="" pid"="" or="" "kill="" -="" sigterm="" pid"="" or="" "kill="" pid".="" you="" can="" use="" "ps="" aux"="" to="" find="" the="" pid="" of="" the="" child="" process.="" so="" the="" only="" thing="" that="" goes="" to="" the="" terminal="" output="" is="" whatever="" the="" parent="" prints,="" if="" anything.="" the="" child="" output="" goes="" to="" the="" corresponding="" .out="" file;="" the="" "exited="" ..."="" or="" "killed="" ..."="" for="" a="" child="" go="" to="" the="" corresponding="" .err="" file.="" 3="" same="" as="" in="" assignment="" 2,="" you="" want="" your="" processes="" to="" get="" spawned="" in="" parallel="" and="" the="" parent="" waits="" for="" any="" process="" to="" finish.="" thus="" you="" don't="" have="" a="" bottleneck="" of="" a="" quick="" process="" waiting="" behind="" a="" long="" process="" to="" finish.="" 4="">
Mar 16, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here