#include <stdio.h> #include <signal.h> #include <string.h> #include <sys/types.h> #include <unistd.h> sig_atomic_t sigusr1_count = 0; void handler(int signal_number) { ++sigusr1_count; } int main(int argc, char* argv[]) { printf("the process ID is %d\n", (int)getpid()); struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = &handler; sigaction(SIGUSR1, &sa, NULL); int i = 0; while(i < 100) { sleep(1); i++; } printf("SIGUSR was raised %d times\n", sigusr1_count); return 0; } 編譯上述代碼為程式sigusr1 gcc -o sigusr1 sigusr1.c 運行程式 [liuchao@localhost Signal]$ ./sigusr1 the process ID is 3401 在另一個終端,用ps命令得到sigusr1的進程號 [liuchao@localhost ~]$ ps -a PID TTY TIME CMD 3401 pts/1 00:00:00 sigusr1 3403 pts/3 00:00:00 ps 向此進程號發送多個sigusr1訊號 [liuchao@localhost ~]$ kill -s SIGUSR1 3401 [liuchao@localhost ~]$ kill -s SIGUSR1 3401 [liuchao@localhost ~]$ kill -s SIGUSR1 3401 [liuchao@localhost ~]$ kill -s SIGUSR1 3401 [liuchao@localhost ~]$ kill -s SIGUSR1 3401 當進程結束後 [liuchao@localhost Signal]$ ./sigusr1 the process ID is 3401 SIGUSR was raised 5 times |