creating a daemon

This is a discussion on creating a daemon within the Linux Programming forums, part of the Platform Specific Boards category; Hi i want to create a programm that "forks" itself into the background. If i use fork and exit the ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    17

    creating a daemon

    Hi

    i want to create a programm that "forks" itself into the background. If i use fork and exit the main process the child process are all terminated. So has anybody help?

  2. #2
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >So has anybody help?
    It's pretty simple, really:
    Code:
    if ((child = fork()) == -1) {
      perror("Forked failed");
      return 1;
    }
    if (child == 0) {
      if (setsid() == -1)
        perror("Set to session leader failed");
      else {
        /* Do what daemons do, hide and toil */
      }
    
      return 1; /* Never returns */
    }
    
    return 0; /* Parent goes *poof!* */
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    I remember we had to make a simple shell last year, and one of the features of the shell was do have a command where you could create a daemon on a terminal. For example, daemon ./program would make ./program a daemon process. Our professor told us that we should fork() twice and then kill the grand parent. I never really figured out how that worked. Can some tell me how that would work?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Forking twice helps to prevent the daemon from having an acquiring terminal. This can only happen under rules, and therefore isn't always necessary.

    Check "Advanced Programming in the
    UNIX Environment" by W. Richard Stevens", or google, where you're sure to find more information.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    So the solution that twm gave still has a terminal attached to a process?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Yasir_Malik
    So the solution that twm gave still has a terminal attached to a process?
    This is what google gave me, it has a quote from Steven's book. I don't have that book to hand, so I can't guarantee it's accurate.
    http://dbforums.com/archives/t316468.html
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    3

    Question Related questions about daemons...

    Hi,
    I experienced some problems while porting some programs from Solaris to Linux. They are all calling fork() after being started so they run in daemon mode. I also have a debug mode that just skip the daemonize function that I wrote. (this function actually just call the fork method and make the parent process to exit normally and the child process to continu the execution of the program).

    My problem is that while it run in daemon mode, it exit after a while without any warning/error/log message/etc... so I really don't know what is happening. I tested for days in debug mode and it works just fine and could run for months without problem.

    So, I guess that it's all coming from the dameonize function that is actually calling fork(). I have few question that may answer and solve my problem:

    1- Normally on Solaris, fork() duplicate the overall memory space of a process, is it the same under Linux or should I make alll my dynamic memory allocation after the fork() call?

    2-What is the difference with the other fork() function: vfork, and so on... ? Is one of them more appropriate for what I'm doing?

    3-Any other thought/suggestions about this?

    Thank you very much!
    Twisgabak.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Daemon process debugger
    By karthigayan in forum C Programming
    Replies: 2
    Last Post: 03-31-2009, 12:28 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Writing a linux (centos 4) c++ daemon that interacts with PHP
    By misterdanny in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2007, 06:48 AM
  4. Daemon problem
    By gandalf_bar in forum Linux Programming
    Replies: 3
    Last Post: 07-20-2004, 06:23 AM
  5. Help using unix socket for daemon
    By Tal0n in forum Linux Programming
    Replies: 1
    Last Post: 01-13-2004, 11:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21