Welcome to MacForumz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

daemon(0, 0) incrementing process ID

 
   Macintosh computer (Home) -> General Discussion RSS
Next:  StringToDate question  
Author Message
Heath Raftery

External


Since: Jul 10, 2003
Posts: 290



(Msg. 1) Posted: Fri Jan 16, 2004 7:06 am
Post subject: daemon(0, 0) incrementing process ID
Archived from groups: comp>sys>mac>programmer>misc (more info?)

I came across this annoyance some time ago, but it has just bit me again in
the last couple of days. I have a Cocoa application which starts a C daemon
using NSTask. I need to keep track of the daemon, and getting its PID is
fairly important. Unfortunately I would always get an off by one error with
this code:

NSTask *daemon = [NSTask launchedTaskWithLaunchPath:daemonPath
arguments:[self getDaemonStartArgs]];
daemonPID = [daemon processIdentifier];

So I wrote this test app:

#include <stdio.h>
#include <unistd.h> // fork()
#include <sys/types.h>

int main(int argc, char* argv[])
{
printf("PID is: %d\n", getpid());

daemon(0, 0);

FILE *outFile = fopen("/Users/liteyear/daemonPIDoutput", "w");
fprintf(outFile, "PID is: %d", getpid());

return 0;
}

And sure enough, the PID in daemonPIDoutput was always one higher than the
one printed to stdout. I searched for confirmation of this, but couldn't
find documentation to that effect. Nonetheless, it was very repeatable, so
I just added 1 to the [daemon processIdentifier] value. I was worried
however, about the PID looping or skipping values, and sure enough, in
Panther I came across a big problem. Just after my daemon started,
fix_prebinding would often start, meaning the PID the daemon ends up is 2
higher than that reported by -processIdentifier.

I tried delaying the call to -processIdentifier, or speeding up the call to
daemon, neither of which would be very robust, and neither of which
actually worked.

In the end, I made the daemon write its PID to a file (in /tmp because
non-root apps can't write to /var/run) just after it becomes a daemon. The
Cocoa application then checks the status of this file every second or so.

I'm after confirmation that the call to daemon gets a new PID, and
suggestions on how to work around this.

Cheers!
--
*--------------------------------------------------------*
| ^Nothing is foolproof to a sufficiently talented fool^ |
| Heath Raftery, HRSoftWorks _\|/_ |
*______________________________________m_('.')_m_________*

 >> Stay informed about: daemon(0, 0) incrementing process ID 
Back to top
Login to vote
Eric Albert1

External


Since: Jan 09, 2004
Posts: 408



(Msg. 2) Posted: Fri Jan 16, 2004 7:06 am
Post subject: Re: daemon(0, 0) incrementing process ID [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <bu7nvj$9n$2@seagoon.newcastle.edu.au>,
Heath Raftery <hraftery.TakeThisOut@myrealbox.com> wrote:

 > So I wrote this test app:
 >
 > #include <stdio.h>
 > #include <unistd.h> // fork()
 > #include <sys/types.h>
 >
 > int main(int argc, char* argv[])
 > {
 > printf("PID is: %d\n", getpid());
 >
 > daemon(0, 0);
 >
 > FILE *outFile = fopen("/Users/liteyear/daemonPIDoutput", "w");
 > fprintf(outFile, "PID is: %d", getpid());
 >
 > return 0;
 > }
 >
 > And sure enough, the PID in daemonPIDoutput was always one higher than the
 > one printed to stdout. I searched for confirmation of this, but couldn't
 > find documentation to that effect.

 > I'm after confirmation that the call to daemon gets a new PID, and
 > suggestions on how to work around this.

You can find the source code for the daemon() implementation in Panther
at
<http://www.opensource.apple.com/darwinsource/10.3/Libc-320/gen/FreeBSD/d
aemon.c>.
If you look at that, you'll see that it calls fork(). That'll give the
process a new PID. There's no way to work around this. I don't think
there's a way to get the new PID, either, other than the file-writing
trick you mentioned.

Hope this helps a bit,
Eric

--
Eric Albert ejalbert.TakeThisOut@cs.stanford.edu
<a style='text-decoration: underline;' href="http://rescomp.stanford.edu/~ejalbert/" target="_blank">http://rescomp.stanford.edu/~ejalbert/</a><!-- ~MESSAGE_AFTER~ -->

 >> Stay informed about: daemon(0, 0) incrementing process ID 
Back to top
Login to vote
Sean McBride

External


Since: Jan 05, 2004
Posts: 498



(Msg. 3) Posted: Fri Jan 16, 2004 3:09 pm
Post subject: Re: daemon(0, 0) incrementing process ID [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <bu7nvj$9n$2@seagoon.newcastle.edu.au>,
Heath Raftery <hraftery.RemoveThis@myrealbox.com> wrote:

 > I came across this annoyance some time ago, but it has just bit me again in
 > the last couple of days. I have a Cocoa application which starts a C daemon
 > using NSTask. I need to keep track of the daemon, and getting its PID is
 > fairly important. Unfortunately I would always get an off by one error with
 > this code:
 >
 > NSTask *daemon = [NSTask launchedTaskWithLaunchPath:daemonPath
 > arguments:[self getDaemonStartArgs]];
 > daemonPID = [daemon processIdentifier];
 >
 > So I wrote this test app:
 >
 > #include <stdio.h>
 > #include <unistd.h> // fork()
 > #include <sys/types.h>
 >
 > int main(int argc, char* argv[])
 > {
 > printf("PID is: %d\n", getpid());
 >
 > daemon(0, 0);
 >
 > FILE *outFile = fopen("/Users/liteyear/daemonPIDoutput", "w");
 > fprintf(outFile, "PID is: %d", getpid());
 >
 > return 0;
 > }

daemon() calls fork(), so your results are expected. Alas, there is no
man page for daemon, but you can look at its source from the Darwin
project. You might also check the Darwin list for discussion about
daemon/fork and then using 'high level' APIs afterwards, it might apply
to you also. The subject of the thread was "open fails in daemon".

Why are you calling daemon()?

 > And sure enough, the PID in daemonPIDoutput was always one higher than the
 > one printed to stdout.

As you have seen, you should not rely on that.<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: daemon(0, 0) incrementing process ID 
Back to top
Login to vote
Heath Raftery

External


Since: Jul 10, 2003
Posts: 290



(Msg. 4) Posted: Tue Jan 20, 2004 1:21 am
Post subject: Re: daemon(0, 0) incrementing process ID [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Eric Albert <ejalbert.DeleteThis@cs.stanford.edu> wrote:
 > In article <bu7nvj$9n$2@seagoon.newcastle.edu.au>,
 > Heath Raftery <hraftery.DeleteThis@myrealbox.com> wrote:

  >> So I wrote this test app:
  >>
  >> #include <stdio.h>
  >> #include <unistd.h> // fork()
  >> #include <sys/types.h>
  >>
  >> int main(int argc, char* argv[])
  >> {
  >> printf("PID is: %d\n", getpid());
  >>
  >> daemon(0, 0);
  >>
  >> FILE *outFile = fopen("/Users/liteyear/daemonPIDoutput", "w");
  >> fprintf(outFile, "PID is: %d", getpid());
  >>
  >> return 0;
  >> }
  >>
  >> And sure enough, the PID in daemonPIDoutput was always one higher than the
  >> one printed to stdout. I searched for confirmation of this, but couldn't
  >> find documentation to that effect.

  >> I'm after confirmation that the call to daemon gets a new PID, and
  >> suggestions on how to work around this.

 > You can find the source code for the daemon() implementation in Panther
 > at
 > <http://www.opensource.apple.com/darwinsource/10.3/Libc-320/gen/FreeBSD/d
 > aemon.c>.
 > If you look at that, you'll see that it calls fork(). That'll give the
 > process a new PID.

Ah! I thought it was behaving a little like fork. I've only
recently become used to the idea that we can often look at the
source of library functions we use. It is often a very rewarding
experience. Good link.

 > Hope this helps a bit,

It does, thank you.
--
*--------------------------------------------------------*
| ^Nothing is foolproof to a sufficiently talented fool^ |
| Heath Raftery, HRSoftWorks _\|/_ |
*______________________________________m_('.')_m_________*<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: daemon(0, 0) incrementing process ID 
Back to top
Login to vote
Display posts from previous:   
   Macintosh computer (Home) -> General Discussion All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]