Last Updated: 2021-02-07 Sun 14:56

CSCI 4061 Lab02: Fork and Wait

CODE DISTRIBUTION: lab02-code.zip

  • Download the code distribution
  • See further setup instructions below

CHANGELOG: Empty

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 lab illustrates as much with a simple looking program with somewhat unexpected behavior.

Grading Policy

Credit for this Lab is earned by completing the exercises 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 coopearte 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 for this lab 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_lab02.org Testing Tests for this lab
test_order_pids Testing Filter to normalize the PIDs in fork_me

3 Overview of Lab Work

Lab leaders will give a short overview examining the fork_me.c code that is central to this lab. Leaders will demonstrate a run of this program to show how its output might be unexpected for those new to the use of the fork() system call.

Leaders may also give a quick overview of the wait() and waitpid() system calls as they are used to give more predictable output in the program.

Student should work in groups to complete the QUIZ and CODE exercises described in the QUESTIONS.txt file. The same conventions for answering multiple choice questions and testing the QUIZ/CODE answers are used here as were used in the previous lab.

  • make test-quiz to check QUIZ answers
  • make test-code to run automated code tests
  • make test to run all tests
  • make zip to produce a zip file for submission

4 QUESTIONS.txt File Contents

Below are the contents of the QUESTIONS.txt file for the lab. Follow the instructions in it to complete the QUIZ and CODE questions for the lab.

                           __________________

                            LAB 02 QUESTIONS
                           __________________





Lab Instructions
================

  Follow the instructions below to experiment with topics related to
  this lab.
  - 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 and run the `fork_me.c' application and compile and run it
  (compiling can be done via a `make'). Then answer the questions below.

  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 order that processes complete is unpredictable.


CODE Create order for fork_me.c
===============================

  Modify the `fork_me.c' program to make sure that the processes
  complete in the same order each run. The easiest way to do this is to
  have parent processes use the `wait()' or `waitpid()' system call
  after creating child which will "block" the parent until the child
  completes.  This should give a consistent ordering of the output and
  prevent any orphan processes.

  You can test your results with `make test-quiz'.

  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| pid: 953670 parent_pid: 929189
  | FINISH| pid: 953674 parent_pid: 953670
  | FINISH| pid: 953672 parent_pid: 953670
  | FINISH| pid: 953675 parent_pid: 953672
  | FINISH| pid: 953673 parent_pid: 953671
  | FINISH| pid: 953671 parent_pid: 953670
  | FINISH| pid: 953676 parent_pid: 953671
  | FINISH| pid: 953670 parent_pid: 929189
  | FINISH| pid: 953679 parent_pid: 953674
  | FINISH| pid: 953680 parent_pid: 953672
  | FINISH| pid: 953681 parent_pid: 953675
  | FINISH| pid: 953682 parent_pid: 953671
  | FINISH| pid: 953684 parent_pid: 953676
  | FINISH| pid: 953677 parent_pid: 953670
  | FINISH| pid: 953678 parent_pid: 953673
  | FINISH| pid: 953683 parent_pid: 953673
  | FINISH| pid: 953685 parent_pid: 953678
  | 
  | > ./fork_me | ./test_order_pids
  | |START| pid: 101 parent_pid: 100
  | FINISH| pid: 101 parent_pid: 100
  | FINISH| pid: 102 parent_pid: 101
  | FINISH| pid: 103 parent_pid: 101
  | FINISH| pid: 104 parent_pid: 102
  | FINISH| pid: 105 parent_pid: 103
  | FINISH| pid: 106 parent_pid: 101
  | FINISH| pid: 107 parent_pid: 101
  | FINISH| pid: 108 parent_pid: 102
  | FINISH| pid: 109 parent_pid: 103
  | FINISH| pid: 110 parent_pid: 104
  | FINISH| pid: 111 parent_pid: 105
  | FINISH| pid: 112 parent_pid: 111
  | FINISH| pid: 113 parent_pid: INIT
  | FINISH| pid: 114 parent_pid: 113
  | FINISH| pid: 115 parent_pid: 106
  | FINISH| pid: 116 parent_pid: INIT
  `----

5 Submission

Follow the instructions at the end of Lab01 if you need a refresher on how to upload your completed lab zip to Gradescope.


Author: Chris Kauffman (kauffman@umn.edu)
Date: 2021-02-07 Sun 14:56