This Is Not The Course Website You Are Looking For

This course website is from a previous semester. If you are currently in the class, please make sure you are viewing the latest course website instead of this old one.

Everyone:

After Spring Break, we will begin examining the underlying system calls that power our applications. This includes things such as file system manipulation and process management. We will cover these in further depth later in the semester when we switch to the C programming language, but for now, we will explore them in the comfort of Python.

TL;DR

The focus of this reading is to introduce you to system calls in Python.

Readings

The readings for this week are:

  1. Real Python

  2. Operating Systems: Three Easy Pieces

Optional Resources

Here are some additional resources:

Quiz

This week the reading is split into two sections: the first part is a short dredd quiz, while the second part involves two short Python scripts: walk.py and doit.py.

To test these scripts, you will need to download the Makefile and test scripts:

$ git checkout master                 # Make sure we are in master branch
$ git pull --rebase                   # Make sure we are up-to-date with GitLab

$ git checkout -b reading08           # Create reading08 branch and check it out

$ cd reading08                        # Go into reading08 folder

# Download Reading 08 Makefile
$ curl -LO https://gitlab.com/nd-cse-20289-sp20/cse-20289-sp20-assignments/raw/master/reading08/Makefile

# Execute tests (and download them)
$ make

Questions

Record the answers to the following Reading 08 Quiz questions in your reading08 branch:

Scripts

For this reading assignment, you will need to implement two scripts: walk.py and doit.py. Each is described below.

walk.py

For the first script, walk.py, you are to implement a Python version of the ls command:

$ ./walk.py
Makefile
README.md
doit.py
test_doit.sh
test_walk.sh
walk.py

$ ./walk.py ..
.git
.gitignore
.gitlab-ci.yml
.scripts
Dockerfile
Makefile
README.md
...
reading12

Skeleton

To help you get started, we have provided you with the following skeleton code:

#!/usr/bin/env python3

import os
import sys

# TODO: Determine which directory to walk from command line argument

directory = None

# TODO: Walk specified directory in sorted order and print out each entry's
# file name

Hints

Testing

To test your walk.py script, you can use the provided test script:

$ ./test_walk.sh
Testing walk.py ...
 walk.py                                  ... Success
 walk.py .                                ... Success
 walk.py ..                               ... Success
 walk.py /etc                             ... Success
 walk.py /var/log                         ... Success
 no system()                              ... Success
   Score 1.50

doit.py

For the second script, doit.py, you are to implement a function called doit that asks similarly to os.system: it uses os.fork to create a new process and then has the child call os.execvp to execute the given command, while the parent performs [os.wait] to get the child's exit status.

$ ./doit ls
doit.py  Makefile  README.md  test_doit.sh  test_walk.sh  walk.py

$ ./doit.py false ; echo $?
1

$ ./doit.py asdf ; echo $?
2

Skeleton

To help you get started, we have provided you with the following skeleton code:

#!/usr/bin/env python3

import os
import sys

def doit(argv):
    try:
        pid = None    # TODO: Create new process
    except OSError:
        return 1

    if pid == 0:      # Child
        try:
            pass      # TODO: Execute new code in current process
        except (IndexError, OSError):
            pass      # TODO: Exit with status 2
    else:             # Parent
        pass          # TODO: Wait for child process
        return None   # TODO: Return exit status of child process

if __name__ == '__main__':
    status = doit(sys.argv[1:])
    sys.exit(status)

Hints

Hints

Low-level System Calls

For doit.py refrain from high-level wrappers such as os.system or the subprocess module. Instead, only use os.fork and os.execvp to create a process and execute new code.

Testing

To test your doit.py script, you can use the provided test script:

$ ./test_doit.sh
Testing doit.py ...
 doit.py                                  ... Success
 doit.py asdf                             ... Success
 doit.py true                             ... Success
 doit.py false                            ... Success
 doit.py ls                               ... Success
 doit.py ls -l                            ... Success
 doit.py find /etc -type f                ... Success
 no system or subprocess                  ... Success
   Score 1.50

Submission

To submit you work, follow the same process outlined in Reading 01:

#--------------------------------------------------
# BE SURE TO DO THE PREPARATION STEPS ABOVE
#--------------------------------------------------

$ cd reading08                        # Go into reading08 folder

$ $EDITOR answers.json                # Edit your answers.json file

$ ../.scripts/submit.py               # Check reading08 quiz
Submitting reading08 assignment ...
Submitting reading08 quiz ...
      Q1 0.60
      Q2 0.40
   Score 1.00

$ git add answers.json                # Add answers.json to staging area
$ git add Makefile                    # Add Makefile to staging area
$ git commit -m "Reading 08: Quiz"    # Commit work

$ $EDITOR walk.py                     # Edit walk.py script
$ ./test_walk.sh                      # Test walk.py script
Testing walk.py ...
 walk.py                                  ... Success
 walk.py .                                ... Success
 walk.py ..                               ... Success
 walk.py /etc                             ... Success
 walk.py /var/log                         ... Success
 no system()                              ... Success
   Score 1.50

$ git add walk.py                     # Add walk.py script to staging area
$ git commit -m "Reading 08: walk.py" # Commit work

$ $EDITOR doit.py                     # Edit doit.py script
$ ./test_doit.sh                      # Test doit.py script
Testing doit.py ...
 doit.py                                  ... Success
 doit.py asdf                             ... Success
 doit.py true                             ... Success
 doit.py false                            ... Success
 doit.py ls                               ... Success
 doit.py ls -l                            ... Success
 doit.py find /etc -type f                ... Success
 no system or subprocess                  ... Success
   Score 1.50

$ git add doit.py                     # Add doit.py script to staging area
$ git commit -m "Reading 08: doit.py" # Commit work

$ git push -u origin reading08        # Push branch to GitLab

Merge Request

Remember to create a merge request and assign the appropriate TA from the Reading 08 TA List.

DO NOT MERGE your own merge request. The TAs use open merge requests to keep track of which assignments to grade. Closing them yourself will cause a delay in grading and confuse the TAs.