another problem with signal in a multi-threaded environment

another problem with signal in a multi-threaded environment

Post by yyla » Fri, 27 Feb 1998 04:00:00



Hi,

I've got another problem about signals in a multi-threaded environment.
The case is that after returning from sigwait(), if I call thr_kill(...)
to signal another thread within the process, the process will die
silently. Could somebody help me ?

The code goes like follows :

main_th()
  sigfillset(&fullset)
  thr_setsigmask(SIG_SETMASK, &fullset..,&th_1)
  thr_create(...,thread_1,THR_BOUND,...)
  rtn = sigwait(&fullset)
  if (rtn == SIGTERM)
      thr_kill(th_1, SIGUSR1)
  ....

thread_1()
  sigemptyset(&enableset)
  sigaddset(&enableset, SIGUSR1)
  thr_setsigmask(SIG_UNBLOCK, &enableset ...)
  waitpid(..)
  .....

Regards,
Jeremy Y.Y.Lai

 
 
 

another problem with signal in a multi-threaded environment

Post by Fletcher Glen » Fri, 27 Feb 1998 04:00:00


You don't show it - does thread_1() have its own signal handler?
You really do have to define a signal handler for each thread.

--
                Fletcher Glenn

                To email: remove "notforspam" from my return address


> Hi,

> I've got another problem about signals in a multi-threaded environment.
> The case is that after returning from sigwait(), if I call thr_kill(...)
> to signal another thread within the process, the process will die
> silently. Could somebody help me ?

> The code goes like follows :

> main_th()
>   sigfillset(&fullset)
>   thr_setsigmask(SIG_SETMASK, &fullset..,&th_1)
>   thr_create(...,thread_1,THR_BOUND,...)
>   rtn = sigwait(&fullset)
>   if (rtn == SIGTERM)
>       thr_kill(th_1, SIGUSR1)
>   ....

> thread_1()
>   sigemptyset(&enableset)
>   sigaddset(&enableset, SIGUSR1)
>   thr_setsigmask(SIG_UNBLOCK, &enableset ...)
>   waitpid(..)
>   .....

> Regards,
> Jeremy Y.Y.Lai


 
 
 

another problem with signal in a multi-threaded environment

Post by yyla » Sat, 28 Feb 1998 04:00:00



> You don't show it - does thread_1() have its own signal handler?
> You really do have to define a signal handler for each thread.

> --
>                 Fletcher Glenn

>                 To email: remove "notforspam" from my return address


> > Hi,

> > I've got another problem about signals in a multi-threaded environment.
> > The case is that after returning from sigwait(), if I call thr_kill(...)
> > to signal another thread within the process, the process will die
> > silently. Could somebody help me ?

> > The code goes like follows :

> > main_th()
> >   sigfillset(&fullset)
> >   thr_setsigmask(SIG_SETMASK, &fullset..,&th_1)
> >   thr_create(...,thread_1,THR_BOUND,...)
> >   rtn = sigwait(&fullset)
> >   if (rtn == SIGTERM)
> >       thr_kill(th_1, SIGUSR1)
> >   ....

> > thread_1()
> >   sigemptyset(&enableset)
> >   sigaddset(&enableset, SIGUSR1)
> >   thr_setsigmask(SIG_UNBLOCK, &enableset ...)
> >   waitpid(..)
> >   .....

> > Regards,
> > Jeremy Y.Y.Lai

That's all in my testing program. thread_1() will wait for child dies
in a loop using waitpid(). main_th() sits in another loop calling
sigwait()
hoping to catch all signals to the process (except SIGCHLD in my
testing).

I was warned by books on multi-thread programming that one must not mix
sigwait
with sighandler but it doesn't make it clear if it is as per thread or
process.

Now if I call sigaction in thread_1() to set up a handler, how should I
code
it such that waitpid will returns as normal ? Am I need to set up some
dummy
handlers ?

Regards,
Jeremy Y.Y.Lai

 
 
 

1. signal in Solaris's multi-threaded environment

Hi,

Basically, I'm quite confused with those signal functions under a
multi-threaded
environment. I found that sigwait behaves quite interesting for SIGCHLD.
I've tried the following cases.

Case 1
  main()
         sigfillset(&waitset);
         thr_sigsetmask(SIG_BLOCK, &waitset, NULL);
         sigwait(&waitset);

Case 2
  main()
         sigfillset(&waitset);
         thr_sigsetmask(SIG_BLOCK, &waitset, NULL);
         thr_create(NULL, 0, wait_child, NULL, THR_BOUND, &new_thread);
         sigwait(&waitset);

  wait_child()
         waitpid((pid_t)0, &cstat, 0);

Case 3
  main()
         sigfillset(&waitset);
         thr_sigsetmask(SIG_BLOCK, &waitset, NULL);
         thr_create(NULL, 0, wait_child, (void *)thr_self(), THR_BOUND,
&new_thread);
         sigwait(&waitset);

  wait_child(void *arg)
         waitpid((pid_t)0, &cstat, 0);
         thr_kill((thread_t)arg, SIGCHLD);

sigwait in Case 1 doesn't return even though all its child are dead.
waitpid in Case 2 return as child dies as expected.
Interesting is that sigwait in Case 3 is able to return SIGCHLD through
thr_kill().

Why ?

In addition, document said that don't try to use signal handlers with
sigwait. I'm using waitpid(), which I presume that it set up its signal
handler, does it violate the rule ?

Using pstack on Case 3's process, I found that there are exactly three
threads running. Presumably, the third thread is created by the thread
package for signal handling as shown below. What exactly the algorithm
this thread employ ?

7152: d1
lwp#1 -----------
 ef6b9ae8 lwp_sema_p (21090)
 ef6b9ae8 __lwp_sema_wait (21090, 0, ef7a3578, 10904, ff00, ff) + 8
 ef786804 _park (20ff8, 10800, 21034, 21090, 21028, 0) + 98
 ef7866a4 _swtch (1000, 8000, 21024, 20ff8, 21034, 21028) + 34c
 ef791748 cond_timewait_sig (ef7a3bd8, effffbd8, effffc00, effffbfc, 0,
effffbf8) + 148
 ef791a74 _ti_sigtimedwait (effffbd8, 0, 0, 0, effffbe8, 0) + 248
 00010c80 main (1, effffd1c, effffd24, 20ff0, 1, 0) + 128
 00010908 _start (0, 0, 0, 0, 0, 0) + 5c
lwp#2 -----------
 ef6b9bd4 signotifywait()
 ef78a324 _dynamiclwps (e, ef7a3fd4, ef7a3fbc, ef7a3634, 0, 20) + 24
 ef78ccb8 _ti_thr_yield(0, 0, 0, 0, 0, 0) + 8c
lwp#3 -----------
 ef6b95e4 waitid (2, 1bef, ef20bca8, 3)
 ef6dd00c _libc_waitpid (ebef, ef20bd90, 3, 0, 0, 2) + 58
 ef79201c _ti_waitpid (1, 69eb02, 0, effffa18, ef7d7df0, effffa18) + 30
 ef7925f0 _thread_start (1, 0, 0, 0, 0, 0) + 34

Finally, is there anything in Solaris that can perform multiple wait on
different synchronization objects just like WaitForMultipleObjects in
Win32 ?

Thanks.

Regards,
Jeremy Y.Y.Lai

2. A stupid DIP question

3. problems about programming signals in Solaris multi-thread environment

4. GCC 2.7.2.1 now available

5. Multi-threaded application thread stops receiving signals

6. Linux networking problems..

7. Question concerning file descriptors in a multi-threaded environment

8. ppp0

9. sigwait in multi-threaded environment

10. pb: catching signals in a multi-threaded process

11. Solaris multi-threaded and multi-processor programming tools

12. Design of a server: multi-threaded or multi-processes?

13. mutexes with multi-threaded program on multi-cpu machine.