Disclaimer

The following note applies to unix-like system calls only. My mojo does not apply to windows.

As tye apparently explained to you, most SysV-style (and by exension, linux) system calls are interruptible. The wait(2) and waitpid(2) calls are examples.

Since your database connection to MySQL needs to use sockets to communicate with the database, you may receive signals (SIGPOLL is common with asynchronous I/O, for example). Likewise, if you have a second process in the background, it can send a SIGCLD when it exits. And timers send SIGALRM.

The fun part of this is that Unix only stacks one of each signal for calls to the handler -- so your signal handler might only get called once even when two processes die. This doesn't just apply to $SIG{FOO} handlers, it also applies to the implicit handler in wait() calls. I haven't (yet) walked the Perl source to satisfy my curiosity, but I expect that since it uses waitpid(), it's trapping that case reasonably well.

Some test cases I ran using system() and alarm() shows that the $! value sometimes (but not always) gets set to "interrupted system call" when this occurs. It depends on where you are in the system call when the signal arrives, I think. In any event, my general rule is to trust the value of ( $? >> 8 ), which gives the return status of the child process. YMMV, especially if you were to create more than one child process -- i don't know which one would end up in $?.

I have definitely seen the case where the signal arrives and the child process continues running to completion long after the return code is "returned." In C, I avoid the whole thing by calling popen() and reading until end-of-file. I don't know if you can apply that technique to wget in perl using open my $fh, '-|', $cmd or not.


In reply to Re: System call + signals = bad return code? by papidave
in thread System call + signals = bad return code? by swkronenfeld

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.