[cs631apue] shell pipeline and ppids

Jan Schaumann jschauma at stevens.edu
Mon Oct 7 23:48:26 EDT 2013


Hello,

After class, two of you asked me about the ppid in the shell pipeline
shown in the lecture slides on page 28.  The page has a graphic showing
an illustration of how the shell forks to establish the pipeline, but
the ps(1) output does not match what we would normally expect when
reading this graphic:

According to the image, the shell (pid 949) forks once (to create pid
1988), then forks again twice (to create pids 1989 and 1990).  As you
noted, the ppid for processes 1989 and 1990 would then have to be 1988,
yet the ps(1) output shows them to be 949.

So it appears that this graphic is simply not an adaequate or proper
representation of what the shell does.  Instead, looking at the output
of ps(1), what appears to happen is that the shell (pid 949) forks once
to create pid 1989 (the 'ps' command), once to create pid 1988 (the
'cat2' command), and once to create pid 1990 (the 'cat1' command).  It
then sets up the pipelines to allow these processes to communicate.

This can also be seen in the shell source code:
http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/bin/sh/eval.c?rev=HEAD&only_with_tag=MAIN

In the function 'evalpipe', you can see the shell iterating over the
commands and calling 'forkshell' for each.

Of course different shells may implement this in different ways, but the
'ps' output we observed reflects this; the graphic is wrong and I will
update it for future classes.

Note also that running strace(1) on linux-lab shows us that that
particular version of Linux doesn't fork(2) at all -- it uses the
clone(2) interface on that platform.  As so often, Linux behaves
somewhat differently from other, more traditional Unix versions.

Anwyay, I'm glad you caught this and hope the explanation here makes
sense.

-Jan


More information about the cs631apue mailing list