Solved

Single Program Instance

Posted on 2004-09-30
9
363 Views
Last Modified: 2010-04-15
How can I make some sort of lock to ensure that only one instance of a program is running at a time?
0
Comment
Question by:joele24
9 Comments
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12195241
In the code, at the beginning you can open a pipe to "ps -elf | grep <progname>" and check whether something with that name is running or not. But his has one drawback, if somebody changes the name of the executable/binary, the process name will be different.

The other way can be writing some information (e.g. process ID) into some file and everytime before starting just check whether that file exists. Similarly you need to remove that file just before the program is exiting.
0
 

Author Comment

by:joele24
ID: 12195454
I have the 2nd way you described but the problem is I appear to be hitting a race condition where 2 will start up and one both will read and write to the file in such a way as to miss that another process is running
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12196206
if you running on windows you can use named mutex object
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 45

Expert Comment

by:Kdo
ID: 12196265

On linux you don't need to write to the file.  Simply open it for exclusive write.  The second program will be unable to open the file because it's "locked" and know that another instance is running.

When the program terminates the lock file will automatically be closed so there's no chance of a deadlock or improper status in the file.



Kent
0
 
LVL 6

Expert Comment

by:Amritpal Singh
ID: 12197428
plz have a look at the following links

http://www.codeproject.com/cpp/avoidmultinstance.asp
http://www.deinmeister.de/wasmfaq_e.htm

i hope they may be of any help
BOl
0
 

Author Comment

by:joele24
ID: 12201615
This is for the linux platform, but thx for all the additonal  info.

Kdo,

so what is wrong with this?

mutex= open("/path/to/file",O_RDWR|O_EXCL);

basically as I run multiple instances of the program they all are able to get this file descriptor and  do not return -1


0
 
LVL 45

Accepted Solution

by:
Kdo earned 125 total points
ID: 12202286

My apologies.  I still tend to think in terms of other environments / systems and it's not quite this easy in *nix.

The O_EXCL flag doesn't work this way.  It's a modifier to to O_CREAT flag.

Basically, you're going to have to create the file, not just open it.

  mutex = open ("/path/to/file", O_RDWR|O_CREAT|O_EXCL, <permissions>);

  if (mutex < 0)
  {
    open failed, presumably because the file already exists.
  }

Then when the program finishes, delete the file.


Kent
0
 

Author Comment

by:joele24
ID: 12202338
ah one quick question do you know if i can call a system("touch path_to_file") or will the lock prevent this
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12202367

You should be able to do that.  If the touch(1) fails (which it shouldn't) the worst that will happen is that system() will return a non-zero value.

0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question