waitpidの+タイムアウト

する#include <stdio.hに>
する#include <unistd.h>
の#include <STDLIB.H>
の#include <ERRNO.H>
の#includeは<sys / types.h>に
する#includeは<sys / wait.h>

静的pid_tのfork_child(ボイド)
{
  int型PID =フォーク()。
  IF(PID == -1){
   perrorは( "フォーク")。
   出口(1)。
  }
  IF(PID == 0){
   プット( "子:睡眠...")。
   睡眠(10)。
   プット( "子:出");
   出口(0);
  }
  PIDを返します。
}

INT waitpid_timeout(pid_t PID、INT mseconds)
{
  sigset_tマスク、orig_mask。
  sigemptyset(およびマスク)
  sigaddset(&マスク、SIGCHLD)。
  (は、sigprocmask(SIG_BLOCK、&マスク&orig_mask)<0){場合
   にperror( "は、sigprocmask")。
   1を返します。
 }
 他{
   構造体TIMESPECタイムアウト。
   timeout.tv_sec = mseconds / 1000;
   timeout.tv_nsec =(mseconds%1000)* 1000000。
   実行{
      IF(sigtimedwait(&マスク、NULL、&タイムアウト)<0){
          IF(errnoにEINTR ==){
             / * SIGCHLD以外のシグナルによって中断。* /
            続けます。
          }
          そうであれば(errnoにEAGAIN ==){
             printf( "タイムアウト、子供\ nを殺します");
             (PID、SIGKILLを)殺します。
          }
         他{
            perrorは( "sigtimedwait")。
            1を返します。
         }
     }
     他のプットは、(「子供が信号を終了しました」);
     ブレーク;
   }(1)一方、
  IF(waitpidの(PID、NULL、0)<0){
     perrorは( "のwaitpid");
     1を返します。
   }
   0を返します。
  }
}

INTメイン(int型ARGC、チャー*のARGV [])
{
  pid_t PID = fork_child()。

  waitpid_timeout(PID、15000)。

  0を返します。
}

おすすめ

転載: www.cnblogs.com/reboost/p/11371348.html