锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 英语翻译 / 信号处理和线程
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。需要全文内容也请联系孙老师。

信号处理和线程

Use sigwait 使用sigwait

This function is much cleaner and avoids races and errors !!!! 这个函数是更简洁的,避免竞争和错误! ! ! !
1. First block all signals 首选阻塞所有信号
2. Put the signals you want to wait for in sigset_t 把信号放在你想要等待sigset_t中
3. Call sigwait 调用sigwait
4. sigwaitblocks the process until at least one of these signals is pending. 调用sigwait块处理的过程,直到在这些信号中至少有一个是待定的。
5. It removes one of the pending signals and gives you the corresponding signal number in the second parameter..删除一个等待信号,给你在第二个参数对应的信号数量. .
6. Do what you want: no signal handler needed. 做你想做的事情:不需要信号处理器。
7. It returns 0 on success and -1 on error with errnoset.它在成功时,返回0,错误时,返回-1

#include <signal.h>
intsigwait(constsigset_t*restrict sigmask, int*restrict signo);

Signal Handling & Threads 信号处理和线程

type and Delivery action 类型和交付行动
asynchronous 异步
Delivered to some thread that has it unblocked 交付给一些线程,它能够畅通
synchronous 同步
Delivered to thread that caused it 传递给线程引起的
directed 导向
Delivered to the identified thread 交付到指定的线程

Directing Signal to Particular Thread 指定信号到特定线程

#include <signal.h>
#include <pthread.h>
intpthread_kill(pthread_tthread, intsig);
If successful pthread_killreturns 0.
If (pthread_kill(pthread_self(), SIGKILL))
fprintf(stderr, “failed to commit suicide”\n”);
Example: Thread kills itself and the process it is in because SIGKILL cannot be caught, blocked, or ignored. 例子:线程杀死自己和它所在的进程,因为SIGKILL不能被捕获阻塞或忽略。
Not always the case for some signals 一些信号并非总是如此

Masking Signals for Threads 为线程屏蔽信号

Signal handlers are process-wide 信号处理程序是线程范围
Each thread has its own signal mask for process wide signals – only use sigprocmaskfor initialization before creating threads 每个线程都有自己的信号掩码的全过程——只有创建线程之前使用sigprocmaskfor初始化
Thread can线程可以:
Examine its signal mask 检查它的信号掩码
Set its signal mask 设置它的信号掩码
sigprocmaskfunction should not be usedwhen the process has multiple threads 当处理多个线程,不应使用sigprocmask功能

Masking Signals for Threads 为线程屏蔽信号

#include <pthread.h>
#include <signal.h>
intpthread_sigmask(inthow,
const sigset_t*restrict set,
sigset_t*restrict pset);
If ‘how’is SIG_SETMASK, then thread’s signal mask is replaced by ‘set’如果'how'是SIG_SETMASK,那么线程的信号掩码被"set"替换
If ‘how’is SIG_BLOCK, then additional signals in ‘set’are blocked by the thread 如果'how'是SIG_BLOCK,那么附加'在“set”中的信号会被该线程阻塞
If ‘how’is SIG_UNBLOCK, then any of blocked signals in ‘set’ are removed from the thread’s current signal mask 如果“how'是SIG_UNBLOCK,那么任何在”set“中受阻的信号会从线程的当前信号的覆盖中被删除

Dedicating Threads for Signal Handling 为信号处理而做的特别线程

Signal handlers are process-wide and installed with sigactionas in single-threaded processes 信号处理程序是进程范围形式的,用sigaction安装是在在单线程进程内
Distinction between process-wide signal handlersand thread-specific signal masksis important!!! 进程级信号处理程序和特定线程的信号掩码之间的区别是很重要的!
Remember: 记住:
When a signal is caught, the signal that caused the event is automatically blocked on entry to the signal handler 当信号被捕获,导致该事件的信号被自动屏蔽,禁止进入信号处理程序
With multi-threaded application, nothing prevents another signal of the same type from being delivered to another thread that has the signal unblocked 在多线程应用程序中,没有什么可以防止相同类型的另一个信号被传递到另一个线程并且信号畅通
A recommended strategy for dealing with signals in multi-threaded processes is to dedicate particular threads to signal handling

处理多线程信号推荐的策略是贡献特别的线程来处理信号

Dedicating Threads for Signal Handling 为信号处理而做的特别线程

Dedicate particular threads to handle signals 专门的线程来处理信号
Main thread blocks all signals before creating any child threads 在创建子线程之前主线程阻塞所有信号
Signal mask is inheritedby children信号屏蔽由子线程继承
Thread dedicated to signal handling executes sigwaiton specific signals to be handled or uses pthread_sigmaskto unblock signal 线程专用信号处理执行sigwaiton特定信号或使用pthread_sigmaskto开启信号
友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内