COMP 3000 Lab 6 2012: Difference between revisions
No edit summary |
No edit summary |
||
(4 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
This lab is due on Friday, Nov. 9, at 11:55 PM. | This lab is due on Friday, Nov. 9, at 11:55 PM. This lab has 16 points. | ||
Run the following three commands in bash. (Note the \ is just to note the command continues on the following line.) | 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"; \ | 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 & | 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=0; while builtin test $y -lt 50000; do echo "P2 C1 $y"; echo "P2 C2 $y"; \ | ||
echo "P2 C3 $ | echo "P2 C3 $y"; let y=$(($y + 1)); done >> race.txt & | ||
The following questions are based on these three lines. | The following questions are based on these three lines. | ||
# [2] 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 the files "P1-done" or "P2-done" are created in the current directory (instead of testing the values of x and y)? | |||
# [2] Approximately how many processes do these two lines generate on your system? How can you tell? | |||
# [2] Change each line into a stand-alone shell script with no semicolons. | |||
# [4] Change the shell scripts so that they never run concurrently using the flock command. If one is running, then the other should wait until it is finished. | |||
# [4] 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. NOTE: You may want to avoid doing this on a non-local filesystem, e.g., don't put race.txt in your home directory on the Lambda machines. Doing this in a VM should work fine. | |||
==Answers== | |||
# The output of the two commands is randomly interleaved. The output is however numerically sequential for both P1 and P2. | |||
# x=0; while ! builtin test -e p1-done; do echo "P1 C1 $x"; echo "P1 C2 $x"; \ echo "P1 C3 $x"; done >> race.txt & <br> y=0; while ! builtin test -e p2-done; do echo "P2 C1 $y"; echo "P2 C2 $y"; \ echo "P2 C3 $y"; done >> race.txt & | |||
# These two lines generate approximately two process on the system. This can be determined using the ps command. | |||
# See code below. | |||
# See code below. | |||
# See code below. | |||
<source lang ="bash"> | |||
#Number 4 shell scripts | |||
#!/bin/bash | |||
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 | |||
#!/bin/bash | |||
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 | |||
#Number 5 shell scripts | |||
#!/bin/bash | |||
( | |||
flock -x 200 | |||
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 | |||
)200>/var/lock/.myscript.exclusivelock | |||
#!/bin/bash | |||
( | |||
flock -x 200 | |||
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 | |||
)200>/var/lock/.myscript.exclusivelock | |||
#Number 6 shell scripts | |||
#!/bin/bash | |||
x=0 | |||
while builtin test $x -lt 50000 | |||
do | |||
( | |||
flock -x 200 | |||
echo "P1 C1 $x" | |||
echo "P1 C2 $x" | |||
echo "P1 C3 $x" | |||
)200>/var/lock/.myscript.exclusivelock | |||
let x=$(($x + 1)) | |||
done >> race.txt | |||
#!/bin/bash | |||
y=0 | |||
while builtin test $y -lt 50000 | |||
do | |||
( | |||
flock -x 200 | |||
echo "P2 C1 $y" | |||
echo "P2 C2 $y" | |||
echo "P2 C3 $y" | |||
)200>/var/lock/.myscript.exclusivelock | |||
let y=$(($y + 1)) | |||
done >> race.txt |
Latest revision as of 15:59, 19 November 2012
This lab is due on Friday, Nov. 9, at 11:55 PM. This lab has 16 points.
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.
- [2] 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 the files "P1-done" or "P2-done" are created in the current directory (instead of testing the values of x and y)?
- [2] Approximately how many processes do these two lines generate on your system? How can you tell?
- [2] Change each line into a stand-alone shell script with no semicolons.
- [4] Change the shell scripts so that they never run concurrently using the flock command. If one is running, then the other should wait until it is finished.
- [4] 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. NOTE: You may want to avoid doing this on a non-local filesystem, e.g., don't put race.txt in your home directory on the Lambda machines. Doing this in a VM should work fine.
Answers
- The output of the two commands is randomly interleaved. The output is however numerically sequential for both P1 and P2.
- x=0; while ! builtin test -e p1-done; do echo "P1 C1 $x"; echo "P1 C2 $x"; \ echo "P1 C3 $x"; done >> race.txt &
y=0; while ! builtin test -e p2-done; do echo "P2 C1 $y"; echo "P2 C2 $y"; \ echo "P2 C3 $y"; done >> race.txt & - These two lines generate approximately two process on the system. This can be determined using the ps command.
- See code below.
- See code below.
- See code below.
<source lang ="bash">
- Number 4 shell scripts
- !/bin/bash
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
- !/bin/bash
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
- Number 5 shell scripts
- !/bin/bash
( flock -x 200 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 )200>/var/lock/.myscript.exclusivelock
- !/bin/bash
( flock -x 200 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 )200>/var/lock/.myscript.exclusivelock
- Number 6 shell scripts
- !/bin/bash
x=0 while builtin test $x -lt 50000 do ( flock -x 200 echo "P1 C1 $x" echo "P1 C2 $x" echo "P1 C3 $x" )200>/var/lock/.myscript.exclusivelock let x=$(($x + 1)) done >> race.txt
- !/bin/bash
y=0 while builtin test $y -lt 50000 do ( flock -x 200 echo "P2 C1 $y" echo "P2 C2 $y" echo "P2 C3 $y" )200>/var/lock/.myscript.exclusivelock let y=$(($y + 1)) done >> race.txt