Control instance

This is a discussion on Control instance within the C Programming forums, part of the General Programming Boards category; Hello All, I have a program that run as a daemon. My problem seems to be simple but I didn't ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    41

    Control instance

    Hello All,

    I have a program that run as a daemon. My problem seems to be simple but I didn't find the answer anywhere.

    How do I alow only one instance of my program to run ?

    Thanks in advance,

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    I'm not certain this can be accomplished with the C or C++ standard. Rather it is OS specific. What OS will your daemon run on, and/or what compiler are you using for this project?


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    I'm assuming these are Linux Daemons right?

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    41
    Yes, it is running on a Linux machine and gcc version 3.3.3 !

    I've create a daemon like this:

    Code:
    int daemon(int nochdir, int noclose)
    {
            int fd;
    
            switch (fork()) {
                    case -1:
                            return -1;
                    case 0:
                            break;
                    default:
                            _exit(0);
            }
    
            if (setsid() == -1) return -1;
            if (!nochdir) chdir("/");
            if (noclose) return 0;
    
            fd = open(_PATH_DEVNULL, O_RDWR, 0);
            if (fd != -1) {
                    dup2(fd, STDIN_FILENO);
                    dup2(fd, STDOUT_FILENO);
                    dup2(fd, STDERR_FILENO);
                    if (fd > 2) close(fd);
            }
            return 0;
    }
    Thanks !

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    41
    Hello All,

    Any help ?

    Thanks again,

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Here are a number of possible ways, none of them ideal.

    1. Parse the output of 'ps' looking for instances of your process
    Code:
    FILE *fp = popen( "ps -e", "r" );
    while ( fgets( buff, sizeof buff, fp ) ) {
    }
    pclose( fp );
    Disadvantage - syntax of the ps command isn't portable.

    2. create a lock file - which is typically the name of your daemon and its process ID
    Eg. /tmp/mydaemon12345.lock
    Cases are
    - lock file missing -> not running so go ahead and be the one
    - lock file present, but process 12345 doesn't exist -> previous instance died badly
    - lock file present, but process 12345 isn't mydaemon -> previous instance died badly
    - lock file present and process 12345 is mydaemon -> already running.
    You need to make good use of atexit() and signal() to install handlers which remove the lock file in all the ways in which your program can exit (either normally or via a crash).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another question :O need ideas and confirmation
    By Akkernight in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 02:29 PM
  2. (Multiline) Edit Control Limit
    By P4R4N01D in forum Windows Programming
    Replies: 9
    Last Post: 05-17-2008, 11:56 AM
  3. Which one is "quicker" ?
    By AloneInTheDark in forum C# Programming
    Replies: 2
    Last Post: 02-08-2008, 08:23 PM
  4. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

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