|
Home > Archive > Unix Shell > September 2006 > PID of Background Function
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
PID of Background Function
|
|
| henrycortezwu@gmail.com 2006-09-23, 7:28 pm |
| Hi All,
How do you get the PID of a background function within that Function?
I know you can get the PID of a background function within the main
script via $!,
but for some coding reason, I really need to get it inside the called
bg function.
Thanks,
Henry 
| |
| Lasse Kliemann 2006-09-23, 7:28 pm |
| henrycortezwu@gmail.com <henrycortezwu@gmail.com> wrote:
> Hi All,
> How do you get the PID of a background function within that Function?
> I know you can get the PID of a background function within the main
> script via $!,
> but for some coding reason, I really need to get it inside the called
> bg function.
Don't know if I got you right, by maybe you mean »$$«, which
expands to the PID of the current process?
| |
| Chris F.A. Johnson 2006-09-23, 7:28 pm |
| On 2006-09-23, henrycortezwu@gmail.com wrote:
> Hi All,
> How do you get the PID of a background function within that Function?
> I know you can get the PID of a background function within the main
> script via $!,
> but for some coding reason, I really need to get it inside the called
> bg function.
If you mean the PID of the current process, it's $$.
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| henrycortezwu@gmail.com 2006-09-24, 1:34 am |
| Hi All,
Nopes not the $$.
Code Example:
[WarDemon@localhost ~]$ cat pidtest
function bgfunction {
echo "bgfunction: main pid: $$"
echo "bgfunction: last bgid: $!"
}
bgfunction &
echo "main script: main pid: $$"
echo "main script: last bgid: $!"
exit 0
Run Example:
[WarDemon@localhost ~]$ sh pidtest
bgfunction: main pid: 16608
bgfunction: last bgid:
main script: main pid: 16608
main script: last bgid: 16609
inside the function, the $!'s value does not show up.
I'm guessing there is a trick to this,...using exec??
Thanks,
Henry 
| |
| Spiros Bousbouras 2006-09-24, 7:31 am |
| henrycortezwu@gmail.com wrote:
> Hi All,
> Nopes not the $$.
>
> Code Example:
>
> [WarDemon@localhost ~]$ cat pidtest
> function bgfunction {
> echo "bgfunction: main pid: $$"
> echo "bgfunction: last bgid: $!"
> }
>
> bgfunction &
> echo "main script: main pid: $$"
> echo "main script: last bgid: $!"
>
> exit 0
>
> Run Example:
>
> [WarDemon@localhost ~]$ sh pidtest
> bgfunction: main pid: 16608
> bgfunction: last bgid:
> main script: main pid: 16608
> main script: last bgid: 16609
>
>
> inside the function, the $!'s value does not show up.
> I'm guessing there is a trick to this,...using exec??
Hmmm , I wonder if there is a bug involved. I'm
not surprised that $! does not have a value inside
the function because the function did not start any
background processes. What I do find surprising is
that the main script gives the process ID of the function
as 16609 but the function gives the process ID of itself
as 16608 which is actually the PID of its parent shell.
| |
| Stephane CHAZELAS 2006-09-24, 7:31 am |
| 2006-09-23, 18:07(-04), Chris F.A. Johnson:
> On 2006-09-23, henrycortezwu@gmail.com wrote:
>
> If you mean the PID of the current process, it's $$.
$$ is the pid of the shell reading the script which is not
necessarily the pid of the current process.
pid=$(($(exec sh -c 'ps -o ppid= -p "$$"')))
might give you the pid of the shell that executes the pid=...
assignment.
--
Stéphane
| |
| Lasse Kliemann 2006-09-24, 7:31 am |
| Spiros Bousbouras <spibou@gmail.com> wrote:
> henrycortezwu@gmail.com wrote:
>
[...][vbcol=seagreen]
> Hmmm , I wonder if there is a bug involved. I'm
> not surprised that $! does not have a value inside
> the function because the function did not start any
> background processes. What I do find surprising is
> that the main script gives the process ID of the function
> as 16609 but the function gives the process ID of itself
> as 16608 which is actually the PID of its parent shell.
I think that this is documented (man sh on NetBSD):
| $ Expands to the process ID of the invoked shell. A subshell
| retains the same value of $ as its parent.
man ksh (for the pdksh) shows a similar statement. However, I am not
sure what the exact definition of 'subshell' is.
| |
| Spiros Bousbouras 2006-09-24, 7:31 am |
| Lasse Kliemann wrote:
> Spiros Bousbouras <spibou@gmail.com> wrote:
>
> [...]
>
> I think that this is documented (man sh on NetBSD):
>
> | $ Expands to the process ID of the invoked shell. A subshell
> | retains the same value of $ as its parent.
Thanks for that. Now that I knew what to look for I saw that the
man page for Bash on Linux says the same thing. Does this mean
then that a subshell has no built-in way of knowing its process ID ?
If yes then it's a design bug.
> man ksh (for the pdksh) shows a similar statement. However, I am not
> sure what the exact definition of 'subshell' is.
Subshell is a subprocess of a shell which is also a shell.
| |
| Spiros Bousbouras 2006-09-24, 1:21 pm |
| Spiros Bousbouras wrote:
> Lasse Kliemann wrote:
>
> Subshell is a subprocess of a shell which is also a shell.
Come to think of it that's not enough. Perhaps it also
needs to run on the background.
| |
| Chris Mattern 2006-09-24, 1:21 pm |
| In article <1159068633.386399.125640@i42g2000cwa.googlegroups.com>,
henrycortezwu@gmail.com wrote:
>Hi All,
> Nopes not the $$.
>
>Code Example:
>
>[WarDemon@localhost ~]$ cat pidtest
>function bgfunction {
>echo "bgfunction: main pid: $$"
>echo "bgfunction: last bgid: $!"
>}
>
>bgfunction &
>echo "main script: main pid: $$"
>echo "main script: last bgid: $!"
>
>exit 0
>
>Run Example:
>
>[WarDemon@localhost ~]$ sh pidtest
>bgfunction: main pid: 16608
>bgfunction: last bgid:
>main script: main pid: 16608
>main script: last bgid: 16609
>
>
>inside the function, the $!'s value does not show up.
>I'm guessing there is a trick to this,...using exec??
>
>Thanks,
>Henry 
>
OK, I'm going to assume you're on Linux here, and therefore
actually running bash, since sh does not have functions.
Your problem is that functions inherit all their environment
variables from the parent, even those that are not exported.
So function bgfunction inherits $$=16608 from the main script,
even though it is running under PID 16609. I'm not sure
of an easy way to get around it--you could write a C program
to call getppid; when called from the function, the C program's
ppid would be the function's pid...
--
Christopher Mattern
"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
| |
| Chris F.A. Johnson 2006-09-24, 1:21 pm |
| On 2006-09-24, Chris Mattern wrote:
>
> OK, I'm going to assume you're on Linux here, and therefore
> actually running bash, since sh does not have functions.
sh has had functions since SVR2 (1984).
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Spiros Bousbouras 2006-09-24, 1:21 pm |
| Chris Mattern wrote:
> OK, I'm going to assume you're on Linux here, and therefore
> actually running bash, since sh does not have functions.
>
sh does have functions. And bash also runs on Solaris.
| |
| Stephane CHAZELAS 2006-09-24, 1:21 pm |
| 2006-09-24, 15:49(-00), Chris Mattern:
[...]
> OK, I'm going to assume you're on Linux here, and therefore
> actually running bash, since sh does not have functions.
???
You must be confusing with csh. Both the Unix shell and the
Bourne shell have functions. The Bourne shell has had functions
since SVR2 (1984). The Unix specification of sh has functions as
a requirement (note that the Bourne shell is not a Unix
conformant sh, it's an old shell found sometimes on some Unices
for backward compatibility).
> Your problem is that functions inherit all their environment
> variables from the parent, even those that are not exported.
Calling a function doesn't exec anything, and doesn't even fork.
So there's no parent/child relationship.
The issue is about subshells, not really about functions.
--
Stéphane
| |
| Lasse Kliemann 2006-09-24, 1:21 pm |
| Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
> On 2006-09-24, Chris Mattern wrote:
>
> sh has had functions since SVR2 (1984).
>
He most likely meant that they are not defined by using the keyword
'function' in sh.
| |
| Barry Margolin 2006-09-24, 7:40 pm |
| In article <slrnehdc5c.7np.stephane.chazelas@spam.is.invalid>,
Stephane CHAZELAS <this.address@is.invalid> wrote:
> 2006-09-24, 15:49(-00), Chris Mattern:
> [...]
>
> ???
>
> You must be confusing with csh. Both the Unix shell and the
> Bourne shell have functions. The Bourne shell has had functions
> since SVR2 (1984). The Unix specification of sh has functions as
> a requirement (note that the Bourne shell is not a Unix
> conformant sh, it's an old shell found sometimes on some Unices
> for backward compatibility).
>
>
> Calling a function doesn't exec anything, and doesn't even fork.
Unless you call the function in the background, which is the subject of
the thread.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Stephane CHAZELAS 2006-09-24, 7:40 pm |
| 2006-09-24, 15:51(-04), Barry Margolin:
[...]
>
> Unless you call the function in the background, which is the subject of
> the thread.
But that's not the calling of the function that puts it in
background, that's the &.
& forces the fork, so does (...).
But forks don't affect the memory, the memory content and thus
every variable is the same in the parent and the child (at the
time of the fork, after which they diverge).
It's exec that modifies the memory (and exec doesn't involve
parent-children relationship). Only the environment variables
(the exported shell variables in shell terminology) are
preserved accross execs (passed as the third argument of the
execve system call).
$$ is a shell variable (and can't be put in the environment by
the shell) set at shell startup and only at that time, so has no
reason to change accross forks.
Of course, one shell implementation could define a "dynamic"
variable that could reflect the pid of the current process, but
that wouldn't necessarily make sense and could be subject to
interpretations.
For instance, in
echo $pid
There will be a fork if "echo" is not built in (or has been
aliased to /bin/echo). Should that output the pid of echo (of
the subshell that execs echo) or the pid of the subshell that
interprets the "echo $pid" command line?
process handling is meant to be mostly transparent to the user.
You don't always know how and when the shell forks. Especially
with modern shells where optimisations are made to avoid forks
as much as possible.
--
Stéphane
| |
| Chris Mattern 2006-09-24, 7:40 pm |
| Reply-To: matternc@comcast.net
User-Agent: slrn/0.9.6.2 (SunOS)
X-Complaints-To: abuse@supernews.com
Lines: 20
Xref: number1.nntp.dca.giganews.com comp.unix.shell:188621
In article <1159114613.362804.26660@i3g2000cwc.googlegroups.com>,
Spiros Bousbouras wrote:
>Chris Mattern wrote:
>
>
>sh does have functions. And bash also runs on Solaris.
>
I meant, you don't call it by saying "function". And when bash
runs on Solaris, your don't call it with /bin/sh.
--
Christopher Mattern
"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
| |
| henrycortezwu@gmail.com 2006-09-25, 1:52 am |
| Hi All,
Thanks for your help. It led me clues on where to focus my research
on.
I found the solution with the following code.
cat pidtest
------------------------
#!/bin/ksh
function bgfunction {
echo "bgfunction: main pid: $$"
echo "bgfunction: last bgid1: $(perl -e 'print getppid();')"
echo "bgfunction: last bgid2: $(sh -c 'echo $PPID')"
exit 0
}
bgfunction &
echo "main script: main pid: $$"
echo "main script: last bgid: $!"
sleep 1
exit 0
pidtest
------------------------
main script: main pid: 26497
main script: last bgid: 26498
bgfunction: main pid: 26497
bgfunction: last bgid1: 26498
bgfunction: last bgid2: 26498
sleep 1 there is important,..or it will look funny, it will exit
immediately,..then print results in a "late manner"
with the resulting bgid numbers,..I believe the code is correct? 
Thanks,
Henry 
| |
| Geoff Clare 2006-09-25, 8:15 am |
| Stephane CHAZELAS <this.address@is.invalid> wrote, on Sun, 24 Sep 2006:
> pid=$(($(exec sh -c 'ps -o ppid= -p "$$"')))
>
> might give you the pid of the shell that executes the pid=...
> assignment.
Why use ps when the exec'd shell can tell you its parent's pid:
... $(exec sh -c 'echo $PPID') ...
--
Geoff Clare <netnews@gclare.org.uk>
| |
| Stephane Chazelas 2006-09-25, 1:27 pm |
| On Mon, 25 Sep 2006 13:51:42 +0100, Geoff Clare wrote:
> Stephane CHAZELAS <this.address@is.invalid> wrote, on Sun, 24 Sep 2006:
>
>
> Why use ps when the exec'd shell can tell you its parent's pid:
>
> ... $(exec sh -c 'echo $PPID') ...
Indeed. I had assumed $PPID was not standard while actually it
was. Thanks for pointing it out.
--
Stephane
|
|
|
|
|