For anyone who has never encountered Linux (or another Unix derivative), a daemon is the equivalent of a Windows service (and just in case... a service is a background application that is normally loaded and started during operating system startup).
Delphi provides ready made support for creating services, although the way it handles it implies that you should put all your code in the service application. This is fine and dandy if you can guarantee you are not going to want to do too much interactive debugging, but generally, it ends up being a nightmare. I'll cover creating services and daemons in a later article as there is quite a lot you can do to make your life easier.
So, for now, lets look at the process of daemonising a process with Kylix. A daemonised process is one that is running outside the confines of a user session. i.e. the process can be started and the user can log off without ending the process. Normally if you log off, the applications you started will be stopped, but a daemonised process is immune from this cull as it is no longer associated with you.
Sounds slightly complex. In actual fact, creating daemons with Kylix is in many respects easier than creating services with Delphi as it takes a single call to achieve. Time for a quick example.
Code:
libc.daemon(0,0);
With Windows services, Delphi provides you with events that are hooked up to catch service messages, the Stop message for example, allowing you to stop the service. With Kylix you have to create a set of signal handlers. The following code segment illustrates how to do this.
Code:
libc.signal(libc.SIGINT,@killDaemon); libc.signal(libc.SIGTERM,@killDaemon); libc.signal(libc.SIGKILL,@killDaemon);
That's the basics of daemonising a process. The next page provides a full set of example code that can be used to daemonize a process.