Slide 1

Slide 1 text

! UNIX FOR DEVELOPERS https://github.com/gregmalcolm/unix_for_programmers_demo @gregmalcolm UNIX-FU: Most rubyists develop for a unix platform. Unix as a platform is designed with programmers in mind. Today I’ll cover a little bit about how to get the most from it.

Slide 2

Slide 2 text

TIMELINE 1969 - Unix was invented at Bell Labs starting with Ken Thompson and shortly followed by Dennis Ritchie who invented C. Over 40 years ago! 1983 - US Goverment breaks up Bell System, FSF founded

Slide 3

Slide 3 text

1969 - UNIX is Born TIMELINE 1972 - Rewritten in C 1977 - BSD UNIX 1981 - IBM PC 1983 - Richard Stallman founds FSF 1991 - Linus Tovalds starts Linux 1969 - Unix was invented at Bell Labs starting with Ken Thompson and shortly followed by Dennis Ritchie who invented C. Over 40 years ago! 1983 - US Goverment breaks up Bell System, FSF founded

Slide 4

Slide 4 text

PHILOSOPHY Doug McIlroy (inventer of unix pipeline): “This is the Unix philosophy: ! Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.” Doug McIlroy’s take on the Unix Philisophy sums it up very nicely

Slide 5

Slide 5 text

PHILOSOPHY Doug McIlroy (inventer of unix pipeline): “This is the Unix philosophy: ! Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.” Doug McIlroy’s take on the Unix Philisophy sums it up very nicely

Slide 6

Slide 6 text

Mike Gancarz (part of X Windows team): “1. Small is beautiful. 2. Make each program do one thing well. 3. Build a prototype as soon as possible. 4. Choose portability over efficiency. 5. Store data in flat text files. 6. Use software leverage to your advantage. 7. Use shell scripts to increase leverage and portability. 8. Avoid captive user interfaces. 9. Make every program a filter. ” PHILOSOPHY I also like Mike Gancarz’s version.

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

STREAMS File Streams ! $ Firstly, theres file streams. I’ll open a couple here, one for output, one another for input. Fairly basic stuff. Each file has a file descriptor. Here is how I can print them from ruby. ! Now, why does the number start from 3? Answer is on the next page

Slide 9

Slide 9 text

STREAMS File Streams ! $ irb ! >> Firstly, theres file streams. I’ll open a couple here, one for output, one another for input. Fairly basic stuff. Each file has a file descriptor. Here is how I can print them from ruby. ! Now, why does the number start from 3? Answer is on the next page

Slide 10

Slide 10 text

STREAMS File Streams ! $ irb ! >> f = File.open('file1.out', 'w') Firstly, theres file streams. I’ll open a couple here, one for output, one another for input. Fairly basic stuff. Each file has a file descriptor. Here is how I can print them from ruby. ! Now, why does the number start from 3? Answer is on the next page

Slide 11

Slide 11 text

STREAMS File Streams ! $ irb ! >> f = File.open('file1.out', 'w') => # >> Firstly, theres file streams. I’ll open a couple here, one for output, one another for input. Fairly basic stuff. Each file has a file descriptor. Here is how I can print them from ruby. ! Now, why does the number start from 3? Answer is on the next page

Slide 12

Slide 12 text

STREAMS File Streams ! $ irb ! >> f = File.open('file1.out', 'w') => # >> f.puts "some text" Firstly, theres file streams. I’ll open a couple here, one for output, one another for input. Fairly basic stuff. Each file has a file descriptor. Here is how I can print them from ruby. ! Now, why does the number start from 3? Answer is on the next page

Slide 13

Slide 13 text

STREAMS File Streams ! $ irb ! >> f = File.open('file1.out', 'w') => # >> f.puts "some text" => nil >> Firstly, theres file streams. I’ll open a couple here, one for output, one another for input. Fairly basic stuff. Each file has a file descriptor. Here is how I can print them from ruby. ! Now, why does the number start from 3? Answer is on the next page

Slide 14

Slide 14 text

STREAMS File Streams ! $ irb ! >> f = File.open('file1.out', 'w') => # >> f.puts "some text" => nil >> f2 = File.open('file2.in', 'r') Firstly, theres file streams. I’ll open a couple here, one for output, one another for input. Fairly basic stuff. Each file has a file descriptor. Here is how I can print them from ruby. ! Now, why does the number start from 3? Answer is on the next page

Slide 15

Slide 15 text

STREAMS File Streams ! $ irb ! >> f = File.open('file1.out', 'w') => # >> f.puts "some text" => nil >> f2 = File.open('file2.in', 'r') => # >> Firstly, theres file streams. I’ll open a couple here, one for output, one another for input. Fairly basic stuff. Each file has a file descriptor. Here is how I can print them from ruby. ! Now, why does the number start from 3? Answer is on the next page

Slide 16

Slide 16 text

STREAMS File Streams ! $ irb ! >> f = File.open('file1.out', 'w') => # >> f.puts "some text" => nil >> f2 = File.open('file2.in', 'r') => # >> f.fileno Firstly, theres file streams. I’ll open a couple here, one for output, one another for input. Fairly basic stuff. Each file has a file descriptor. Here is how I can print them from ruby. ! Now, why does the number start from 3? Answer is on the next page

Slide 17

Slide 17 text

STREAMS File Streams ! $ irb ! >> f = File.open('file1.out', 'w') => # >> f.puts "some text" => nil >> f2 = File.open('file2.in', 'r') => # >> f.fileno => 3 >> Firstly, theres file streams. I’ll open a couple here, one for output, one another for input. Fairly basic stuff. Each file has a file descriptor. Here is how I can print them from ruby. ! Now, why does the number start from 3? Answer is on the next page

Slide 18

Slide 18 text

STREAMS File Streams ! $ irb ! >> f = File.open('file1.out', 'w') => # >> f.puts "some text" => nil >> f2 = File.open('file2.in', 'r') => # >> f.fileno => 3 >> f2.fileno Firstly, theres file streams. I’ll open a couple here, one for output, one another for input. Fairly basic stuff. Each file has a file descriptor. Here is how I can print them from ruby. ! Now, why does the number start from 3? Answer is on the next page

Slide 19

Slide 19 text

STREAMS File Streams ! $ irb ! >> f = File.open('file1.out', 'w') => # >> f.puts "some text" => nil >> f2 = File.open('file2.in', 'r') => # >> f.fileno => 3 >> f2.fileno => 4 >> Firstly, theres file streams. I’ll open a couple here, one for output, one another for input. Fairly basic stuff. Each file has a file descriptor. Here is how I can print them from ruby. ! Now, why does the number start from 3? Answer is on the next page

Slide 20

Slide 20 text

Input/Output Streams STREAMS There are 3 specials streams available in unix, STDIN, STDOUT and STDERR. These streams are never closed and use up File Descriptors 0, 1 and 2. STDIN by default is for input (keyboard). STDOUT is for program output, usually going to the console. STDOUT also defaults to outputing to the console, but it is easy to redirect to an error log.

Slide 21

Slide 21 text

! $ STREAMS STDIN STDIN is usually inputted through the keyboard. In this example I’m asking running the word count command. The -l tells it to report how many lines. Ctrl+D acts as an end of stream indication.

Slide 22

Slide 22 text

wc -l ! $ STREAMS STDIN STDIN is usually inputted through the keyboard. In this example I’m asking running the word count command. The -l tells it to report how many lines. Ctrl+D acts as an end of stream indication.

Slide 23

Slide 23 text

wc -l green eggs and ham ! $ STREAMS STDIN STDIN is usually inputted through the keyboard. In this example I’m asking running the word count command. The -l tells it to report how many lines. Ctrl+D acts as an end of stream indication.

Slide 24

Slide 24 text

wc -l green eggs and ham 4 $ ! $ STREAMS STDIN STDIN is usually inputted through the keyboard. In this example I’m asking running the word count command. The -l tells it to report how many lines. Ctrl+D acts as an end of stream indication.

Slide 25

Slide 25 text

! $ STREAMS STDOUT STDOUT by default outputs to the console. For example, if I run ls I'll get output to the console.

Slide 26

Slide 26 text

ls ! $ STREAMS STDOUT STDOUT by default outputs to the console. For example, if I run ls I'll get output to the console.

Slide 27

Slide 27 text

ls README monitor.sh sample_in.text $ ! $ STREAMS STDOUT STDOUT by default outputs to the console. For example, if I run ls I'll get output to the console.

Slide 28

Slide 28 text

! $ STREAMS STDERR Standard Error also outputs to the console by default. For example, if I run ruby with a bad option the message is sent via STDERR.

Slide 29

Slide 29 text

ruby --make-presentation-for-me ! $ STREAMS STDERR Standard Error also outputs to the console by default. For example, if I run ruby with a bad option the message is sent via STDERR.

Slide 30

Slide 30 text

ruby --make-presentation-for-me ruby: invalid option --make-presentation-for-me (-h will show valid options) (RuntimeError) $ ! $ STREAMS STDERR Standard Error also outputs to the console by default. For example, if I run ruby with a bad option the message is sent via STDERR.

Slide 31

Slide 31 text

! $ STREAMS Redirecting STDIN We can override the default streams through use of redirections. In this example we redirect to the Dr Seusse text into STDIN. By the way, this time we’re using the -w option with wc to count words.

Slide 32

Slide 32 text

cat seuss.text ! $ STREAMS Redirecting STDIN We can override the default streams through use of redirections. In this example we redirect to the Dr Seusse text into STDIN. By the way, this time we’re using the -w option with wc to count words.

Slide 33

Slide 33 text

cat seuss.text one fish two fish red fish blue fish $ ! $ STREAMS Redirecting STDIN We can override the default streams through use of redirections. In this example we redirect to the Dr Seusse text into STDIN. By the way, this time we’re using the -w option with wc to count words.

Slide 34

Slide 34 text

cat seuss.text one fish two fish red fish blue fish $ wc -w

Slide 35

Slide 35 text

cat seuss.text one fish two fish red fish blue fish $ wc -w

Slide 36

Slide 36 text

! $ STREAMS Redirecting STDOUT and STDERR For stdout we can use > to redirect to a file. Or >> if we just to append to an existing file. Similarly we can redirect to STDERR with 2> or 2>> (ruby doesn't know how to parse the argument) Note you can stick a 1 in front of STDERR redirctions and a 0 in front of STDIN, but its only when it comes to STDERR that you really have a need. ! Btw, if you used << for STDIN it actually does something a bit different. cat << ENDWORD will accept text until the word "ENDWORD" is encountered.

Slide 37

Slide 37 text

echo “Redirect stdout to a file” >file.text ! $ STREAMS Redirecting STDOUT and STDERR For stdout we can use > to redirect to a file. Or >> if we just to append to an existing file. Similarly we can redirect to STDERR with 2> or 2>> (ruby doesn't know how to parse the argument) Note you can stick a 1 in front of STDERR redirctions and a 0 in front of STDIN, but its only when it comes to STDERR that you really have a need. ! Btw, if you used << for STDIN it actually does something a bit different. cat << ENDWORD will accept text until the word "ENDWORD" is encountered.

Slide 38

Slide 38 text

echo “Redirect stdout to a file” >file.text ! $ ! $ STREAMS Redirecting STDOUT and STDERR For stdout we can use > to redirect to a file. Or >> if we just to append to an existing file. Similarly we can redirect to STDERR with 2> or 2>> (ruby doesn't know how to parse the argument) Note you can stick a 1 in front of STDERR redirctions and a 0 in front of STDIN, but its only when it comes to STDERR that you really have a need. ! Btw, if you used << for STDIN it actually does something a bit different. cat << ENDWORD will accept text until the word "ENDWORD" is encountered.

Slide 39

Slide 39 text

echo “Redirect stdout to a file” >file.text ! $ cat file.txt ! $ STREAMS Redirecting STDOUT and STDERR For stdout we can use > to redirect to a file. Or >> if we just to append to an existing file. Similarly we can redirect to STDERR with 2> or 2>> (ruby doesn't know how to parse the argument) Note you can stick a 1 in front of STDERR redirctions and a 0 in front of STDIN, but its only when it comes to STDERR that you really have a need. ! Btw, if you used << for STDIN it actually does something a bit different. cat << ENDWORD will accept text until the word "ENDWORD" is encountered.

Slide 40

Slide 40 text

echo “Redirect stdout to a file” >file.text ! $ cat file.txt ! $ Redirect stdout to a file $ STREAMS Redirecting STDOUT and STDERR For stdout we can use > to redirect to a file. Or >> if we just to append to an existing file. Similarly we can redirect to STDERR with 2> or 2>> (ruby doesn't know how to parse the argument) Note you can stick a 1 in front of STDERR redirctions and a 0 in front of STDIN, but its only when it comes to STDERR that you really have a need. ! Btw, if you used << for STDIN it actually does something a bit different. cat << ENDWORD will accept text until the word "ENDWORD" is encountered.

Slide 41

Slide 41 text

echo “Redirect stdout to a file” >file.text ! $ cat file.txt ! $ Redirect stdout to a file $ echo “Redirect and append stdout” >>file.text STREAMS Redirecting STDOUT and STDERR For stdout we can use > to redirect to a file. Or >> if we just to append to an existing file. Similarly we can redirect to STDERR with 2> or 2>> (ruby doesn't know how to parse the argument) Note you can stick a 1 in front of STDERR redirctions and a 0 in front of STDIN, but its only when it comes to STDERR that you really have a need. ! Btw, if you used << for STDIN it actually does something a bit different. cat << ENDWORD will accept text until the word "ENDWORD" is encountered.

Slide 42

Slide 42 text

echo “Redirect stdout to a file” >file.text ! $ cat file.txt ! $ Redirect stdout to a file $ echo “Redirect and append stdout” >>file.text ! $ STREAMS Redirecting STDOUT and STDERR For stdout we can use > to redirect to a file. Or >> if we just to append to an existing file. Similarly we can redirect to STDERR with 2> or 2>> (ruby doesn't know how to parse the argument) Note you can stick a 1 in front of STDERR redirctions and a 0 in front of STDIN, but its only when it comes to STDERR that you really have a need. ! Btw, if you used << for STDIN it actually does something a bit different. cat << ENDWORD will accept text until the word "ENDWORD" is encountered.

Slide 43

Slide 43 text

echo “Redirect stdout to a file” >file.text ! $ cat file.txt ! $ Redirect stdout to a file $ echo “Redirect and append stdout” >>file.text ! $ cat file.txt STREAMS Redirecting STDOUT and STDERR For stdout we can use > to redirect to a file. Or >> if we just to append to an existing file. Similarly we can redirect to STDERR with 2> or 2>> (ruby doesn't know how to parse the argument) Note you can stick a 1 in front of STDERR redirctions and a 0 in front of STDIN, but its only when it comes to STDERR that you really have a need. ! Btw, if you used << for STDIN it actually does something a bit different. cat << ENDWORD will accept text until the word "ENDWORD" is encountered.

Slide 44

Slide 44 text

echo “Redirect stdout to a file” >file.text ! $ cat file.txt ! $ Redirect stdout to a file $ echo “Redirect and append stdout” >>file.text ! $ cat file.txt Redirect stdout to a file Redirect and append stdout $ STREAMS Redirecting STDOUT and STDERR For stdout we can use > to redirect to a file. Or >> if we just to append to an existing file. Similarly we can redirect to STDERR with 2> or 2>> (ruby doesn't know how to parse the argument) Note you can stick a 1 in front of STDERR redirctions and a 0 in front of STDIN, but its only when it comes to STDERR that you really have a need. ! Btw, if you used << for STDIN it actually does something a bit different. cat << ENDWORD will accept text until the word "ENDWORD" is encountered.

Slide 45

Slide 45 text

echo “Redirect stdout to a file” >file.text ! $ cat file.txt ! $ Redirect stdout to a file $ echo “Redirect and append stdout” >>file.text ! $ cat file.txt Redirect stdout to a file Redirect and append stdout $ $ ruby “Redirect stderr to a file” 2>err.text STREAMS Redirecting STDOUT and STDERR For stdout we can use > to redirect to a file. Or >> if we just to append to an existing file. Similarly we can redirect to STDERR with 2> or 2>> (ruby doesn't know how to parse the argument) Note you can stick a 1 in front of STDERR redirctions and a 0 in front of STDIN, but its only when it comes to STDERR that you really have a need. ! Btw, if you used << for STDIN it actually does something a bit different. cat << ENDWORD will accept text until the word "ENDWORD" is encountered.

Slide 46

Slide 46 text

echo “Redirect stdout to a file” >file.text ! $ cat file.txt ! $ Redirect stdout to a file $ echo “Redirect and append stdout” >>file.text ! $ cat file.txt Redirect stdout to a file Redirect and append stdout $ $ ruby “Redirect stderr to a file” 2>err.text ! $ STREAMS Redirecting STDOUT and STDERR For stdout we can use > to redirect to a file. Or >> if we just to append to an existing file. Similarly we can redirect to STDERR with 2> or 2>> (ruby doesn't know how to parse the argument) Note you can stick a 1 in front of STDERR redirctions and a 0 in front of STDIN, but its only when it comes to STDERR that you really have a need. ! Btw, if you used << for STDIN it actually does something a bit different. cat << ENDWORD will accept text until the word "ENDWORD" is encountered.

Slide 47

Slide 47 text

echo “Redirect stdout to a file” >file.text ! $ cat file.txt ! $ Redirect stdout to a file $ echo “Redirect and append stdout” >>file.text ! $ cat file.txt Redirect stdout to a file Redirect and append stdout $ $ ruby “Redirect stderr to a file” 2>err.text ! $ ruby “or append to a file” 2>>err.text STREAMS Redirecting STDOUT and STDERR For stdout we can use > to redirect to a file. Or >> if we just to append to an existing file. Similarly we can redirect to STDERR with 2> or 2>> (ruby doesn't know how to parse the argument) Note you can stick a 1 in front of STDERR redirctions and a 0 in front of STDIN, but its only when it comes to STDERR that you really have a need. ! Btw, if you used << for STDIN it actually does something a bit different. cat << ENDWORD will accept text until the word "ENDWORD" is encountered.

Slide 48

Slide 48 text

echo “Redirect stdout to a file” >file.text ! $ cat file.txt ! $ Redirect stdout to a file $ echo “Redirect and append stdout” >>file.text ! $ cat file.txt Redirect stdout to a file Redirect and append stdout $ $ ruby “Redirect stderr to a file” 2>err.text ! $ ruby “or append to a file” 2>>err.text ! $ STREAMS Redirecting STDOUT and STDERR For stdout we can use > to redirect to a file. Or >> if we just to append to an existing file. Similarly we can redirect to STDERR with 2> or 2>> (ruby doesn't know how to parse the argument) Note you can stick a 1 in front of STDERR redirctions and a 0 in front of STDIN, but its only when it comes to STDERR that you really have a need. ! Btw, if you used << for STDIN it actually does something a bit different. cat << ENDWORD will accept text until the word "ENDWORD" is encountered.

Slide 49

Slide 49 text

STREAMS ! $ Redirecting to File Descriptors You can also redirect to open file descriptors. To redirect an STDOUT to STDERR use >&2 (ie redirect from File Descriptor #1 to File Descriptor #2) On line 4 we open File.out into File Descriptor no 3. In this instance, using the word exec is like saying "For output in this current process...", so "For output in this current process redirect FD3 to fileout". 3>&- is kind of saying "Don't redirect FD3 to anything." In other words, close FD3.

Slide 50

Slide 50 text

STREAMS ! $ echo "ERROR: Out of jello" >&2 Redirecting to File Descriptors You can also redirect to open file descriptors. To redirect an STDOUT to STDERR use >&2 (ie redirect from File Descriptor #1 to File Descriptor #2) On line 4 we open File.out into File Descriptor no 3. In this instance, using the word exec is like saying "For output in this current process...", so "For output in this current process redirect FD3 to fileout". 3>&- is kind of saying "Don't redirect FD3 to anything." In other words, close FD3.

Slide 51

Slide 51 text

STREAMS ! $ echo "ERROR: Out of jello" >&2 Error: Out of jello $ Redirecting to File Descriptors You can also redirect to open file descriptors. To redirect an STDOUT to STDERR use >&2 (ie redirect from File Descriptor #1 to File Descriptor #2) On line 4 we open File.out into File Descriptor no 3. In this instance, using the word exec is like saying "For output in this current process...", so "For output in this current process redirect FD3 to fileout". 3>&- is kind of saying "Don't redirect FD3 to anything." In other words, close FD3.

Slide 52

Slide 52 text

STREAMS ! $ echo "ERROR: Out of jello" >&2 Error: Out of jello $ ! $ Redirecting to File Descriptors You can also redirect to open file descriptors. To redirect an STDOUT to STDERR use >&2 (ie redirect from File Descriptor #1 to File Descriptor #2) On line 4 we open File.out into File Descriptor no 3. In this instance, using the word exec is like saying "For output in this current process...", so "For output in this current process redirect FD3 to fileout". 3>&- is kind of saying "Don't redirect FD3 to anything." In other words, close FD3.

Slide 53

Slide 53 text

STREAMS ! $ echo "ERROR: Out of jello" >&2 Error: Out of jello $ ! $ exec 3> file.out # like f=File.open in ruby $ Redirecting to File Descriptors You can also redirect to open file descriptors. To redirect an STDOUT to STDERR use >&2 (ie redirect from File Descriptor #1 to File Descriptor #2) On line 4 we open File.out into File Descriptor no 3. In this instance, using the word exec is like saying "For output in this current process...", so "For output in this current process redirect FD3 to fileout". 3>&- is kind of saying "Don't redirect FD3 to anything." In other words, close FD3.

Slide 54

Slide 54 text

STREAMS ! $ echo "ERROR: Out of jello" >&2 Error: Out of jello $ ! $ exec 3> file.out # like f=File.open in ruby $ echo 'Armadillos!' >&3 $ Redirecting to File Descriptors You can also redirect to open file descriptors. To redirect an STDOUT to STDERR use >&2 (ie redirect from File Descriptor #1 to File Descriptor #2) On line 4 we open File.out into File Descriptor no 3. In this instance, using the word exec is like saying "For output in this current process...", so "For output in this current process redirect FD3 to fileout". 3>&- is kind of saying "Don't redirect FD3 to anything." In other words, close FD3.

Slide 55

Slide 55 text

STREAMS ! $ echo "ERROR: Out of jello" >&2 Error: Out of jello $ ! $ exec 3> file.out # like f=File.open in ruby $ echo 'Armadillos!' >&3 $ 3>&- # f.close in ruby $ Redirecting to File Descriptors You can also redirect to open file descriptors. To redirect an STDOUT to STDERR use >&2 (ie redirect from File Descriptor #1 to File Descriptor #2) On line 4 we open File.out into File Descriptor no 3. In this instance, using the word exec is like saying "For output in this current process...", so "For output in this current process redirect FD3 to fileout". 3>&- is kind of saying "Don't redirect FD3 to anything." In other words, close FD3.

Slide 56

Slide 56 text

STREAMS ! $ echo "ERROR: Out of jello" >&2 Error: Out of jello $ ! $ exec 3> file.out # like f=File.open in ruby $ echo 'Armadillos!' >&3 $ 3>&- # f.close in ruby $ cat file.out Redirecting to File Descriptors You can also redirect to open file descriptors. To redirect an STDOUT to STDERR use >&2 (ie redirect from File Descriptor #1 to File Descriptor #2) On line 4 we open File.out into File Descriptor no 3. In this instance, using the word exec is like saying "For output in this current process...", so "For output in this current process redirect FD3 to fileout". 3>&- is kind of saying "Don't redirect FD3 to anything." In other words, close FD3.

Slide 57

Slide 57 text

STREAMS ! $ echo "ERROR: Out of jello" >&2 Error: Out of jello $ ! $ exec 3> file.out # like f=File.open in ruby $ echo 'Armadillos!' >&3 $ 3>&- # f.close in ruby $ cat file.out Armadillos! $ Redirecting to File Descriptors You can also redirect to open file descriptors. To redirect an STDOUT to STDERR use >&2 (ie redirect from File Descriptor #1 to File Descriptor #2) On line 4 we open File.out into File Descriptor no 3. In this instance, using the word exec is like saying "For output in this current process...", so "For output in this current process redirect FD3 to fileout". 3>&- is kind of saying "Don't redirect FD3 to anything." In other words, close FD3.

Slide 58

Slide 58 text

Whenever you create a new process in UNIX you actually fork off from the current process. Meaning create a clone of the current process as a child process.

Slide 59

Slide 59 text

Illustration of how forking works: We start off with the Agent Smith program which we want to fork off from. We do so and end up with a child process which is an exact clone. Because its a clone it even has access to the same open File Descriptors. This can prove very useful! On forking we load a new program into memory over the top of the old Agent Smith program. So Agent Smith becomes Horace from CSI Miami by running "exec csi_duty.rb". Remember exec from the previous example? running a command through exec causes it to replace the current program with a new one. Eventually Horace will finish his work and exit. The process will actually close down, but there is still an entry in the process table. When in this state it is referred to as a Zombie Process (or a Rob Zombie process in this case?). Meanwhile while Horace has been doing his thing Agent Smith has been completing his work and will eventually call "wait" to wait for the event of a child process exiting. When he finds that Horace has exited and retrieved the exit status the zombie process is cleared away.

Slide 60

Slide 60 text

PID = 1998 Illustration of how forking works: We start off with the Agent Smith program which we want to fork off from. We do so and end up with a child process which is an exact clone. Because its a clone it even has access to the same open File Descriptors. This can prove very useful! On forking we load a new program into memory over the top of the old Agent Smith program. So Agent Smith becomes Horace from CSI Miami by running "exec csi_duty.rb". Remember exec from the previous example? running a command through exec causes it to replace the current program with a new one. Eventually Horace will finish his work and exit. The process will actually close down, but there is still an entry in the process table. When in this state it is referred to as a Zombie Process (or a Rob Zombie process in this case?). Meanwhile while Horace has been doing his thing Agent Smith has been completing his work and will eventually call "wait" to wait for the event of a child process exiting. When he finds that Horace has exited and retrieved the exit status the zombie process is cleared away.

Slide 61

Slide 61 text

PID = 1998 PID = 2012 Illustration of how forking works: We start off with the Agent Smith program which we want to fork off from. We do so and end up with a child process which is an exact clone. Because its a clone it even has access to the same open File Descriptors. This can prove very useful! On forking we load a new program into memory over the top of the old Agent Smith program. So Agent Smith becomes Horace from CSI Miami by running "exec csi_duty.rb". Remember exec from the previous example? running a command through exec causes it to replace the current program with a new one. Eventually Horace will finish his work and exit. The process will actually close down, but there is still an entry in the process table. When in this state it is referred to as a Zombie Process (or a Rob Zombie process in this case?). Meanwhile while Horace has been doing his thing Agent Smith has been completing his work and will eventually call "wait" to wait for the event of a child process exiting. When he finds that Horace has exited and retrieved the exit status the zombie process is cleared away.

Slide 62

Slide 62 text

PID = 1998 PID = 2012 # # # Illustration of how forking works: We start off with the Agent Smith program which we want to fork off from. We do so and end up with a child process which is an exact clone. Because its a clone it even has access to the same open File Descriptors. This can prove very useful! On forking we load a new program into memory over the top of the old Agent Smith program. So Agent Smith becomes Horace from CSI Miami by running "exec csi_duty.rb". Remember exec from the previous example? running a command through exec causes it to replace the current program with a new one. Eventually Horace will finish his work and exit. The process will actually close down, but there is still an entry in the process table. When in this state it is referred to as a Zombie Process (or a Rob Zombie process in this case?). Meanwhile while Horace has been doing his thing Agent Smith has been completing his work and will eventually call "wait" to wait for the event of a child process exiting. When he finds that Horace has exited and retrieved the exit status the zombie process is cleared away.

Slide 63

Slide 63 text

PID = 1998 PID = 2012 # # # Illustration of how forking works: We start off with the Agent Smith program which we want to fork off from. We do so and end up with a child process which is an exact clone. Because its a clone it even has access to the same open File Descriptors. This can prove very useful! On forking we load a new program into memory over the top of the old Agent Smith program. So Agent Smith becomes Horace from CSI Miami by running "exec csi_duty.rb". Remember exec from the previous example? running a command through exec causes it to replace the current program with a new one. Eventually Horace will finish his work and exit. The process will actually close down, but there is still an entry in the process table. When in this state it is referred to as a Zombie Process (or a Rob Zombie process in this case?). Meanwhile while Horace has been doing his thing Agent Smith has been completing his work and will eventually call "wait" to wait for the event of a child process exiting. When he finds that Horace has exited and retrieved the exit status the zombie process is cleared away.

Slide 64

Slide 64 text

PID = 1998 PID = 2012 # # # exec csi_duty.rb ! $ Illustration of how forking works: We start off with the Agent Smith program which we want to fork off from. We do so and end up with a child process which is an exact clone. Because its a clone it even has access to the same open File Descriptors. This can prove very useful! On forking we load a new program into memory over the top of the old Agent Smith program. So Agent Smith becomes Horace from CSI Miami by running "exec csi_duty.rb". Remember exec from the previous example? running a command through exec causes it to replace the current program with a new one. Eventually Horace will finish his work and exit. The process will actually close down, but there is still an entry in the process table. When in this state it is referred to as a Zombie Process (or a Rob Zombie process in this case?). Meanwhile while Horace has been doing his thing Agent Smith has been completing his work and will eventually call "wait" to wait for the event of a child process exiting. When he finds that Horace has exited and retrieved the exit status the zombie process is cleared away.

Slide 65

Slide 65 text

PID = 1998 PID = 2012 # # # exec csi_duty.rb ! $ Illustration of how forking works: We start off with the Agent Smith program which we want to fork off from. We do so and end up with a child process which is an exact clone. Because its a clone it even has access to the same open File Descriptors. This can prove very useful! On forking we load a new program into memory over the top of the old Agent Smith program. So Agent Smith becomes Horace from CSI Miami by running "exec csi_duty.rb". Remember exec from the previous example? running a command through exec causes it to replace the current program with a new one. Eventually Horace will finish his work and exit. The process will actually close down, but there is still an entry in the process table. When in this state it is referred to as a Zombie Process (or a Rob Zombie process in this case?). Meanwhile while Horace has been doing his thing Agent Smith has been completing his work and will eventually call "wait" to wait for the event of a child process exiting. When he finds that Horace has exited and retrieved the exit status the zombie process is cleared away.

Slide 66

Slide 66 text

PID = 1998 PID = 2012 # # # exec csi_duty.rb ! $ # Illustration of how forking works: We start off with the Agent Smith program which we want to fork off from. We do so and end up with a child process which is an exact clone. Because its a clone it even has access to the same open File Descriptors. This can prove very useful! On forking we load a new program into memory over the top of the old Agent Smith program. So Agent Smith becomes Horace from CSI Miami by running "exec csi_duty.rb". Remember exec from the previous example? running a command through exec causes it to replace the current program with a new one. Eventually Horace will finish his work and exit. The process will actually close down, but there is still an entry in the process table. When in this state it is referred to as a Zombie Process (or a Rob Zombie process in this case?). Meanwhile while Horace has been doing his thing Agent Smith has been completing his work and will eventually call "wait" to wait for the event of a child process exiting. When he finds that Horace has exited and retrieved the exit status the zombie process is cleared away.

Slide 67

Slide 67 text

PID = 1998 PID = 2012 # # # Illustration of how forking works: We start off with the Agent Smith program which we want to fork off from. We do so and end up with a child process which is an exact clone. Because its a clone it even has access to the same open File Descriptors. This can prove very useful! On forking we load a new program into memory over the top of the old Agent Smith program. So Agent Smith becomes Horace from CSI Miami by running "exec csi_duty.rb". Remember exec from the previous example? running a command through exec causes it to replace the current program with a new one. Eventually Horace will finish his work and exit. The process will actually close down, but there is still an entry in the process table. When in this state it is referred to as a Zombie Process (or a Rob Zombie process in this case?). Meanwhile while Horace has been doing his thing Agent Smith has been completing his work and will eventually call "wait" to wait for the event of a child process exiting. When he finds that Horace has exited and retrieved the exit status the zombie process is cleared away.

Slide 68

Slide 68 text

PID = 1998 PID = 2012 # # # Illustration of how forking works: We start off with the Agent Smith program which we want to fork off from. We do so and end up with a child process which is an exact clone. Because its a clone it even has access to the same open File Descriptors. This can prove very useful! On forking we load a new program into memory over the top of the old Agent Smith program. So Agent Smith becomes Horace from CSI Miami by running "exec csi_duty.rb". Remember exec from the previous example? running a command through exec causes it to replace the current program with a new one. Eventually Horace will finish his work and exit. The process will actually close down, but there is still an entry in the process table. When in this state it is referred to as a Zombie Process (or a Rob Zombie process in this case?). Meanwhile while Horace has been doing his thing Agent Smith has been completing his work and will eventually call "wait" to wait for the event of a child process exiting. When he finds that Horace has exited and retrieved the exit status the zombie process is cleared away.

Slide 69

Slide 69 text

PID = 1998 PID = 2012 # # # wait 2012 ! $ Illustration of how forking works: We start off with the Agent Smith program which we want to fork off from. We do so and end up with a child process which is an exact clone. Because its a clone it even has access to the same open File Descriptors. This can prove very useful! On forking we load a new program into memory over the top of the old Agent Smith program. So Agent Smith becomes Horace from CSI Miami by running "exec csi_duty.rb". Remember exec from the previous example? running a command through exec causes it to replace the current program with a new one. Eventually Horace will finish his work and exit. The process will actually close down, but there is still an entry in the process table. When in this state it is referred to as a Zombie Process (or a Rob Zombie process in this case?). Meanwhile while Horace has been doing his thing Agent Smith has been completing his work and will eventually call "wait" to wait for the event of a child process exiting. When he finds that Horace has exited and retrieved the exit status the zombie process is cleared away.

Slide 70

Slide 70 text

PID = 1998 # # # wait 2012 ! $ Illustration of how forking works: We start off with the Agent Smith program which we want to fork off from. We do so and end up with a child process which is an exact clone. Because its a clone it even has access to the same open File Descriptors. This can prove very useful! On forking we load a new program into memory over the top of the old Agent Smith program. So Agent Smith becomes Horace from CSI Miami by running "exec csi_duty.rb". Remember exec from the previous example? running a command through exec causes it to replace the current program with a new one. Eventually Horace will finish his work and exit. The process will actually close down, but there is still an entry in the process table. When in this state it is referred to as a Zombie Process (or a Rob Zombie process in this case?). Meanwhile while Horace has been doing his thing Agent Smith has been completing his work and will eventually call "wait" to wait for the event of a child process exiting. When he finds that Horace has exited and retrieved the exit status the zombie process is cleared away.

Slide 71

Slide 71 text

$ ./fork_if.rb Parent PID 14724 This is a simulation of running fork_if.rb (code available in https://github.com/gregmalcolm/unix_for_programmers_demo)

Slide 72

Slide 72 text

puts "Parent pid is #{$$}" $ ./fork_if.rb Parent PID 14724 This is a simulation of running fork_if.rb (code available in https://github.com/gregmalcolm/unix_for_programmers_demo)

Slide 73

Slide 73 text

puts "Parent pid is #{$$}" unless fork $ ./fork_if.rb Parent pid is 14724 Parent PID 14724 This is a simulation of running fork_if.rb (code available in https://github.com/gregmalcolm/unix_for_programmers_demo)

Slide 74

Slide 74 text

puts "Parent pid is #{$$}" unless fork puts "Parent pid is #{$$}" unless fork $ ./fork_if.rb Parent pid is 14724 Parent PID 14724 Child PID 14725 This is a simulation of running fork_if.rb (code available in https://github.com/gregmalcolm/unix_for_programmers_demo)

Slide 75

Slide 75 text

puts "Parent pid is #{$$}" unless fork puts "In child process. Pid is now #{$$}" puts "Parent pid is #{$$}" unless fork puts "In child process. Pid is now #{$$}" $ ./fork_if.rb Parent pid is 14724 In child process. Pid is now 14725 Parent PID 14724 Child PID 14725 This is a simulation of running fork_if.rb (code available in https://github.com/gregmalcolm/unix_for_programmers_demo)

Slide 76

Slide 76 text

puts "Parent pid is #{$$}" unless fork puts "In child process. Pid is now #{$$}" exit 42 puts "Parent pid is #{$$}" unless fork puts "In child process. Pid is now #{$$}" exit 42 $ ./fork_if.rb Parent pid is 14724 In child process. Pid is now 14725 Parent PID 14724 Child PID 14725 This is a simulation of running fork_if.rb (code available in https://github.com/gregmalcolm/unix_for_programmers_demo)

Slide 77

Slide 77 text

puts "Parent pid is #{$$}" unless fork puts "In child process. Pid is now #{$$}" exit 42 end puts "Parent pid is #{$$}" unless fork puts "In child process. Pid is now #{$$}" exit 42 end $ ./fork_if.rb Parent pid is 14724 In child process. Pid is now 14725 Parent PID 14724 Child PID 14725 This is a simulation of running fork_if.rb (code available in https://github.com/gregmalcolm/unix_for_programmers_demo)

Slide 78

Slide 78 text

puts "Parent pid is #{$$}" unless fork puts "In child process. Pid is now #{$$}" exit 42 end child_pid = Process.wait puts "Parent pid is #{$$}" unless fork puts "In child process. Pid is now #{$$}" exit 42 end child_pid = Process.wait $ ./fork_if.rb Parent pid is 14724 In child process. Pid is now 14725 Parent PID 14724 Child PID 14725 This is a simulation of running fork_if.rb (code available in https://github.com/gregmalcolm/unix_for_programmers_demo)

Slide 79

Slide 79 text

puts "Parent pid is #{$$}" unless fork puts "In child process. Pid is now #{$$}" exit 42 end child_pid = Process.wait puts "Child (#{child_pid}) terminated with status # {$?.exitstatus}" puts "Parent pid is #{$$}" unless fork puts "In child process. Pid is now #{$$}" exit 42 end child_pid = Process.wait puts "Child (#{child_pid}) terminated with status # {$?.exitstatus}" $ ./fork_if.rb Parent pid is 14724 Child (pid 14725) terminated with status 42 In child process. Pid is now 14725 Parent PID 14724 Child PID 14725 This is a simulation of running fork_if.rb (code available in https://github.com/gregmalcolm/unix_for_programmers_demo)

Slide 80

Slide 80 text

SOURCES http://mij.oltrelinux.com/devel/unixprg/ http://www.faqs.org/docs/artu/ http://en.wikipedia.org/wiki/Pipeline_%28Unix%29 http://whynotwiki.com/Ruby_/_Process_management http://www.unix.com/unix-dummies-questions-answers/100737-how-do-you-create-zombie-process.html http://ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html#Kernel.fork http://cheezburger.com/ http://en.wikipedia.org/wiki/Unix_philosophy http://vimeo.com/11202537 http://cheezburger.com/ https://github.com/gregmalcolm/unix_for_programmers_demo @gregmalcolm