This is probably obvious to old school Unix hackers but won’t make ANY SENSE AT ALL until I realized that:
- When
fork
gets called without a block it spawns off a child process. - Which means that the
if
statement will get run twice, once on the parent process and the other on the child process. if fork
evaluates to true in the parent process sincefork
returns the process ID of the child procecess.- In the child process
if fork
goes through theelse
statement instead becausefork
always returnsnil
in a child process. - The child process will be left hanging in a zombie state untill
Process.wait
gets called in the parent process. - You can’t write to a pipe until you close the read port and you can’t read from a pipe until you close the write port.