LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 11-27-2002, 06:22 AM   #1
mayankjohri
Member
 
Registered: Mar 2002
Location: INDIA
Distribution: Mandrake Linux 9.2, Knoppix 3.0
Posts: 158

Rep: Reputation: 30
How to find instance of a program


In C how can i have only one instance of it running



mayank
 
Old 11-28-2002, 02:01 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Hi ...

Not having a coding example handy, but you
could do a system( ps ) [on my box this
( ps -Ac --no-headers | grep your_progs_name )
would provide the desired information] and
terminate if it's already running?

Cheers,
Tink
 
Old 11-28-2002, 05:18 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
It maybe a bit of a workaround, but could you make your program create a certain file as soon as it starts in /tmp or /var/run. And check before if the file exists, and if it does exit (as not to start another instance).

A good way to do this, is to open the file from the start of the program and not closing it anywhere. Instead delete (unlink) it immediately after opening.
The file will then be deleted when the program exits, or when it's killed. This way the file will also be deleted if the program is killed some day. And also it wouldn't matter is another program tries to delete it, while your program is running.

Last edited by Hko; 11-28-2002 at 05:21 PM.
 
Old 12-08-2002, 10:46 PM   #4
zenon
LQ Newbie
 
Registered: Dec 2002
Posts: 1

Rep: Reputation: 0
Instead of creating a lock file, I believe a better method
(under UNIX) is to use a semaphore -- with options that
the starting program creates a semaphore and that the
semaphore will allow only one program to increment it.
Dying program will clear its increment, so it will automagically
make the next program startup possible.
The SEM_UNDO option should be used for auto-clear of the
semop() increment on process exit.

Another possibility is indeed to create a file with the
pid of the process in it.
Any other process may open this file and check if the
process with given pid is still running. If not, it will remove
the file and will re-create it with own pid.
To avoid theoretically possible startup of more than one
process "at the *same* time", one should provide an
exclusive lock to the file.

-z
 
Old 12-09-2002, 11:39 PM   #5
mayankjohri
Member
 
Registered: Mar 2002
Location: INDIA
Distribution: Mandrake Linux 9.2, Knoppix 3.0
Posts: 158

Original Poster
Rep: Reputation: 30
Thanks all for the answers and providing help


mayank johri
 
  


Reply



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
restricting multiple instance of a program V_Ganesh Programming 14 01-23-2008 10:42 AM
cant find program Circuit Monkey Linux - Newbie 3 03-25-2005 04:36 AM
Passing parameters to already running instance of a program saravkrish Programming 5 10-31-2004 02:19 PM
Using grep to find only first instance roballen Linux - General 2 01-29-2004 03:21 AM
limit program to one instance stevenhasty Slackware 2 10-28-2003 04:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration