Metal Slime
Metal Slime

Reputation: 149

Why my <signal.h> define __sig_atomic_t to int instead of volatile int

I am reading "Computer Systems: A Programmer's Perspective", in chapter 8, it says sig_atomic can be use to be the type for flag which can be used in signal handler. But when I trace my header file, it is defined as int without volatile, isn't it means there is a possibility that when main modify the flag, it may use 'write back' instead of 'write through', which might be incorrect?

Upvotes: 2

Views: 488

Answers (1)

Because sig_atomic_t should in practice always be qualified with volatile (at least for variables set inside signal handlers). It is not (formally) related to C11 atomic types (even if the name could be confusing). But sig_atomic_t should in practice be some integral type which can be written to or read from memory in a single machine code instruction (and you need to qualify it as volatile to make that happen as needed, and instruct the compiler to not cache it in some register etc...).

For Linux, see also signal(7) and most importantly signal-safety(7).

Notice that the C11 standard does not tell a lot about signals (check by reading n1570, notably §7.14). In practice, you probably need some operating system support for them. See also POSIX signal concepts.

Read also Operating Systems: Three Easy Pieces

Upvotes: 4

Related Questions