Skip to content
Advertisement

How to use SIGEV_THREAD, sigevent for linux-timer(s) expiration handling in C?

Problem: I have timer(s) running, upon expiration of timer(s) certain function needs to be invoked. Output: There is a segfault inside Hndlr() function

As per man page of sigevent, it says,

SIGEV_THREAD – Notify the process by invoking sigev_notify_function “as if” it were the start function of a new thread. (Among the implement‐ tation possibilities here are that each timer notification could result in the creation of a new thread, or that a single thread is created to receive all notifications.)

The function (sigev_notify_function) is invoked with sigev_value as its sole argument

I did refer to this: UNIX/Linux signal handling: SIGEV_THREAD and it says,

sigev_value contains supplementary data that is passed to the function

So, I have written the following,

JavaScript

}

JavaScript

Since, I am using union sigval *sv in Hndlr, I am receiving this warning.

Q) How to I pass enum type to Hndlr as pass-by-ptr and change it, ie., t1.TimerStatus = Expire

PS: I haven’t included the entire code involving timer_set() etc, and it also involves multiple instances of timer. So, How can I achieve this functionality (Q) ?

Advertisement

Answer

A few mistakes in the code:

  • Wrong function prototype for timer expiry function Hndlr.
  • Setting all members of union sigval, whereas only one member must be set.
  • A variable modified and read in another thread must be atomic.

A working example (compiler options -std=c11 -pthread -W{all,extra}, linker options -std=c11 -pthread -lrt):

JavaScript

In this particular usage, when the timer callback function just stores into a variable, there is no need to use another thread with SIGEV_THREAD, SIGEV_SIGNAL would work just as well (setup code changes are required), as long as blocking functions that can be interrupted with the signal handle EINTR.

Advertisement