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:
Flocking the socket filehandle does not grant an exclusive lock.* set autoflush to on ($| = 1) * using print and syswrite (both are garbled) * flocking the socket filehandle in the child processes
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
In reply to forking() flocking() on sockets by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |