linux 信号与多线程
在Linux的多线程中使用信号机制,与在进程中使用信号机制有着根本的区别,可以说是完全不同。在进程环境中,对信号的处理是,先注册信号处理函数,当信号异步发生时,调用处理函数来处理信号。它完全是异步的(我们完全不知到信号会在进程的那个执行点到来!)。然而信号处理函数的实现,有着许多的限制;比如有一些函数不能在信号处理函数中调用;再比如一些函数read、recv等调用时会被异步的信号给中断(interrupt),因此我们必须对在这些函数在调用时因为信号而中断的情况进行处理(判断函数返回时 enno 是否等于 EINTR)。
- sigwait - wait for a signal
- #include <signal.h>
- int sigwait(const sigset_t *set, int *sig);
- Description
- The sigwait() function suspends execution of the calling thread until the delivery of one
- of the signals specified in the signal set set. The function accepts the signal (removes
- it from the pending list of signals), and returns the signal number in sig.
- The operation of sigwait() is the same as sigwaitinfo(2), except that:
- * sigwait() only returns the signal number, rather than a siginfo_t structure describing
- the signal.
- * The return values of the two functions are different.
- Return Value
- On success, sigwait() returns 0. On error, it returns a positive error number.
- pthread_sigmask - examine and change mask of blocked signals
- #include <signal.h>
- int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);
- Compile and link with -pthread.
- DESCRIPTION
- The pthread_sigmask() function is just like sigprocmask(2), with the difference that its use
- in multithreaded programs is explicitly specified by POSIX.1-2001.
- Other differences are noted in this page.
- For a description of the arguments and operation of this function, see sigprocmask(2).
- RETURN VALUE
- On success, pthread_sigmask() returns 0; on error, it returns an error number.
- NOTES
- A new thread inherits a copy of its creator's signal mask.
- (from man sigprocmask: )
- The behavior of the call is dependent on the value of how, as follows.
- SIG_BLOCK
- The set of blocked signals is the union of the current set and the set argument.
- SIG_UNBLOCK
- The signals in set are removed from the current set of blocked signals. It is permissible
- to attempt to unblock a signal which is not blocked.
- SIG_SETMASK
- The set of blocked signals is set to the argument set.
- If oldset is non-NULL, the previous value of the signal mask is stored in oldset.
- If set is NULL, then the signal mask is unchanged (i.e., how is ignored), but the current
- value of the signal mask is nevertheless returned in oldset (if it is not NULL).
- #include <signal.h>
- int pthread_kill(pthread_t thread, int sig);
- Compile and link with -pthread.
- DESCRIPTION
- The pthread_kill() function sends the signal sig to thread, another thread in the same
- process as the caller. The signal is asynchronously directed to thread.
- If sig is 0, then no signal is sent, but error checking is still performed; this can be
- used to check for the existence of a thread ID.
- RETURN VALUE
- On success, pthread_kill() returns 0; on error, it returns an error number, and no signal
- is sent.
- ERRORS
- ESRCH No thread with the ID thread could be found.
- EINVAL An invalid signal was specified.
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <errno.h>
- /* Simple error handling functions */
- #define handle_error_en(en, msg) \
- do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
- static void *
- sig_thread(void *arg)
- {
- sigset_t *set = (sigset_t *) arg;
- int s, sig;
- for (;;) {
- s = sigwait(set, &sig);
- if (s != 0)
- handle_error_en(s, "sigwait");
- printf("Signal handling thread got signal %d\n", sig);
- }
- }
- int
- main(int argc, char *argv[])
- {
- pthread_t thread;
- sigset_t set;
- int s;
- /*
- Block SIGINT; other threads created by main() will inherit
- a copy of the signal mask.
- */
- sigemptyset(&set);
- sigaddset(&set, SIGQUIT);
- sigaddset(&set, SIGUSR1);
- s = pthread_sigmask(SIG_BLOCK, &set, NULL);
- if (s != 0)
- handle_error_en(s, "pthread_sigmask");
- s = pthread_create(&thread, NULL, &sig_thread, (void *) &set);
- if (s != 0)
- handle_error_en(s, "pthread_create");
- /*
- Main thread carries on to create other threads and/or do
- other work
- */
- pause(); /* Dummy pause so we can test program */
- return 0;
- }
- digdeep@ubuntu:~/pthread/learnthread$ gcc -Wall -pthread -o pthread_sigmask pthread_sigmask.c
- digdeep@ubuntu:~/pthread/learnthread$ ./pthread_sigmask &
- [1] 4170
- digdeep@ubuntu:~/pthread/learnthread$ kill -QUIT %1
- digdeep@ubuntu:~/pthread/learnthread$ Signal handling thread got signal 3
- digdeep@ubuntu:~/pthread/learnthread$ kill -USR1 %1
- digdeep@ubuntu:~/pthread/learnthread$ Signal handling thread got signal 10
- digdeep@ubuntu:~/pthread/learnthread$ kill -TERM %1
- digdeep@ubuntu:~/pthread/learnthread$
- [1]+ Terminated ./pthread_sigmask
- digdeep@ubuntu:~/pthread/learnthread$
linux 信号与多线程的更多相关文章
- 【转】 Linux下的多线程编程
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/原文链接:http://www.cnblogs.com/gnuhpc/archive/2012/12/07/280 ...
- Linux下的多线程编程
1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(proces ...
- 【转】Linux下的多线程编程
1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(proces ...
- 《转》Linux下的多线程编程
原地址:http://linux.chinaunix.net/doc/program/2001-08-11/642.shtml 1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程 ...
- [转帖]Windows和Linux对决(多进程多线程)
Windows和Linux对决(多进程多线程) https://blog.csdn.net/world_2015/article/details/44920467 太长了 还没看完.. 还是没太理解好 ...
- Linux信号机制
Linux信号(signal) 机制分析 [摘要]本文分析了Linux内核对于信号的实现机制和应用层的相关处理.首先介绍了软中断信号的本质及信号的两种不同分类方法尤其是不可靠信号的原理.接着分析了内核 ...
- Linux C++的多线程编程(转)
1. 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的Unix也支持线程的概念,但是在一个进程(proces ...
- xenomai内核解析之信号signal(一)---Linux信号机制
版权声明:本文为本文为博主原创文章,转载请注明出处.如有错误,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ 目录 1. Linux信号 1.1注册信号处理函数 ...
- 基于linux信号的timeout装饰器
在做基于ray的分布式任务处理时,偶尔遇到由于ray集群不稳定导致的长时间连接不上,进而导致程序卡死,无法向后端返回任务状态的情况.但是ray的初始化函数本身未实现超时机制,因此设计基于多线程+信号的 ...
随机推荐
- .Net在线编辑器:KindEditor及CkEditor+CkFinder配置说明
Net在线编辑器:KindEditor及CkEditor+CkFinder配置说明 一.KindEditor(免费) KindEditor是一套开源的HTML可视化编辑器,主要用于让用户在网站上获得所 ...
- Oracle Rman 增量备份与差异备份
一.增量与差异 关于Incremental增量备份级别: Oracle 9i 共有五种级别 0 1 2 3 4,0级最高-4级最低,0级是1级的基础以此类推. Oracle 10g官方文档明确指出增量 ...
- ReentrantReadWriteLock——读读共享(一)
多个线程可以同时读,读读是异步的.非互斥的 1.Service.java(封装的方法,供线程A和B访问) package ReentrantReadWriteLock; import java.uti ...
- 多种数据库之间的同步工具SymmetricDS
代码:https://github.com/JumpMind/symmetric-ds 原理: 通过触发器模式同步时,是将数据库的变化记录到某个系统表中,然后在客户端建立缓冲,并定期将变化push到接 ...
- 序列化 NSKeyedArchiver,NSPropertyListSerialization
到目前为止,看到oc实现的序列化方式有两种:NSKeyedArchiver,NSPropertyListSerialization. 在这两种序列化方式中,NSData都是序列化的目标.两种方式的不同 ...
- vue-cli 添加到生产环境问题总结
1. 路径问题 部署到生产环境后的实际链接为: 服务器项目路径 + serviceUrl的路径 创建 GlobalConstant.js 分别配置 开发环境和生产环境的 路径 (注:此处生 ...
- Object-C 基础笔记4---ARC内存管理
内存管理的原则 1,对你自己拥有的对象负责.你只能释放自己拥有的对象.(谁污染谁治理). 2,凡是通过retain,alloc,copy等于段获得了所有权对象,都必须在你不再使用的时候释放.调用rel ...
- C语言指针使用小记 (深入理解C指针 读后小记)
最近正值过年在家,新年初一,闲暇时间无事可做便把以前看过的书籍整理了一下,顺手也把这本“深入理解C指针”的书重新读了一遍,这本书总体感觉比较简单,但是还是不免有些地方是平时没有想到过或者没有注意到的, ...
- <div class="clear"></div>
<div class="clear"></div> 这里的clear是样式名.样式写在CSS文件中 从名称来看估计你的样式为:.clear {clear:b ...
- Java乱码解决之道
1.常见字符编码 ASCII编码: ASCII,American Standard Code for Information Interchange,是基于拉丁字母的一套电脑编码系统,主要用于显示现代 ...