I don't see any need for threads. Just do your work within the sig handler. I believe that Perl sets up sigaction mask to block all signals while inside of a handler, so there is no extra stuff required to prevent a re-entrant call into the handler. The below code calls handler every 5 seconds. If you do a kill -s USR1 pid, that causes an extra call to handler. The sleep 100 never actually completes because Perl implements sleep in terms of ALRM and when the alarm goes off, it cancels the sleep. I would definitely not use INT as one of your signals. I mean "read database and send to socket" is not an action one would normally think of for CTL-C! USR1 is a lot better and intended for this sort of thing.
#!/usr/bin/perl -w
use strict;
$SIG{USR1} = \&handler;
$SIG{ALRM} = \&handler;
while (1){alarm 5; sleep 100;}
sub handler
{
print "Do work \n"; #your get_message,send_message calls
}
Update: If SIGPIPE happens in send_message(), it won't be handled until after handler finishes. I don't see a big deal with that as since you don't have a handler installed for SIGPIPE, you are going to die anyway. I would consider connecting to the DB before the while(1)..leave connection open if you are going to use it every 10 seconds, ditto for the other socket.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.