Hallo monks,
I'm using

perl5 (revision 5.0 version 8 subversion 0)

on

Linux devvo 2.4.20-8smp #1 SMP Thu Mar 13 17:45:54 EST 2003 i686 i686 i386 GNU/Linux

I don't know if this is really a Perl problem or just a problem of my ignorance about forks.
In any case I've fixed it but I'm curious to know what is going on.
I had a daemon that listened on a socket using IO::Socket::SSL

while accept fork a handler
This was fine, but I needed to do some statistical analysis of connections and, in particular needed to be able to raise alarm events if particular nodes persistently didn't connect at all. To this end, each handler wrote little IPC files to say how they got on with the connection that they were given to handle. I then needed to have the main server periodically hoover up these files and do appropriate things. I thought 'I know, I'll put a call to the monitoring routine in the loop':

while accept fork a handler check any handler results
This would have been fine except in the most important case of failure (no-one connects at all) the loop would block on the accept and the check routine never ran so no alarm would be raised.
Easy, I thought, just fork off a monitor at the start:
fork a monitor to check results while accept fork a handler
And this worked. But it only ever handled one connection and then just appeared to block on the accept, ignoring any attempted connections. If I removed the call to fork the monitor everything worked fine. Odd.
Actually I've missed a crucial bit out of this. I was forking this daemon into background at the start like this:
fork myself into background fork a monitor while accept fork a handler
It occurred to me that it looked as though the 'fork a handler' fork was getting confused about which process was the right one to continue in after the fork. Almost like it was trying to resume in the most-recently-forked 'monitor' fork (which doesn't have the socket open) instead of the fork it came from. So I moved the monitor fork like this:
fork a monitor fork myself into background while accept fork a handler
And this works exactly as I would expect. What I'm curious about is, was the behaviour on the former configuration exactly what a well-informed person should expect or was it buggy aberrant behaviour?

In reply to Mangled Forks by mungohill

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.