标签:
// signals/sigusr.c 10-1
#include "apue.h"
static void sig_usr(int); /* one handler for both signals */
int main(void)
{
if (signal(SIGUSR1, sig_usr) == SIG_ERR)
{
err_sys("can‘t catch SIGUSR1");
}
if (signal(SIGUSR2, sig_usr) == SIG_ERR)
{
err_sys("can‘t catch SIGUSR2");
}
/* SIGKILL is not available for this function
if (signal(SIGKILL, sig_usr) == SIG_ERR)
{
err_sys("can‘t catch SIGKILL");
}
*/
for ( ; ; )
{
pause();
}
}
static void sig_usr(int signo) /* argument is signal number */
{
if (signo == SIGUSR1)
{
printf("received SIGUSR1\n");
}
else if (signo == SIGUSR2)
{
printf("received SIGUSR2\n");
}
else
{
err_dump("received signal %d\n", signo);
}
}
// lib/signal.c
#include "apue.h"
/* Reliable version of signal(), using POSIX sigaction(). */
Sigfunc* signal(int signo, Sigfunc *func)
{
struct sigaction act, oact;
act.sa_handler = func;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
if (signo == SIGALRM) {
#ifdef SA_INTERRUPT
act.sa_flags |= SA_INTERRUPT;
#endif
} else {
#ifdef SA_RESTART
act.sa_flags |= SA_RESTART;
#endif
}
if (sigaction(signo, &act, &oact) < 0)
return(SIG_ERR);
return(oact.sa_handler);
}
标签:
原文地址:http://www.cnblogs.com/sunyongjie1984/p/4277819.html