Re: why does ps |grep sometimes not return itself?

2010-06-10 Thread Jonathan McKeown
On Thursday 10 June 2010 03:30:14 Pieter de Goeje wrote:
 On Wednesday 09 June 2010 09:34:40 Matthew Seaman wrote:
  On 09/06/2010 08:15:23, Eitan Adler wrote:
   Why do I sometimes see the grep in ps's output and sometimes not see
   it?
 
  When you run that pipeline the OS doesn't start both programs exactly
  simultaneously. [...]  It's a race condition.

 I would like to add that you can avoid the issue entirely by using this
 command:
 % ps aux -p `pgrep sh`
[output snipped due to bad wrapping]

Or the old trick:

ps | grep '[s]h'

Jonathan
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


why does ps |grep sometimes not return itself?

2010-06-09 Thread Eitan Adler
Why do I sometimes see the grep in ps's output and sometimes not see it?
[ei...@alphabeta ~ ]% ps aux|grep Me
eitan 96325  0.0  0.0  1856   724   5  RL+  10:14AM   0:00.00 grep Me
[ei...@alphabeta ~ ]% ps aux|grep Me
[ei...@alphabeta ~ !1! ]%

-- 
Eitan Adler
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: why does ps |grep sometimes not return itself?

2010-06-09 Thread Matthew Seaman
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 09/06/2010 08:15:23, Eitan Adler wrote:
 Why do I sometimes see the grep in ps's output and sometimes not see it?
 [ei...@alphabeta ~ ]% ps aux|grep Me
 eitan 96325  0.0  0.0  1856   724   5  RL+  10:14AM   0:00.00 grep Me
 [ei...@alphabeta ~ ]% ps aux|grep Me
 [ei...@alphabeta ~ !1! ]%
 

When you run that pipeline the OS doesn't start both programs exactly
simultaneously. It starts ps(1) first, then grep(1) together with
creating the pipeline by connecting ps's stdout to grep's stdin.
Depending on system load and various other factors, this may allow ps(1)
to grab the snapshot of the process table that it works on before
grep(1) has started.  It's a race condition.

Whether you see this effect or not will depend very much on system
conformation and load.  I can't reproduce the effect on a lightly loaded
dual processor machine, which always shows the grep process, whereas on
a single processor virtual machine running under VirtualBox, I never see
grep in the ps output unless I renice the ps(1) process.

Cheers

Matthew

- -- 
Dr Matthew J Seaman MA, D.Phil.   7 Priory Courtyard
  Flat 3
PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate
JID: matt...@infracaninophile.co.uk   Kent, CT11 9PW
-BEGIN PGP SIGNATURE-
Version: GnuPG/MacGPG2 v2.0.14 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkwPRBAACgkQ8Mjk52CukIxkWgCeNSQX37XoGvaZn2A/0vYrmUka
1yMAnjtZJoYNqOdo9UDkWks/4cYpVFPy
=Rkg3
-END PGP SIGNATURE-
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: why does ps |grep sometimes not return itself?

2010-06-09 Thread Pieter de Goeje
On Wednesday 09 June 2010 09:34:40 Matthew Seaman wrote:
 On 09/06/2010 08:15:23, Eitan Adler wrote:
  Why do I sometimes see the grep in ps's output and sometimes not see it?
  [ei...@alphabeta ~ ]% ps aux|grep Me
  eitan 96325  0.0  0.0  1856   724   5  RL+  10:14AM   0:00.00 grep Me
  [ei...@alphabeta ~ ]% ps aux|grep Me
  [ei...@alphabeta ~ !1! ]%

 When you run that pipeline the OS doesn't start both programs exactly
 simultaneously. It starts ps(1) first, then grep(1) together with
 creating the pipeline by connecting ps's stdout to grep's stdin.
 Depending on system load and various other factors, this may allow ps(1)
 to grab the snapshot of the process table that it works on before
 grep(1) has started.  It's a race condition.

 Whether you see this effect or not will depend very much on system
 conformation and load.  I can't reproduce the effect on a lightly loaded
 dual processor machine, which always shows the grep process, whereas on
 a single processor virtual machine running under VirtualBox, I never see
 grep in the ps output unless I renice the ps(1) process.


I would like to add that you can avoid the issue entirely by using this 
command:
% ps aux -p `pgrep sh`
USERPID %CPU %MEM   VSZ   RSS  TT  STAT STARTED  TIME COMMAND
root   1326  0.0  0.1  6680  3664  ??  Is1:09AM   0:00.00 /usr/sbin/sshd
pyotr  1460  0.0  0.1  3972  2696  v0  I 1:09AM   0:00.02 -zsh (zsh)

-- 
Pieter de Goeje
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org