#include <signal.h>
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
struct sigaction {
void (*sa_handler) (int);
void (*sa_sigaction)(int, siginof_t *, void *);
int sa_flags;
void (*sa_restorer) (void);
}
siginfo_t {
int si_signo; /* Signal number ,信号编号*/
int si_errno; /* An errno value ,错误码*/
int si_code; /* Signal code ,信号产生的原因*/
int si_trapno; /* Trap number that caused ,引起硬件产生信号的陷阱号(在大部分的系统中不用)
hardware-generated signal
(unused on most architectures) */
pid_t si_pid; /* Sending process ID ,发送信号的进程 ID*/
uid_t si_uid; /* Real user ID of sending process ,发送信号进程真实用户 ID*/
int si_status; /* Exit value or signal ,状态编号*/
clock_t si_utime; /* User time consumed ,耗费的用户空间的时间*/
clock_t si_stime; /* System time consumed ,耗费的系统内核的时间*/
sigval_t si_value; /* Signal value ,信号值*/
int si_int; /* POSIX.1b signal ,用于传递数据*/
void *si_ptr; /* POSIX.1b signal ,用户传递数据*/
int si_overrun; /* Timer overrun count; POSIX.1b timers ,超时计数*/
int si_timerid; /* Timer ID; POSIX.1b timers ,时间 ID*/
void *si_addr; /* Memory location which caused fault ,产生内存访问错误的内存地址*/
int si_band; /* Band event ,band 事件*/
int si_fd; /* File descriptor 文件描述符*/
}
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int temp = 0;
struct sigaction act;
/*信号处理函数*/
void handler_sigint(int signo)
{
printf("recv SIGINT\\n");
sleep(5);
temp += 1;
printf("the value of temp is: %d\\n", temp);
sleep(5);
temp = temp + 2 ;
printf("再加一次temp :%d\\n",temp);
printf("in handler_sigint, after sleep\\n");
}
int main()
{
// struct sigaction act;
act.sa_handler = handler_sigint;
act.sa_flags = SA_NOMASK;
/*安装信号处理函数*/
sigaction(SIGINT, &act, NULL);
while(1)
;
return 0;
}
beyes@linux-beyes:~/C/base>beyes@linux-beyes:~/C/base> ./sigaction.exe
^Crecv SIGINT
^Crecv SIGINT
the value of temp is: 1
再加一次temp :3
in handler_sigint, after sleep
the value of temp is: 4
再加一次temp :6
in handler_sigint, after sleep
^\\退出
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int temp = 0;
struct sigaction act;
/*信号处理函数*/
void handler_sigint(int signo)
{
printf("recv SIGINT\\n");
sleep(5);
temp += 1;
printf("the value of temp is: %d\\n", temp);
printf("in handler_sigint, after sleep\\n");
}
int main()
{
// struct sigaction act;
act.sa_handler = handler_sigint;
// act.sa_flags = SA_NOMASK;
/*安装信号处理函数*/
sigaction(SIGINT, &act, NULL);
while(1)
;
return 0;
}
beyes@linux-beyes:~/C/base> ./sigaction.exe
^Crecv SIGINT
^C^C^C^C^Cthe value of temp is: 1
in handler_sigint, after sleep
recv SIGINT
the value of temp is: 2
in handler_sigint, after sleep
^\\退出
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |