473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 2322
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.