#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