sig_atomic_t实际上如何工作? [英] How does sig_atomic_t actually work?

查看:70
本文介绍了sig_atomic_t实际上如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译器或OS如何区分sig_atomic_t类型和普通的int类型变量,并确保该操作是原子操作?两者都使用的程序具有相同的汇编代码.如何特别注意使操作原子化?

How does the compiler or OS distinguish between sig_atomic_t type and a normal int type variable, and ensures that the operation will be atomic? Programs using both have same assembler code. How extra care is taken to make the operation atomic?

推荐答案

sig_atomic_t不是原子数据类型.仅仅是允许您在信号处理程序的上下文中使用的数据类型,仅此而已.因此最好将其名称理解为相对于信号处理而言是原子的".

sig_atomic_t is not an atomic data type. It is just the data type that you are allowed to use in the context of a signal handler, that is all. So better read the name as "atomic relative to signal handling".

为了保证与信号处理程序之间的通信,仅需要原子数据类型的属性之一,即读取和更新将始终看到一致的值.其他数据类型(例如long long)可以用一些汇编程序指令来写上下两部分,例如保证可以一次性读取和写入sig_atomic_t.

To guarantee communication with and from a signal handler, only one of the properties of atomic data types is needed, namely the fact that read and update will always see a consistent value. Other data types (such as perhaps long long) could be written with several assembler instructions for the lower and higher part, e.g. sig_atomic_t is guaranteed to be read and written in one go.

因此,平台可以选择任何整数基类型作为sig_atomic_t,为此,它可以保证volatile sig_atomic_t可以安全地用于信号处理程序中.许多平台为此选择了int,因为他们知道int是用一条指令编写的.

So a platform may choose any integer base type as sig_atomic_t for which it can make the guarantee that volatile sig_atomic_t can be safely used in signal handlers. Many platforms chose int for this, because they know that for them int is written with a single instruction.

最新的C标准C11具有原子类型,但这是完全不同的东西.其中的一些(无锁"功能)也可以在信号处理程序中使用,但这又是一个完全不同的故事.

The latest C standard, C11, has atomic types, but which are a completely different thing. Some of them (those that are "lockfree") may also be used in signal handlers, but that again is a completely different story.

这篇关于sig_atomic_t实际上如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆