
1. 与中断handler对应的,信号也有handler。每个特定的中断对应一个handler,每个特定的信号也对应一个handler。
2. 中断通过硬件或者int指令等方式触发handler执行,信号通过pthread_kill向某个线程发送信号触发handler执行。该线程收到信号后,会打断当前执行流并跳转到信号handler执行,之后返回到当前执行流。
- sigaction ———— 查询或设置信号处理方式
- (1)头文件
- #include
- (2)函数原型
- int sigaction(int signum,const struct sigaction *act ,struct sigaction *oldact);
- sigaction()会根据参数signum指定的信号编号来设置该信号的处理函数。参数signum可以指定SIGKILL和SIGSTOP以外的所有信号。
- (3)数据结构
- struct sigaction
- {
- void (*sa_handler) (int);
- sigset_t sa_mask;
- int sa_flags;
- void (*sa_restorer) (void);
- }
- sa_handler此参数和signal()的参数handler相同,代表新的信号处理函数,其他意义请参考signal()。
- sa_mask 用来设置在处理该信号时暂时将sa_mask 指定的信号搁置。
- sa_restorer 此参数没有使用。
- sa_flags 用来设置信号处理的其他相关操作,下列的数值可用。
- sigfillset ———— 将所有信号加入此信号集
- (1)头文件
- #include
- (2)函数原型
- int sigfillset(sigset_t * set);
- sigfillset()用来 将参数set信号集初始化,然后把所有的信号加入到此信号集里。
- sigemptyset ———— 初始化信号集
- (1)头文件
- #include
- (2)函数原型
- int sigemptyset(sigset_t *set);
- sigemptyset()用来将参数set信号集初始化并清空。
- pthread_sigmask ———— 更改或检查调用线程的信号掩码
- (1)头文件
- #include
- #include
- (2)函数原型
- int pthread_sigmask(int how, const sigset_t *new, sigset_t *old);
-
- how用来确定如何更改信号组,可以为以下值之一:
- SIG_BLOCK:向当前的信号掩码中添加new,其中new表示要阻塞的信号组。
- SIG_UNBLOCK:向当前的信号掩码中删除new,其中new表示要取消阻塞的信号组。
- SIG_SETMASK:将当前的信号掩码替换为new,其中new表示新的信号掩码。
- pthread_kill ———— 向线程发送信号
- (1) 头文件
- #include
- #include
- (2)函数原型
- int pthread_kill(thread_t tid, int sig);
- pthread_kill() 将信号sig发送到由tid指定的线程。tid所指定的线程必须与调用线程在同一个进程中。
- #include
- #include
- #include
-
- #define NUMTHREADS 3
- void sighand(int signo);
-
- void *threadfunc(void *parm)
- {
- pthread_t tid = pthread_self();
- int rc;
-
- printf("Thread %u entered/n", tid);
- rc = sleep(30); /* 若有信号中断则返回剩余秒数 */
- printf("Thread %u did not get expected results! rc=%d/n", tid, rc);
- return NULL;
- }
-
- void *threadmasked(void *parm)
- {
- pthread_t tid = pthread_self();
- sigset_t mask;
- int rc;
-
- printf("Masked thread %lu entered/n", tid);
-
- sigfillset(&mask); /* 将所有信号加入mask信号集 */
-
- /* 向当前的信号掩码中添加mask信号集 */
- rc = pthread_sigmask(SIG_BLOCK, &mask, NULL);
- if (rc != 0)
- {
- printf("%d, %s/n", rc, strerror(rc));
- return NULL;
- }
-
- rc = sleep(15);
- if (rc != 0)
- {
- printf("Masked thread %lu did not get expected results! rc=%d /n", tid, rc);
- return NULL;
- }
- printf("Masked thread %lu completed masked work/n", tid);
- return NULL;
- }
-
- int main(int argc, char **argv)
- {
- int rc;
- int i;
- struct sigaction actions;
- pthread_t threads[NUMTHREADS];
- pthread_t maskedthreads[NUMTHREADS];
-
- printf("Enter Testcase - %s/n", argv[0]);
- printf("Set up the alarm handler for the process/n");
-
- memset(&actions, 0, sizeof(actions));
- sigemptyset(&actions.sa_mask); /* 将参数set信号集初始化并清空 */
- actions.sa_flags = 0;
- actions.sa_handler = sighand;
-
- /* 设置SIGALRM的处理函数 */
- rc = sigaction(SIGALRM,&actions,NULL);
-
- printf("Create masked and unmasked threads/n");
- for(i=0; i
- {
- rc = pthread_create(&threads[i], NULL, threadfunc, NULL);
- if (rc != 0)
- {
- printf("%d, %s/n", rc, strerror(rc));
- return -1;
- }
-
- rc = pthread_create(&maskedthreads[i], NULL, threadmasked, NULL);
- if (rc != 0)
- {
- printf("%d, %s/n", rc, strerror(rc));
- return -1;
- }
- }
-
- sleep(3);
- printf("Send a signal to masked and unmasked threads/n");
-
- /* 向线程发送SIGALRM信号 */
- for(i=0; i
- {
- rc = pthread_kill(threads[i], SIGALRM);
- rc = pthread_kill(maskedthreads[i], SIGALRM);
- }
-
- printf("Wait for masked and unmasked threads to complete/n");
- for(i=0; i
- rc = pthread_join(threads[i], NULL);
- rc = pthread_join(maskedthreads[i], NULL);
- }
-
- printf("Main completed/n");
- return 0;
- }
-
- void sighand(int signo)
- {
- pthread_t tid = pthread_self();
-
- printf("Thread %lu in signal handler/n", tid);
- return;
- }
参考:
-
相关阅读:
AI语音机器人的重点功能配置之话术
flink cdc 没有Replication client ,Replication slave权限,报错,处理
离散数学复习:谓词逻辑
redis安装(Windows和linux)
浏览器开发者工具打开检测
二十一、数组(1)
泰拉瑞亚EasyBuildMod便捷建造模组开发详细过程
Hibernate 环境搭建
小球放盒子公式总结
webpack5 使用Thead多进程打包提升打包构建速度
-
原文地址:https://blog.csdn.net/denglin12315/article/details/126619152