LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-17-2008, 09:12 PM   #1
hcj9999
LQ Newbie
 
Registered: Jun 2008
Posts: 3

Rep: Reputation: 0
How can i make sure there is only one my programme is running at the same time


I have writed a programe a.out,I can run it many times, so there are lots of running a.out in the systerm.I want to make sure there is only one a.out is running at the same time?
 
Old 06-17-2008, 09:53 PM   #2
jdelongpre
LQ Newbie
 
Registered: Sep 2006
Location: Kentwood, MI
Distribution: Fedora, mostly. Testing SUSE, Testing eee pc
Posts: 9

Rep: Reputation: 0
One way to make the issue less confusing is to call your program something other than a.out. To do that, use the -o tag. for example
gcc -o hello hello.c

You could also run a script to see if your program is already running. For instance,
the command ps shows all processes, or ps -e shows all processes for all users.
Therefor, ps -e | grep hello would return any process containing hello and
ps -e | grep -q hello would just set a condition that you could test before
you start an instance of hello.
 
Old 06-18-2008, 01:41 AM   #3
devnux
LQ Newbie
 
Registered: Sep 2005
Distribution: Debian for Server / Ubuntu for Desktop
Posts: 14

Rep: Reputation: 0
A better way to do it is to store a file somewhere (in /tmp for example) that indicates a running instance of your executable. Once your program has finished its job it must delete it.
So the pseudo-code would be :
Code:
main(){
 if there is no file called /tmp/myexec.run {
    create a file called /tmp/myexec.run
    ...
    ...
    delete the file.
 }
 else; // An instance is running. Nothing to do (or maybe print a message)
}
Usually programs running as root store this file in /var/run/myprocess.pid. You can take a look at some of them.

Hope it helps.
 
Old 06-18-2008, 02:27 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
The tmp file method will work. But for a more solid method, see this thread.
 
Old 06-18-2008, 01:14 PM   #5
jdelongpre
LQ Newbie
 
Registered: Sep 2006
Location: Kentwood, MI
Distribution: Fedora, mostly. Testing SUSE, Testing eee pc
Posts: 9

Rep: Reputation: 0
I agree with Hko, given that you have the source for your program. Since you already said that, I DID know. Still, name your program something other than a.out, too.
 
Old 06-18-2008, 09:03 PM   #6
hcj9999
LQ Newbie
 
Registered: Jun 2008
Posts: 3

Original Poster
Rep: Reputation: 0
thank you very much! a.out is just a exmaple.
 
Old 06-19-2008, 05:49 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
In addition to the file-locking proposed by Hko, there are plenty of IPC mechanisms available (e.g., UNIX domain sockets or semaphores).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error Running A Programme Using Xlib sridhar_yerram Linux - Software 0 01-20-2008 10:16 AM
1st time installing redhat - Make and Make Install does not work runlikeanantelope Linux - Newbie 4 02-19-2007 03:58 PM
Running a C programme in LINUX background continuously bhuvana Linux - General 9 07-06-2006 08:04 AM
running a programme written in BASIC in linux debsan Fedora 1 06-27-2006 12:55 PM
Warning on running "make check", 2nd time satimis Linux From Scratch 8 07-11-2005 03:46 AM


All times are GMT -5. The time now is 08:37 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration