![]() |
sig_atomic_t Hi, What is the purpose of sig_atomic_t? Thanks. |
Re: sig_atomic_t On 2003-11-24, Vijay Kumar R Zanvar <vijoeyz@hotpop.com> wrote:[color=blue] > What is the purpose of sig_atomic_t?[/color] 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 |
Re: sig_atomic_t In <64ydnYty4Lc8UlyiRVn-iw@comcast.com> James Hu <jxh@despammed.com> writes: [color=blue] >On 2003-11-24, Vijay Kumar R Zanvar <vijoeyz@hotpop.com> wrote:[color=green] >> What is the purpose of sig_atomic_t?[/color] > >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.[/color] 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: Dan.Pop@ifh.de |
Re: sig_atomic_t On 2003-11-24, Dan Pop <Dan.Pop@cern.ch> wrote:[color=blue] > In <64ydnYty4Lc8UlyiRVn-iw@comcast.com> James Hu <jxh@despammed.com> writes: >[color=green] >>On 2003-11-24, Vijay Kumar R Zanvar <vijoeyz@hotpop.com> wrote:[color=darkred] >>> What is the purpose of sig_atomic_t?[/color] >> >>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.[/color] > > While correct, this is not a direct answer to OP's question.[/color] Thanks for the clarification. [color=blue] > 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).[/color] 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 |
Re: sig_atomic_t [color=blue] >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. > >[/color] Do you mean that i cant modify a global variable in my signal handler? Why this particular restriction? Sudheer |
Re: sig_atomic_t Sudheer Reddy Vakati <sudheer@agere.com> wrote: <unsnipped>[color=blue] > James Hu wrote:[color=green] > > Dan Pop wrote:[color=darkred] > >> 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).[/color][/color][/color] </unsnipped>[color=blue][color=green] > >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.[/color] > > Do you mean that i cant modify a global variable in my signal handler? > Why this particular restriction?[/color] You can, but these objects have to be declared as 'volatile sig_atomic_t' to avoid undefined behaviour. Regards -- Irrwahn (irrwahn33@freenet.de) |
Re: sig_atomic_t [Someone wrote:][color=blue][color=green] >>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.[/color][/color] In article <news:bq7lef$359$1@alageremail2.agere.com> Sudheer Reddy Vakati <sudheer@agere.com> writes:[color=blue] >Do you mean that i cant modify a global variable in my signal handler?[/color] Not exactly, but mostly. [color=blue] >Why this particular restriction?[/color] 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. |
| All times are GMT. The time now is 02:43 AM. |