CSCI 4061 HW02: Fork, Spoon, and Wait
- Due: 11:59pm Mon 2/8/2021
- Approximately 0.83% of total grade
CODE DISTRIBUTION: hw02-code.zip
- Download the code distribution every HW
- See further setup instructions below
CHANGELOG: Empty
1 Rationale
Processes are created in Unix by fork()
'ing an existing process so
mastery of these system calls is essential to bringing new running
programs into the system. Processes created in this way are the
children of the creating parent process which can detect changes in
their state using the wait()
family of calls. Many programming
paradigms rely on the parent tracking its children necessitating an
understanding of the wait()
family. Children execute the same code
code as parents unless a call to a member of the exec()
family is
made which replaces a running child's instructions with a different
program.
1.1 Associated Reading
- Ch 8 of Stevens and Rago introduces and shows many examples of
fork(), wait(), exec()
- Ch 3 of Robbins & Robbins also discusses these system calls
1.2 Grading Policy
Credit for this HW is earned by taking the associate Quiz which is
linked under Gradescope
. The quiz will ask similar questions as
those that are present in the QUESTIONS.txt
file and those that
complete all answers in QUESTIONS.txt
should have no trouble with
the quiz.
See the full policy in the syllabus.
2 Codepack
The codepack for the HW contains the following files:
File | Description |
---|---|
QUESTIONS.txt |
Questions to answer |
fork_report.c |
C file for Problem 1 |
run_child.c |
C file for Problem 2 |
NOTE: No Makefile is provided; use gcc file.c for these simple programs |
3 What to Understand
Make sure you get a good understanding of
- How
fork
is used to clone a running process to create a child - How each process has a unique
pid
, process ID, available via library calls - How
wait()
andwaitpid()
can be used to ensure order in execution by causing a parent to wait for a child to finish - How
execvp()
is used to replace a running program with a different program
All of these things will show up in projects.
4 Questions about fork(), wait(), and exec()
_________________ HW 02 QUESTIONS _________________ - Name: (FILL THIS in) - NetID: (THE kauf0095 IN kauf0095@umn.edu) Write your answers to the questions below directly in this text file. HW quiz questions will be related to the questions in this file. PROBLEM 1 `fork_report.c' ========================= A ~ Compile and run the program in `fork_report.c'. Run the program several times. Show the output of a typical run below. B ~ Is the output ordered or disordered with respect to the "I am number..." statements? Why? C ~ Add calls to the wait() or waitpid() function to ensure that the output is ordered. Compile and rerun the code to ensure that your code will enforce ordered output. Paste your completed code below. D ~ How many parents and children are there in this program? What is their arrangement? How are the parent process(es) numbered? PROBLEM 2 `run_child.c' ======================= A ~ Compile and run the program in `run_child.c'. Show it's output below. B ~ Adjust the contents of the `child_argv[]' array. Try removing the "-l" element, try adding on ".." prior to the final NULL, or make other modifications INCLUDING for the 0th "ls" string. Recompile and describe whether the output has changed. C ~ Change the child_cmd string from "ls" to be something else like "gcc" or "cat". A good combination is "gcc" with the "--version" as the only option in the child_argv array. Recompile and explain what happens now. D ~ Currently `run_child.c' does not reach the bottom statements. ,---- | printf("------------------\n"); | printf("Child Finished\n"); `---- Correct this by doing the following: - Fork a child process - Have ONLY the child process call execvp() - Have the parent process wait() for the child Paste your completed code below.