Search code examples
clinuxsignalsreal-time

Is it possible to send out float value with realtime signal?


So I have two apps in c. First one has to send data to the other, using real-time signals. The data I want to send is float value. As far as I know the only way to send data with rt signal is to use union sigval that goes out with it. However, its sival_int field is of type int, and the other field is a pointer which I don't think would mean a lot in the other process that gets the signal. I know from my teacher that somehow it is possible, but have no clue how to approach this problem. Any ideas?

Edit: to clarify, my teacher said it is possible to achieve this using realtime signal and union sigval and I am curious how.


Solution

  • In all current Linux hardware architectures, sizeof (int) == sizeof (float) and neither have any padding bits; their storage representations are the same, and type-punning a float to an int and then back to a float always yields the original float (and storage representation).

    This means that you need to either copy the storage representation of the float to the int via memcpy(&value.sival_int, &float, sizeof (int)), or use an union to type-pun the float and an int (this being an extension supported by GCC, clang, icc, and other C compilers usable in Linux); and then send the signal and value to the target process via sigqueue().

    To receive the signal and value, you can either install a signal handler using sigaction() (using .sa_sigaction for the function pointer, and including SA_SIGINFO in .sa_flags); or block the signal (via sigprocmask()) and catch it explicitly using sigwaitinfo() or sigtimedwait().