[LINUX] Behavior when SIGEV_THREAD is set in sigev_notify of sigevent with timer_create (C language)

background

In the embedded system I am currently developing, various methods are used for periodic processing. Among them, the method of setting SIGEV_THREAD in sigev_notify of the sigevent structure with the timer_create function and starting the specified function as one thread (timer thread) after a certain period of time is one of the commonly used methods. In the system under development, there are a plurality of places where a periodic timer is realized by this method, and when the system is started, the timer thread is always generated and terminated. At this time, I was wondering where each of the generated timer threads was created, so I looked up the thread name of the timer thread. (Because I thought that the name of the timer thread and the name of the thread that called timer_create match.) Then, all the generated timer threads had the same thread name. Therefore, I investigated the operation of the periodic timer when SIGEV_THREAD was set in timer_create.

Overview of timer_create

int timer_create(clockid_t clockid, struct sigevent *restrict evp, timer_t *restrict timerid)

argument: First argument: Clock ID Second argument: sigevent information → Details Third argument: Pointer for storing timer ID

Create a timer for each process. When the timer expires, the operation set by sigevent is performed. (Set the cycle time and start the timer with the timer_settime function.)

Operation when SIGEV_THREAD is set (* In my environment)

When the first timer with SIGEV_THREAD set is created, a timer thread for starting is created when the timer expires. (The thread name of the parent thread is inherited.) When the timer expires, the function specified in the first generated timer thread is started. → Even if multiple timers with SIGEV_THREAD set are created, the function is started in the timer thread created first. Therefore, the timer thread name is different from the thread that created the timer, except for the thread that created the timer thread. However, if another timer expires while the timer thread is already running, a new timer thread is created. (The thread name at this time is the same as the thread name that starts when the timer expires. When the function ends, the timer thread ends immediately.)

References

This article was written with reference to the following information.

timer_create() manual http://www.yosbits.com/opensonar/rest/man/freebsd/man/ja/man2/timer_create.2.html

SIGEVENT manual https://linuxjm.osdn.jp/html/LDP_man-pages/man7/sigevent.7.html

Recommended Posts

Behavior when SIGEV_THREAD is set in sigev_notify of sigevent with timer_create (C language)
Differences in the behavior of each LL language when the list index is skipped
Behavior in each language when coroutines are reused with for
Segfault with 16 characters in C language
Behavior when returning in the with block
Set up a UDP server in C language
Behavior when Trainable = False of Container in Keras
I asked the problem of the tribonacci sequence in C ++ & the number of function calls when writing with the recurrence function (python is also available)
Behavior when multiple servers are specified in nameservers of dnspython
Behavior when giving a list with shell = True in subprocess
[C language] Close () It is dangerous to retry when it fails
Set public key authentication when connecting with SSH in Teraterm