Difference between revisions of "COMP 3000 Lab 6 2012"

From Soma-notes
Jump to navigation Jump to search
Line 12: Line 12:
# Describe the output of the above lines.  In particular, how is the output of the two commands intermingled in the file race.txt?
# Describe the output of the above lines.  In particular, how is the output of the two commands intermingled in the file race.txt?
# How could you modify the lines so that output to the file race.txt continues until a file "done" is created in the current directory (instead of testing the values of x and y)?
# How could you modify the lines so that output to the file race.txt continues until a file "done" is created in the current directory (instead of testing the values of x and y)?
# How many processes do these two lines generate on your system?
# Approximately how many processes do these two lines generate on your system? How can you tell?
# Change each line into a stand-alone shell script with no semicolons.
# Use flock in the shell script versions of the commands to keep the output of the three echo's in the inner loop from being broken apart (make them "atomic").  Thus, in race.txt, you'll have lines starting with:
  P1 C1 ...
  P1 C2 ...
  P1 C3 ...
  P2 C1 ...
  P2 C2 ...
  P2 C3 ...
  P2 C1 ...
  P2 C2 ...
  P2 C3 ...
  P1 C1 ...
  P1 C2 ...
  P1 C3 ...
Note that the interleaving of output from P1 and P2 can be in any order so long as the sequence for each is always C1, C2, then C3.

Revision as of 11:45, 5 November 2012

This lab is due on Friday, Nov. 9, at 11:55 PM.

Run the following three commands in bash. (Note the \ is just to note the command continues on the following line.)

x=0; while builtin test $x -lt 50000; do echo "P1 C1 $x"; echo "P1 C2 $x"; \
  echo "P1 C3 $x"; let x=$(($x + 1)); done >> race.txt &
y=0; while builtin test $y -lt 50000; do echo "P2 C1 $y"; echo "P2 C2 $y"; \
  echo "P2 C3 $y"; let y=$(($y + 1)); done >> race.txt &

The following questions are based on these three lines.

  1. Describe the output of the above lines. In particular, how is the output of the two commands intermingled in the file race.txt?
  2. How could you modify the lines so that output to the file race.txt continues until a file "done" is created in the current directory (instead of testing the values of x and y)?
  3. Approximately how many processes do these two lines generate on your system? How can you tell?
  4. Change each line into a stand-alone shell script with no semicolons.
  5. Use flock in the shell script versions of the commands to keep the output of the three echo's in the inner loop from being broken apart (make them "atomic"). Thus, in race.txt, you'll have lines starting with:
 P1 C1 ...
 P1 C2 ...
 P1 C3 ...
 P2 C1 ...
 P2 C2 ...
 P2 C3 ...
 P2 C1 ...
 P2 C2 ...
 P2 C3 ...
 P1 C1 ...
 P1 C2 ...
 P1 C3 ...

Note that the interleaving of output from P1 and P2 can be in any order so long as the sequence for each is always C1, C2, then C3.