Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,
How do i do to create a “daemon” process? Furthermore, this daemon process should create 3 new processes.(In C language)
I've already read some articles about it, but none of them have some useful source code example.
Can you help me?
Tks
Posted

The actual rules for UNIX are a little complex, and it is some time since I have done this myself. As far as I recall, the main program should start by forking, and the parent should then terminate. The child process then needs to alter some settings, something like:
C
chdir("/");    // switch away from user directory
setsid();      // become session leader
umask(0);      // clear file creation mask

This process now becomes the session leader and should fork the other children which will communicate back to it.

I suggest you check the man pages for the three commands above to verify that my suggestion is correct.

If you do not already, I would suggest buying a copy of W. Richard Stevens's[^] book Advanced Programming in the UNIX Environment. It contains excellent examples of subjects such as this.
 
Share this answer
 
v2
 
Share this answer
 
claudiatita wrote:
How do i do to create a “daemon” process?


It depends on what platform, and what you expect it to do. On most modern operating systems user created daemons have to follow some fairly complex rules in order not to disrupt the system. Perhaps you could clarify what it is you hope to achieve.
 
Share this answer
 
Well, i'm studying unix process structure, i.e, how to create and manage processes in Unix, using C.

I know how to use function fork() to create a child, however, to change the "parent" to a deamon process it is not clear to me how to put that in C code.

I need to daemonize a process that will create 3 other processes, each one will be responsible for executing different actions (that comunicate to their parent using unnamed pipes).

Something like this:
(...)
for (i = 0; i < 3; i++) {

       pid_t id = fork();

       if (id == 0) {

           switch (i) {
           case 0: //child 1 do some stuff
           case 1: //child 2 do something else
           case 2: //child 3 ... whatever...
           }
else {
      //parent stuff ...
        }
}//close for

But this code is only for a "normal" process...

Question one: first of all, i create the childs first and after that will change a parent process to a deamon one?

Question two: how?

Tks in advance,
Claudia
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900