signal()信号-3-实验
一、信号监控
#include <stdio.h> #include <unistd.h> #include <signal.h> #include <stdlib.h> #define LOG_TAG "Ham: " #include <log/log.h> int smenu[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64 }; #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0])) int g_choice; void sig_handler(int sig) { printf("Ham: Caught signal %d\n", sig); ALOGI("Caught signal %d\n", sig); if (g_choice == 0) exit(1); } int main(int argc, char *argv[]) { int i; if (argc > 1) g_choice = 1; for (i = 0; i < ARRAY_SIZE(smenu); i++) { signal(smenu[i], sig_handler); } while(1) { sleep(1); } return 0; }
kill -9、-19 使用这两个不可屏蔽不可捕获的信号,还是可杀掉,且信号值打印不出来,不可捕捉。kill -l 可查看各个信号的值。
若想adb终端退出后程序不退出,让其捕捉 SIGHUP 信号,adb shell终端退出再次进入,之前窗口启的放后台运行的进程还在。
放前台运行的进程,即使其捕获了 SIGHUP 信号,窗口退出后还是会退出。这说明窗口中启动的前台运行进程在窗口退出后还有其它机制会杀它。
posted on 2024-03-28 11:44 Hello-World3 阅读(17) 评论(0) 编辑 收藏 举报