#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);
}
我假设我这样做是对的,我将如何在命令行中调用 sighup?它需要调用 sighup 来打印我的计数。
最佳答案
从技术上讲,在信号处理程序中执行 I/O 是不安全的,最好设置一个标志,观察它,并根据标志进行打印。 在 posix 系统上,您应该能够从命令行“kill -HUP”来发送信号。
关于c - 带有 sighup 的信号处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17734083/