diff options
| author | mo khan <mo@mokhan.ca> | 2013-08-28 07:20:13 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2013-08-28 07:20:13 -0600 |
| commit | db1191ec0e7305d684383f8974e2fa437f77ad5a (patch) | |
| tree | 5e27b197dff849d22d8e1f50eb75aa499b16bd06 /code/snippets/wait2.rb | |
Diffstat (limited to 'code/snippets/wait2.rb')
| -rw-r--r-- | code/snippets/wait2.rb | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/code/snippets/wait2.rb b/code/snippets/wait2.rb new file mode 100644 index 0000000..329cbc7 --- /dev/null +++ b/code/snippets/wait2.rb @@ -0,0 +1,26 @@ +# We create 5 child processes. +5.times do + fork do + # Each generates a random number. If even they exit + # with a 111 exit code, otherwise they use a 112 exit code. + if rand(5).even? + exit 111 + else + exit 112 + end + end +end + +5.times do + # We wait for each of the child processes to exit. + pid, status = Process.wait2 + + # If the child process exited with the 111 exit code + # then we know they encountered an even number. + if status.exitstatus == 111 + puts "#{pid} encountered an even number!" + else + puts "#{pid} encountered an odd number!" + end +end + |
