CMSC216 Lab09: Fork and Wait
- Due: 11:59pm Mon 11-Nov-2024 on Gradescope
- Approximately 1.00% of total grade
CODE DISTRIBUTION: lab09-code.zip
- Download the code distribution
- See further setup instructions below
CHANGELOG:
- Mon Nov 4 10:02:24 AM EST 2024
An incompatibility of the testing script
testy
and the installed Python version on GRACE was discovered during the early discussion sections. This causes a testing error when runningmake test
on Lab09. A fix totesty
has been added tolab09-code.zip
. Students who wish to update their existing lab can type the following commands in the lab code directory:lab09-code>> rm -f testy lab09-code>> wget https://www.cs.umd.edu/~profk/216/testy
Table of Contents
1 Rationale
The fork()
and wait()
system calls are among the most basic
facilities to create and coordinate processes in Unix systems. While
their semantics can seem simple at first, use of fork()
in
particular can be a bit deceptive. This exercise illustrates as much with
a simple looking program with somewhat unexpected behavior. It also
gives practice on how a parent process can coordinate its execution in
a limited fashion with child processes and receive a small amount of
information about the child process via collecting their exit codes.
Grading Policy
Credit for this exercise is earned by completing the code/asnwers here
and submitting a Zip of the work to Gradescope. Students are
responsible to check that the results produced locally via make test
are reflected on Gradescope after submitting their completed
Zip. Successful completion earns 1 Engagement Point.
Lab Exercises are open resource/open collaboration and students are encouraged to cooperate on labs. Students may submit work as groups of up to 5 to Gradescope: one person submits then adds the names of their group members to the submission.
See the full policies in the course syllabus.
2 Codepack
The codepack is linked at the top of this document. Always download it and unzip/unpack it. It should contain the following files which are briefly described.
File | Use | Description |
---|---|---|
QUESTIONS.txt |
EDIT | Questions to answer: fill in the multiple choice selections in this file. |
fork_me_main.c |
EDIT | C file to; edit it to complete for testing |
QUESTIONS.txt.bk |
Backup | Backup copy of the original file to help revert if needed |
Makefile |
Build | Enables make test and make zip |
testy |
Testing | Test running scripts |
test_lab09.org |
Testing | Tests for this exercise |
test_order_pids |
Testing | Filter to normalize the PIDs in fork_me |
3 QUESTIONS.txt File Contents
Below are the contents of the QUESTIONS.txt
file for the exercise.
Follow the instructions in it to complete the QUIZ and CODE questions
for the exercise.
_________________ LAB09 QUESTIONS _________________ Exercise Instructions ===================== Follow the instructions below to experiment with topics related to this exercise. - For sections marked QUIZ, fill in an (X) for the appropriate response in this file. Use the command `make test-quiz' to see if all of your answers are correct. - For sections marked CODE, complete the code indicated. Use the command `make test-code' to check if your code is complete. - DO NOT CHANGE any parts of this file except the QUIZ sections as it may interfere with the tests otherwise. - If your `QUESTIONS.txt' file seems corrupted, restore it by copying over the `QUESTIONS.txt.bk' backup file. - When you complete the exercises, check your answers with `make test' and if all is well, create a zip file with `make zip' and upload it to Gradescope. Ensure that the Autograder there reflects your local results. - IF YOU WORK IN A GROUP only one member needs to submit and then add the names of their group. QUIZ Questions on fork_me.c =========================== Analyze the `fork_me.c' application then compile and run it. Compiling can be done via a `make'. Answer the questions below about its behavior. How many total processes are created by running this program including the initial process? - ( ) 3 - ( ) 4 - ( ) 5 - ( ) 16 Which of the following best describes the relationship between the processes created by running this program? - ( ) There is a "chain" of processes with single parent, single child, single grand child, etc. - ( ) There is a "fan" of processes with a single parent and multiple children - ( ) There is a lobsided "tree" of processes with a single parent, several children, some of which have grand children, some of which have great grand children, etc. - ( ) There are several individual parents each of which has a single child. Concerning when the processes complete and produce their FINISH output... - ( ) The order that the processes complete is always the same. - ( ) The entire output order varies and cannot be predicted - ( ) Portions of the output appear predictably but other portions vary - ( ) Only the process IDs vary but the printing order does not change Check ALL that are true about the initial version of he `fork_me.c' program - ( ) Sometimes when running the program, a process will report its Parent PID as 1 - ( ) All processes report a global_total of 4 - ( ) Sometimes the START message will appear after some FINISH messages - ( ) The intended output of child and grandchild processes print more "--" symbols does not work and all processes appear at the same level You can test your results for the above quiz questions via `make test-quiz'. CODE Create order for fork_me.c =============================== Modify the `fork_me.c' program to fulfill the following requirements 1. Every run of the program leads to the same ordering of output between parent, child, grandchild, etc. processes 2. Parent processes pause each time a child is created and do not proceed until a child process finishes. 3. The parent process examines the status of the child process and extracts its exit code / return value and adds it to the `global_total' variable. The code modifications required include the use of some of the following system calls and associated macros. ----------------------------------------------------------------------------------------------------------------------------- Call/Macro Effect ----------------------------------------------------------------------------------------------------------------------------- wait(NULL); Wait for any immediate child process to finish wait(&status); Wait for any immediate child process to finish AND store info about its completion waitpid(cpid,NULL,0); Wait specifically for child cpid to finish waitpid(cpid,&status,0); Wait specifically for child cpid to finish AND store info about its completion WIFEXITED(status) Evaluates to "truthy" if status contains info on a process that exited normally WEXITSTATUS(status) For processes that exited normally, extracts the exit code / return value for the program WIFSIGNALLED(status) Evaluates to "truthy" if status contains info on a process that were terminated due to errors WTERMSIG(status) For processes that were terminated, returns the Signal number related to the cause of termination ----------------------------------------------------------------------------------------------------------------------------- Not all of these need be used in `fork_me.c' but they may all be useful in an upcoming project. You can test your code `make test-code'. NOTE that since the PIDs from run to run are unpredictable, the `make test-quiz' command uses the `test_order_pids' to create consistent PIDs for testing purposes. EXAMPLE: ,---- | >> ./fork_me | START| parent_pid: 1010387 pid: 568951 | FINISH| pid: 568951 parent_pid: 1010387 global_total: 4 | --FINISH| pid: 568953 parent_pid: 568951 global_total: 4 | ----FINISH| pid: 568954 parent_pid: 568952 global_total: 4 | ----FINISH| pid: 568957 parent_pid: 568953 global_total: 4 | ----FINISH| pid: 568956 parent_pid: 568952 global_total: 4 | ----FINISH| pid: 568960 parent_pid: 568955 global_total: 4 | ----FINISH| pid: 568961 parent_pid: 568952 global_total: 4 | --FINISH| pid: 568955 parent_pid: 568951 global_total: 4 | --FINISH| pid: 568952 parent_pid: 568951 global_total: 4 | ------FINISH| pid: 568964 parent_pid: 1 global_total: 4 | ------FINISH| pid: 568965 parent_pid: 1 global_total: 4 | ----FINISH| pid: 568962 parent_pid: 1 global_total: 4 | ------FINISH| pid: 568959 parent_pid: 1 global_total: 4 | --FINISH| pid: 568958 parent_pid: 568951 global_total: 4 | ------FINISH| pid: 568963 parent_pid: 1 global_total: 4 | --------FINISH| pid: 568966 parent_pid: 1 global_total: 4 | | >> ./fork_me | ./test_order_pids | START| parent_pid: 101 pid: 100 | FINISH| pid: 100 parent_pid: 101 global_total: 4 | --FINISH| pid: 102 parent_pid: 100 global_total: 4 | --FINISH| pid: 103 parent_pid: 100 global_total: 4 | ----FINISH| pid: 104 parent_pid: 102 global_total: 4 | --FINISH| pid: 105 parent_pid: 100 global_total: 4 | ----FINISH| pid: 106 parent_pid: 105 global_total: 4 | ------FINISH| pid: 107 parent_pid: 106 global_total: 4 | ----FINISH| pid: 108 parent_pid: INIT global_total: 4 | ----FINISH| pid: 109 parent_pid: INIT global_total: 4 | ------FINISH| pid: 110 parent_pid: INIT global_total: 4 | ------FINISH| pid: 111 parent_pid: INIT global_total: 4 | ----FINISH| pid: 112 parent_pid: INIT global_total: 4 | --------FINISH| pid: 113 parent_pid: 111 global_total: 4 | ----FINISH| pid: 114 parent_pid: INIT global_total: 4 | --FINISH| pid: 115 parent_pid: INIT global_total: 4 | ------FINISH| pid: 116 parent_pid: INIT global_total: 4 `----
4 Submission
Follow the instructions at the end of Lab01 if you need a refresher on how to upload your completed lab zip to Gradescope.