At 5:30 PM -0800 3/30/01, Randy J. Ray wrote:
>I understand the forking model of Apache, and what that means in terms of
>data initialized in the start-up phase being ready-to-go in each child
>process. But what I need to do is manage it so that a particular value is
>shared between all children, such that changes made by one are recognized
>by the others. In a simple case, imagine wanting to count how many times
>total the given handler is invoked. Bumping a global counter will still be
>local to the given child process, and if part of the handler's interface is
>to report this counter value, then the reported number is going to be
>dependent upon which child answers the request.
>
>I'm needing to implement a handler that uses a true Singleton pattern for
>the class instance. One per server, not just one per process (or thread).
>
You'll need to use some form of persistance mechanism such as a
database, file, or perhaps (assuming you're on a Unix system)
something like System V shared memory or semaphores.
One quick 'n cheap way to implement mutual exclusion between Unix
processes (executing on the same processor) is to use mkdir, which is
atomic (ie once a process requests a mkdir, the mkdir will either be
done or rejected before the requesting process is preempted by any
other process).
So you can do
mkdir "xyz"
if "xyz" already exists, wait or return an error
read or write shared variable on disc
rmdir "xyz"
to guarantee that only one process at a time can be trying to access
a disc file.
There are many possible variations on this theme.