Subj : Re: threaded program and wait() call To : comp.programming.threads From : tadek Date : Sun Aug 07 2005 08:46 pm Hi Patrick, Thank you for your comments and suggestions. I do agree that creating 'main' and 'worker' thread does not make much sense. But I am relatively new in the organization I work for and since I am porting old and supposably reliable code from QNX to Linux platform I just wanted to make it work. Even before I've got your reply, over the weekend I decided to get rid of main/worker thread anyway. But before I do it let me clarify few points which perhaps were not clear in my original posting: 1) 'worker'thread spawns many children (not one child) and it serves two purposes: - to monitor whether any of them exits and then to respown it - to allow orderly shudown of the application by killing all children when 'main'thread receives SIGTERM signal (by calling kill -s SIGTERM pid of this process) 2) according to man page of waitpid/wait: "The wait() function shall suspend execution of the calling thread until status information for one of the the terminated child processes of the the calling process is available, or until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process". The signal handler which I installed before starting 'worker' thread catches SIGTERM and then causese 'main'thread to call pthread_kill with SIGHUP as a signal. The signal handler catches SIGHUP, but it never causes wait() to get it. I haven't tried your suggestion (SIGCLD) (I'm away from my target system today), but I don't see how would it help. BTW shouldn't I use SIGCHLD instead? As far as LinuxThreads vs NPTL part: my question is how do I know which one am I using? It is very likely silly question, but all I know is that I compile my source with -pthread option and I am at the 'mercy' of the linker to select one for me. I am using 3.3.3 version of gcc. Cheers, Tad Dragowski Patrick TJ McPhee wrote: > In article <1123280898.049380.245390@g44g2000cwa.googlegroups.com>, > tadek wrote: > > % Process creates two threads. In the 'main' thread module, I install > % signal handler for SIGTERM and SIGHUP. > > I don't understand why you're creating two threads here. Why not simply > fork() off the child? After all, your drone thread seems to be waiting > for the worker to crash, but if the worker crashes, it will take the > drone with it. This is better done by forking off a child and having > the parent process block in waitpid() (or not block in waitpid() if > it has something useful to do. > > If there is some reason to use threads, you should ensure you block > SIGCLD in the drone thread, and you might as well use g_shutdown to > tell the child to shut down, rather than screwing around with signals. > > % PS I am also confused whether I am using LinuxThreads or NPTL when > % linking with glibc and making calls to pthread_* functions. > > You'll be better off using NPTL. It has more portable behaviour, it is > the future, and it's here now. > > -- > > Patrick TJ McPhee > North York Canada > ptjm@interlog.com .