<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic SIGCHLD delivery problem in multithreaded app in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/sigchld-delivery-problem-in-multithreaded-app/m-p/5147546#M683402</link>
    <description>Hi guys,&lt;BR /&gt;I'm working on stand-alone web-service (OS=HP-11.23, Compiler=aCC). It is multithreaded application based on boss-workers model (I have one boss thread and N working threads). Web-service receives queries and perform some work. This work include executing external process(es). In order to perform query I need to get result of external process, so I need to wait until it terminates. In the first version of this program (that is working for few years without problems) I used fork-exec-waitpid sequence in worker-thread. Since external process takes some time I loose this time, because worker is blocked at waitpid call. Now, I decided to use this time in better way. I execute fork-exec (saving pid somewhere) in worker thread and after that I immediately return my worker to the pool to get new job. Of course, now I need notification that my external process exited I and I can get the result (file or smth. else). &lt;BR /&gt;I created special thread (let's call it TSignalHandler) that is waiting for SIGCHLD. After it catches SIGCHLD, it calls wait() and gets pid (and status) of completed process. pid and status are sent to boss and everything Ok. &lt;BR /&gt;Now, the problem. When I test my application on small load everything goes w/o problems. All SIGCHLD arrive. But during heavier load I notice that some SIGCHLD are missing that is, of course, absolutely unacceptable.&lt;BR /&gt;Now, few details. I block all signals for all threads.&lt;BR /&gt;  sigset_t mask;&lt;BR /&gt;  sigfillset(&amp;amp;mask);&lt;BR /&gt;  int rc = pthread_sigmask(SIG_BLOCK, &amp;amp;mask, 0);&lt;BR /&gt;&lt;BR /&gt;And for open SIGCHLD delivery only for TSignalHandler:&lt;BR /&gt;here is simplified thread function loop:&lt;BR /&gt;&lt;BR /&gt;while(true)&lt;BR /&gt;{&lt;BR /&gt;  int sig_number;&lt;BR /&gt;  sigset_t signal_set;&lt;BR /&gt;  sigemptyset(&amp;amp;signal_set);&lt;BR /&gt;  sigaddset(&amp;amp;signal_set, SIGCHLD);&lt;BR /&gt;  sigaddset(&amp;amp;signal_set, SIGUSR1);&lt;BR /&gt;  pthread_sigmask(SIG_BLOCK, &amp;amp;signal_set, 0);&lt;BR /&gt;  sigwait (&amp;amp;signal_set, &amp;amp;sig_number);&lt;BR /&gt;&lt;BR /&gt;  switch(sig_number)&lt;BR /&gt;  {&lt;BR /&gt;    case SIGCHLD:&lt;BR /&gt;      Wait();&lt;BR /&gt;    break;&lt;BR /&gt;&lt;BR /&gt;    case SIGUSR1:&lt;BR /&gt;      Stop();&lt;BR /&gt;    break;&lt;BR /&gt;&lt;BR /&gt;    default:&lt;BR /&gt;    break;&lt;BR /&gt;  }&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;And Wait() function code: &lt;BR /&gt;&lt;BR /&gt;void TSignalHandler::Wait()&lt;BR /&gt;{&lt;BR /&gt;  int status;&lt;BR /&gt;  int pid = wait(&amp;amp;status);&lt;BR /&gt;  if(pid &amp;gt; 0)&lt;BR /&gt;  {&lt;BR /&gt;    m_dispatcher-&amp;gt;PostMessage(new TGenerationStatusMessage(TInt32Int32Bool(pid, (int)time(0), ..smth..)));&lt;BR /&gt;  }&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;So, only TSignalHandler thread may receive SIGCHLD. &lt;BR /&gt;My questions are: &lt;BR /&gt;1. Are SIGCHLD 100% delivered to application?&lt;BR /&gt;2. May be somebody faces similar problems and can help me ?&lt;BR /&gt;&lt;BR /&gt;Thanks in advance.&lt;BR /&gt;Serge Zabinskis&lt;BR /&gt;//Source of TSignalHandler is attached</description>
    <pubDate>Mon, 22 Dec 2008 09:17:48 GMT</pubDate>
    <dc:creator>sergejusz</dc:creator>
    <dc:date>2008-12-22T09:17:48Z</dc:date>
    <item>
      <title>SIGCHLD delivery problem in multithreaded app</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sigchld-delivery-problem-in-multithreaded-app/m-p/5147546#M683402</link>
      <description>Hi guys,&lt;BR /&gt;I'm working on stand-alone web-service (OS=HP-11.23, Compiler=aCC). It is multithreaded application based on boss-workers model (I have one boss thread and N working threads). Web-service receives queries and perform some work. This work include executing external process(es). In order to perform query I need to get result of external process, so I need to wait until it terminates. In the first version of this program (that is working for few years without problems) I used fork-exec-waitpid sequence in worker-thread. Since external process takes some time I loose this time, because worker is blocked at waitpid call. Now, I decided to use this time in better way. I execute fork-exec (saving pid somewhere) in worker thread and after that I immediately return my worker to the pool to get new job. Of course, now I need notification that my external process exited I and I can get the result (file or smth. else). &lt;BR /&gt;I created special thread (let's call it TSignalHandler) that is waiting for SIGCHLD. After it catches SIGCHLD, it calls wait() and gets pid (and status) of completed process. pid and status are sent to boss and everything Ok. &lt;BR /&gt;Now, the problem. When I test my application on small load everything goes w/o problems. All SIGCHLD arrive. But during heavier load I notice that some SIGCHLD are missing that is, of course, absolutely unacceptable.&lt;BR /&gt;Now, few details. I block all signals for all threads.&lt;BR /&gt;  sigset_t mask;&lt;BR /&gt;  sigfillset(&amp;amp;mask);&lt;BR /&gt;  int rc = pthread_sigmask(SIG_BLOCK, &amp;amp;mask, 0);&lt;BR /&gt;&lt;BR /&gt;And for open SIGCHLD delivery only for TSignalHandler:&lt;BR /&gt;here is simplified thread function loop:&lt;BR /&gt;&lt;BR /&gt;while(true)&lt;BR /&gt;{&lt;BR /&gt;  int sig_number;&lt;BR /&gt;  sigset_t signal_set;&lt;BR /&gt;  sigemptyset(&amp;amp;signal_set);&lt;BR /&gt;  sigaddset(&amp;amp;signal_set, SIGCHLD);&lt;BR /&gt;  sigaddset(&amp;amp;signal_set, SIGUSR1);&lt;BR /&gt;  pthread_sigmask(SIG_BLOCK, &amp;amp;signal_set, 0);&lt;BR /&gt;  sigwait (&amp;amp;signal_set, &amp;amp;sig_number);&lt;BR /&gt;&lt;BR /&gt;  switch(sig_number)&lt;BR /&gt;  {&lt;BR /&gt;    case SIGCHLD:&lt;BR /&gt;      Wait();&lt;BR /&gt;    break;&lt;BR /&gt;&lt;BR /&gt;    case SIGUSR1:&lt;BR /&gt;      Stop();&lt;BR /&gt;    break;&lt;BR /&gt;&lt;BR /&gt;    default:&lt;BR /&gt;    break;&lt;BR /&gt;  }&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;And Wait() function code: &lt;BR /&gt;&lt;BR /&gt;void TSignalHandler::Wait()&lt;BR /&gt;{&lt;BR /&gt;  int status;&lt;BR /&gt;  int pid = wait(&amp;amp;status);&lt;BR /&gt;  if(pid &amp;gt; 0)&lt;BR /&gt;  {&lt;BR /&gt;    m_dispatcher-&amp;gt;PostMessage(new TGenerationStatusMessage(TInt32Int32Bool(pid, (int)time(0), ..smth..)));&lt;BR /&gt;  }&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;So, only TSignalHandler thread may receive SIGCHLD. &lt;BR /&gt;My questions are: &lt;BR /&gt;1. Are SIGCHLD 100% delivered to application?&lt;BR /&gt;2. May be somebody faces similar problems and can help me ?&lt;BR /&gt;&lt;BR /&gt;Thanks in advance.&lt;BR /&gt;Serge Zabinskis&lt;BR /&gt;//Source of TSignalHandler is attached</description>
      <pubDate>Mon, 22 Dec 2008 09:17:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sigchld-delivery-problem-in-multithreaded-app/m-p/5147546#M683402</guid>
      <dc:creator>sergejusz</dc:creator>
      <dc:date>2008-12-22T09:17:48Z</dc:date>
    </item>
    <item>
      <title>Re: SIGCHLD delivery problem in multithreaded app</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sigchld-delivery-problem-in-multithreaded-app/m-p/5147547#M683403</link>
      <description>Hi&lt;BR /&gt;&lt;BR /&gt;I just have 2 recommendations:&lt;BR /&gt;1) don't fork in a multithreaded application because the perf cost of fork() in that case is really high.&lt;BR /&gt;-&amp;gt; use simple monothreaded coprocesses which you communicate with a pipe. This coprocess started once launch subprocesses&lt;BR /&gt;-&amp;gt; use /dev/echo and I_SENDFD/I_RCVFD to share file descriptors and pipes, or unix domain sockets&lt;BR /&gt;2) SIGCHLD are all delivered to the application, but if more than one is delivered to the application while not on sigwait, only one is kept.&lt;BR /&gt;The best way to avoid it is to use sigaction()since the actions may be queued.&lt;BR /&gt;&lt;BR /&gt;Workaround to the lose of signal:&lt;BR /&gt;call an extra waitpid(-1,&amp;amp;status,WNOHANG)&lt;BR /&gt;void TSignalHandler::Wait()&lt;BR /&gt;{&lt;BR /&gt;int status;&lt;BR /&gt;int pid;&lt;BR /&gt;&lt;BR /&gt;while ( (pid= waitpid(-1,&amp;amp;status,WNOHANG))&amp;gt;0)&lt;BR /&gt;{  &lt;BR /&gt;   if(pid &amp;gt; 0)&lt;BR /&gt;   {&lt;BR /&gt;m_dispatcher-&amp;gt;PostMessage(new TGenerationStatusMessage(TInt32Int32Bool(pid, (int)time(0), ..smth..)));&lt;BR /&gt;   }&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 22 Dec 2008 11:29:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sigchld-delivery-problem-in-multithreaded-app/m-p/5147547#M683403</guid>
      <dc:creator>Laurent Menase</dc:creator>
      <dc:date>2008-12-22T11:29:34Z</dc:date>
    </item>
    <item>
      <title>Re: SIGCHLD delivery problem in multithreaded app</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sigchld-delivery-problem-in-multithreaded-app/m-p/5147548#M683404</link>
      <description>Thanks Laurent for your very valuable response.&lt;BR /&gt;&lt;BR /&gt;kind regards&lt;BR /&gt;Serge&lt;BR /&gt;</description>
      <pubDate>Mon, 22 Dec 2008 11:56:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sigchld-delivery-problem-in-multithreaded-app/m-p/5147548#M683404</guid>
      <dc:creator>sergejusz</dc:creator>
      <dc:date>2008-12-22T11:56:07Z</dc:date>
    </item>
    <item>
      <title>Re: SIGCHLD delivery problem in multithreaded app</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sigchld-delivery-problem-in-multithreaded-app/m-p/5147549#M683405</link>
      <description>&amp;gt;I block all signals for all threads.&lt;BR /&gt;&lt;BR /&gt;Don't block the program error signals, Signal 4,5,10,11, these will cause loops if you get them.</description>
      <pubDate>Mon, 22 Dec 2008 12:41:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sigchld-delivery-problem-in-multithreaded-app/m-p/5147549#M683405</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2008-12-22T12:41:16Z</dc:date>
    </item>
    <item>
      <title>Re: SIGCHLD delivery problem in multithreaded app</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sigchld-delivery-problem-in-multithreaded-app/m-p/5147550#M683406</link>
      <description>Of course Dennis, you are right!&lt;BR /&gt;&lt;BR /&gt;Kind regards&lt;BR /&gt;Serge</description>
      <pubDate>Mon, 22 Dec 2008 12:46:43 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sigchld-delivery-problem-in-multithreaded-app/m-p/5147550#M683406</guid>
      <dc:creator>sergejusz</dc:creator>
      <dc:date>2008-12-22T12:46:43Z</dc:date>
    </item>
    <item>
      <title>Re: SIGCHLD delivery problem in multithreaded app</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sigchld-delivery-problem-in-multithreaded-app/m-p/5147551#M683407</link>
      <description>Ok</description>
      <pubDate>Tue, 23 Dec 2008 07:50:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sigchld-delivery-problem-in-multithreaded-app/m-p/5147551#M683407</guid>
      <dc:creator>sergejusz</dc:creator>
      <dc:date>2008-12-23T07:50:34Z</dc:date>
    </item>
  </channel>
</rss>

