help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Backgrounded commands and wait


From: Chet Ramey
Subject: Re: [Help-bash] Backgrounded commands and wait
Date: Wed, 27 Mar 2013 20:06:53 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130216 Thunderbird/17.0.3

On 3/27/13 5:33 PM, DJ Mills wrote:
> When you fork a command with "&", but do not use "wait", will bash call
> wait(3) when the command terminates?

Yes.  Bash will get the SIGCHLD and reap the child.  Posix says it has to
keep CHILD_MAX exit statuses around for a possible later `wait'.  Bash-4.3
will check for pid reuse at fork time and `forget' the old exit status.

> In other words, will the PID possibly
> be reused before the shell exits?

Yes, though it would require unusual circumstances.  In most cases, it
would take a very long-running process on a very busy system to wrap around
the entire pid space.

> For example:
> some_command & pid=$!; { sleep 10; kill "$pid"; } &
> 
> If "some_command" completes before sleep, is it possible that the PID could
> be reused before "kill" executes? That could get quite nasty. I wouldn't
> think that would be the case because you could theoretically use ''wait
> "$pid"'' 10 lines later, and would still need to get the correct exit
> status. I'm just looking for confirmation, couldn't find it in the docs.

Yes, it is, in theory, possible for another process to create a process
with the same pid.  It's very unlikely on most common systems.  Bash won't
remember old exit statuses for pids it forks, but it's possible that
another process would create it.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]