1
\$\begingroup\$

I'm writing a linux-based app using posix timers, and the idea is to send certain commands over UART every specific intervals. For instance, <CMD-A> needs to be sent every X seconds whereas <CMD-B> every Y seconds.

My initial thought was having a timer for each command (via timer_create()) and setting the expiry/reload time accordingly. So if I have 5 different commands, I'd have 5 different timers that would invoke a callback within a new thread every expiry time, and the callback sends the passed-in command over to serial port.

I have a couple questions:

  • Would it sound feasible to have timers instantiated for each command? Say, TimerA sends <CMD_A> every X seconds whereas TimerB sends <CMD_B> every Y seconds. This way these two timers are independently invoking a callback that sends a corresponding command over to serial port. Are there better ways around it? Could I really make it work with a single timer? I have been able to run it fine on my end but i'm uncertain of consequences as I scale up the app.
  • Given I could have timer for each function, in what scenario would it make sense to have timers defined inside respective threads?
\$\endgroup\$
1
  • \$\begingroup\$ Things probably depend a bit on the lengths of the various intervals, both relative to each other (at some point you’ll end up having both timers firing at the same time) and relative to the time it takes to process each callback, especially if the actual send operation is not atomic (in which case you may have bits of both commands interleaved). \$\endgroup\$
    – jcaron
    Aug 11, 2021 at 7:23

1 Answer 1

1
\$\begingroup\$

Would it sound feasible to have timers instantiated for each command?

It seems like this is alright. The timer_create manual says that a process may have multiple timers, and the number of timers that can be created depends on the RLIMIT_SIGPENDING resource limit. This limit is ordinarily in the tens of thousands, so using five doesn't sound like a big deal.

Given I could have timer for each function, in what scenario would it make sense to have timers defined inside respective threads?

Eh?

\$\endgroup\$
2
  • \$\begingroup\$ what's the typical RLIMIT_SIGPENDING? thing though is the number of commands needed to be sent is variable and could increase too in the future. Regarding the second point, it's just I wanted to ensure the timers kick off around the same time, cause with my current implementation, each timer is created in a sequential manner \$\endgroup\$
    – xyf
    Aug 11, 2021 at 5:41
  • \$\begingroup\$ RLIMIT_SIGPENDING on my machine is 1174, which means I could have a max of 1174 timers? \$\endgroup\$
    – xyf
    Aug 11, 2021 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.