gmpassos has asked for the wisdom of the Perl Monks concerning the following question:

I need to make some amount of data shared between 5 process on Win32. Well, they aren't process, they are forked childs. And the amount of data can be only 1 byte.

For now I do this using a read & write FILEHANDLE where they write to the last byte of a file. Then I check the end of the file to synchronize the childs. But I can't use a file in the final release, since I can't write data to the machine! There is a way to do using only the memory on Win32, without threads?

This can't be done by pipe, since I need to read & write and is for more than 2 childs.

But why I need this? I'm using a shared socket, in the port 80, between the childs. This socket has listen set to 5 and reuse is on. On Win32, if 2 childs make an accept() in the same time, they can't get the new connection of the client, and we need to wait for the socket timeout to try the accept again. In other words, I can't share very well the port between the childs.

What I'm doing is a HTTPD that accept multi connections in the same time to run Perl scripts, simulating CGI.

Or if someone know how to avoid the accept() problem, please reply! I already tried to use IO::Select, but nothing. I tried to use random sleep() or select(undef,undef,undef,rand(2)) to unsynchronize the accept() call, but doesn't work very well!

Or if someone already made a shared HTPPD on Perl, please comment.

Note, for who have tried to make a shared port with HTTP::Daemon, you need to fix a bug in the module, that unable the use of the socket handle inside childs, fork, or threads. Just put this in your code:

package HTTP::Daemon ; # Fix a bug on sockname of IO::Socket, that block the app for multi ch +ilds with # the same server opened. Or for a GLOB blessed in a package different + than # IO::Socket, since the socket of the server is blessed to HTTP::Daemo +n! (I saw the bug only on Win32). sub sockname { #getsockname($_[0]); getsockname( fileno($_[0]) ); }

PS: Need to work with Perl-5.6.1.

Graciliano M. P.
"The creativity is the expression of the liberty".

Replies are listed 'Best First'.
Re: Shared Memory on Win32? And Share port for HTTP::Daemon.
by Jenda (Abbot) on Feb 24, 2003 at 16:15 UTC

    I think Win32::Mutex could be of help here. If you can't block all children on select(), use a mutex. So that 1 child owns the mutex, waits on the select() and the other children wait for the mutex. When the first child wakes up on the select() it releases the mutex, starts processing the data and one of the other children wakes up as well, goes to the select() and blocks there.:

    use Win32::Mutex; $mut = Win32::Mutex->new(0); ... fork_the_children; ... #and then in the children use ... $mut->wait(); $nfound = select($rout=$rin, $wout=$win, $eout=$ein, undef); $mut->release(); ... #!!! you should preven the mutex from being destroyed by # any of the children. It should only be destroyed by the # main thread and only after all children finished !!! ... # just before a child thread finishes bless $mut, 'do not destroy'; }

    Jenda

Re: Shared Memory on Win32? And Share port for HTTP::Daemon.
by meetraz (Hermit) on Feb 24, 2003 at 19:59 UTC
    There is a shared memory module for Win32, called Win32::MemMap which is even easier to use if combined with Tie::Win32MemMap

    I looked for a download, but couldn't find one. If anybody knows where this is at, please let me know.

      You can install both by PPM from http://jenda.krynicky.cz/perl/

      Jenda

        Thanks Jenda.. I tried that in the past, but I've always gotten this error:

        ppm> install Win32::MemMap Can't call method "find_impl" without a package or object reference at + E:/Perl/site/lib/PPM/UI.pm line 1160, <$__ANONIO__> line 2.

        And I've also tried this:

        ppm> install http://jenda.krynicky.cz/perl/Win32-MemMap.ppd Can't call method "name" without a package or object reference at E:/P +erl/site/lib/PPM/UI.pm line 984, <FIN> line 2.