Re: My way to check if a progam is already running
- From: Roger Leigh <${rleigh}@invalid.whinlatter.ukfsn.org.invalid>
- Date: Tue, 26 Jun 2007 01:15:44 +0100
Jan Panteltje <pNaonStpealmtje@xxxxxxxxx> writes:
On a sunny day (Sun, 24 Jun 2007 00:59:43 +0200) it happened Michal Nazarewicz
<mina86@xxxxxxx> wrote in <877ipuqnts.fsf@xxxxxxxxxxxxxxxx>:
I'm not saying that you have to lock one of the data files, however
if it would be possible for two newsreader operate on the same data
files I think that would be the best option.
What I'm proposing is to create a lock file for example in a directory
with all the data files. That is, if you keep all the configuration and
data files in "~/.my-newsreader" you can create a file
"~/.my-newsreader/.lockfile" (using int fd = create(foo, 0600)) and then
lock it (using flock(fd, LOCK_EX)). If locking succeed (providing that
you are not using NFS) you're the only application if it fails there's
some application running already.
Yes, sorry, after posting that last night it hit me that that was
what you were suggesting, I thought you did mean lock every single
file. Yes yours clearly is the best solution, and I will have go at
implementing that.
This thread seemed to be missing a concrete impementation of the
code. Here's some from schroot, abridged for clarity.
I'm afraid it's C++, but it can be easily adapted for C, as you will
see below. Note error() is an exception type. lock::error is a
locking error exception type. sbuild::file_lock is a C++ wrapper
around fcntl (or lockf if you wanted) which is used to acquire and
release the lock.
int fd = open("lockfile", O_CREAT|O_EXCL|O_WRONLY, 0664);
if (fd < 0)
throw error();
sbuild::file_lock lock(fd);
try
{
lock.set_lock(lock::LOCK_EXCLUSIVE, 2);
}
catch (lock::error const& e)
{
throw error();
}
// We now have the lock.
// Do whatever can only be done by one process at once here.
try
{
lock.unset_lock();
}
catch (lock::error const& e)
{
throw error();
}
You can look at the use of the underlying C implementation here (this
is what you might like to adapt for use in a plain C program). Note
the GPL licence. You also get an itimer/signal-based lock timeout for
free so you don't have to block forever on it.
http://svn.debian.org/wsvn/buildd-tools/trunk/schroot/sbuild/sbuild-lock.h?op=file&rev=0&sc=0
http://svn.debian.org/wsvn/buildd-tools/trunk/schroot/sbuild/sbuild-lock.cc?op=file&rev=0&sc=0
This isn't perfect (e.g. completely exception-safe), but I hope it's
good for some ideas!
Regards,
Roger
--
.''`. Roger Leigh
: :' : Debian GNU/Linux http://people.debian.org/~rleigh/
`. `' Printing on GNU/Linux? http://gutenprint.sourceforge.net/
`- GPG Public Key: 0x25BFB848 Please GPG sign your mail.
Attachment:
pgptmtIOxHJWx.pgp
Description: PGP signature
- References:
- My way to check if a progam is already running
- From: Jan Panteltje
- Re: My way to check if a progam is already running
- From: Michal Nazarewicz
- Re: My way to check if a progam is already running
- From: Jan Panteltje
- Re: My way to check if a progam is already running
- From: Michal Nazarewicz
- Re: My way to check if a progam is already running
- From: Jan Panteltje
- Re: My way to check if a progam is already running
- From: Michal Nazarewicz
- Re: My way to check if a progam is already running
- From: Jan Panteltje
- Re: My way to check if a progam is already running
- From: Michal Nazarewicz
- Re: My way to check if a progam is already running
- From: panteltje
- Re: My way to check if a progam is already running
- From: Michal Nazarewicz
- Re: My way to check if a progam is already running
- From: Jan Panteltje
- My way to check if a progam is already running
- Prev by Date: Re: My way to check if a progam is already running
- Next by Date: Re: My way to check if a progam is already running
- Previous by thread: Re: My way to check if a progam is already running
- Next by thread: Re: My way to check if a progam is already running
- Index(es):