StevenWang
StevenWang

Reputation: 3816

how a signal to a process affects its child threads?

gurus, I got 2 problems with the linux functions:

  1. sleep

    As far as I know sleep in process will get interrupted by any signals sent its process. Is this right?

  2. still the sleep

    If I create several threads in the process, and I insert sleep functions in the threads, and, then if I send signals into the process, will the sleep be interrupted?

BTW if I send a signal to a process, when the process get the signal, will it send to its child threads?

Thanx for your answers

Upvotes: 2

Views: 1127

Answers (4)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136306

1: sleep, as i know sleep in process will get interrupted by any signals sent its process. is it right?

It will get interrupted, unless the particular signal is ignored or blocked using sigprocmask.

2: still the sleep, if I create several threads in the process, and i insert sleep functions in the threads, and then if i send signals into the process, will the sleep be interrupted?

Only one thread receives a signal sent to the process even if there are several threads within that process that have that signal unblocked. See Signal Concepts for more details. The standard way to handle signals in a multi-threaded process is to have all signals blocked in all threads but the one that handles the signals (normally it's the main thread).

BTW: If i send a signal to a process , when the process get the signal, will it send to its child threads?

It won't.

However, if the process is a session group leader controlling the terminal, its group will receive SIGHUP signal when the session leader terminates. Also, when a process group becomes orphaned its processes get sent SIGHUP followed by a SIGCONT.

Upvotes: 3

caf
caf

Reputation: 239071

A process-directed signal is delivered to one of the process's threads. If that thread is sleeping, it will be woken.

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122391

1: Yes that's right. If you examine errno after the sleep() it will be set to EINTR (interrupted).

2: Each thread can set it's own signal mask using pthread_sigmask() and if it blocks the signal it won't be sent it. However it's undefined which of the threads receives the signal, if they don't block the signal explicitly.

You can send a signal directly to a specific thread using pthread_kill(), however.

Upvotes: 1

Mickey
Mickey

Reputation: 429

  1. You should use condition wait instead of sleep.

https://computing.llnl.gov/tutorials/pthreads/#ConVarOverview

You only need to read section "Condition Variables". It helps you get overview of pthread for your purpose.

Upvotes: -1

Related Questions