Re: Preventing multiple invocations of script from accessing the same file

From: Martin Gregorie (martin_at_see.sig.for.address)
Date: 11/27/05


Date: Sun, 27 Nov 2005 22:18:58 +0000

Mark Hobley wrote:
> I am writing a CGI script using the Bourne shell. I want to update a file
> based on input obtained.
>
> If another person is accessing the same web page, the script may
> be invoked a second time, whilst the first ocurrence is still running.
>
> How can I configure some sort of lock, so that the second copy of the
> script will not continue, until the first copy of the script has done
> the update.
>
I'd suggest that you don't do it that way.

The easy way to avoid conflicts without using locks is to move all the
critical stuff into a single threaded server which only ever runs as a
single instance and listens on a named pipe or a port.

Your script needs to call a simple program that opens a connection to
the server, passes the request to the server, waits for the response,
closes the connection and quits.

The server should be written to receive a request, process it using
synchronous i/o and return a response. Because the server is single
threaded it will serialize requests: while it is handling one request it
won't accept another connection, let alone overlap it with another
request. The server needs to use poll() to handle the listener port and
the accepted connections but should not use it for anything else apart
from trapping signals. This assumes a single copy of a permanent server
is started at boot time.

The other way to run the server is to let xinetd start the server on
demand and configure xinetd to allow only one copy of the server to be
started. This makes the server as easy to write as a normal filter. It
gets the request via stdin and writes the response to stdout. It doesn't
use poll() at all.

HTH

-- 
martin@   | Martin Gregorie
gregorie. |
org       | Zappa fan & glider pilot


Relevant Pages

  • Re: Anonymous Anonymity - Request For Comments
    ... > and request that you reply directly to my e-mail address. ... > for the entity wishing to preserve their anonymity. ... > the machine can perform as a Intermediary Server and / or as a Intermediary ... > The software then attempts connection to a Intermediary Server. ...
    (Bugtraq)
  • Re: Performance Issue with Runtime Image
    ... >> the client, closes the connection, then dies. ... request before even accepting the next incoming connection. ... The client program is unaffected so presumably the server is ...
    (comp.lang.smalltalk.dolphin)
  • Re: Difference between storing files on folder and in mysql db
    ... a separate instance of an image display script (and a separate ... connection if images were stored in the file system. ... know or care if the image is from a database or not. ... but the DB server does. ...
    (comp.lang.php)
  • Re: IIS 6.0 Windows Authentication 401 Every Request
    ... both working for an internal server. ... every request to a page, it'll throw a 401, and then the next request ... It is up to the client to provide evidence, ... the request or connection maintained. ...
    (microsoft.public.inetserver.iis.security)
  • Re: Does apache stop a script mid execution ?
    ... If the user calls a time consuming script and then stops or refreshes ... How does it relate to e.g. a script performing a large mysql query? ... 2.b) how the connection is set up. ... the server stops the script execution. ...
    (comp.lang.php)