Understanding Processes and Pipes
In this exercise we consider a program ‘dpipe,’ which, in connection with a ‘netcat’ utility program, can be used to turn any ordinary program into a network server. A ‘netcat’ utility program is a program that extends the standard Unix ‘cat‘ utility over a network.
iuww520iuww520iuww520iuww520iuww520iuww520iuww520iuww520
The learning goals of this exercise are:
• Understand how to write C code that makes system calls.
• Understand how to start and wait for processes in Unix with the
fork()
and
waitpid() system calls.
• Understand how to create pipes as an example of an IPC (interprocess communication) facility.
• Understand how to manipulate a process’s standard input and standard output file streams using standard Unix system call facilities (e.g. dup2()
) that affect low-level file descriptors. A very similar file descriptor manipulation is performed by a shell when it sets up I/O redirection or pipes.
• Understand how to use the strace facility to trace a process’s system calls.
• Understand how to use the
/proc
file system to obtain information about processes in a system.
• Gain a deeper understanding of process states using a practical example application that combines process management, IPC, and network I/O.
In this exercise, you are asked to observe, reverse-engineer, and then reimplement the dpipe program. The dpipe binary is provided in ~cs3214/bin/dpipe.variants/ dpipe-ABC on our machines, where
ABC
is the unique id created for you. For netcat, use the ~cs3214/bin/gnetcat6
binary.
To allow you to invoke those commands directly, make sure that
~cs3214/bin
and (optionally) ~cs3214/bin/dpipe.variants
are included in your
PATH environment variable.1 You should already have your PATH set up from exercise 0 to include the firstdirectory. If you haven’t, do this now.
Here’s an example session of how to use it. Say you’re logged in to the machine ‘locust’ and run the command dpipe-ABC wc gnetcat6 -l 15999
2
. This will produce output as follows:
[cs3214@locust sys1]$ ~cs3214/bin/dpipe.variants/dpipe-ABC wc gnetcat6 -l 15999
Starting: wc
Starting: gnetcat6
Running in server mode
Note that the shell is still waiting for dpipe to complete at this point. The questions listed in part 1 must be answered at this point, after you have started dpipe, but before the next step. You may wish to continue reading and then return to this point, after opening a second terminal on the same machine.3
The next command can be run on
any
rlogin machine, but you must replace
locust
with the name of the machine on which started the earlier command:
[cs3214@chinkapin ~]$ gnetcat6 locust-rlogin.cs.vt.edu 15999 < /etc/passwd
Running in client mode 47 119 2690
[cs3214@chinkapin ~]$
The gnetcat6 instance started here connected to the gnetcat6 instance running on locust
4
, and sent its input there (in this case, the content of file /etc/passwd
). Gnetcat (running on locust) outputs this data to its standard output stream, which is connected via a pipe to the standard input stream of the ‘wc’ program. The standard output stream of ‘wc’ is connected to the standard input stream of locust’s gnetcat6 instance. Anything out
put by ‘wc’ will then be forwarded to chinkapin’s gnetcat6 instance, which outputs it to its standard output, thus appearing on the user’s terminal on chinkapin. You have just turned the ordinary ‘wc’ program into a networkservice! The entire scenario is shown in Figure 1.