📄 11-7.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
static sigset_t signal_mask; /* 阻塞信号 */
int main (int argc, char *argv[])
{
pthread_t sig_thr_id; /* 信号处理线程ID */
int rc; /* 返回编码*/
sigemptyset (&signal_mask);
sigaddset (&signal_mask, SIGINT);
sigaddset (&signal_mask, SIGTERM);
rc = pthread_sigmask (SIG_BLOCK, &signal_mask, NULL);
if (rc != 0) {
/* 做错误处理 */
...
}
/* 所创建的线程都可以继承信号mask*/
rc = pthread_create (&sig_thr_id, NULL, signal_thread, NULL);
if (rc != 0) {
/*出错处理 */
...
}
/* 应用程序代码 */
}
void *signal_thread (void *arg)
{
int sig_caught; /* 捕捉信号*/
int rc; /* 返回编码 */
rc = sigwait (&signal_mask, &sig_caught);
if (rc != 0) {
/* 出错处理 */
}
switch (sig_caught)
{
case SIGINT: /* SIGINT信号处理 */
...
break;
case SIGTERM: /* SIGTERM信号处理 */
...
break;
default: /* 默认处理*/
fprintf (stderr, "\nUnexpected signal %d\n", sig_caught);
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -