加入收藏 | 设为首页 | 会员中心 | 我要投稿 威海站长网 (https://www.0631zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Signal Handling--ref

发布时间:2021-01-25 08:01:09 所属栏目:Linux 来源:网络整理
导读:signal is a software interrupt delivered to a process. The operating system uses signals to report exceptional situations to an executing program. Some signals report errors such as references to invalid memory addresses; others report asy

int
main (void)
{
static struct two_words zeros = { 0,0 },ones = { 1,1 };
signal (SIGALRM,handler);
memory = zeros;
alarm (1);
while (1)
{
memory = zeros;
memory = ones;
}
}

memorywith zeros,ones,zeros,alternating forever; meanwhile,once per second,the alarm signal handler prints the current contents. (Callingprintfin the handler is safe in this program because it is certainly not being called outside the handler when the signal happens.)

memory,and the value is stored one word at a time. If the signal is delivered in between these instructions,the handler might find thatmemory.ais zero andmemory.bis one (or vice versa).

memorywith just one instruction that cannot be interrupted. On these machines,the handler will always print two zeros or two ones.

sig_atomic_t. Reading and writing this data type is guaranteed to happen in a single instruction,so there's no way for a handler to run "in the middle" of an access.

sig_atomic_tis always an integer data type,but which one it is,and how many bits it contains,may vary from machine to machine.

sig_atomic_t
This is an integer data type. Objects of this type are always accessed atomically.

intand other integer types no longer thanintare atomic. You can also assume that pointer types are atomic; that is very convenient. Both of these are true on all of the machines that the GNU C library supports,and on all POSIX systems we know of.

openorreadis waiting for an I/O device. If the signal handler returns,the system faces the question: what should happen next?

EINTR. This is flexible,but usually inconvenient. Typically,POSIX applications that use signal handlers must check forEINTRafter each library function that can return it,in order to try the call again. Often programmers forget to check,which is a common source of error.

TEMP_FAILURE_RETRY:

TEMP_FAILURE_RETRY(expression)
This macro evaluatesexpressiononce. If it fails and reports error codeEINTR,TEMP_FAILURE_RETRYevaluates it again,and over and over until the result is not a temporary failure.

(编辑:威海站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读