Re: cocoa-dev digest, Vol 1 #731 - 16 msgs
Re: cocoa-dev digest, Vol 1 #731 - 16 msgs
- Subject: Re: cocoa-dev digest, Vol 1 #731 - 16 msgs
- From: Brian Hill <email@hidden>
- Date: Wed, 17 Oct 2001 18:39:08 -0500
On Wednesday, October 17, 2001, at 05:20 PM, Jason Bobier wrote:
I wonder why our loop gets shutdown then. Does NSDefaultRunLoopMode
automatically connect to the WindowServer?
It probably has to do with who the 'parent process' of your server is.
If you start a CLI application via Terminal, for example, even if it has
no run loop or GUI, it's parent process is Terminal. When Terminal wants
to quit, it will kill off all of it's child processes.
If you want to do the equivalent of 'nohupping' your sub-process
programmatically, you need to 'divorce' it from it's controlling TTY, so
that it doesn't get killed when it's parent process wants to quit.
Here's one incantation to do this:
BOOL daemonizeProcess()
{
BOOL isDaemon = NO;
pid_t pid;
chdir("/"); //give up any claims on the user's home dir, for example
freopen("/dev/null","r", stdin); //plug up stdin
freopen("/dev/console","a", stdout); //switch stdout to something
that can stay open
freopen("/dev/console","a", stderr); //switch stderr to something
that can stay open
switch(pid = fork())
{
case 0:
isDaemon = YES;
setsid(); //this is the important step to 'detach' from the
parent process
break;
default:
isDaemon = NO;
return isDaemon;
break;
}
return isDaemon;
}
int main (int argc, const char *argv[])
{
if(!daemonizeProcess())
{
sleep(1);
exit(0);
}
//else we're a daemon now
//start runloop, etc...
}
Brian
email@hidden
http://personalpages.tds.net/~brian_hill
"Why? I came into this game for adventure - go anywhere, travel
light, get in, get out, wherever there's trouble, a man alone.
Now they've got the whole country sectioned off and you can't
move without a form. I'm the last of a breed."
-- Archibald "Harry" Tuttle, Rogue HVAC Repairman