Single application instance using lockfiles
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Single application instance using lockfiles




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Single application instance using lockfiles  
John Smith


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-05 01:06 PM

I heard that lockfiles is the prefered method to prevent two instances of
same application running at the same time.

Does anyone know some resource where I could read more about it?

I need to write some C code which can do this so 2nd instances will
terminate.
Is it also possible to avoid external users like root from deleting the
files to allow a 2nd instance?

Thanks in advance.
-- John







[ Post a follow-up to this message ]



    Re: Single application instance using lockfiles  
Måns Rullgård


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-05 01:06 PM

"John Smith" <john.smith@x-formation.com> writes:

> I heard that lockfiles is the prefered method to prevent two instances of
> same application running at the same time.
>
> Does anyone know some resource where I could read more about it?

Using open() with the O_EXCL flag is one method.

> I need to write some C code which can do this so 2nd instances will
> terminate.
> Is it also possible to avoid external users like root from deleting the
> files to allow a 2nd instance?

root can do anything, there is, and should not be, anything to stop
that.

--
Måns Rullgård
mru@inprovide.com





[ Post a follow-up to this message ]



    Re: Single application instance using lockfiles  
Jens.Toerring@physik.fu-berlin.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-05 01:06 PM

Måns Rullgård <mru@inprovide.com> wrote:
> "John Smith" <john.smith@x-formation.com> writes:
 
[vbcol=seagreen]
> Using open() with the O_EXCL flag is one method.

And writing the programs PID into it is quite reasonable. Using that
the next instance of the program can figure out if the lockfile is
still relevant or has become stale because the previous instance of
the program crashed without deleteing the lockfile.

Regards, Jens
--
\   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Re: Single application instance using lockfiles  
Måns Rullgård


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-05 01:06 PM

Jens.Toerring@physik.fu-berlin.de writes:

> Måns Rullgård <mru@inprovide.com> wrote: 
> 
> 
>
> And writing the programs PID into it is quite reasonable. Using that
> the next instance of the program can figure out if the lockfile is
> still relevant or has become stale because the previous instance of
> the program crashed without deleteing the lockfile.

Big fat warning: O_EXCL does not work over NFS, and PIDs are obviously
meaningless there too.

--
Måns Rullgård
mru@inprovide.com





[ Post a follow-up to this message ]



    Re: Single application instance using lockfiles  
Chuck Dillon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-05 11:02 PM

Jens.Toerring@physik.fu-berlin.de wrote:
> Måns Rullgård <mru@inprovide.com> wrote:
> 
>
> 
>
> 
>
>
> And writing the programs PID into it is quite reasonable. Using that
> the next instance of the program can figure out if the lockfile is
> still relevant or has become stale because the previous instance of
> the program crashed without deleteing the lockfile.
>

It should be pointed out that using a stored PID to guess whether or
not a process is still running is suspect on all systems if a
significant time has passed and suspect on some systems no matter how
much time has passed.

-- ced


--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.





[ Post a follow-up to this message ]



    Re: Single application instance using lockfiles  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-05 11:02 PM


"John Smith" <john.smith@x-formation.com> wrote in message
news:cusjau$dcp$1@news.net.uni-c.dk...

>I heard that lockfiles is the prefered method to prevent two instances of
> same application running at the same time.
>
> Does anyone know some resource where I could read more about it?
>
> I need to write some C code which can do this so 2nd instances will
> terminate.
> Is it also possible to avoid external users like root from deleting the
> files to allow a 2nd instance?

What's your outer problem? If two different users want to run your
application, why is it okay for them to run it on two different machines but
not okay for them to run it on the same machine? The answer to your question
will depend a lot on exactly what it is you're trying to avoid.

DS







[ Post a follow-up to this message ]



    Re: Single application instance using lockfiles  
John Smith


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-05 11:02 PM

>     What's your outer problem? If two different users want to run your
> application, why is it okay for them to run it on two different machines
but
> not okay for them to run it on the same machine? The answer to your
question
> will depend a lot on exactly what it is you're trying to avoid.
>

I have a network daemon which hosts a database to clients. The database can
only be accessed by one daemon at a time. Thats why I want to lock out other
daemons so only one instance is running. Naturally you can easily hosts more
daemons+database on each their machine but not on one.
So the idea was to create a lockfile to e.g. /var/tmp. Maybe theres some api
which can give a specific temp path for the running system instead of
assuming that /var/tmp exist?

Since root will be able to delete files I assume I must have a mechanism to
detect if the lock is still valid. E.g. run a thread every now and then and
make sure we are still locked.

-- John







[ Post a follow-up to this message ]



    Re: Single application instance using lockfiles  
Eric Sosman


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-05 11:02 PM



John Smith wrote: 
>
> but
> 
>
> question
> 
>
>
> I have a network daemon which hosts a database to clients. The database ca
n
> only be accessed by one daemon at a time.

Why?  That is, does the database enforce this?  If so, it
seems the database itself could be your exclusion mechanism:
second daemon instance attempts to connect, fails, says "Aha!
Somebody is already using the database," and exits.

What happens if process P1 connects to the database and
then P2 attempts to connect?

--
Eric.Sosman@sun.com






[ Post a follow-up to this message ]



    Re: Single application instance using lockfiles  
Mark Rafn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-05 11:02 PM

>>     What's your outer problem? If two different users want to run your 

John Smith <john.smith@x-formation.com> wrote:[vbcol=seagreen]
>I have a network daemon which hosts a database to clients. The database can
>only be accessed by one daemon at a time.

You should perhaps look into locking on the database (assuming you mean a
database file, rather than an RDBMS connection), rather than a
system-wide restriction.  Is it possible that two different underlying
databases might need to be accessed by two distinct daemons?

Alternately, if the daemon listens on a specific network port, you already g
et
locking because you'll fail to bind to it if another process already has
it.  If you need to run two, you can, but they need to specify different
ports.

>daemons so only one instance is running. Naturally you can easily hosts mor
e
>daemons+database on each their machine but not on one.

A lot of systems can hosts multiple instances of a database on one machine,
with different connection methods.

>Since root will be able to delete files I assume I must have a mechanism to
>detect if the lock is still valid. E.g. run a thread every now and then and
>make sure we are still locked.

Yes, if this is a problem, you'll want to exit if your lockfile is removed o
r
changed.  If you define your uniqueness requirement to be network port or
specific database file, this won't be a problem, as root would have to kill
your process or destroy your database to nullify your lock.
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>





[ Post a follow-up to this message ]



    Re: Single application instance using lockfiles  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-16-05 07:48 AM


"John Smith" <john.smith@x-formation.com> wrote in message
news:4212480c$0$48638$edfadb0f@dread15.news.tele.dk...
 
[vbcol=seagreen]
> I have a network daemon which hosts a database to clients. The database
> can
> only be accessed by one daemon at a time. Thats why I want to lock out
> other
> daemons so only one instance is running. Naturally you can easily hosts
> more
> daemons+database on each their machine but not on one.

Since the database can only be accessed by one daemon at a time,
attempting to start a second will fail when it tries to access the database.
So you don't have a problem.

DS







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:47 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register