[plug] [OT] Perl Sub Modify Global Variable

Russell Steicke r.steicke at bom.gov.au
Wed Feb 9 20:44:07 WST 2005


On Wed, Feb 09, 2005 at 03:31:14PM +0800, Timothy White wrote:
> Ok. Still no luck.
> It appears that because I am forking I can't share between forks. This
> is a real bummer.
> It is especially annoying because I am writing a forking server to
> handle requests and I want the user_database to be shared between them all.
> 
> Any ideas?
> 
> Tim
> NB: Sorry anyone who pays per kb, I know I should just post the code on
> a website.
> p.s. I tried a small test script[2] to see if I could get the global and
> forking working. below are the results[1].
> Basically I have a fork which runs 2 subs. One of the subs sits there
> monitoring the variable and changing it to a preset value each time. The
> other sub calls 2 subs, one after the other, each one checks the
> variable and then updates it.

You might want to look at a more traditional unix way of handling
things like this: select().  No threads, so no locking issues.  Your
server is likely to be waiting on IO most of the time anyway, so it
uses select() to wait for any of the file descriptors that become
ready.  Your read set in the select always includes the listening
socket file descriptor.  When that fd returns readable, call accept on
it and add the new file descriptor to the read set.  Each time
select() returns you look at the active file descriptors and do
whatever they require.

select_tut(2) will tell you more.

You actually end up writing a state machine driven by select(), but
it's not that hard.




-- 
Russell Steicke

-- Fortune says:
FOOLED you!  Absorb EGO SHATTERING impulse rays, polyester poltroon!!



More information about the plug mailing list