我在 Linux 上有一个相对简单的 pthread 程序。为此,我将它剥离成一小块,贴在最后。
wait(2) 的所有手册页都说 wait() 将阻塞,除非使用 WNOHANG(甚至不能为 wait() 指定)。查看 truss 输出,它只是不停地重复:
5460 wait4(-1, 0x7f8ee479fea8, 0, NULL) = -1 ECHILD (No child processes)
再次查看 wait4(2) 的手册页,它说返回结果与 waitpid() 的返回结果相同,除非存在 WNOHANG,否则 waitpid 应该阻塞,并且 truss 显示调用是在选项为 0 的情况下进行的。
供参考,这是:
- 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
- 编译: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;
}