how to allow only one instance?

A

ataraxia2500

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

#include <stdlib.h>

int main(void)
{
    (void)system("daemon.exe");
    (void)system("gui.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
 
T

Tom St Denis

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

#include <stdlib.h>

int main(void)
{
(void)system("daemon.exe");
(void)system("gui.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?

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
 
J

Joona I Palaste

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

Which part of the ISO C standard defines these functions?
 
C

CBFalconer

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

#include <stdlib.h>

int main(void)
{
(void)system("daemon.exe");
(void)system("gui.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?

Rewrite it as follows:

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

int main(void)
{
if (!running("daemon.exe") (void)system("daemon.exe");
if (!running("gui.exe") (void)system("gui.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.
 
T

Tom St Denis

CBFalconer said:
Rewrite it as follows:

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

int main(void)
{
if (!running("daemon.exe") (void)system("daemon.exe");
if (!running("gui.exe") (void)system("gui.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.

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

You fail it.

Tom
 
A

Alan Balmer

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

#include <stdlib.h>

int main(void)
{
    (void)system("daemon.exe");
    (void)system("gui.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

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.
 
K

Keith Thompson

Tom St Denis said:
Joona said:
Tom St Denis <[email protected]> scribbled the following: [...]
This isn't a clc question but what the heck. [...]
Second you will want to learn about fork() and exec*() functyions
as well as various PID related functions. Some simple
pseudo-code

Which part of the ISO C standard defines these functions?

Appendix C.

Tom

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

Dan Pop

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

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.
You fail it.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top