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

Hi,

I have a parent process that forks 10 children. I would like each child to get a copy of a 'master' socket, do some work, and then write to master socket - atomically.

Unfortunately, 1 out 10 times I run the script, the results are garbled - my current theory is the children are not doing atomic writes to the socket.

I've tried:

* set autoflush to on ($| = 1) * using print and syswrite (both are garbled) * flocking the socket filehandle in the child processes
Flocking the socket filehandle does not grant an exclusive lock.

The situation it similar to multiple web servers appending to a log file. I've read that one solution is to set the filehandle to '>> append' mode and use syswrite - but I'm not sure if this is going to work with sockets.

I'm also thinking about trying to 'lock' the socket by using a filesystem lock on a PID file - but this seems a bit kludgy.

And ideas?

Regards,

Nige

p.s. prior to forking the children I copy the socket to STDOUT with: *STDOUT = *{$c}; # $c = socket

Edit by tye

Replies are listed 'Best First'.
Re: forking() flocking() on sockets
by tadman (Prior) on Jan 24, 2002 at 20:54 UTC
    Child processes writing to a "shared socket" is a Bad Thing, and is probably best avoided. Apache manages to do this, but only with some very careful signal handling and locking, and even then, Apache is only trying to distribute the listen/accept component.

    I would suggest making a parent process which handles the multiplexing of the sockets. Like perlipc recommends, you would create a pipe between the parent and each child process, and then later, read these and forward anything read out the main socket. The parent just multiplexes and the children do the actual work.

    Use IO::Select and your job will be that much easier.

    As a note, the reason you use syswrite instead of print is that syswrite is unbuffered, always, regardless of $|. If your output is buffered, there are occasions where each process only writes a portion of a line, and this can make for bad output data, such as "AAAAAAAA\nBBBBBBBB\n" becoming "AAAABBBBBBBB\nAAAA\n".
Re: forking() flocking() on sockets
by ehdonhon (Curate) on Jan 24, 2002 at 23:36 UTC