#include <stdio.h>
#include <stdlib.h>
#include <sys/signal.h>
#include <string.h>
void handler (int sig);
int count;
int main() {
struct sigaction act;
memset (&act, 0, sizeof (act));
act.sa_handler = handler;
if (sigaction (SIGHUP, &act, NULL) < 0) {
perror ("sigaction");
exit (-1);
}
int count = 0;
while(1) {
sleep(1);
count++;
}
}
void handler (int signal_number){
printf ("count is %d\n", count);
}
我假设我正在做这个权利,我将如何去在命令行中呼叫叹息?它需要打电话来打印我的计数。信号处理程序与叹息
为什么输出总是“计数为0”?为什么不是计数++处理 – DDukesterman
你重新声明了主数。 sig处理程序正在读取全局“count”。 – aet
修复了它。知道它俯瞰计数 – DDukesterman