• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

Single Program Instance

How can I make some sort of lock to ensure that only one instance of a program is running at a time?
0
joele24
Asked:
joele24
1 Solution
 
pankajtiwaryCommented:
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
 
joele24Author Commented:
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
 
vadim_tiCommented:
if you running on windows you can use named mutex object
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
Amritpal SinghCommented:
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
 
joele24Author Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
joele24Author Commented:
ah one quick question do you know if i can call a system("touch path_to_file") or will the lock prevent this
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now