Can I Get The PID Of The Program That Launched My Program?

H

Hal Vaughan

I'm working on Linux. I have some Perl programs that launch a program by
just putting the command line in backticks. So if program A launches
program B this way, is there any way for program B to get the PID of
program A?

Thanks!

Hal
 
H

Hal Vaughan

jim said:
Linux provide getppid(2) which gets the parent process ID. Perl supports
getppid().

Great! That's exactly what I need! (It also saves the pain of adding
functions to make launching programs add an argument like
"--launchpid=$pid" as well as reading it in the launched program!)

Thanks.

Hal
 
T

Tad McClellan

Linux provide getppid(2) which gets the parent process ID. Perl supports
getppid().


Which will probably return the PID of the shell that the
backticks started rather than the PID of program A.
 
H

Hal Vaughan

Tad said:
Which will probably return the PID of the shell that the
backticks started rather than the PID of program A.

I tested it. When I start the program from a console window, it gives me
the PID of the console. When I start program B from program A with
backticks, it gives me the PID of program A.

I did notice, though, that the PID for program B was always at least 2
higher than program A. If A's PID was 2257, B's was 2259.

Hal
 
H

Hal Vaughan

Chris said:
This behaviour is not something you can rely on. Sometimes the PID of B
will be less than that of A, and sometimes the gap will be greater or
smaller.

I'm not counting on that. Actually, in some tests I ran, B was 5-8 PID
numbers higher than A, which I figure was because of other programs working
in the background, for example, I had several loops running in other bash
shells that would reprint info every X seconds. And, yes, if A has a high
enough PID, then B's will be back at the bottom of the list.

My implication was that there was at least one process that was launched and
given a PID between A and B.

Hal

P.S. What I'm more concerned is that, after months of overwork, my brain is
so fried that when I pick a random number out of my head, it turns out to
be an obscure science fiction reference connected to Saucer C57D. Am I so
exhausted my brain is retreating into all the old sf films I watched
growing up? -- and even more so is that I notice it when re-reading my
post!
 
W

Willie Wilson

Chris said:
This behaviour is not something you can rely on. Sometimes the PID of B
will be less than that of A, and sometimes the gap will be greater or
smaller.

This behavior is especially unreliable because you never know if the
user will be using something like grsecurity (or SELinux), where PIDs
can be randomized, and not predictable. :)

-Willie
 
N

Naveen

Well.... following this thread, I had a query. Thought to post it here
instead in a new one. I am running Parallel Fork Manager
(http://search.cpan.org/~dlux/Parallel-ForkManager-0.7.5/ForkManager.pm
) to implement forking. I want to know the PID of the child process
instead of the Parent process. If A.pl is run and it fires B.pl, i want
to know the PID of B.pl. This thing could go on for thousands of
parallel processes, so i would like to know how to go about it?
 
J

Joe Smith

Hal said:
I tested it. When I start the program from a console window, it gives me
the PID of the console. When I start program B from program A with
backticks, it gives me the PID of program A.

Sounds like you didn't properly test the two types of backquote
environments.

linux% perl -le '$_=`./test.pl`; chomp; print "[$_] $$"'
[18382 invoked by 18381] 18381
linux% perl -le '$_=`./test.pl </dev/null`; chomp; print "[$_] $$"'
[18385 invoked by 18384] 18383

No shell metacharacters: getppid() = immediate parent.
With shell metacharacters: getppid() = pid of /bin/sh, not perl!
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top