SD 212 Spring 2024 / Homeworks


hw22: Shakes

  • Due before the beginning of class on Friday, March 22

Today’s homework asks you to run a small bash script that executes a provided Python program 10 times. You will then modify the script so that the 10 runs all happen concurrently and compare the results.

You will need to submit a completed markdown file answering a few questions, as well as your modified bash script.

Downloads

Download these two files into your sd212 folder:

Tasks and questions

  1. Look at the code in shakes.py. Maybe try running it on the command line by typing for example python3 shakes.py 15.

    Describe briefly what this program does.

  2. The bash script you downloaded run-shakes.sh just runs the Python program with ten different numbers. Time the execution by running on the command line:

    time bash run-shakes.sh

    You will see three timing numbers reported. The first (“real”) is the actual elapsed “real” time, and the second (“user”) is the amount of CPU time that was used. Answer this question by reporting these two numbers.

  3. Now modify run-shakes.sh so that it runs the Python programs concurrently by backgrounding them. You need to make two changes:

    1. Add a & at the end of each line so that the python3 programs run in the background, like this:

      python3 shakes.py 16 &
    2. Add a new line at the end with the single command wait

    After making these changes, time the execution again:

    time bash run-shakes.sh

    and report the real and user time numbers.

  4. Besides the timing, what is different about the output from the “parallel”/backgrounded version compared to the original version?

Submit command

Submit your completed markdown file AND your modified bash script.

To submit files for this homework, run one of these commands:

submit -c=sd212 -p=hw22 hw22.md run-shakes.sh
club -csd212 -phw22 hw22.md run-shakes.sh
Download the file hw22.md to fill in and submit for this homework