标签:apue unix环境高级编程 信号 signal sigaction
#include <signal.h>
void (*signal(int signo,void (*func)(int)))(int);
Returns: previous disposition of signal (see following) if OK,SIG_ERR on error
#define SIG_ERR (void (*)())-1
#define SIG_DFL (void (*)())0
#define SIG_IGN (void (*)())
typedef void Sigfunc(int);
Sigfunc *signal(int, Sigfunc *)
#include "apue.h"
#include "myerr.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");
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);
}
~
windeal@ubuntu:~/Windeal/apue$ ./exe &
[1] 2982
windeal@ubuntu:~/Windeal/apue$ kill -USR1 2982
windeal@ubuntu:~/Windeal/apue$ received SIGUSR1
kill -USR2 2982
received SIGUSR2
windeal@ubuntu:~/Windeal/apue$ kill 2982
[1]+ Terminated ./exe
windeal@ubuntu:~/Windeal/apue$
void sig_int(int), sig_quit(int);
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
signal(SIGINT, sig_int);
if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
signal(SIGQUIT, sig_quit);
/* STATUS_1 */
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
/* STATUS_2 */
signal(SIGINT, sig_int);
/* STATUS_3 */
#include <signal.h>
int sigaction(int signo,const struct sigaction *restrict act,struct sigaction *restrict oact);
Returns: 0 if OK,?1 on error
struct sigaction {
void (*sa_handler)(int); /* addr of signal handler, */
/* or SIG_IGN, or SIG_DFL */
sigset_t sa_mask; /* additional signals to block */
int sa_flags; /* signal options, Figure 10.16 */
/* alternate handler */
void (*sa_sigaction)(int, siginfo_t *, void *);
};
//设置新的信号屏蔽字, 阻塞信号
sigaction(signo, act, oact);//调用sigaction函数
//复位信号屏蔽字
sa_sigaction(int signo, siginfo_t* info,void *context);
sa_handler(signo);
struct siginfo {
int si_signo; /* signal number */
int si_errno; /* if nonzero, errno value from errno.h */
int si_code; /* additional info (depends on signal) */
pid_t si_pid; /* sending process ID */
uid_t si_uid; /* sending process real user ID */
void *si_addr; /* address that caused the fault */
int si_status; /* exit value or signal number */
union sigval si_value; /* application-specific value */
/* possibly other fields also */
};
union sigval{
int sival_int;
void *sival_ptr;
}
#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 {
act.sa_flags |= SA_RESTART;
}
if (sigaction(signo, &act, &oact) < 0)
return(SIG_ERR);
return(oact.sa_handler);
}
APUE学习笔记——10信号——信号接口函数 signal 和 sigaction
标签:apue unix环境高级编程 信号 signal sigaction
原文地址:http://blog.csdn.net/windeal3203/article/details/39293453