Professional Web Applications Themes

How to make a C program work as a Daemon - Linux / Unix Administration

Hi, I know some basic C programming, but I do not know the advanced concepts and new to network programming. Now, I'm assigned the responsibility of writing a C program as a Daemon. I've my C program ready.(It connects to Vertex database and performs tax calculations).I do not know how to make that to work as a daemon.Can you please send me a basic program to get started. Any doentation in this regard will be very helpful. Really appreciate your help Kind Regards Madhavi...

  1. #1

    Default How to make a C program work as a Daemon

    Hi,
    I know some basic C programming,
    but I do not know the advanced concepts and new to
    network programming. Now, I'm assigned the responsibility
    of writing a C program as a Daemon. I've my C program
    ready.(It connects to Vertex database and performs
    tax calculations).I do not know how to make that to
    work as a daemon.Can you please send me a basic program
    to get started.
    Any doentation in this regard will be very helpful.

    Really appreciate your help

    Kind Regards
    Madhavi
    Madhavi Guest

  2. #2

    Default Re: How to make a C program work as a Daemon

    [email]clatha10[/email] (Madhavi) writes:
    > I know some basic C programming,
    > but I do not know the advanced concepts and new to
    > network programming. Now, I'm assigned the responsibility
    > of writing a C program as a Daemon. I've my C program
    > ready.(It connects to Vertex database and performs
    > tax calculations).I do not know how to make that to
    > work as a daemon.Can you please send me a basic program
    > to get started.
    Check if you have the "daemon" function.

    --
    Måns Rullgård
    [email]mrukth.se[/email]
    Måns Rullgård Guest

  3. #3

    Default Re: How to make a C program work as a Daemon

    The simplest way is to double-fork. fork, then get the child process
    to fork again. Let's call this third process a grandchild. Once the
    child fork()s successfully, it should die. The original parent should
    waitpid() right after forking, and die with the child. The grandchild
    is orphaned and now belongs to the init process, and is thus a pure
    daemon process.

    - Time -

    Madhavi wrote:
    > Hi,
    > I know some basic C programming,
    > but I do not know the advanced concepts and new to
    > network programming. Now, I'm assigned the responsibility
    > of writing a C program as a Daemon. I've my C program
    > ready.(It connects to Vertex database and performs
    > tax calculations).I do not know how to make that to
    > work as a daemon.Can you please send me a basic program
    > to get started.
    > Any doentation in this regard will be very helpful.
    >
    > Really appreciate your help
    >
    > Kind Regards
    > Madhavi
    --
    Max Belushkin
    Maxim Belushkin Guest

  4. #4

    Default Re: How to make a C program work as a Daemon

    [email]clatha10[/email] (Madhavi) wrote:
    >Hi,
    > I know some basic C programming,
    >but I do not know the advanced concepts and new to
    >network programming. Now, I'm assigned the responsibility
    >of writing a C program as a Daemon. I've my C program
    >ready.(It connects to Vertex database and performs
    >tax calculations).I do not know how to make that to
    >work as a daemon.Can you please send me a basic program
    >to get started.
    >Any doentation in this regard will be very helpful.
    >
    >Really appreciate your help
    >
    >Kind Regards
    >Madhavi
    The best advice in one line that I can give you is to buy a copy
    of "Advanced Programming in a UNIX Environment" by W. Richard
    Stevens. Chapter 13 is only 10 pages long, but it is titled
    "Daemon Processes".

    Here is an (edited for compression) list that Stevens says are
    the "rules" for coding a daemon process.

    1) Call fork() and have the parent die. The child process
    inherits the process group ID, but gets a new process ID,
    which means we can guarantee it is not a process group
    leader.

    2) Call setsid() to create a new session. The process 1)
    becomes a session leader of a new session, 2) becomes the
    group leader of a new process group, and 3) has no
    controlling terminal.

    Under SVR4 calling fork() again might be useful. This
    guarantees that the process cannot acquire a
    controlling terminal. Alternately, that can be
    avoided by always opening a terminal device only with
    O_NOCTTY specified.

    3) Change the current working directory to the root directory
    or some other specific directory. This prevent the daemon
    from interfering with unmounting of whatever filesystem the
    original current working directory was on.

    4) Set the file mode creation mask to 0.

    5) Close unneeded filedescriptors. Which one are unneeded
    depends on what the daemon process needs.

    Here is the example funtion that Stevens provided. Note that his
    "curhdr.h" file probably includes stdlib.h and unistd.h.

    #include <sys/types.h>
    #include <sys/stats.h>
    #include <fcntl.h>
    #include "curhdr.h"

    int daemon_init(void)
    pit_t pid;

    if ((pid = fork()) < 0)
    return -1;
    else if (pid != 0)
    exit(0); /* parent goes bye-bye */

    /* child continues */
    setsid(); /* become session leader */
    chdir("/"); /* change working directory */
    umask(0); /* clear our file mode creation mask */
    return 0;
    }

    Half of Stevens' chapter on daemon processes is devoted to using
    syslog, and no doubt you'll also discover that is of equal
    importance to all of the above.

    --
    Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
    Ukpeagvik (Barrow, Alaska) [email]floydbarrow.com[/email]
    Floyd Davidson Guest

  5. #5

    Default Re: How to make a C program work as a Daemon

    On Fri, 31 Oct 2003 11:34:22 -0800, Madhavi wrote:
    > Any doentation in this regard will be very helpful.
    Surprisingly nobody else referred you to the comp.unix.programmer
    faq.

    See section 1.7 and the associated examples at the end of the
    faq:

    [url]http://www.erlenstar.demon.co.uk/unix/[/url]

    --
    mail1dotstofanetdotdk

    Bjorn Reese Guest

  6. #6

    Default Re: How to make a C program work as a Daemon

    Madhavi wrote:
    > Hi,
    > I know some basic C programming,
    > but I do not know the advanced concepts and new to
    > network programming. Now, I'm assigned the responsibility
    > of writing a C program as a Daemon. I've my C program
    > ready.(It connects to Vertex database and performs
    > tax calculations).I do not know how to make that to
    > work as a daemon.Can you please send me a basic program
    > to get started.
    > Any doentation in this regard will be very helpful.
    >
    > Really appreciate your help
    >
    > Kind Regards
    > Madhavi
    Paraphrased from Stevens' "Advanced Programming in the Unix Environment",
    Chapter 13.3

    1) fork, and have the parent exit

    2) call setsid to create a new session
    (under svr5, some people recommend calling fork again, and having the
    parent terminate)

    3) change the current working directory to the root directory

    4) set the file creation mode mask to 0

    5) close unneeded file descriptors



    --
    Lew Pitcher

    Master Codewright and JOAT-in-training
    Registered Linux User #112576 ([url]http://counter.li.org/[/url])
    Slackware - Because I know what I'm doing.

    Lew Pitcher Guest

  7. #7

    Default Re: How to make a C program work as a Daemon

    pid_t pid, sid;

    pid = fork();
    if(pid < 0)
    {
    syslog(LOG_ERR, "%s\n", perror);
    exit(EXIT_FAILURE);
    }
    if(pid > 0)
    exit(EXIT_SUCCESS); // In the parent, let's bail

    // In the child...
    // Open the system log
    openlog("Linux_processd", LOG_PID, LOG_DAEMON);

    // First, start a new session
    if((sid = setsid()) < 0)
    {
    syslog(LOG_ERR, "%s\n", "setsid");
    exit(EXIT_FAILURE);
    }

    // Next, make / the current directory
    if((chdir("/")) < 0)
    {
    syslog(LOG_ERR, "%s\n", "chdir");
    exit(EXIT_FAILURE);
    }

    // Reset the file mode
    umask(0);

    //Close unneeded file descriptors
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
    */
    "Madhavi" <clatha10>
    ??????:859236c7.0310311134.2d3151bposting.google. com...
    > Hi,
    > I know some basic C programming,
    > but I do not know the advanced concepts and new to
    > network programming. Now, I'm assigned the responsibility
    > of writing a C program as a Daemon. I've my C program
    > ready.(It connects to Vertex database and performs
    > tax calculations).I do not know how to make that to
    > work as a daemon.Can you please send me a basic program
    > to get started.
    > Any doentation in this regard will be very helpful.
    >
    > Really appreciate your help
    >
    > Kind Regards
    > Madhavi

    postbeerliu Guest

Similar Threads

  1. Any extention or program to make rounded tables?
    By Dreamweaver2k2 in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 6
    Last Post: April 20th, 09:26 PM
  2. How do I make Acrobat my default PDF program?
    By kurt_triffet@adobeforums.com in forum Adobe Acrobat Macintosh
    Replies: 4
    Last Post: January 24th, 12:37 AM
  3. Make an autonomous VC++ program
    By jerome_pages@adobeforums.com in forum Adobe Acrobat SDK
    Replies: 4
    Last Post: December 14th, 03:48 PM
  4. file download with ftp program does nor work
    By estina in forum Macromedia Contribute General Discussion
    Replies: 0
    Last Post: May 4th, 10:22 AM
  5. Replies: 2
    Last Post: September 15th, 08:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139