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. (Callingprintf in 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.a is zero andmemory.b is 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 thanint are 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.
openorread is 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 forEINTR after 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 code
EINTR ,TEMP_FAILURE_RETRY evaluates it again,and over and over until the result is not a temporary failure. (编辑:威海站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|