0

Linux上に比較的単純なpthreadプログラムがあります。この目的のために、私はそれを小さな断片に剥ぎ取り、最後に投稿しました。

wait(2)のすべてのマニュアルページには、WNOHANGが使用されない限りwait()がブロックされると記載されています(wait()を指定することもできません)。トラス出力を見ると、次のことをノンストップで繰り返しています。

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()呼び出しは文書化されたとおりに機能します。あなたが遭遇しているのは、ここ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 に答える
1

wait()待機する子プロセスがない場合も、値-1errno設定されてECHILD(これがstrace表示されている)、すぐに戻ります。子プロセスが存在しない場合、それが何をすることを期待していましたか?

于 2012-04-15T06:35:59.513 に答える