473,480 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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("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
Nov 14 '05 #1
9 1552
ataraxia2500 wrote:
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

Nov 14 '05 #2
Tom St Denis <to*@securescience.net> scribbled the following:
ataraxia2500 wrote:
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


Which part of the ISO C standard defines these functions?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Bad things only happen to scoundrels."
- Moominmamma
Nov 14 '05 #3
Joona I Palaste wrote:
Tom St Denis <to*@securescience.net> scribbled the following:
ataraxia2500 wrote:
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


Which part of the ISO C standard defines these functions?


Appendix C.

Tom
Nov 14 '05 #4
ataraxia2500 wrote:

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.

--
Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses. - James Rhodes.
Nov 14 '05 #5
CBFalconer wrote:
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
Nov 14 '05 #6
On Fri, 09 Apr 2004 14:36:34 +0200, ataraxia2500
<me**************@yahoo.com> wrote:
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.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #7
Tom St Denis wrote:
Joona I Palaste wrote:
Tom St Denis <to*@securescience.net> scribbled the following:
.... snip ...
Second you will want to learn about fork() and exec*()
functyions as well as various PID related functions. Some ...


Which part of the ISO C standard defines these functions?


Appendix C.


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.
Nov 14 '05 #8
Tom St Denis <to*@securescience.net> writes:
Joona I Palaste wrote:
Tom St Denis <to*@securescience.net> 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.

--
Keith Thompson (The_Other_Keith) ks***@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"
Nov 14 '05 #9
In <hN******************@news04.bloor.is.net.cable.ro gers.com> Tom St Denis <to*@securescience.net> writes:
CBFalconer wrote:
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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
1661
by: Bob Rock | last post by:
Hello, I'd like to be able to allow instanciation of a class (class Class_A) only from another class method (class Class_B). And I'd like to write the necessary code to enforce this behavior...
2
2607
by: Neil Schemenauer | last post by:
python-dev@python.org.] The PEP has been rewritten based on a suggestion by Guido to change str() rather than adding a new built-in function. Based on my testing, I believe the idea is...
1
2808
by: Luther Miller | last post by:
I've created a web setup project that works great for installing an ASP.NET application to a virtual directory on a server. I'd like to be able to use the same setup program to install multiple...
2
1878
by: André Nogueira | last post by:
Hi there guys I want my program not to allow more than one instance per user. Now I have this code CODE Dim Processos() As Process Processos = Process.GetProcessesByName("Taskfind") If...
3
3618
by: Hardy Wang | last post by:
How to allow only one instance of console application to run on one single machine? Thanks! -- WWW: http://hardywang.1accesshost.com ICQ: 3359839 yours Hardy
3
13652
by: binder | last post by:
I am designing a new table with a few columns that may or may not have a value on each row that is inserted. What issues determine whether to allow a NULL value to be inserted for that column or...
6
20893
by: 3Dfelix | last post by:
Hi, someone could help me with this problem when trying to insert a record in a SQL express database? I can view record but not edit or insert. When connecting to SQL Server 2005, this failure...
5
7301
by: deekay | last post by:
I want to allow users to resize and reposition columns of a datasheet but for a prompt to be brought up and only the layout only to be saved if they select "save changes". This is the way it works...
2
1171
devonknows
by: devonknows | last post by:
hi, ive looked in the search a few times, and ive seen the code to only allow one instance of your window, so when its loaded again it stops it, but i cant seem to addapt it to my application, can...
0
1981
by: aboutjav.com | last post by:
Hi, I need some help. I am getting this error after I complete the asp.net register control and click on the continue button. It crashed when it tries to get it calls this Profile property ...
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7081
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6737
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5336
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4776
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.