Thread: linux daemon design

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    linux daemon design

    hi,

    does anyone know of any good daemon design resources for linux.

    i'm trying to find out if there are standard methods or templates available.

    TIA, rotis23

  2. #2
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    I don't think there's any special method for writing a daemon, it depends on what you want it to do.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The best way, I believe, is to fork immediately, then set your process group/create your own session, and your away.

    Something like
    Code:
        switch (pid = fork())
        {      
            case -1:    
                perror("fork");
                return (EXIT_FAILURE);
                break;
            case 0:
                sleep(1);
                if( setsid() == -1 )
                {
                    perror("setsid");
                }
                printf("Daemon started");
                break;
            default:
                exit(EXIT_SUCCESS);
                break;
        }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    thanks chaps,

    the daemon needs to do some processing every 5 minutes.

    so do some processsing and sleep for x amount of time.

    i'm really interested in how the daemon announces itself to the linux environment (start-up,allocation of pid etc) and how the linux environment interacts with the daemon (signals, kill etc, greaceful termination).

    thanks Hammer, but their must be some linux docs on how this should be done properly.

    TIA, rotis23

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    ok, just to inform on my research.

    the best way to construct a daemon is as follows. if there are any errors, please inform:

    1) fork and setsid (as per Hammer) - hammer, could you tell me why its important to fork - i can't find any info on this?

    2) set going in a loop, or listening for events

    3) add capability to handle signals, SIGTERM, SIGINT (also good to handle SIGSEGV). When these signals are made, the daemon can shutdown gracefully - close db connections, write log etc.

    4) init scripts can then be created using:

    - daemon process (to start - run in background etc)
    - killproc process (to stop - sending signal)

    and that's it. have i missed anything?

    rotis23

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>hammer, could you tell me why its important to fork
    It helps split away from the parent in a clean manner. Within the program that forks, the parent part terminates immediately, thus telling the calling process that it's all done. The child goes on living, without a parent (actually it gets adopted by init, if I remember rightly).

    And yes, you can use signals to help control the program. For example, I've user SIGUSR1 to tell the program to dump some stats about it's uptime and what it's currently doing etc.
    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
    Aug 2002
    Posts
    351
    thanks hammer!

    just some more info i can't find anywhere!

    what is the difference - in terms of the effect on the process of the following signals:

    SIGINT (kill -2) - Interrupt
    SIGTERM (kill -15) - Terminate
    SIGKILL (kill -9) - cannot handle
    SIGHUP (kill -1) - Hangup

    I know that SIGKILL is the one that is not handled and stops a process dead. I just want to know the difference between the other three.

    TIA, rotis23

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    SIGHUP
    Hangup. Sent when a terminal is hung up to every process for which it is the control terminal. Also sent to each process in a process group when the group leader terminates for any reason. This simulates hanging up on terminals that can't be physically hung up, such as a personal computer.
    SIGINT
    Interrupt. Sent to every process associated with a control terminal when the interrupt key (Control-C) is hit. This action of the interrupt key may be suppressed or the interrupt key may be changed using the stty command. Note that suppressing the interrupt key is completely different from ignoring the signal, although the effect (or lack of it) on the process is the same.
    SIGTERM
    Sofftware termination. The standard termination signal. It's the default signal sent by the kill command, and is also used during system shutdown to terminate all active processes. A program should be coded to either let this signal default or else to clean up quickly (e.g., remove temporary files) and call exit

    More here.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wireless Network Linux & C Testbed
    By james457 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-11-2009, 11:03 AM
  2. Writing a linux (centos 4) c++ daemon that interacts with PHP
    By misterdanny in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2007, 07:48 AM
  3. installing linux for the first time
    By Micko in forum Tech Board
    Replies: 9
    Last Post: 12-06-2004, 05:15 AM
  4. Linux GUI Design
    By WebmasterMattD in forum Linux Programming
    Replies: 1
    Last Post: 01-29-2003, 08:26 PM