473,385 Members | 1,712 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,385 software developers and data experts.

Controlling number of instances of a program (Cross platform)

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
11 2209
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
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
> 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
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
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

"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
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
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
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
[]
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: demibee | last post by:
Thought this might be of interest to some out there... For those who've never encountered it, it's a free, open-source, cross-platform GUI library (Windows/Mac/Linux). Forte's Agent 2.0 will be...
2
by: xiaotom | last post by:
I want my software to be independant of operation system and databases. That's why I want to use odbc, and don't want to use MFC. Here I have some questions to ask: 1. On unix (like sun...
8
by: GaryDean | last post by:
We have been noticing that questions on vs.2005/2.0 don't appear to get much in answers so I'm reposting some questions posted by some of the programmers here in our organization that never got...
9
by: Susan Rice | last post by:
I'm running a simple win32 console application and I want to impliment a "Press any key to continue", so I print that prompt, and then what's the easiest way to impliment reading any key? Do I use...
7
by: Charles | last post by:
I'd like to develop a simple cross-platform application in C++. I'd like it to run in Windows, OS X, PC-BSD and Linux. From my research, it seems I should use Qt or Gtk as a graphical library. Do...
13
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
8
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
I would like to find out how I can launch an independent Python program from existing one in a cross-platform way. The result I am after is that a new terminal window should open (for io...
11
by: John | last post by:
Is there a way to find the number of processors on a machine (on linux/ windows/macos/cygwin) using python code (using the same code/cross platform code)?
1
by: Vinod Sadanandan | last post by:
Cross Platform Migration An Unproblematic Approach (Windows-UNIX ) Oracle 10\11g The principal restriction on cross-platform transportable database is that the source and destination platform...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.