473,769 Members | 2,062 Online
Bytes | Software Development & Data Engineering Community
+ 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)syste m("daemon.exe") ;
****(void)syste m("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 2338
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("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?


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*@securescie nce.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("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?
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.hel sinki.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*@securescie nce.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("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?

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("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?


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.
Nov 14 '05 #5
CBFalconer wrote:
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.


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)syst em("daemon.exe" );
****(void)syst em("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*@securescie nce.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*@securescie nce.net> writes:
Joona I Palaste wrote:
Tom St Denis <to*@securescie nce.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_Keit h) 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.b loor.is.net.cab le.rogers.com> Tom St Denis <to*@securescie nce.net> writes:
CBFalconer wrote:
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.
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
1698
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 even inside the same assembly where class Class_A and Class_B are defined. I've written the code that does this BUT I'd like to understand if this is the most correct way to obtain such a result. This is my simplified code:
2
2631
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 feasible. It would be helpful if people could test the patched Python with their own applications and report any incompatibilities. PEP: 349
1
2827
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 instances - e.g., DEV, TEST, etc., as I see fit. However, when once instance has already been installed, the web setup project will not install further instances; instead, I get a message that it has already been installed and I must use Control...
2
1892
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 Processos.Length > 1 Then NotifyIcon1.Visible = False
3
3633
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
13665
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 define a default value to be used? I want to think through the repercussions of this decision before I get into production.
6
20916
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 may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) thanks
5
7357
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 at the moment for queries but for datasheet forms I find that it automatically saves column positions and widths without prompting to save changes. I should mention i am using Access 2000
2
1184
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 anyone help me in getting it so that if its already open it switches to it, or if not, open the application please instead of continuously opening the exe over and over again. Project - BonkerCollection Formname - NewForm.frm if anyone can help...
0
2008
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 ((string)(this.GetPropertyValue("Address1")));
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.