Subj : Re: Problem with system() calls in a multithreaded program on HPUX To : comp.programming.threads,comp.sys.hp.hpux From : Stefaan A Eeckels Date : Fri Feb 11 2005 05:41 pm On Fri, 11 Feb 2005 07:48:37 -0500 "Joe Seigh" wrote: > On 11 Feb 2005 03:46:23 -0800, Mahesh Kumar > wrote: [...] > > Explanation: > > ============ > > If I increase the value of noOfThreads to say 3, 4 and so on. The > > program hangs say around when noOfThreads is 6 or 7. Now as the > > problem occurs, two three defunct processes are created. I ran "ps > > -f > > -u" command and output was something like this (mtreg is the name of > > above program) > > -bash-2.05b$ ps -f -u mkumar > > UID PID PPID C STIME TTY TIME COMMAND > > mkumar 1726 1190 0 00:06:12 pts/ta 0:10 mtreg > > mkumar 1190 1189 0 23:04:02 pts/ta 0:01 -bash > > mkumar 1731 1726 0 00:06:20 pts/ta 0:00 > > mkumar 1730 1726 2 00:06:20 pts/ta 0:00 > > mkumar 1743 0 0 00:06:20 pts/ta 0:00 mtreg > > mkumar 1741 1726 0 00:06:21 pts/ta 0:00 sh -c perl strip.pl > > /export/home/configdev/tmp/FAAa01726mod0 > > mkumar 1742 1741 0 00:06:21 pts/ta 0:00 perl strip.pl > > /export/home/configdev/tmp/FAAa01726mod0456a.m > > mkumar 1751 1190 5 00:07:48 pts/ta 0:00 ps -f -u mkumar > > > [...] > > > > Can anyone please tell me why system() calls are causing problem in > > HPUX 11 whereas the same thing runs fine on Solaris? It would be > > really great if you can suggest a possible solution? > > > > Probably the SIGCHLD signal handing got messed up. You have defunct > programs that have finished but have not had their exit status > collected > yet. Even though system() is supposed to be thread safe it's way too > sensitive to signal disposition to be using in anything but a a single > threaded program. The Solaris 9 man page says that system() isn't thread-safe: USAGE The system() function manipulates the signal handlers for SIGINT, SIGQUIT, and SIGCHLD. For this reason it is not safe to call system() in a multithreaded process. Concurrent calls to system() will interfere destructively with the disposition of these signals, even if they are not manipu- lated by other threads in the application. See popen(3C) for a replacement for system() that is thread-safe. So the fact that the program runs on Solaris is pure luck. > Change your program to be single threaded and use fork(), exec(), > and wait(). See the unix programming books by Stevens on how to > do it. The correct way to go about this is to use popen(). There's no need to be so drastic and forgo multi-threading altogether, but the OP should ask himself if it's really required to achieve the desired result. > Using system() from threads was a major violation of the KISS rule > and you should expect to have problems when that happens. And you > should expect that we aren't going to try to make programs, that are > much more complicated than they should be, work. Well, that depends on what one considers to be "simple". At first sight, "system()" is less complex than "popen()". System() abuse lures the inexperienced programmer. -- Stefaan -- As complexity rises, precise statements lose meaning, and meaningful statements lose precision. -- Lotfi Zadeh .