[英]SIGHUP signal handler reset to default when using system() on embedded linux with busybox
[英]signal handler with sighup
#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系統上,你應該能夠從命令行“殺死-HUP”來發送信號。
您可以使用kill -SIGHUP <pid>
,其中<pid>
是代碼的進程ID。
在控制台中試試這個:
ps -a
讀出你的程序的pid
kill -s HUP target_pid
在這里,您有一個kill的手冊頁,其中包含一系列信號。
編輯:更簡單,你可以使用killall:
killall -HUP nameofyourbinary
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.