Is this correct? Daemonizing an app

This is a discussion on Is this correct? Daemonizing an app within the C Programming forums, part of the General Programming Boards category; I am sorry, this may be a stupid question. I am trying to make a very simple application become a ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    6

    Is this correct? Daemonizing an app

    I am sorry, this may be a stupid question.


    I am trying to make a very simple application become a daemon but it seems I can't work it out. I tried to "ps-ef" the application but it returns nothing.

    The "daemonizing" code is below.

    Code:
    // App.c
    #include <stdio.h>
    
    int daemon_init(const char * parAppName){
        int    iCounter;
        int    iPid;
        char   *pTmp;
    
        // if the fork() call fails, this service won't be started
        if( (iPid = fork()) != 0 ){
            printf("%s Daemonizing failed 1... \n", parAppName);
            exit(0);
        }
        
        // set session id
        setsid();
    
        //signal( SIGHUP, SIG_IGN);
    
        // if the fork() call fails, this service won't be started
        if( (iPid = fork()) !=0 ){
            printf("%s Daemonizing failed 2... \n", parAppName);
            exit(0);
        }
    
        chdir("/");
    
        umask(0);
    
        printf("%s Daemonizing Complete... \n", parAppName);
    
        return 0;
    }
    
    
    int main( int argc, char * argv[] ){
        daemon_init(argv[0]);
        return 0;
    }
    when I try to run the program, it results to this :

    App Daemonizing Complete...
    App Daemonizing failed 2...
    App Daemonizing failed 1...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    fork() returns -1, AND NO CHILD when it fails.

    If it succeeds, then there are TWO processes, one which returns 0 (the child), and one which returns a pid (the parent).

    Oh, and daemons usually end up in some kind of infinite loop, waiting to complete their mission.
    Code:
    while ( 1 ) {
      // wait for something to happen
      // do it
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    6

    Thumbs up

    Quote Originally Posted by Salem View Post
    fork() returns -1, AND NO CHILD when it fails.

    If it succeeds, then there are TWO processes, one which returns 0 (the child), and one which returns a pid (the parent).

    Oh, and daemons usually end up in some kind of infinite loop, waiting to complete their mission.
    Code:
    while ( 1 ) {
      // wait for something to happen
      // do it
    }
    I got it! Thanks a lot for answering. =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  2. getting stderr from an app launched via execv
    By KPexEA in forum Linux Programming
    Replies: 1
    Last Post: 08-16-2008, 10:36 PM
  3. c++ consle app HELP
    By programer345 in forum C++ Programming
    Replies: 15
    Last Post: 06-29-2008, 04:38 AM
  4. An app to add an rpath?
    By valis in forum Tech Board
    Replies: 1
    Last Post: 07-15-2006, 01:50 AM
  5. Client - Server TCP/IP MFC app..... Help!
    By amedinadiaz in forum C++ Programming
    Replies: 0
    Last Post: 10-26-2005, 11:57 AM

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