Solved

Single Program Instance

Posted on 2004-09-30
9
362 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
 
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now