锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

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

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

线程捕获、忽略和等待信号

Outline 大纲

Use signal masks and handlers 使用信号掩码和处理程序
Signals and threads 信号和线程

Catching and Ignoring Signals -SIGACTION 捕获信号和忽略信号-SIGACTION

sigactionfunction allows the caller to examine or specify action associated with a specific signal sigactionfunction允许调用者检查或指定与特定信号相关行动
Program installs signal handler by calling sigaction with the name of a user-written function. 程序通过调用sigaction,配合一个用户编写的函数的名称,来安装信号处理程序。
The functionsigactionis used to specify what is to happen to a signal when it is delivered. sigaction功能用来指定,当信号被传输时,发生了什么
#include <signal.h>
intsigaction(intsigno, const structsigaction*act,
structsigaction*oact);
structsigaction{
void (*sa_handler)(int);/* SIG_DFL, SIG_IGN or pointer to function */
sigset_tsa_mask;/* additional signals to be blocked during execution of handler */
intsa_flags;/*special flags and options */
void(*sa_sigaction) (int, siginfo_t*, void *);/* real-time handler */ };
1. Eitheractor oact may be NULL. Eitheractor oact 可能是NULL。
2. If SA_SIGINFOflagof sa_flagsfield is clear,sa_handlerspecifies the action to be taken. 如果其中sa_flags的SA_SIGINFO标志清晰,sa_handler就要采取行动。

Example: Set up Signal Handler for SIGINT 例子:设置信号处理程序为SIGINT

  structsigactionnewact;
newact.sa_handler= mysighand; /*set new handler*/设立新的处理程序
newact.sa_flags= 0;/* no special options *// / *没有特殊选项
if ((sigemptyset(&newact,sa_mask)== -1) ||
(sigaction(SIGINT, &newact, NULL)== -1))
perror(“Failedto install SIGINT signal handler”);

Set up Signal Handler that Catches SIGINT Generated by Ctrl-C 设置信号处理程序,捕捉按Ctrl-C生成的SIGINT

 void catchctrlc(intsigno){
char handmsg[] = "I found Ctrl-C\n";
intmsglen= sizeof(handmsg);
write(STDERR_FILENO, handmsg, msglen);
}

structsigactionact;
act.sa_handler= catchctrlc;
act.sa_flags= 0;
if ((sigemptyset(&act.sa_mask)== -1) ||
(sigaction(SIGINT, &act, NULL)== -1))
perror("Failedto set SIGINT to handle Ctrl-C");
Note: write is async-signal safe –meaning it can be called inside a signal handler. Not so for print for strlen.
注意:write是安全的异步信号——意思是它能够在信号处理程序中被调用。 但print和strlen不一样。

Waiting for Signals 等待信号

Signals provide method for waiting for event without busy waiting 信号提供不忙碌等待事件的方法

Busy waiting 忙碌等待

Means continually using CPU cycles to test for occurrence of event 意味着不断使用CPU周期来测试事件的发生
Means a program does this testing by checking the value of variable in loop 是指一个程序通过检查变量的循环中值执行此测试

More Efficient Approach 更有效的方法

Suspend process until waited-for event occurs 暂停进程,等待事件发生
POSIX pause, sigsuspend, sigwait provide three mechanisms to suspend process until signal occurs POSIX标准的pause,sigsuspend,sigwait三种机制来挂起进程,直到发生信号
pause()–too simple, and problematic if signals are delivered before pause (cannot unblock the signal and start pause() in atomic way)

pause() - 太简单了,如果信号在pause前发布,会有疑问(不能对信号解除阻塞,并以原子的方式开始pause())

sigsuspend
sigsuspendfunction sets signal mask and suspends process until signal is caughtby the process sigsuspend函数设置信号掩码,直到信号被处理捕获暂停过程
sigsuspend returns when signal handler of the caught signal returns. 当时已捕获信号的信号处理程序返回,sigsuspend返回

#include <signal.h>
intsigsupend(constsigset_t*sigmask);

Example: sigsuspend
What’s wrong?
sigfillset(&sigmost);
sigdelset(&sigmost, signum);
sigsuspend(&sigmost);

signum is the only signal unblocked that can cause sigsuspend to return signum是唯一可能会导致sigsuspend返回的阻塞信号
If the signal signumis delivered before the start of the code segment, the process still suspends itself and deadlocksif another signumis not generated

如果信号是代码段开始前递送的signum,这个过程仍然自己暂停,并且另外一个锁定的signum不会产生。

Example: sigsuspend(Correct Way to Wait for Single Signal) 例如:sigsuspend(正确的方式等待单次信号)

 static volatile sig_atomic_tsigreceived=0;
/*assume signal handler has been setup for signumand it sets sigreceived=1*/
sigset_tmaskall, maskmost, maskold;
intsignum= SIGUSR1;
sigfillset(&maskall);
sigfillset(&maskmost);
sigdelset(&maskmost, signum);
sigprocmask(SIG_SETMASK, &maskall, &maskold);
if (sigreceived== 0)
sigsuspend(&maskmost);
sigprocmask(SIG_SETMASK, &maskold, NULL);
友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内