daemons
Hi all.
What do I need to do to have a program invoke as a daemon; i.e sit and ait for a particular event to happen, and then do something, all in the background?
Thanks,
Ameer
Quoted 3 lines · ameer armaly
What do I need to do to have a program invoke as a daemon; i.e sit and ait for a particular event to happen, and then do something, all in the background?
The topic of daemon processes gets its own chapter in Stevens[1], but the short version is:
1. Call fork(), and have the parent exit.
2. Call setsid() to create a new session and disconnect from the controlling terminal.
3. Call chdir() to change the current directory to the daemon's working directory, or the root directory if it doesn't have one.
4. Call umask(0) to reset the file permissions mask.
5. Close all unused file descriptors.
[1] Advanced Programming in the Unix(R) Environment W Richard Stevens
Addison-Wesley
Depending upon what the daemon does, other useful steps may include:
6. Reset the environment to a fixed set of values.
7. Set up descriptors 0, 1 and 2 (use /dev/null if nothing else is appropriate).
8. Reset other aspects of the process' environment which will have been inherited, e.g. resource limits.
Signature · 1 line
Glynn Clements <[email protected]>
On Sat, 14 Feb 2004 23:14:45 -0500 (EST)
ameer armaly <[email protected]> wrote:
So, You can do it =)
You must call the fork(),close all enviroment and so all =) some like this
//closing the enviroment
int pid;
if((pid=fork())==0)
{
for(;;)
{
// something useful
}
}
so, it's all
---===---
Alphex Kaanoken
Senior developer of Crew IT research labs
web: http://crew.org.ru
mailto:[email protected]