We help IT Professionals succeed at work.

How to daemonize a C++ application

IMARTIN
IMARTIN asked
on
Medium Priority
976 Views
Last Modified: 2010-08-05
I am porting an application from W2K to AIX and should daemonize it. Which are the steps for making an application a daemon?
Comment
Watch Question

Commented:
This article will be useful:
http://www.cuj.com/articles/1999/9905/9905a/9905a.htm?topic=articles

To daemonize a program, you just have to register the PID with register_daemon(demo code in the article).  But that's only the start.  You'll have to respond to SIGs instead of messages, and you'll probably find a host of other incompatabilities in the source.  But if it's been written with the port in mind, it shouldn't be too bad (I've only taken apps the other way, though :-)
CERTIFIED EXPERT

Commented:
Note also that the startup/shutdown semantics are different from an NT service.  Un*x daemons are usually launched from a text startup script; the location and format of these varies across different OS implementations.  You'll want to compare against an existing daemon on AIX (sendmail, routed, ftpd, sshd are a few that might be running) to use as an example.

If you're not using it for production, or this is only for personal use, it's sufficient to drop it into /etc/rc.local, which is a script indicating customized startup stuff for the local machine.
You'll also want to fork yourself twice in order to escape from the  main process (the second fork gets rid of zombies).  Then, in the third and final child, call setsid() to become the session leader.

if(fork() != 0)
  exit(0);

if(fork() != 0)
  exit(0);

setsid();

Hope this helps.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.