Le 24/06/2013 16:31, David Korn a écrit :
cc: [email protected]
Subject: Re: Re: [ast-users] Trouble with getting the child process pid ("$!")
in ksh93 ...
--------
On Mon, Oct 23, 2006 at 10:23 PM, David Korn <[email protected]> wrote:
cc: [email protected]
Subject: Re: [ast-users] Trouble with getting the child process pid ("$!") in
ksh93 ...
--------
While working on another project I hit a problem with obtaining the
process ID of the current child process (I'd like to "renice" it to a
lower priority (however in a different way than the "bgnice" option
does)).
Example:
-- snip --
$ (ksh93 -c 'px() { echo "p1=${$} p2=${!}" ; } ; px ; ( px ) &')
p1=27961 p2=
p1=27961 p2=
-- snip --
The output of $$ is expected... but how can I get the process ID of the
current child process ?
For comparisation "bash" returns the current background process ID in $!
_within_ the child:
-- snip --
$ (bash -c 'px() { echo "p1=${$} p2=${!}" ; } ; px ; ( px ) &')
p1=27964 p2=
p1=27964 p2=27965
-- snip --
I am not sure whether this behaviour is POSIX conformant... but it is
IMO usefull...
... any ideas/suggestions/rants/etc. welcome...
The way $! is defined is that it is the pid of the last background
process or coprocess. When a subshell starts, there is no
most recent background job so there is not pid.
However, the standard isn't clear as to what the value of $! is
when there was no background jobs executed. Perhaps initializing
it to the current pid makes sense.
I will post this onto the austin group list and see whether
anyone thinks that it would violate the standard.
ping! ... has there been any feedback yet ? I just stumbled over the
issue (again) while trying to craft a realtime signal test which nests
child processes like a "Matryoshka doll" (e.g. see
http://en.wikipedia.org/wiki/Matryoshka_doll) ...
... the issue I ran into is that $$ always refers to the base shell
process and AFAIK there no easy way to pass the pid of the current
shell child process to it's child processes...
I have it on my list. I am not sure which shell is correct.
I ran strace of bash on
======cut here======
px() { echo "p1=${$} p2=${!}" ; } ; ( px ) &
======cut here======
14896 execve("/usr/common/bin/bash", ["bash", "./bug228"], [/* 75 vars */]) = 0
...
14896 clone(child_stack=0,
flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,
child_tidptr=0x7f79be0bc9d0) = 14897
...
14897 write(1, "p1=14896 p2=\n", 13) = 13
14897 exit_group(0) = ?
So how can $! be 14896 in process 14897 since 14896 is a parent of 14897,
not a child and $! is supposed to be a child process.
This one is still on my bug list as unresolved.
David Korn
[email protected]
Hi,
even if the idea seems good, I'm not sure about portability ?!
imagine the following situation :
some process &
(sleep 60; kill $!) &
wait
$! should point to "some process" and not the current process inside ()
IMHO, it is much better to have a new shell variable such as $= or
whatever (.sh.child)
AFAIK, the better and actual way to get the current pid inside a child is :
ksh -c '(pid=$(exec ksh -c "echo \$PPID"); echo $pid) & wait; echo $!'
I am wrong ?
Regards,
Cyrille Lefevre
--
mailto:[email protected]
_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users