The readings for April 10, 2017 are:

  1. System Programming Wiki

  2. Linux Programming Interface


The focus of this reading is to introduce basic system calls dealing with files and processes in C.

Optional Resources

  1. System Calls Make the World Go Round

  2. Beej's Guide to Unix IPC

  3. Operating Systems: Three Easy Pieces


In your reading10/, response to the following questions:

  1. Identify which system calls you would use to perform the following actions by briefly listing example C code:

    1. Print out the error message associated with a failed system call.

    2. Truncate a file.

    3. Move to the 10th byte of a file.

    4. Check if a path is a directory.

    5. Create a copy of the current process.

    6. Replace the code in the current process with another program.

    7. Tell a process to terminate.

    8. Receive the exit status of a child process.

  2. Write the C equivalent, walk.c, to the Python code below (

    #!/usr/bin/env python2.7
    import os
    for name in os.listdir('.'):
        if os.path.isfile(name):
            print name, os.path.getsize(name)

    This should output something like this:

    $ ./
    walk.c 728
    walk 14496
    Makefile 535 23
    walk.o 8272
    walk.log 688 137


    Be sure to include a Makefile that builds the walk.c program:

    # Build walk program
    $ make
    Compiling walk.o...
    Linking walk...
    # Run walk program
    $ ./walk
    walk.c 728
    walk 14496
    Makefile 535 23
    walk.o 8272
    walk.log 688 137
    # Cleanup
    $ make clean

    Your program must:

    1. Compile with no warnings (be sure to have -Wall in your CFLAGS).

    2. Not contain any memory errors (as reported by Valgrind).

    3. Check if system calls fail (when appropriate).


    Consider using the following system calls:

    • opendir: To open a directory.

    • readdir: To read one directory entry at a time.

    • stat: To read a file's metadata (including file size).

    • closedir: To close a directory.


    To test your program, you can compare the output of your walk program to the output of the script:

    # Using the bash shell
    $ diff <(./walk | sort) <(./ | sort)

    Feel free to add a test rule to your Makefile to automate this.

    Note: Since the above command requires bash, you must set the SHELL variable in your Makefile to bash:

    SHELL=    bash

    Here is a possible trace:

    # Trace
    $ make -n test
    echo Compiling walk.o...
    gcc -g -gdwarf-2 -Wall -std=gnu99 -c -o walk.o walk.c
    echo Linking walk...
    gcc -L.  -o walk walk.o
    echo Testing walk...
    diff <(./walk | sort) <(./ | sort) > walk.log
    [ `valgrind --leak-check=full ./walk 2>&1 | tee -a walk.log | grep ERROR | awk '{print $4}'` = 0 ] && ! grep -q 'failed' walk.log || cat walk.log


If you have any questions, comments, or concerns regarding the course, please provide your feedback at the end of your response.


To submit your assignment, please commit your work to the reading10 folder in your assignments GitLab repository. Your reading10 folder should only contain the following files: