Linux的:为什么sig_atomic_t Typedef的为int? [英] Linux: Why is sig_atomic_t typedef'ed to int?
问题描述
在我的Linux系统中, sig_atomic_t
是一个普通的老式 INT
。做整数
拥有一种特殊的原子质量?
$ gcc的-v
使用内置的规格。
目标:x86_64的-Linux的GNU
...
线程模型:POSIX
gcc版本4.3.2(Debian的4.3.2-1.1)$回声'的#include<&signal.h中GT;' | GCC -E - | grep的原子
的typedef INT __sig_atomic_t;
的typedef __sig_atomic_t sig_atomic_t;
C99 sig_atomic_t
只听的原子一个非常弱的定义,因为的 C99有没有并发的概念的,只可中断。 (C2011增加了并发模型,并用它在 _Atomic
类型,使强大的保证,但是,据我所知 sig_atomic_t
不变,因为它的的存在的理由的仍然是信号处理,不能跨线程的通信。)
这是一切C99说,关于 sig_atomic_t
:
(§7.14
< signal.h中>
,第2段)中定义的类型sig_atomic_t
,这是(可能volatile限定)的整数类型,它可以作为一个原子实体进行访问,甚至在异步中断的presence的对象。 (§7.14<&signal.h中GT;
,第2段)
(§7.14p5)如果[A]不是调用
中止
或筹集
功能,行为是如果信号处理程序是指任何对象,具有比其他静态存储持续时间通过分配一个值来声明为一个对象未定义挥发性sig_atomic_t
。
如果
sig_atomic_t
(见7.14)(其他整数类型,第3款的§7.18.3限制)被定义为有符号整数类型,值SIG_ATOMIC_MIN
不得低于-127更大SIG_ATOMIC_MAX
的值应不小于127;否则,sig_atomic_t被定义为一个无符号整数类型和值SIG_ATOMIC_MIN
应是0和SIG_ATOMIC_MAX值
应不小于255的。
块引用>术语原子实体未在标准任何地方所定义。从翻译标准-ESE,在意图的是,CPU可以完全更新存储器类型
sig_atomic_t
(静态存储时间)的一个变量,一条机器指令。因此,在无并发,precisely中断C99抽象机,这是不可能的一个信号处理器来观察键入sig_atomic_t
的中途通过的变量更新的。该§7.18.3p3语言许可这种类型的是,如果必要的为字符
小。注意:请在的完全没有涉及跨处理器的一致性任何一种语言的的有需要多个指令来写一个大于
字符
大内存的CPU实际。还有一些需要多个指令真正的CPU将值写入比机器字小的(通常,但不一定,同为INT
)存储器。在GNU C库手册的语言是现在不准确的。它重新presents对原作者的一部分的愿望,以消除他们所看到的不必要许可证C实现做怪异的狗屎使人生更加难以应用程序员。不幸的是,这非常许可证是什么使得它可能有下以所有的一些实机。有至少一个嵌入式Linux端口(以AVR)并未INT
指针也可以写入内存在一个指令。 (人们在做手工更准确的工作,例如见<一href=\"http://sourceware.org/ml/libc-alpha/2012-02/msg00651.html\">http://sourceware.org/ml/libc-alpha/2012-02/msg00651.html -sig_atomic_t
似乎在那一个被错过,虽然)On my Linux box,
sig_atomic_t
is a plain oldint
. Doints
posses a special atomic quality?$ gcc -v Using built-in specs. Target: x86_64-linux-gnu ... Thread model: posix gcc version 4.3.2 (Debian 4.3.2-1.1) $ echo '#include <signal.h>' | gcc -E - | grep atomic typedef int __sig_atomic_t; typedef __sig_atomic_t sig_atomic_t;
解决方案C99
sig_atomic_t
conforms only to a very weak definition of "atomicity", because C99 has no concept of concurrency, only interruptibility. (C2011 adds a concurrency model, and with it the_Atomic
types that make stronger guarantees; however, AFAIKsig_atomic_t
is unchanged, since its raison d'être is still communication with signal handlers, not across threads.)This is everything C99 says about
sig_atomic_t
:(§7.14
<signal.h>
, paragraph 2) The type defined issig_atomic_t
, which is the (possibly volatile-qualified) integer type of an object that can be accessed as an atomic entity, even in the presence of asynchronous interrupts. (§7.14<signal.h>
, paragraph 2)(§7.14p5) If [a] signal occurs other than as the result of calling the
abort
orraise
function, the behavior is undefined if the signal handler refers to any object with static storage duration other than by assigning a value to an object declared asvolatile sig_atomic_t
.(§7.18.3 Limits of other integer types, paragraph 3) If
sig_atomic_t
(see 7.14) is defined as a signed integer type, the value ofSIG_ATOMIC_MIN
shall be no greater than −127 and the value ofSIG_ATOMIC_MAX
shall be no less than 127; otherwise, sig_atomic_t is defined as an unsigned integer type, and the value ofSIG_ATOMIC_MIN
shall be 0 and the value ofSIG_ATOMIC_MAX
shall be no less than 255.The term "atomic entity" is not defined anywhere in the standard. Translating from standards-ese, the intent is that the CPU can completely update a variable of type
sig_atomic_t
in memory ("static storage duration") with one machine instruction. Thus, in the concurrency-free, precisely interruptible C99 abstract machine, it is impossible for a signal handler to observe a variable of typesig_atomic_t
halfway through an update. The §7.18.3p3 language licenses this type to be as small aschar
if necessary. Note please the complete absence of any language relating to cross-processor consistency.There are real CPUs which require more than one instruction to write a value larger than
char
to memory. There are also real CPUs which require more than one instruction to write values smaller than a machine word (often, but not necessarily, the same asint
) to memory. The language in the GNU C Library manual is now inaccurate. It represents a desire on the part of the original authors to eliminate what they saw as unnecessary license for C implementations to do weird shit that made life harder for application programmers. Unfortunately, that very license is what makes it possible to have C at all on some real machines. There is at least one embedded Linux port (to the AVR) for which neitherint
nor pointers can be written to memory in one instruction. (People are working on making the manual more accurate, see e.g. http://sourceware.org/ml/libc-alpha/2012-02/msg00651.html --sig_atomic_t
seems to have been missed in that one, though.)这篇关于Linux的:为什么sig_atomic_t Typedef的为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!