Re: Is getpid supposed to return PID-1?
Re: Is getpid supposed to return PID-1?
- Subject: Re: Is getpid supposed to return PID-1?
- From: email@hidden
- Date: Tue, 13 Nov 2001 16:15:16 -0800
On Tuesday, November 13, 2001, at 06:29 , Kevin Bohan wrote:
Hi,
I have a BSD question and I apologise if this is not the right list, as
this is not really my area. However I have a daemon that writes its PID
to an external file when it starts up and I have a script that looks
for this file, extracts the PID in order to provide a commandline
interface that allows the process to be killed.
However, when I call getpid to write the PID to the text file it always
seems to write a value that is 1 less than the PID shown when I use ps
to view the process in question. Also if I call kill on the value
returned by getpid it tells me that the process doesn't exist. It would
seem that ps is right an getpid is wrong.
I can obviously just add one to the value returned by getpid, but is
this safe/future-proof? Is this a known issue? What should I do?
'getpid()' is not likely to be wrong, so the smart money would bet that
there's a "bug" in your code somewhere. At a guess, what is happening
is that you call getpid() early in the process's existance, and that
sometime after that, your code does a 'fork', with the parent (which is
the process that called 'getpid()') exiting. Then the child continues
on as the daemon (it's pid is different than the result of the earlier
'getpid()'). This is fairly standard for daemons, and is, in fact, what
the daemon(3) call does.
Regards,
Justin
PS: this is definitely the correct list.
---
Justin C. Walker, Curmudgeon-At-Large *
Institute for General Semantics | When LuteFisk is outlawed
| Only outlaws will have
| LuteFisk
*--------------------------------------*-------------------------------*