1

I'm writing a library and need to send a custom signal to threads, I used signal(SIGRTMIN, handler); and all works fine.

Since this is a library, I'm worried that some one who used my library will also use this SIGRTMIN, so this there any way in linux/posix_thread to get an unique SIGRTMIN id?

currently my way to solve this is to add a magic number, like SIGRTMIN + MAGIC_NUMBER to reduce the change of duplicate signals, but I wondered if there's a better solution.

  • I don't think signals and threads interact well, discussion here https://stackoverflow.com/questions/2575106/posix-threads-and-signals – Darren Smith Oct 31 '19 at 09:22
  • @DarrenSmith, one comparatively low-voted answer to the question you reference, by a very low-rep user, opines that threads and signals do not interact well. The argument presented seems more to be against signals in general than against combining them with threads. Other, better received answers by more highly reputed users do not convey that idea, nor do I share it myself. Interactions between threads and signals are well documented and understood, though threads do make everything more complicated, including signal handling. – John Bollinger Oct 31 '19 at 15:52

1 Answers1

0

There is no viable way to programmatically identify an otherwise unused signal number. Fixed signal numbers used by a library should be considered part of its API, and it is up to the library client to avoid clashes.

However, it should be possible to set up a mechanism by which the library client can register its choice of signal number with the library at runtime. That could be in the form either of an absolute number or an offset from SIGRTMIN. The client would be expected to call a function to specify the signal number, and the library could register the handler to listen for the caller-specified signal.

Note also, by the way, that you should always prefer sigaction() over signal() for registering signal handlers. Several important details of the latter's behavior are not well defined and do vary from implementation to implementation, but sigaction() allows the programmer to specify all the relevant details.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • So the library client could, for example, tell the library what signal it shall use during some sort of library init function, e.g, `lib_init_signal(SIGRTMIN + 20)`. However, in addition to this, does the client, and/or main thread, also need to perform additional setup? Such as setting signal mask on the main thread? – Darren Smith Oct 31 '19 at 16:04
  • @DarrenSmith, what threads require what signal masking is entirely a function of the application's specific behavior and requirements. It is also outside the scope of the question. The OP claims to be satisfied with the behavior they now get in their signal-using, multithreaded program, and the question is focused exclusively on how to choose which signal to use as they generalize to a reusable library. – John Bollinger Nov 01 '19 at 02:27
  • But an important consideration in multi-threaded programs is controlling which thread gets selected by the kernel to deliver a signal callback. The OP does indicate this is part of their problem, so this relevant (and is not included in your solution). So in addition to the client program deciding which signal-number the library shall use, effort may also be needed to control thread selection (for example, so that a thread internal to library gets selected by the kernel). And that is no easy task, hence the general advice, beware mixing signals and threads. – Darren Smith Nov 01 '19 at 10:17
  • @DarrenSmith, as far as I can see, the OP expresses no concern whatever about the matter of which threads receive the signals in question. The only issue I see raised is about avoiding signal-number collisions. Moreover, they have accepted this answer without posing any followup question. I do not deny that one of the complications surrounding combining signals with threads is ensuring that each signal is handled by an appropriate thread (which is not necessarily unique), but if that's within the scope of the question posed then you'll need to show me how so. – John Bollinger Nov 01 '19 at 11:46
  • "I'm writing a library and need to send a custom signal to threads," ... I take that to indicate the library cares about threads. But aside, when giving advice on using signals & threads, in my opinion it is worthwhile to also highlight an intrinsic danger & pitfall of the approach, particularly when this is wide-known concern shared by plenty of other developers. – Darren Smith Nov 01 '19 at 12:05
  • @DarrenSmith, the library involved certainly cares about threads, but the OP has asked a fairly specific question, *exactly as SO prefers that they do*, and I have answered the question posed. If you would like to present more information then you are of course welcome to write your own answer. – John Bollinger Nov 01 '19 at 12:28