1

In my application ,I have multiple thread and one of these threads is responsible for catching signals and handling them. My main problem is since the OS picks up one of the threads in the process randomly and deliver to it the signal to handle it. So the thread which is picked up might be not the one which is responsible for handling the signal.

I have to block the signals in the main thread and any new thread will inherit the mask of the main thread, so they won't be able to handle the signals and only the corresponding thread will do that.

So can anyone provide me with a sample code in C, how to do that?

I have already wrote the part related to the keep the thread sleeping and wake it up upon the receipt of a signal, you can find the following stackoverflow - Thread blocked waiting for a signal.

Community
  • 1
  • 1
IoT
  • 607
  • 1
  • 11
  • 23
  • 2
    See the following [How to block all SIGNALS in thread](http://stackoverflow.com/questions/8093755/how-to-block-all-signals-in-thread-without-using-sigwait) and [How to block signals in C](http://stackoverflow.com/questions/13481186/how-to-block-signals-in-c) and also see this article [All about Linux Signals](http://www.linuxprogrammingblog.com/all-about-linux-signals). – Richard Chambers Apr 20 '14 at 16:07
  • Thanks a lot, I tried to block all the signals and it worked using `sigset_t set; sigfillset(&set); pthread_sigmask(SIG_SETMASK, &set, NULL);` I called this in the main thread, and all other threads inherited that. – IoT Apr 20 '14 at 16:37
  • But my question is it better to block all signals in all threads or to deliver signal per thread? – IoT Apr 20 '14 at 16:38
  • Your question as to block or not is really beyond my knowledge of signals, linux threading, and your actual application. In general threads tend to be focused on a particular task so you would want the signals associated for that task to be designated to that thread and blocked for the others. In general if you have a general purpose signal thread, its only purpose would be to determine what notifications need to be sent to other threads so the general purpose thread acts as a kind of dispatcher. I would want a really good reason for that architecture because it can lead to duplication. – Richard Chambers Apr 20 '14 at 16:57
  • Signal disposition it per process, so you cannot deliver signals on a per thread base. – alk Apr 20 '14 at 17:17
  • @alk can you give example please? – IoT Apr 20 '14 at 19:05
  • Example of what, please? – alk Apr 20 '14 at 19:57

0 Answers0