Re: [LIP] check instance of a running application

Binand Sethumadhavan <[email protected]>
Newsgroups gmane.user-groups.linux.india.programmers
Message-ID <[email protected]>
On Wed, Dec 24, 2003 at 07:59:29PM +0530, Raj Mathur wrote:
> Create a 1-byte file in /tmp and put a lock on it.  If the lock fails
> then the application is already running.  If it succeeds, this is the
> first instance.

Is locking required? You just have to creat() a 0-byte file in /tmp with
O_CREAT|O_EXCL. If creat() fails with errno = EEXIST, then another copy
of the program is running. You will have to register an exit handler
via atexit() to clean up this file, though.

Another way. Try to link() a file to another temporary file (say gotten
via mkstemp()). If link() succeeds and stat() of the temporary file
says st_nlink = 2, then we are the first instance of the process. If
link() fails with EEXIST, then another instance is running. atexit()
is required here too.

Incidentally, all solutions suggested so far seem to require a writable
file system. Anything that doesn't have this requirement?

Taking the risk of going off-topic, how do the CD-only distributions
(like Knoppix?) handle such software? ramfs?

Binand

-- 
Linux - It is now safe to power ON your computer.


-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.