ÏÈÉÏ´úÂ룬¾¹ý¼ò»¯ÁË£¬Ö»ÇгöÀ´ÐźŵÄÒ»²¿·Ö£¬ÊÇÓÃÀ´²âÊÔÏ̳߳Øͬ²½µÄ£¬Ï̳߳ع¤×÷ÍêÈ«Õý³££¬Ã»ÓÐÈκÎÎÊÌ⣡
ÐźÅÔÚÏ̳߳ØÖеÄ×÷ÓÃҲûÓÐÈκÎÎÊÌâ¡£
ÔÚÅäÖÃSIGINTÐźŴ¦Àí¹ý³ÌÖУ¬ÒѾÔÚSA_MASKÖÐÉèÖÃ×èÈûSIGALRMÐźÅ
ÎÒµÄÀí½âÊÇ£ºÔÚsig_intFunº¯Êý´¦Àí¹ý³ÌÖУ¬SIGALRMËäÈ»ÒѾ²úÉú£¬µ«ÓÉÓÚÒѾÉèÖÃ×èÈûSIGALRMÐźţ¬
ËùÒÔsig_alarmFunº¯ÊýÖ»ÓÐÔÚsig_intFunº¯Êýµ÷ÓÃÍêÁ˲Żᱻµ÷Óá£
****************************************************************
ËùÒÔÔ¤Æڵĵ÷Óýá¹ûÊÇ£º
---test---
---test---
---test---
ͨ¹ýCTRL+C·¢ËÍSIGINTÐźÅ
--got a sigint--
--sleeping--
--sleeping--
...
--sleeping--
--got a sigalrm-- //ÔÚ10´ÎSLEEPÊä³öºó²ÅÊä³ö
*****************************************************************
µ«ÊÇʵ¼ÊµÄÊä³öÊÇ£º
---test---
---test---
---test---
ͨ¹ýCTRL+C·¢ËÍSIGINTÐźÅ
--got a sigint--
--sleeping--
--sleeping--
--got a sigalrm-- //alarm·¢Ëͺó£¬Á¢¼´²¶»ñSIGALRMÐźÅ
...
--sleeping--
ʵ¼Ê´úÂëÖУ¬sig_alarmFunÖÐÓÐÏß³ÌÖÕÖ¹µÄFLAG£¬´ÓÏß³ÌÖÕÖ¹µÄÐÅÏ¢À´¿´£¬sig_intFunÔÚµ÷ÓÃʱȷʵûÓÐ×èÈûSIGALRM
***********************´úÂë**************************
static void sig_alarmFun(int signo)
{
printf("--got a sigalrm--\n");
fflush(stdout);
}
static void sig_intFun(int signo)
{
int i;
printf("--got a sigint--\n");
while(i < 10)
{
printf("--sleeping--\n");
fflush(stdout);
sleep(1);
}
}
int main(int argc, char **argv)
{
struct sigaction act, oact;
act.sa_handler = sig_intFun;
sigemptyset(&act.sa_mask);
//ÒѾÉèÖÃÔÚSIGINT´¦Àíº¯ÊýÆÚ¼ä×èÈûSIGALRMÐźÅ
sigaddset(&act.sa_mask, SIGALRM);
act.flags = SA_RESTART | SA_RESETHAND;
sigaction(SIGINT, &act, &oact);
alarm(5);
act.sa_handler = sig_alarmFun;
sigemptyset(&act.sa_mask);
act.flags = SA_RESTART | SA_RESETHAND;
sigaction(SIGALRM, &act, &oact);
while(1)
{
printf("---test---\n");
fflush(stdout);
sleep(1);
}
return 0;
}
Çó¸÷λ´óÉñ°ïæ·ÖÎöһϵ½µ×ÊÇʲôÔÒò£¬ÎªÊ²Ã´ÀíÂÛÉÏsig_intFunµ÷ÓÃʱÀíÓ¦×èÈûSIGALRM£¬µ«Êµ¼Ê½á¹ûȴûÓÐÄØ£¿ÏÈлл´ó¼ÒÁË!
(¸Õ²ÅºÃÏñûµÇ½·¢ÁËÒ»´Î£¬µ«Ã»¿´µ½£¬Èç¹û·¢ÖØÁ˷dz£²»ºÃÒâ˼£¡)
Gout ÓÚ 2012-12-05 16:29:07·¢±í:
°ï¶¥