Readings

This week the readings will focus on system calls related to creating processes in Python.

The readings for Wednesday, March 30 are:

  1. Executing BASH from Python

  2. Python Advanced: Fork and Processes

  3. Python and parallel programming

Optional Resources:

  1. Linux Programming Interface - Chapter 24: Process Creation

Activities

For this week's activities, you are to write the following Python scripts:

  1. watch.py: This is a Python implementation of the [watch] command. It should support the following flags:

    $ ./watch.py -h
    Usage: watch.py [-n INTERVAL] COMMAND
    
    Options:
    
        -n INTERVAL   Specify update interval (in seconds)
    
    $ ./watch.py ls
    Every 2.0s: ls
    
    bin  doc  pub  src  tmp  YESTRDAY
    
    $ ./watch.py -n 5 w
    Every 5.0s: w
    
     16:47:32 up 3 days, 50 min, 27 users,  load average: 39.44, 39.75, 39.43
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    yzhang16 pts/0    ivors-mbp.dhcp.n 10:12    1:38m  0.31s  0.31s -csh
    mvalenc2 pts/1    melinas-mbp.dhcp 14:35    2:06m  0.20s  0.06s vim find.py
    root     pts/3    mdpl-00.cse.nd.e Thu15   24:53m  0.17s  0.17s -bash
    klatimer pts/4    10.31.1.159      16:44    1:03   0.21s  0.21s -csh
    nkirkpat pts/5    darrow.cc.nd.edu 16:08    0.00s  0.82s  0.82s -csh
    xhu2     pts/6    xuehengs-mbp.dhc 16:40    6:54   0.35s  0.24s emacs program1.sh
    cklenke  pts/8    10.31.1.141      14:16    8:57   0.29s  0.29s -csh
    acummin1 pts/10   ashleys-mbp.dhcp 16:24    6.00s  9.63s  0.54s -csh
    knewaz   pts/11   lyf-z-a-drm.dhcp 16:09    2:43   0.39s  0.39s -csh
    nsmith9  pts/12   10.31.1.115      13:12    3.00s  2.04s  0.04s vim find.py
    tgayle   pts/13   traviss-air.dhcp 15:40    5:12  22.84s  0.14s -csh
    ebrady5  pts/14   10.31.1.133      15:42    1:44   1.38s  1.31s gedit README.md
    kgifaldi pts/16   desktop-t9l7vmu. 13:34    8:22   2.30s  0.06s vim find.py
    pbui     pts/17   129.74.161.85    16:46   31.00s  0.41s  0.03s python2.7 ./watch.py -n 5 w
    wtheisen pts/19   bilbo991-pc.dhcp 11:54    1:26m  0.81s  0.56s nano find.py
    marangu1 pts/20   marias-mbp.dhcp. 12:58   10:25   0.72s  0.72s -csh
    eturley  pts/21   erins-air.dhcp.n 12:59    2:00   1.48s  1.48s -csh
    ebradfo2 pts/27   10.31.1.151      14:18    9:05  15.49s  0.40s -csh
    mthomann pts/28   10.26.123.51     13:07    1:58   1.18s  1.18s -csh
    rbrannin pts/29   royces-mbp.dhcp. 15:08    8.00s  2.85s  2.68s vim find.py
    cklenke  pts/30   10.31.1.141      14:22    1:20   0.33s  0.33s -csh
    mvalenc2 pts/31   melinas-mbp.dhcp 16:35   40.00s  0.14s  0.14s -csh
    wmarkley pts/22   10.31.1.129      16:29   15:30   0.17s  0.17s -csh
    apaek1   pts/32   andrewspc.dhcp.n 13:51   11.00s  0.86s  0.86s -csh
    apaek1   pts/34   andrewspc.dhcp.n 13:51    0.00s 10.88s 10.75s vim dd.py
    apaek1   pts/35   andrewspc.dhcp.n 13:53   51:33   0.27s  0.12s vim test_dd.sh
    nkirkpat pts/41   darrow.cc.nd.edu 15:19    1:25m  0.32s  0.32s -csh
    

    Note: The default INTERNAL is 2. You should display the INTERVAL and COMMAND above output of the command (the exact formatting is not important).

    Hints

    1. You should execute external commands by using os.system.

    2. You can clear the screen between execution by executing the clear command.

    3. You can compose the COMMAND by using str.join on the command line arguments.

    4. To wait the specified INTERVAL you can use time.sleep.

    5. To prevent a messy traceback when you Control-C the script, you can catch or except the KeyboardInterrupt exception and simply exit.

  2. imv.py: This is a Python utility that allows users to interactively rename files using their favorite EDITOR. A copy of this program can be found on the student machines at: ~pbui/pub/bin/imv

    $ ./imv.py -h
    Usage: imv.py FILES...
    
    $ ls              # List current files
    0.txt 1.txt 2.txt
    
    $ imv.py *.txt    # Use imv to rename first and last file in $EDITOR
    0.png
    1.txt
    2.png
    
    $ ls              # List current files
    0.png 1.txt 2.png
    

    This program does the following:

    1. Reads in filenames from the command line arguments.

    2. Stores the paths to a temporary file.

    3. Opens an $EDITOR on the temporary file, which allows the user to modify the filenames.

    4. After the user has exited the $EDITOR, renames the files based on the information in the temporary file.

    5. Removes temporary file.

    Hints

    1. To create a temporary file, you can use the tempfile.NamedTemporaryFile class with the delete argument set to False. Alternatively, you can use tempfile.mkstemp and os.fdopen.

    2. After writing the the filenames to the temporary file, you will want to call the close on the temporary file object.

    3. Use the os.system function to execute the $EDITOR on the temporary file. Be sure to check the exit status.

    4. If the program execution was successful, use open to read back the data from the temporary file. The zip function may be useful for associating the entries in FILES with the data form the temporary file.

    5. For each file entry, use the os.rename or shutil.move function to perform the rename. Note, the rename or move operations may fail, so be sure to handle any exceptions (print the error and exit).

    6. Use the os.unlink function to delete the temporary file.

Feedback

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

Submission

To submit your assignment, please commit your work to the reading10 folder in your Assignments Bitbucket repository by 11:59 PM on Wednesday, March 30, 2016.