Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Controlling number of instances of a program (Cross platform)

Reply
Thread Tools

Controlling number of instances of a program (Cross platform)

 
 
Paul Tremblay
Guest
Posts: n/a
 
      06-16-2005
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

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      06-16-2005
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).
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-16-2005
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.

 
Reply With Quote
 
BigBrian
Guest
Posts: n/a
 
      06-16-2005
> 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.

 
Reply With Quote
 
kieran@cyrocom.co.uk
Guest
Posts: n/a
 
      06-16-2005
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

 
Reply With Quote
 
John Dibling
Guest
Posts: n/a
 
      06-16-2005
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

 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      06-16-2005

"Ron Natalie" <> skrev i en meddelelse
news:42b14982$0$6739$ ...
> 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


 
Reply With Quote
 
Swampmonster
Guest
Posts: n/a
 
      06-16-2005
Peter Koch Larsen wrote:
> "Ron Natalie" <> skrev i en meddelelse
> news:42b14982$0$6739$ ...
>
>>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.
 
Reply With Quote
 
Swampmonster
Guest
Posts: n/a
 
      06-16-2005
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.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-16-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Controlling number of zeros of exponent in scientific notation Terry Reedy Python 0 03-06-2013 05:45 AM
Re: Controlling number of zeros of exponent in scientific notation Dave Angel Python 0 03-05-2013 08:56 PM
Controlling number of precisions Sarath C++ 4 03-03-2009 11:43 AM
dicts,instances,containers, slotted instances, et cetera. ocschwar@gmail.com Python 8 01-29-2009 09:52 AM
list of class instances within a list of a class instances John Wohlbier Python 2 02-22-2004 08:41 AM



Advertisments