Controlling number of instances of a program (Cross platform)

P

Paul Tremblay

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
 
R

Ron Natalie

Paul said:
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).
 
R

Rolf Magnus

Paul said:
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.
 
B

BigBrian

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.
 
J

John Dibling

Ron said:
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
 
P

Peter Koch Larsen

Ron Natalie said:
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
 
S

Swampmonster

Rolf said:
Paul Tremblay 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. 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.
 
R

Rolf Magnus

Swampmonster said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,298
Messages
2,571,542
Members
48,284
Latest member
RedaBruno6

Latest Threads

Top