Linux SIGTERM 信号如何有效捕获处理?

2026-05-23 23:011阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计209个文字,预计阅读时间需要1分钟。

Linux SIGTERM 信号如何有效捕获处理?

c#include

int main() { printf(Hello, World!); return 0;}

Linux SIGTERM 信号如何有效捕获处理?

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 testgcc send.c -o send

运行程序:

./test./send 12345 //12345是test的pid

终端下输入: kill 12345 也会给test发送SIGTERM 信号。 or kill -15 12345 kill -SIGTERM 12345

本文共计209个文字,预计阅读时间需要1分钟。

Linux SIGTERM 信号如何有效捕获处理?

c#include

int main() { printf(Hello, World!); return 0;}

Linux SIGTERM 信号如何有效捕获处理?

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 testgcc send.c -o send

运行程序:

./test./send 12345 //12345是test的pid

终端下输入: kill 12345 也会给test发送SIGTERM 信号。 or kill -15 12345 kill -SIGTERM 12345