加入收藏 | 设为首页 | 会员中心 | 我要投稿 威海站长网 (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

...
{
sig_atomic_t prev = last_process_status_change;
last_process_status_change = process_status_change;
if (last_process_status_change != prev) {
struct process *p;
for (p = process_list; p; p = p->next)
if (p->have_status) {
... Examine p->status ...
}
}
}

intvariable into another can take two instructions on most machines.

    If your handler needs to access any global variables from your program,declare those variablesvolatile. This tells the compiler that the value of the variable might change asynchronously,and inhibits certain optimizations that would be invalidated by such modifications.
  • If you call a function in the handler,make sure it isreentrantwith respect to signals,or else make sure that the signal cannot interrupt a call to a related function.

    If a function uses a static variable or a global variable,or a dynamically-allocated object that it finds for itself,then it is non-reentrant and any two calls to the function can interfere. For example,suppose that the signal handler usesgethostbyname. This function returns its value in a static object,reusing the same object each time. If the signal happens to arrive during a call togethostbyname,or even after one (while the program is still using the value),it will clobber the value that the program asked for. However,if the program does not usegethostbynameor any other function that returns information in the same object,or if it always blocks signals around each use,then you are safe. There are a large number of library functions that return values in a fixed object,always reusing the same object in this fashion,and all of them cause the same problem. The description of a function in this manual always mentions this behavior.
  • If a function uses and modifies an object that you supply,then it is potentially non-reentrant; two calls can interfere if they use the same object. This case arises when you do I/O using streams. Suppose that the signal handler prints a message withfprintf. Suppose that the program was in the middle of anfprintfcall using the same stream when the signal was delivered. Both the signal handler's message and the program's data could be corrupted,because both calls operate on the same data structure--the stream itself. However,if you know that the stream that the handler uses cannot possibly be used by the program at a time when signals can arrive,then you are safe. It is no problem if the program uses some other stream.
  • On most systems,mallocandfreeare not reentrant,because they use a static data structure which records what memory blocks are free. As a result,no library functions that allocate or free memory are reentrant. This includes functions that allocate space to store a result. The best way to avoid the need to allocate memory in a handler is to allocate in advance space for signal handlers to use. The best way to avoid freeing memory in a handler is to flag or record the objects to be freed,and have the program check from time to time whether anything is waiting to be freed. But this must be done with care,because placing an object on a chain is not atomic,and if it is interrupted by another signal handler that does the same thing,you could "lose" one of the objects. The relocating allocation functions (see section) are certainly not safe to use in a signal handler.
  • Any function that modifieserrnois non-reentrant,but you can correct for this: in the handler,save the original value oferrnoand restore it before returning normally. This prevents errors that occur within the signal handler from being confused with errors from system calls at the point the program is interrupted to run the handler. This technique is generally applicable; if you want to call in a handler a function that modifies a particular object in memory,you can make this safe by saving and restoring that object.
  • Merely reading from a memory object is safe provided that you can deal with any of the values that might appear in the object at a time when the signal can be delivered. Keep in mind that assignment to some data types requires more than one instruction,which means that the handler could run "in the middle of" an assignment to the variable if its type is not atomic. See section.
  • Merely writing into a memory object is safe as long as a sudden change in the value,at any time when the handler might run,will not disturb anything.

#include 
#include 

struct two_words { int a,b; } memory;

void
handler(int signum)
{
printf ("%d,%dn",memory.a,memory.b);
alarm (1);
}

(编辑:威海站长网)

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

热点阅读