test.c
#include
#include
void handler(void); int main(int argc, char argv) { sigset( SIGTERM, handler );//signal( SIGTERM, handler ); printf( "Process_pid=[%d]\n", getpid() ); while(1); return 0; } void handler(void) { printf("Get a SIGTERM signal!\n"); }
send.c 用于发送SIGTERM 信号给test
#include
#include
int main( int argc, char argv ) { if( argc != 2 ) { printf( "Usage: ./send process_pid\n" ); return -1; } printf( "You will send a signal to the process=[%s]!\n", argv[1] ); kill( atoi(argv[1]), SIGTERM ); printf( "Send over!\n" ); return 0; }
SIGTERM : 程序结束信号,与SIGKILL不同的是该信号可以被阻塞和处理。通常用来要求程序自己正常退出。Shell命令kill 默认产生这个信号。
gcc test.c -o test gcc send.c -o send
运行程序:
./test
./send 12345 //12345是test的pid
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/207404.html原文链接:https://javaforall.net
