【linux】linux C 程序 注册信号处理函数
static void signal_handler_reboot(int32_t theSignal)
{
int i;
int sockfd;
char log_str_buf[LOG_STR_BUF_LEN];
signal(SIGPIPE, SIG_IGN);
if (SIGKILL == theSignal || SIGTERM == theSignal) //we can know when system excute child thread is end
{
snprintf(log_str_buf, LOG_STR_BUF_LEN, "receive kill signal exit the server.");
LOG_INFO(LOG_LEVEL_FATAL, log_str_buf);
if (listen_fd != -1)
{
closesocket(listen_fd);
listen_fd = -1;
}
exit_flag = 1;
exit_accept_flag = 1;
for (i = 0; i < MAX_EVENTS; i++)
{
sockfd = get_fd_by_event_index(i);
if (sockfd != -1)
{
closesocket(sockfd);
}
}
#if CONNECT_TO_SQL_SUCCESS
sql_pool_destroy();
#endif
}
else if (SIGPIPE == theSignal)
{
LOG_INFO(LOG_LEVEL_INFO, "SIGPIPE received.\n");
}
}
//注册信号函数,程序接收到对应信号后,做相应的处理
signal(SIGKILL, signal_handler_reboot); // register the signal
signal(SIGTERM, signal_handler_reboot);
signal(SIGPIPE, signal_handler_reboot);