Last Updated: 2021-02-21 Sun 16:46

CSCI 4061 HW05: Exam 1 Review


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


1 Rationale

This HW gives a few points of review prior to Exam 1. The provided questions reflect a culmination of what we have covered thus far in the course.

1.1 Associated Reading

Review the following chapters of Stevens and Rago

  • Ch 1, 2 on OS and Unix Basics
  • Ch 3.1-12 on File I/O system calls
    • 3.13 and 3.14 which will be discussed post examine
  • Ch 4 will not factor into the exam and will be discussed more after hte exame
  • Ch 5 on the Standard C I/O Library and its relation to Unix files
  • Ch 6 will not factor into the exam
  • Ch 7 & 8 on Processes, their Environment, and Relationships
  • Ch 15.2-3 on Pipes

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.

NOTE: This is a review homework and the solution will be posted with the HW itself.

2 Review Questions


                         HW 05 REVIEW QUESTIONS

- Name: (FILL THIS in)
- NetID: (THE kauf0095 IN

Write your answers to the questions below directly in this text file.
HW quiz questions will be related to the questions in this file.

Specific Conceptual Review Questions

  Answer the following question on OS system calls and concepts we have
  discussed so far.


  Describe the `fork()' system call, its arguments and effect. Describe
  its return value and how it is used.


  When a parent process creates a child process, a number of things are
  copied from the parent to the child. Describe at least 3 items that
  are copied.


  When starting a program in a shell, a user has several ways to
  communicate information to a program. Describe 2 major ways that we
  discussed and are demonstrated in `commando' to turn on/off echoing.


  Describe why it is difficult to predict the order that different
  processes will execute. Suggest a useful system call to coordinate
  between a parent and child process.

Code Review


  Consider the following code from the provided `fork_shape.c' file
  |  1  // fork_shape.c: predict the shape of this set of fork() calls
  |  2  #include <stdio.h>
  |  3  #include <unistd.h>
  |  4  #include <wait.h>
  |  5  int main(void) {
  |  6    pid_t pid = -1;
  |  7    for(int i=0; i<4; i++){
  |  8      pid = fork();
  |  9      if(pid != 0){
  | 10        break;
  | 11      }
  | 12    }
  | 13    waitpid(pid, NULL, 0);
  | 14    printf("pid = %d, parent pid = %d\n", getpid(), getppid());
  | 15    return 0;
  | 16  }

  1. How many total processes will print their PIDs?
  2. What shape of process hierarchy will this code produce? Draw a
     picture of it.
  3. Will the order of printing be consistent or arbitrary? Why?


  Write a short C program that uses low level system calls to output the
  entire contents of a file that is named as the 1st command line
  argument to the program.
  - Use only `read()' to obtain bytes from the file
  - Do NOT read 1 byte at a time as this is generally not efficient;
    instead pick a medium sized buffer between 32 and 512 bytes large
    and `read()' as much as possible into it per system call
  - Use only `write()' to output bytes
  - Write the total bytes in the file at the end formatting the message
    using the `sprintf()' or `snprintf()' function and then using
    `write()' on it.

  Sample runs on data files provided in the code pack are below.
  | > gcc my_cat.c 
  | > ./a.out data.txt 
  | 1234567890
  | 11 bytes total
  | > ./a.out 21.txt 
  | 1
  | 2
  | 3
  | 4
  | 5
  | 6
  | 7
  | 8
  | 9
  | 10
  | 11
  | 12
  | 13
  | 14
  | 15
  | 16
  | 17
  | 18
  | 19
  | 20
  | 21
  | 54 bytes total
  | > ./a.out gettysburg.txt 
  | Four score and seven years ago our fathers brought forth on this
  | continent, a new nation, conceived in Liberty, and dedicated to the
  | proposition that all men are created equal.
  | Now we are engaged in a great civil war, testing whether that nation,
  | or any nation so conceived and so dedicated, can long endure. We are
  | met on a great battle-field of that war. We have come to dedicate a
  | portion of that field, as a final resting place for those who here
  | gave their lives that that nation might live. It is altogether fitting
  | and proper that we should do this.
  | But, in a larger sense, we can not dedicate -- we can not consecrate
  | -- we can not hallow -- this ground. The brave men, living and dead,
  | who struggled here, have consecrated it, far above our poor power to
  | add or detract. The world will little note, nor long remember what we
  | say here, but it can never forget what they did here. It is for us the
  | living, rather, to be dedicated here to the unfinished work which they
  | who fought here have thus far so nobly advanced. It is rather for us
  | to be here dedicated to the great task remaining before us -- that
  | from these honored dead we take increased devotion to that cause for
  | which they gave the last full measure of devotion -- that we here
  | highly resolve that these dead shall not have died in vain -- that
  | this nation, under God, shall have a new birth of freedom -- and that
  | government of the people, by the people, for the people, shall not
  | perish from the earth.
  | Abraham Lincoln
  | November 19, 1863
  | 1511 bytes total

Topic Review

  Consider reviewing the following topics we have discussed in the
  course as they may appear on the exam in code or explanation form.

Basic Unix, Shell, and C Discipline

  - Directory navigation via shell
  - Compiling C programs and `Makefile' basics
  - Basic commands to determine running processes
  - Shell commands to terminate misbehaving processes
  - Memory image of a running process: stack, heap, globals
  - Environment variables and command line args

Forking Processes

  - Semantics of the `fork()' system call, its return value for both
    parent and children
  - What properties parents and children share after a fork
  - Creating and tracking multiple children from the same parent process
  - Replacing a running process with another program image
  - Semantics of the `exec()' family of functions
  - Detecting errors with `exec()' on failures

Waiting for Children

  - Semantics of system calls associated with a parent waiting for a
    child process: `wait()' and `waitpid()'
  - Return values of these functions and how parameters may be used to
    adjust the behavior of `waitpid()'
  - Retrieving the status of a finished child
  - Difference between blocking and non-blocking waits
  - Notion of a "zombie" child process

I/O System Calls

  - Semantics of the `read()' and `write()' system calls
  - Obtaining file descriptors via the `open()' system call
  - Standard file descriptors opened automatically for each process
    (standard in, standard out, and standard error)
  - Creation of a pipe via `pipe()' system call
  - Use of a pipe to allow parent and child to communicate

I/O Redirection

  - Shell syntax to redirect program output into files
  - Shell syntax to get input for a program from a file
  - Use of the `dup2()' to redirect file operations to a different file
  - Pictures associated with a process file table and the effect of
    `dup2()' on the table
  - Use of `dup()' to make "backups" of file descriptors to restore
    redirected I/O
  - Redirection of output into a pipe or file
  - Redirection of input to come from a pipe or file

C Standard I/O versus Low-Level I/O

  - Relation between a C `FILE *' and a Unix file descriptor
  - Internal buffering used by standard `printf()' and `scanf()' and
    related functions
  - Strange effects that can result from mixing standard C I/O functions
    and low-level Unix I/O calls
  - Basic file stats via `stat()'
  - Reading through directories using `opendir()' and `readdir()'
  - File permissions, hard and symbolic links

Project 1 Commando

  - Basic architecture, data structures to coordinate child processes
  - Possible extensions such as redirection of I/O from/to children and
    between children

Author: Chris Kauffman (
Date: 2021-02-21 Sun 16:46