0

我在 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;

}
4

2 回答 2

1

wait()如果没有要等待的子进程,也将立即返回,值-1errno设置为ECHILD(这是您strace向您展示的内容)。在不存在子进程的情况下,您希望它做什么?

于 2012-04-15T06:35:59.513 回答
1

wait()呼叫按记录工作。您遇到的是此处ERRORS部分中描述的错误情况:

   ECHILD (for wait()) The calling process does not have any unwaited-for
          children.

   ECHILD (for waitpid() or waitid()) The process specified by pid (waitpid()) or
          idtype and id (waitid()) does not exist or is not a child of the
          calling process.  (This can happen for one's own child if the action
          for SIGCHLD is set to SIG_IGN.  See also the Linux Notes section about
          threads.)

   EINTR  WNOHANG was not set and an unblocked signal or a SIGCHLD was caught;
          see signal(7).

   EINVAL The options argument was invalid.

因此它不会阻塞,因为阻止错误是没有意义的。

于 2012-04-15T06:35:52.133 回答