Operating System - HP-UX
1830060 Members
2295 Online
109998 Solutions
New Discussion

Creating a daemon process

 
SOLVED
Go to solution
Stack
Occasional Advisor

Creating a daemon process

I am trying to create a daemon process using the setsid() system call. However, the call to setsid() is failing, giving me an error of "Not Owner" (via perror()).

Can anyone point me to the steps for creating a daemon process, using setsid(), invoked from C?

Thanks!

- Scott
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Creating a daemon process

I'm betting that you haven't fork()'ed.

e.g.

if ((pid = fork()) < 0)
{
(void) fprintf(stderr,"Can't fork %d\n",errno);
(void) fflush(stderr);
exit(errno);
}
if (pid != 0) /* parent process */ exit(0);
/* you are now the child */
setsid();
If it ain't broke, I can fix that.
Stack
Occasional Advisor

Re: Creating a daemon process

Right you are! Actually, I was forking, but got the parent & child reversed. Thanks for your help!
A. Clay Stephenson
Acclaimed Contributor

Re: Creating a daemon process

Let me give you one other option to consider: You might want to write your daemon in Perl. The code is very similar to what you would do in C includin fork() and setsid(). The development is much faster and the code execites almost as fast as C; it's also much more portable. Unless you are doing some extremely specialized tasks, I think you will find that Perl is an extremely good alternative - I've not had to write a daemon in C in over two years but I've probably written 20 or so in Perl.

I've attached an example; don't worry about what this does (it's the server piece of a daemon that handles event synchronization across hosts via socket connections) just look for the fork() and setsid() calls and note that it is very similar to what you do in C.

If it ain't broke, I can fix that.