Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-17-2008, 10:12 PM   #1
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, 10:53 PM   #2
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, 02:41 AM   #3
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 :
 if there is no file called /tmp/ {
    create a file called /tmp/
    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/ You can take a look at some of them.

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

Rep: Reputation: 110Reputation: 110
The tmp file method will work. But for a more solid method, see this thread.
Old 06-18-2008, 02:14 PM   #5
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, 10:03 PM   #6
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, 06:49 PM   #7
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

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


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 11:16 AM
1st time installing redhat - Make and Make Install does not work runlikeanantelope Linux - Newbie 4 02-19-2007 04:58 PM
Running a C programme in LINUX background continuously bhuvana Linux - General 9 07-06-2006 09:04 AM
running a programme written in BASIC in linux debsan Fedora 1 06-27-2006 01:55 PM
Warning on running "make check", 2nd time satimis Linux From Scratch 8 07-11-2005 04:46 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:39 PM.

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