Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Single Program Instance

Posted on 2004-09-30
9
Medium Priority
?
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:Kent Olsen
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 46

Accepted Solution

by:
Kent Olsen earned 500 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 46

Expert Comment

by:Kent Olsen
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 how to use strings and some functions related to them in the C programming language.

618 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