473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

sig_atomic_t

Hi,

What is the purpose of sig_atomic_t?

Thanks.
Nov 13 '05 #1
6 23400
On 2003-11-24, Vijay Kumar R Zanvar <vi*****@hotpop.com> wrote:
What is the purpose of sig_atomic_t?


It is an integer type that is guaranteed by the standard to not be
partially written or partially read in the presence of asynchronous
interrupts.

-- James
Nov 13 '05 #2
In <64********************@comcast.com> James Hu <jx*@despammed.com> writes:
On 2003-11-24, Vijay Kumar R Zanvar <vi*****@hotpop.com> wrote:
What is the purpose of sig_atomic_t?


It is an integer type that is guaranteed by the standard to not be
partially written or partially read in the presence of asynchronous
interrupts.


While correct, this is not a direct answer to OP's question.

sig_atomic_t, along with the character types, are the only types that can
be reliably used in a signal handler. External objects of other types
used in a signal handler must be defined as const (or merely be de facto
constant in the program).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3
On 2003-11-24, Dan Pop <Da*****@cern.ch> wrote:
In <64********************@comcast.com> James Hu <jx*@despammed.com> writes:
On 2003-11-24, Vijay Kumar R Zanvar <vi*****@hotpop.com> wrote:
What is the purpose of sig_atomic_t?
It is an integer type that is guaranteed by the standard to not be
partially written or partially read in the presence of asynchronous
interrupts.


While correct, this is not a direct answer to OP's question.


Thanks for the clarification.
sig_atomic_t, along with the character types, are the only types that can
be reliably used in a signal handler. External objects of other types
used in a signal handler must be defined as const (or merely be de facto
constant in the program).


Your second sentence hints at this, but I'll state it explicitly. The
signal handler can use objects of types other than sig_atomic_t as long
as the objects are not ever accessed by the program outside of the
signal handler's context.

-- James
Nov 13 '05 #4
Your second sentence hints at this, but I'll state it explicitly. The
signal handler can use objects of types other than sig_atomic_t as long
as the objects are not ever accessed by the program outside of the
signal handler's context.

Do you mean that i cant modify a global variable in my signal handler?
Why this particular restriction?
Sudheer

Nov 13 '05 #5
Sudheer Reddy Vakati <su*****@agere.com> wrote:

<unsnipped>
James Hu wrote:
Dan Pop wrote:
sig_atomic_t, along with the character types, are the only types that
can be reliably used in a signal handler. External objects of other
types used in a signal handler must be defined as const (or merely be
de facto constant in the program).
</unsnipped>Your second sentence hints at this, but I'll state it explicitly. The
signal handler can use objects of types other than sig_atomic_t as long
as the objects are not ever accessed by the program outside of the
signal handler's context.


Do you mean that i cant modify a global variable in my signal handler?
Why this particular restriction?


You can, but these objects have to be declared as
'volatile sig_atomic_t' to avoid undefined behaviour.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #6
[Someone wrote:]
Your second sentence hints at this, but I'll state it explicitly. The
signal handler can use objects of types other than sig_atomic_t as long
as the objects are not ever accessed by the program outside of the
signal handler's context.

In article <news:bq**********@alageremail2.agere.com>
Sudheer Reddy Vakati <su*****@agere.com> writes:Do you mean that i cant modify a global variable in my signal handler?
Not exactly, but mostly.
Why this particular restriction?


Consider the following example. Suppose you have a machine with
32-bit "long"s, 32-bit CPU registers, 64-bit "long long"s, and an
implementation that handles something like:

/* long x; */
x = 7;

by generating machine instructions of the form:

move #7, reg
store reg, [memory location for x]

But if "y" is a "long long" (and thus 64 bits wide), it requires
two separate stores to set it to a new value -- for instance:

/* long long y; */
y = 0x1111111122222222LL;

might compile to:

move #0x11111111, reg
store reg, [memory location for y]
move #0x22222222, reg
store reg, [(memory location for y) + 4]

Now, suppose that you wrote C code of the form:

printf("y is %llx\n", y);

and that this compiles to code of the form:

load reg, [y]
--> push reg
load reg, [y+4]
push reg
move #stringaddr("y is 0x%llxLL\n"), reg
push reg
call printf

Suppose further that you manage to invoke your signal handler right
when the processor has finished the first "load" but has not yet
started the second "load", i.e., is about to execute (or has just
executed) the instruction marked with the "-->" arrow. If y used
to hold the value 0x8888888899999999LL, the code will have handled
the first half -- 0x88888888 -- but not the second. Then your
signal handler will change y to 0x1111111122222222LL, and only
when that finishes will the CPU resume and execute the second "load".
The output from printf() will then be:

y is 0x8888888822222222LL

which, as you will see, is an "impossible" value -- y is *supposed*
to be either 0x8888888899999999LL or 0x1111111122222222LL, but not
0x1111111199999999LL nor 0x8888888822222222LL.

Now, you might say "oh, well, in that case I will just avoid using
64-bit variables" -- but what if your CPU has 32-bit "int"s and
16-bit registers, or 16-bit "int"s and 8-bit registers, or any
other number of possibilities? Even if you use a size that happens
to match your CPU hardware -- either by accident, or by knowing
all the details about your CPU -- what if the compiler chooses to
decide: "aha, I know for sure that I only need to modify the low
byte of variable y here"? (Admittedly, the "volatile" keyword is
likely to inhibit such an optimization.)

The one guarantee you get, from the ANSI/ISO C standard, is that
you can assign to a "volatile sig_atomic_t" variable and see *only*
either the old value or the new value, never any weird intermediate
mix. Use any other type and the C-level guarantee is gone. This
does not mean you *cannot* use any other type, only that if you do,
you must look elsewhere for guarantees about it.

(Incidentally, if you study the above assembly too closely, you
will observe that "long long"s are big-endian in RAM but little-endian
when pushed on the stack for printf(). This is partly for exposition,
but also deliberate: there is nothing that says they must be
consistent; the va_arg() macro could assemble "long long"s in
reverse word-endian-ness order.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Mark Piffer | last post by:
Does a typedef like typedef sig_atomic_t atomic_int; produce an atomically write/readable type? From what I read in the standard I would guess yes, as sig_atomic_t itself is produced by a typedef...
6
by: j0mbolar | last post by:
how is atomicity guaranteed on say a 32 bit architecture? or a pentium? because doesn't sig_atomic_t have to be an alias for a type that is the smallest unit (a signed char or unsigned char)? if...
14
by: Ian Pilcher | last post by:
It's pretty common to see declarations such as: static volatile sig_atomic_t caught_signal = 0; C99 defines sig_atomic_t as a "... (possibly volatile-qualified) integer type of an object that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.