Last Updated: 2019-02-04 Mon 10:38

CSCI 4061 Lab02: Fork, Spoon, and Wait

CODE DISTRIBUTION: lab02-code.zip

  • Download the code distribution every lab
  • See further setup instructions below

CHANGELOG:

Mon Feb 4 10:36:48 CST 2019
The original version of the lab had incorrect weights for the checkoff/submission. These have been corrected to 30% / 70% respectively.

1 Rationale

Processes are created in Unix by forking 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 Clarification

  • 30% of lab credit is earned by showing your lab leader answers during lab
  • 70% of lab credit is earned by submitting required files

See the full policy in the syllabus.

2 Questions about fork(), wait(), and exec()

The codepack for the lab contains two C files

  • fork1.c: demonstration of fork() and getpid()
  • fork-exec1.c: demonstration of the execvp() call

The file QUESTIONS.txt contains several problems concerning these short codes. Answer the questions by compiling, running, and making changes. A copy of QUESTIONS.txt is below.

                           __________________

                            LAB 02 QUESTIONS
                           __________________


- Name: (FILL THIS in)
- NetID: (THE kauf0095 IN kauf0095@umn.edu)

Answer the questions below according to the lab specification. Write
your answers directly in this text file and submit it to complete Lab01.


PROBLEM 1 `fork1.c'
===================

A
~

  Compile and run the program in `fork1.c'. Show it's output 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. Paste your code below.


D
~

  How many parents and children are there in this program? What is their
  arrangement?


PROBLEM 2 `fork-exec1.c'
========================

A
~

  Compile and run the program in `fork-exec1.c'. Show it's output below.


B
~

  Adjust the contents of the `child_argv[]' array. Try remove 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". Recompile ad explain what happens now.


D
~

  Currently `fork-exec1.c' does not reach the bottom statements.
  ,----
  |   printf("------------------\n");
  |   printf("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 code below.

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() and waitpid() 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 What to Turn In

Submit your completed QUESTIONS.txt file to Canvas under the appropriate lab heading. Make sure to paste in any new code you were to write at appropriate locations in the file.


Author: Chris Kauffman (kauffman@umn.edu)
Date: 2019-02-04 Mon 10:38