只需检查c中的状态过程

我想知道一个过程的状态。 我想我可以使用等待系列function,但实际上我不想等待这个过程,只需检查状态并继续。

我想要类似的东西

checkStatusOfProcess(&status); if(status == WORKING) { //do something } else if(status == exited) { //do something else } else \\I dont care about other states 

然后你想使用带有WNOHANG选项的waitpid函数:

 #include  #include  int status; pid_t return_pid = waitpid(process_id, &status, WNOHANG); /* WNOHANG def'd in wait.h */ if (return_pid == -1) { /* error */ } else if (return_pid == 0) { /* child is still running */ } else if (return_pid == process_id) { /* child is finished. exit status in status */ } 

我想你想和WNOHANG一起WNOHANG

 waitpid(pid, &status, WNOHANG); 

用信号0杀掉它并检查返回值。