I have a relatively simple pthread program on Linux. For this purpose, I’ve stripped it to a small piece, posted at the end.
All the man pages for wait(2) say that wait() will block unless WNOHANG is used (which is not even possible to specify for wait()). Looking at truss output, it’s just a non-stop repetition of:
5460 wait4(-1, 0x7f8ee479fea8, 0, NULL) = -1 ECHILD (No child processes)
again, looking at man page of wait4(2), it says that the return results are same as for waitpid(), and waitpid should block unless there is WNOHANG, and truss shows that the call was made with options being 0.
For reference, this is:
- 2.6.32-300.3.1.el6uek.x86_64
- glibc-2.12-1.47.el6_2.9.x86_64
- gcc-4.4.6-3.el6.x86_64
- compiled with: gcc -pthread -lpthread -o ptw -Wall ptw.c
—
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
static pthread_mutex_t _lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t _sem = PTHREAD_COND_INITIALIZER;
static void* waiter(void*);
int main(int argc, char ** argv) {
pthread_t thr;
if (pthread_create(&thr, 0, waiter, 0)) {
perror("pthread_create");
return 1;
}
pthread_mutex_lock(&_lock);
pthread_cond_wait(&_sem, &_lock);
return 0;
}
void* waiter(void* arg) {
while (1) {
int status;
int p = wait(&status);
if (p<0) {
perror("wait");
// fprintf(stderr, "wait returned without exited child\n");
}
}
return 0;
}
wait()will also return immediately if there is no child process to wait for, with value-1anderrnoset toECHILD(which is what yourstraceis showing you). What were you expecting it to do in the case where no child processes exist?