只需检查c中的状态过程

gva*_*o87 3 c fork process wait

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

我想要类似的东西

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

mob*_*mob 11

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

#include <sys/types.h>
#include <sys/wait.h>

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 */
}
Run Code Online (Sandbox Code Playgroud)