how to allow only one instance?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ataraxia2500

    #1

    how to allow only one instance?

    I have a little exe that is used to launched a daemon and a gui:

    #include <stdlib.h>

    int main(void)
    {
        (void)system("d aemon.exe");
        (void)system("g ui.exe");
        return 0;
    }

    If the daemon is already running I want the gui to be launched only, unless
    it's running too in which can cased nothing should be launched.
    how can I make it check whether the daemon and the gui are already running?

    thanx in advance
  • Tom St Denis

    #2
    Re: how to allow only one instance?

    ataraxia2500 wrote:
    [color=blue]
    > I have a little exe that is used to launched a daemon and a gui:
    >
    > #include <stdlib.h>
    >
    > int main(void)
    > {
    > (void)system("d aemon.exe");
    > (void)system("g ui.exe");
    > return 0;
    > }
    >
    > If the daemon is already running I want the gui to be launched only,
    > unless it's running too in which can cased nothing should be launched.
    > how can I make it check whether the daemon and the gui are already
    > running?[/color]

    This isn't a clc question but what the heck.

    First don't ignore the returns of functions. They return values for a
    reason.

    Second you will want to learn about fork() and exec*() functyions as well as
    various PID related functions. Some simple pseudo-code


    fork()
    if (child) {
    check for .PID file, if exists print message [program running] and exit
    emit PID to a .PID file
    exec???() the program
    }
    // repeat for other process

    //
    wait on both child processes.
    delete both .PID files (well only delete the ones this instance of the
    program made!)

    Tom

    Comment

    • Joona I Palaste

      #3
      Re: how to allow only one instance?

      Tom St Denis <tom@securescie nce.net> scribbled the following:[color=blue]
      > ataraxia2500 wrote:[color=green]
      >> I have a little exe that is used to launched a daemon and a gui:
      >> #include <stdlib.h>
      >>
      >> int main(void)
      >> {
      >> (void)system("d aemon.exe");
      >> (void)system("g ui.exe");
      >> return 0;
      >> }
      >>
      >> If the daemon is already running I want the gui to be launched only,
      >> unless it's running too in which can cased nothing should be launched.
      >> how can I make it check whether the daemon and the gui are already
      >> running?[/color][/color]
      [color=blue]
      > This isn't a clc question but what the heck.[/color]
      [color=blue]
      > First don't ignore the returns of functions. They return values for a
      > reason.[/color]
      [color=blue]
      > Second you will want to learn about fork() and exec*() functyions as well as
      > various PID related functions. Some simple pseudo-code[/color]

      Which part of the ISO C standard defines these functions?

      --
      /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
      \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
      "Bad things only happen to scoundrels."
      - Moominmamma

      Comment

      • Tom St Denis

        #4
        Re: how to allow only one instance?

        Joona I Palaste wrote:
        [color=blue]
        > Tom St Denis <tom@securescie nce.net> scribbled the following:[color=green]
        >> ataraxia2500 wrote:[color=darkred]
        >>> I have a little exe that is used to launched a daemon and a gui:
        >>> #include <stdlib.h>
        >>>
        >>> int main(void)
        >>> {
        >>> (void)system("d aemon.exe");
        >>> (void)system("g ui.exe");
        >>> return 0;
        >>> }
        >>>
        >>> If the daemon is already running I want the gui to be launched only,
        >>> unless it's running too in which can cased nothing should be launched.
        >>> how can I make it check whether the daemon and the gui are already
        >>> running?[/color][/color]
        >[color=green]
        >> This isn't a clc question but what the heck.[/color]
        >[color=green]
        >> First don't ignore the returns of functions. They return values for a
        >> reason.[/color]
        >[color=green]
        >> Second you will want to learn about fork() and exec*() functyions as well
        >> as
        >> various PID related functions. Some simple pseudo-code[/color]
        >
        > Which part of the ISO C standard defines these functions?[/color]

        Appendix C.

        Tom

        Comment

        • CBFalconer

          #5
          Re: how to allow only one instance?

          ataraxia2500 wrote:[color=blue]
          >
          > I have a little exe that is used to launched a daemon and a gui:
          >
          > #include <stdlib.h>
          >
          > int main(void)
          > {
          > (void)system("d aemon.exe");
          > (void)system("g ui.exe");
          > return 0;
          > }
          >
          > If the daemon is already running I want the gui to be launched
          > only, unless it's running too in which can cased nothing should
          > be launched. how can I make it check whether the daemon and the
          > gui are already running?[/color]

          Rewrite it as follows:

          #include <stdlib.h>
          #include "running.h"

          int main(void)
          {
          if (!running("daem on.exe") (void)system("d aemon.exe");
          if (!running("gui. exe") (void)system("g ui.exe");
          return 0;
          }

          I leave it to you to write running.h and the associated
          running.c. If you require assistance in that you should try a
          newsgroup that deals with your particular system. The words
          running, daemon, gui are not specified in the C standard.

          --
          Churchill and Bush can both be considered wartime leaders, just
          as Secretariat and Mr Ed were both horses. - James Rhodes.


          Comment

          • Tom St Denis

            #6
            Re: how to allow only one instance?

            CBFalconer wrote:[color=blue]
            > Rewrite it as follows:
            >
            > #include <stdlib.h>
            > #include "running.h"
            >
            > int main(void)
            > {
            > if (!running("daem on.exe") (void)system("d aemon.exe");
            > if (!running("gui. exe") (void)system("g ui.exe");
            > return 0;
            > }
            >
            > I leave it to you to write running.h and the associated
            > running.c. If you require assistance in that you should try a
            > newsgroup that deals with your particular system. The words
            > running, daemon, gui are not specified in the C standard.[/color]

            Um, system() blocks. So even with a running() function this won't work.

            You fail it.

            Tom

            Comment

            • Alan Balmer

              #7
              Re: how to allow only one instance?

              On Fri, 09 Apr 2004 14:36:34 +0200, ataraxia2500
              <messageboardfa n1@yahoo.com> wrote:
              [color=blue]
              >I have a little exe that is used to launched a daemon and a gui:
              >
              >#include <stdlib.h>
              >
              >int main(void)
              >{
              >    (void)system("d aemon.exe");
              >    (void)system("g ui.exe");
              >    return 0;
              >}
              >
              >If the daemon is already running I want the gui to be launched only, unless
              >it's running too in which can cased nothing should be launched.
              >how can I make it check whether the daemon and the gui are already running?
              >
              >thanx in advance[/color]

              As noted elsethread, the solution is implementation-dependent and
              off-topic for c.l.c. You might want to think about the opposite
              approach of having the launched programs check whether another
              instance already exists, and exiting if so.

              --
              Al Balmer
              Balmer Consulting
              removebalmercon sultingthis@att .net

              Comment

              • CBFalconer

                #8
                Re: how to allow only one instance?

                Tom St Denis wrote:[color=blue]
                > Joona I Palaste wrote:[color=green]
                >> Tom St Denis <tom@securescie nce.net> scribbled the following:
                >>[/color][/color]
                .... snip ...[color=blue][color=green]
                >>[color=darkred]
                >>> Second you will want to learn about fork() and exec*()
                >>> functyions as well as various PID related functions. Some ...[/color]
                >>
                >> Which part of the ISO C standard defines these functions?[/color]
                >
                > Appendix C.[/color]

                That is an out and out childish lie. You know very well that you
                should not be posting off-topic replies in c.l.c.

                --
                Churchill and Bush can both be considered wartime leaders, just
                as Secretariat and Mr Ed were both horses. - James Rhodes.


                Comment

                • Keith Thompson

                  #9
                  Re: how to allow only one instance?

                  Tom St Denis <tom@securescie nce.net> writes:[color=blue]
                  > Joona I Palaste wrote:[color=green]
                  > > Tom St Denis <tom@securescie nce.net> scribbled the following:[/color][/color]
                  [...][color=blue][color=green][color=darkred]
                  > >> This isn't a clc question but what the heck.[/color][/color][/color]
                  [...][color=blue][color=green][color=darkred]
                  > >> Second you will want to learn about fork() and exec*() functyions
                  > >> as well as various PID related functions. Some simple
                  > >> pseudo-code[/color]
                  > >
                  > > Which part of the ISO C standard defines these functions?[/color]
                  >
                  > Appendix C.
                  >
                  > Tom[/color]

                  Is that supposed to be a joke? Appendix C is about sequence points in
                  both C90 and C99.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                  Schroedinger does Shakespeare: "To be *and* not to be"

                  Comment

                  • Dan Pop

                    #10
                    Re: how to allow only one instance?

                    In <hNydc.10732$PV 5.7896@news04.b loor.is.net.cab le.rogers.com> Tom St Denis <tom@securescie nce.net> writes:
                    [color=blue]
                    >CBFalconer wrote:[color=green]
                    >> Rewrite it as follows:
                    >>
                    >> #include <stdlib.h>
                    >> #include "running.h"
                    >>
                    >> int main(void)
                    >> {
                    >> if (!running("daem on.exe") (void)system("d aemon.exe");
                    >> if (!running("gui. exe") (void)system("g ui.exe");
                    >> return 0;
                    >> }
                    >>
                    >> I leave it to you to write running.h and the associated
                    >> running.c. If you require assistance in that you should try a
                    >> newsgroup that deals with your particular system. The words
                    >> running, daemon, gui are not specified in the C standard.[/color]
                    >
                    >Um, system() blocks. So even with a running() function this won't work.[/color]

                    system() blocks *only* if the command it executes blocks. I see no
                    a priori indication that either daemon.exe or gui.exe block the
                    command processor used to start them.
                    [color=blue]
                    >You fail it.[/color]

                    As usual, you're the failure in this thread.

                    OTOH, if a program is *designed* so that only one instance can run on a
                    system, the test really belongs to that program, as the user might try
                    to start it directly.

                    Chuck is also wrong when recommending another newsgroup, as long as a
                    portable solution is possible within the framework of the C standard,
                    even if not perfect: lock files.

                    #define LOCKFILE "/tmp/daemon.exe"
                    ...
                    void unlock(void)
                    {
                    remove(LOCKFILE );
                    }

                    int main()
                    {
                    FILE *fp;

                    if (fopen(LOCKFILE , "r") != NULL) /* terminate on the spot */ ;
                    if ((fp = fopen(LOCKFILE, "w")) == NULL) /* terminate on the spot */ ;
                    atexit(unlock);
                    close(fp);
                    ...
                    }

                    The only nonportable part of the code is the definition of the LOCKFILE
                    macro, as each system has its own conventions about how local files
                    that can be created and opened in reading mode *by any user* should
                    be named. The "by any user" bit is important, because the failure
                    to open in read mode is interpreted as inexistence of the file (the C
                    standard could have been a little bit more helpful by requiring fopen()
                    to set errno to one of a minimal set of sensible values, in case of
                    failure -- after all, the implementation does know why it failed).

                    The drawback of this solution is that it is open to a race condition, if
                    two instances of the program are started at the same time and both
                    discover the absence of the lock file at the same time and decide that it
                    is safe to proceed. If this possibility is scaring you, replace fopen()
                    by one of your OS primitives that is more flexible and can create a file
                    only if it doesn't already exist and report error otherwise (e.g. the
                    O_WRONLY | O_CREAT | O_EXCL mode of open() on POSIX systems).

                    Dan
                    --
                    Dan Pop
                    DESY Zeuthen, RZ group
                    Email: Dan.Pop@ifh.de

                    Comment

                    Working...