test.c
#include <stdio.h>
#include <signal.h>
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 <stdio.h>
#include <signal.h>
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
终端下输入:
kill 12345
也会给test发送SIGTERM 信号。
or kill -15 12345
kill -SIGTERM 12345