By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,366 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,366 IT Pros & Developers. It's quick & easy.

Controlling number of instances of a program (Cross platform)

P: n/a
Hi,

Is there a way to ensure that only one instance of an application runs
on a physical machine. I would want to do this in a cross platform way.
One ide I have is to obtain a list of the running processes and checking
the list - but then (presumably), the checking code would always be on
the list - so this defeats the point somewhat.

Any ideas?

many thanks

Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Paul Tremblay wrote:
Hi,

Is there a way to ensure that only one instance of an application runs
on a physical machine.


No portable way. It's not even overly easy on UNIX. Enumerating
the processes on Windows works (and I believe the only way on NT-derived
kernels).
Jul 23 '05 #2

P: n/a
Paul Tremblay wrote:
Hi,

Is there a way to ensure that only one instance of an application runs
on a physical machine. I would want to do this in a cross platform way.
One ide I have is to obtain a list of the running processes and checking
the list - but then (presumably), the checking code would always be on
the list - so this defeats the point somewhat.

Any ideas?


There is no really platform indepedant way. One often used way is that the
process simply creates a file at start and removes it at the end.
Before starting up, it checks whether the file already exists and if it
does, exits immediately.
The main problem about this is handling in a platform independant way the
case that the program crashed and left the file there.

Jul 23 '05 #3

P: n/a
> Is there a way to ensure that only one instance of an application runs
on a physical machine. I would want to do this in a cross platform way.
One ide I have is to obtain a list of the running processes and checking
the list


Put obtaining this list is platform specific. So you'd have to
implement this for every platform which you want to run on. Maybe
there's a library which already does this.

You could have your program try to bind to a socket to specific port.
If another instance tries to bind to the same port, then it will fail.
You would then handle this failure by exiting, leaving only the first
instance running.

Or when an instance starts, it could check a flag that's stored in a
file, which indicates if another instance is already running.

Jul 23 '05 #4

P: n/a
Paul Tremblay wrote:
Hi,

Is there a way to ensure that only one instance of an application runs
on a physical machine.


There isn't a way in standard C++ to do this.

(OT from here on)

However, in the cross-platform library wxWidgets you could use the
class wxSingleInstanceChecker. For a detailed way of how to do this
consult the latest wxWidgets documentation:
http://www.wxwidgets.org/manuals/2.6...cechecker.html

Cheers,
Kieran

Jul 23 '05 #5

P: n/a
Ron Natalie wrote:
Paul Tremblay wrote:
Hi,

Is there a way to ensure that only one instance of an application runs
on a physical machine.


No portable way. It's not even overly easy on UNIX. Enumerating
the processes on Windows works (and I believe the only way on NT-derived
kernels).


The socket approach mentioned elsewhere sounds promising.

Platform-specific: On Windows, what I sometimes do is create a named
event using ::CreateEvent(). When the application starts up, do a WFSO
on that named event with a timeout of zero, thereby testing the state
and returning immediately. If WFSO returns with a value of
WAIT_TIMEOUT, then you know that this instance is a second instance of
the running program. When the application which raised the event
sucessfully shuts down, that's the time to lower the event.

Make sense?

Take care,

John Dibling

Jul 23 '05 #6

P: n/a

"Ron Natalie" <ro*@spamcop.net> skrev i en meddelelse
news:42**********************@news.newshosting.com ...
Paul Tremblay wrote:
Hi,

Is there a way to ensure that only one instance of an application runs on
a physical machine.


No portable way. It's not even overly easy on UNIX. Enumerating
the processes on Windows works (and I believe the only way on NT-derived
kernels).


A better approach would be to create a named semaphore,

/Peter
Jul 23 '05 #7

P: n/a
Peter Koch Larsen wrote:
"Ron Natalie" <ro*@spamcop.net> skrev i en meddelelse
news:42**********************@news.newshosting.com ...
Paul Tremblay wrote:
Hi,

Is there a way to ensure that only one instance of an application runs on
a physical machine.


No portable way. It's not even overly easy on UNIX. Enumerating
the processes on Windows works (and I believe the only way on NT-derived
kernels).

A better approach would be to create a named semaphore,

/Peter


Or a named mutex, which is what I usually do.
Jul 23 '05 #8

P: n/a
Rolf Magnus wrote:
Paul Tremblay wrote:

Hi,

Is there a way to ensure that only one instance of an application runs
on a physical machine. I would want to do this in a cross platform way.
One ide I have is to obtain a list of the running processes and checking
the list - but then (presumably), the checking code would always be on
the list - so this defeats the point somewhat.

Any ideas?

There is no really platform indepedant way. One often used way is that the
process simply creates a file at start and removes it at the end.
Before starting up, it checks whether the file already exists and if it
does, exits immediately.
The main problem about this is handling in a platform independant way the
case that the program crashed and left the file there.


I think just opening a file for write-access should do fine. If you
don't close the handle no other process can open it. And if the file
is left on the disk after the program has terminated it's not a problem,
since the test wouldn't be whether the file exists but whether the new
process can open it.
Jul 23 '05 #9

P: n/a
Swampmonster wrote:
There is no really platform indepedant way. One often used way is that
the process simply creates a file at start and removes it at the end.
Before starting up, it checks whether the file already exists and if it
does, exits immediately.
The main problem about this is handling in a platform independant way the
case that the program crashed and left the file there.


I think just opening a file for write-access should do fine. If you
don't close the handle no other process can open it.


That's not guaranteed, and on many systems, it is in fact not true.

Jul 23 '05 #10

P: n/a
[]
I think just opening a file for write-access should do fine. If you
don't close the handle no other process can open it.


That's not guaranteed, and on many systems, it is in fact not true.


Hm. Didn't know that. You mean if I 'fopen("some file", "w")' some file
then it's _not_ guaranteed that another process that tries the same will
fail to do so (before I 'fclose()' my 'FILE*' or course)?

Is there at leat another way to assure that no other process can alter
the contents of a file using the standard C++ lib? If not I'd consider
that a huge missing feature. I mean just think about the problem of
ad-hoc generating a name for a temp-file to use... how can one be sure
that the file does not already exist the very moment one want's to open
it, and that no other process is using it at that moment?

Nevertheless I apologise for stating something that wasn't true.
Jul 23 '05 #11

P: n/a
Swampmonster wrote:
[]
I think just opening a file for write-access should do fine. If you
don't close the handle no other process can open it.
That's not guaranteed, and on many systems, it is in fact not true.


Hm. Didn't know that. You mean if I 'fopen("some file", "w")' some file
then it's _not_ guaranteed that another process that tries the same will
fail to do so (before I 'fclose()' my 'FILE*' or course)?


Yup. Under Linux, this is the case. I think that's a typical behavior on
Un*x-like systems.
Is there at leat another way to assure that no other process can alter
the contents of a file using the standard C++ lib?
No.
If not I'd consider that a huge missing feature.
Well, standard C++ does assume only one program running. It doesn't deal at
all with anything concerning multiple processes.
I mean just think about the problem of ad-hoc generating a name for a
temp-file to use...
Generating the name and opening the file should best not be two separate
operations, but rather one atomic one. POSIX has functions for this, like
tmpfile() or mkstemp(), but the C++ standard library has nothing for that.
how can one be sure that the file does not already
exist the very moment one want's to open it, and that no other process is
using it at that moment?
In standard C++, no.
Nevertheless I apologise for stating something that wasn't true.


Take it as a learning experience. At least that's what I usually do in such
cases. ;-)

Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.