I found following document for GNU libc. This particular chapter describes how to write
the signal procedure. However it may seem to be straight forward, there
are many rules which have to be followed.
For instance to exchange the data between the signal handler and user code, up to now I used the int type. This method is not portable and may be unsafe. The reason is because the int type may have from 16 to 64 bits, depending on architecture and types model (for more info please read this document).
The obvious fact is that your int variable access may be atomic on one platform, while non atomic on other.
To solve this issue, you need to use the sig_atomic_t (here is short info) This type does not have strictly defined number of bits but it guarantee that read/write access will be done in atomic manner. Therefore it can be used to exchange the data between the signal handler and user code.
This type does not have associated increment/decrement functions (opposite to similar atomic_t in kernel). Because of this you cannot base on fact that increment operation of the sig_atomic_t variable will be atomic.
The sig_atomic_t type doesn't provide atomic updates. The ++ operator
may read the object, and then store a new value on some architectures. That two-access
transaction isn't atomic. A signal handler may be invoked between the
read and write, and that signal handler may read the old value and act
on it or it may store a new value which is about to be overwritten.
Brak komentarzy:
Prześlij komentarz