473,659 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2233
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 wxSingleInstanc eChecker. 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.ne t> skrev i en meddelelse
news:42******** **************@ news.newshostin g.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.ne t> skrev i en meddelelse
news:42******** **************@ news.newshostin g.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2046
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 built with it. AVG is as well.. "wxWindows An open source C++ GUI framework to make cross-platform programming child's play." <http://www.wxwindows.org/>
2
2786
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 solaris), does there exists any kind of midware like odbc? 2. I search on the internet, and find most of the examples are based on PHP. Is there any c++ examples? 3.Once I chose odbc, does that mean that whatever kind of database I
8
2175
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 answered... In 1.1 we always did our own myDataAdapter.fills and we liked that control for lots of good reasons. Now the new DataSource (or is it a TableAdapter:Dataset) automatically fills the Gridview. How can we control that fill? In a...
9
64394
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 'getchar', or 'gets', or some get function? or 'cin', or what's an easy way? What I want is the program to pause before exiting so the user can read whatever messages there are before the console window disappears.
7
2630
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 you agree? Do you have other tips? How Cygwin could help me? Thanks.
13
2307
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 add-ons for this purpose? If not, any urls which demonstrates code snippets management?
8
2276
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 independent of the original script). The following seems to work correctly under Ubuntu and Windows ... but I haven't been able to find a way to make it work under Mac OS. def exec_external(code, path): """execute code in an external process
11
4912
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
7818
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 must share the same endian format. Redo log files and control files from the source database are not transported to destination database . New control files and redo log files are created for the new database during the transport process, and an ...
0
8330
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,...
1
8523
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,...
1
6178
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
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
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
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
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.