List of running processes in C++ - Linux

This is a discussion on List of running processes in C++ - Linux ; I have C++ programs that I am porting to Linux from Windows and they need to be able to check if a copy of it is already running. Currently, the program uses PSAPI to look at a list of running ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: List of running processes in C++

  1. List of running processes in C++

    I have C++ programs that I am porting to Linux from Windows and they
    need to be able to check if a copy of it is already running.

    Currently, the program uses PSAPI to look at a list of running
    programs and exits if the current program name is in the list. I will
    eventually build a more sophisticated system in about a month, since a
    different customer needs something advanced, but I need to get these
    programs ported asap. Is there a Linux/POSIX equivalent of this for
    commercial use, or any ideas for another easy method that isn't
    dependent on the program not crashing?

    Having a simple lock file will get very annoying, since we often get
    badly mangled data that crashes the program. Fixing the crashes
    aren't worth the processing overhead, since data has to be processed
    so quickly on cheap servers to remain cost effective (we're about the
    only company who can actually accomplish it). Having to monitor
    automated processes to make sure there isn't a stray lock file would
    be a bigger pain than manually making sure the program isn't running
    twice.

    I know there is process information in /proc/[1-9]*, but opening /
    proc/
    [1-9]*/cmdline (or any other file) in C++, parsing it and overcoming
    permission issues is probably a bit more work than I want to put into
    this temporary fix. After porting, we have a lot of changes we want
    to implement.

  2. Re: List of running processes in C++

    On 2008-07-11, jbo5112 wrote:
    > I have C++ programs that I am porting to Linux from Windows and they
    > need to be able to check if a copy of it is already running.


    read/write a pid file in /var/run
    if you find a running copy send the pid signal 0 if you get an error
    it died.

    use atexit() to clean up after onesself.

    Bye.
    Jasen

  3. Re: List of running processes in C++

    jbo5112 writes:
    > I have C++ programs that I am porting to Linux from Windows and they
    > need to be able to check if a copy of it is already running.
    >
    > Currently, the program uses PSAPI to look at a list of running
    > programs and exits if the current program name is in the list. I will
    > eventually build a more sophisticated system in about a month, since a
    > different customer needs something advanced, but I need to get these
    > programs ported asap. Is there a Linux/POSIX equivalent of this for
    > commercial use, or any ideas for another easy method that isn't
    > dependent on the program not crashing?
    >
    > Having a simple lock file will get very annoying, since we often get
    > badly mangled data that crashes the program. Fixing the crashes
    > aren't worth the processing overhead,


    Delete the check. It's not worth the processing overhead either.

  4. Re: List of running processes in C++

    On 2008-07-11, jbo5112 wrote:

    > Having a simple lock file will get very annoying, since we often get
    > badly mangled data that crashes the program. Fixing the crashes
    > aren't worth the processing overhead, since data has to be processed
    > so quickly on cheap servers to remain cost effective (we're about the
    > only company who can actually accomplish it). Having to monitor
    > automated processes to make sure there isn't a stray lock file would
    > be a bigger pain than manually making sure the program isn't running
    > twice.
    >
    > I know there is process information in /proc/[1-9]*, but opening /
    > proc/
    > [1-9]*/cmdline (or any other file) in C++, parsing it and overcoming
    > permission issues is probably a bit more work than I want to put into
    > this temporary fix. After porting, we have a lot of changes we want
    > to implement.


    If you want a quick hack arrange for init to relauch it when it exits.
    (I assume that it exits when it crashes - you don't say)

    you can do do this by adding a line to /etc/inittab that runs getty on an
    unneeded VC with your program instead of /bin/login

    eg:

    9:23:respawn:/sbin/getty -nl /etc/ppp/persist 38400 tty9

    here /etc/ppp/persist was a shell script I knocked together that would
    relaunch an occasionally failing pppd processes.

    if you're targeting a system that doesn't use /etc/inittab it's still
    possible but the details are slightly different (you put a similar line
    in a different file)

    Bye.
    Jasen

  5. Re: List of running processes in C++

    jbo5112 wrote:
    > I have C++ programs that I am porting to Linux from Windows and they
    > need to be able to check if a copy of it is already running.


    > Currently, the program uses PSAPI to look at a list of running
    > programs and exits if the current program name is in the list. I will
    > eventually build a more sophisticated system in about a month, since a
    > different customer needs something advanced, but I need to get these
    > programs ported asap. Is there a Linux/POSIX equivalent of this for
    > commercial use, or any ideas for another easy method that isn't
    > dependent on the program not crashing?


    > Having a simple lock file will get very annoying, since we often get
    > badly mangled data that crashes the program. Fixing the crashes
    > aren't worth the processing overhead, since data has to be processed
    > so quickly on cheap servers to remain cost effective (we're about the
    > only company who can actually accomplish it). Having to monitor
    > automated processes to make sure there isn't a stray lock file would
    > be a bigger pain than manually making sure the program isn't running
    > twice.


    One solution is to have your program on start try to open a
    local connection on a certain port. If the connection can be
    established (and on the other side something responds in the
    way you expect it to) then another instance of the program is
    already running. If not spawn a process that listens on this
    port for incoming connections and regularly (e.g. once a se-
    cond) checks if the parent process is still alive (just test
    if a signal could be send with kill(ppid, 0)). If the parent
    process has gone this extra administrative process also quits.
    Since the "administrative" process is very simple it should
    never crash. Of course, there's no protection against a user
    that deletes the "administrative" process, but that doesn't
    seem to be a concern in your case.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  6. Re: List of running processes in C++

    In message
    <355b744a-5936-4410-ad30-56e550d778fe@b1g2000hsg.googlegroups.com>, jbo5112
    wrote:

    > Is there a Linux/POSIX equivalent of this for
    > commercial use, or any ideas for another easy method that isn't
    > dependent on the program not crashing?


    A lock file maintained by a wrapper program.



  7. Re: List of running processes in C++

    jbo5112 wrote in news:355b744a-5936-4410-ad30-
    56e550d778fe@b1g2000hsg.googlegroups.com:

    > I have C++ programs that I am porting to Linux from Windows and they
    > need to be able to check if a copy of it is already running.


    > Having a simple lock file will get very annoying, since we often get
    > badly mangled data that crashes the program.


    Here's another idea for you. Create a unix domain (AF_UNIX) socket
    endpoint in your program. You don't necessarily have to do anything
    with it. Then you can simply check for its existence in /proc/net/unix.
    If your program crashes, it will get automatically removed. See also
    "man 7 unix".

    Even better, you can create an additional thread that actually listens
    on the socket; then you can check for its existence by attempting to
    connect() to it. If connect succeeds, you know it's already running.
    Sample program:

    #include
    #include
    #include
    #include
    #include

    int main(int ac, char **av)
    {
    struct sockaddr_un sun;
    int namelen;
    int accept_fd;
    int listen_fd = socket(PF_UNIX, SOCK_STREAM, 0);
    if (listen_fd == -1) {
    perror("socket failed");
    return 1;
    }
    sun.sun_family = AF_UNIX;
    strcpy(sun.sun_path + 1, "mysock"); // 0 byte at sun_path[0]
    namelen = offsetof(struct sockaddr_un, sun_path) + 6 + 1;
    if (connect(listen_fd, (struct sockaddr *)&sun, namelen) != -1) {
    printf("Server already running\n");
    return 0;
    }
    if (bind(listen_fd, (struct sockaddr *)&sun, namelen) == -1) {
    perror("bind failed");
    return 1;
    }
    listen(listen_fd, 4);
    while (1) {
    accept_fd = accept(listen_fd, NULL, NULL);
    close(accept_fd);
    }
    return 0;
    }


    GH

+ Reply to Thread