【发布时间】:2018-03-31 22:22:58
【问题描述】:
我已经构建了一个函数(基于示例),它允许我忽略信号SIGINT。该函数计算用户按下CONTROL + C(中断SIGINT)的次数。函数如下
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
sig_atomic_t sigint_count = 0;
void handler (int signal_number)
{
++sigint_count;
printf ("SIGINT was raised %d times\n", sigint_count);
}
int main ()
{
struct sigaction sa; //Declaração da estrutura sigaction
memset (&sa, 0, sizeof (sa));//Libertação da memória usada
sa.sa_handler = &handler;
sigaction (SIGINT, &sa, NULL);
while(1);
return 0;
}
我怀疑是这行代码
sigaction (SIGINT, &sa, NULL);
我试图写另一个与NULL 不同的东西,但它不起作用。为什么NULL? sigaction中的那个NULL是什么意思?
PS:它可以按我的意愿工作
【问题讨论】:
-
我已经读过同一个网页但我还是不明白...
-
具体有什么不明白的?手册页逐字说明“如果 oldact 为非 NULL,则先前的操作将保存在 oldact 中。”对我来说,没有什么不要理解的,但如果你仍然理解,请告诉我们它到底是什么。
-
请问,究竟是什么“不起作用”?
-
OT:在许多其他函数中,从信号处理程序调用
printf()是不省事的。