*BSD News Article 91003


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!news.cs.su.oz.au!inferno.mpx.com.au!nsw1.news.telstra.net!news.telstra.net!psgrain!news.sprintlink.net!news-stk-11.sprintlink.net!news-pull.sprintlink.net!news.sprintlink.net!news-peer.sprintlink.net!howland.erols.net!rill.news.pipex.net!pipex!dispatch.news.demon.net!demon!erlenstar.demon.co.uk!erlenstar.demon.co.uk!not-for-mail
From: Andrew Gierth <andrew@erlenstar.demon.co.uk>
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: A shell oddity (bug?)
Date: 12 Mar 1997 15:21:55 +0000
Organization: disorganised
Message-ID: <87vi6xt9t8.fsf@erlenstar.demon.co.uk>
References: <jpt2g5.q2c.ln@dolphin.neosoft.com>
NNTP-Posting-Host: localhost
X-NNTP-Posting-Host: erlenstar.demon.co.uk
X-Attribution: AG
X-Mayan-Date: Long count = 12.19.3.17.15; tzolkin = 11 Men; haab = 18 Kayab
Cc: Conrad Sabatier <conrads@neosoft.com>
X-Newsreader: Gnus v5.3/Emacs 19.34
Lines: 39
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.freebsd.misc:37002

>>>>> "Conrad" == Conrad Sabatier <conrads@neosoft.com> writes:

 Conrad> ps -xU news | grep fetch

 Conrad> Assuming there's a running /usr/local/sbin/fetch (part of the
 Conrad> leafnode package), then all one *should* see as a result
 Conrad> would be:

 Conrad> <PID> .... /usr/local/sbin/fetch

 Conrad> However, sometimes the "grep fetch" itself also appears in
 Conrad> the output!

 Conrad> This makes no sense to me.  Grep should be receiving the
 Conrad> piped output of ps taken from *before* grep was running.

No, the grep and the ps run at approximately the same time.

The sequence of events goes approximately like this. (Details vary
somewhat between shells.)

Shell parses the command line into a 2-command pipeline.
Shell creates pipe.
Shell forks to create subshell for 1st command.
  (Subshell does necessary plumbing, then execs 'ps')
Shell forks to create subshell for 2nd command.
  (Subshell does necessary plumbing, then execs 'grep')
Shell waits for subshells to exit.

The two subshells run independently. So, when ps gets to reading the
list of processes, the second subshell may not have been started yet, or
it may not have exec()ed yet (in which case it's command line probably
still looks like '-sh'). So, it's indeterminate whether the grep will
show up.

If you need to be sure, stick a 'grep -v grep' on the end of your pipeline.

-- 
Andrew.