Pthreads: How to process signals in the main thread, which creates other threads? (specific code shown)

I have a main thread that stays in the main function, i.e. I do not create it specifically, as in pthread_create, because it is optional. This thread opens the file, then creates other threads, waits for their completion (i.e. makes a connection), clears everything (pointers, semaphores, conditional variables, etc.).

Now I have to apply this code to block SIGINT:

sigset_t set; int sig; sigemptyset(&set); sigaddset(&set, SIGINT); pthread_sigmask(SIG_BLOCK, &set, NULL); while (1) { sigwait(&set, &sig); switch (sig) { case SIGINT: /* handle interrupts */ break; default: /* unexpected signal */ pthread_exit((void *)-1); } } 

and he says that you should use the main () function to start the N + 1 threads and wait for them to complete. If the SIGINT signal enters the program, it must be processed by the main thread to close the program and its threads in a clean way.

My doubt is how do I put this code? Is it wrong to put on the background thread created in main ()? Since I already have a cicle with an exit flag that creates and joins all other threads, so I don’t understand if this code corresponds to the main function, where everything is executed / called to run the program. If I put it in a stream, with this code and a handler for cleaning, is it considered busy, waiting?

+4
source share
1 answer

"It says"? What does it say? Homework?

The first thing you should understand about programming with streams and signals is that you have very little control over the stream to which the signal is being transmitted. If your main thread wants to be the only one to receive the signal, it should block the signal before creating any new streams and possibly unlock it after the completion of its creation to ensure that the signal will not be delivered to them.

However , if you use the best methods for signal handlers, it probably doesn't matter which stream processes the signal. The entire signal handler must set a global flag or write a byte to the channel (depending on what is best for the main thread to notice that this signal has occurred. (Note that you cannot use variable conditions or any blocking primitives from signal handlers !) As in the code snippet in your question may block the signal and use sigwait (remember that it should be blocked in all threads), but most programs can "t afford to stop and wait only for a signal They also need to wait for variable conditions and / or data from files One way to solve this problem -. Make a dedicated thread to call sigwait ., But it's pretty wasteful best solution if you are already using select , it would switch to pselect , which can expect signals, as well as file descriptor events (at the same time).

Instead of asking us for answers (which would be difficult to give in any case, not seeing the complete program with which you are trying to do this work), it will be much better for you to try to understand the subtleties of signals using streams.

+4
source

All Articles