...
| Code Block | ||
|---|---|---|
| ||
#include <signal.h>
sig_atomic_t i;
void handler(int signum) {
i = 0;
}
int main(void) {
i = 1;
signal(SIGINT, handler);
while (i) {
/* do something */
}
return 0;
}
|
Compliant Solution
By adding the volatile qualifier, i is guaranteed to be accessed from its original address for every iteration of the while loop.
| Code Block | ||
|---|---|---|
| ||
#include <signal.h>
volatile sig_atomic_t i;
void handler(int signum) {
i = 0;
}
int main(void) {
i = 1;
signal(SIGINT, handler);
while (i) {
/* do something */
}
return 0;
}
|
The sig_atomic_t type is the integer type of an object that can be accessed as an atomic entity, even in the presence of asynchronous interrupts. The type of sig_atomic_t is implementation-defined, although there are constraints. Only assign integer values from 0 through 127 to a variable of type sig_atomic_t to be fully portable.
...