我想知道进程的状态。我想我可以使用 wait 系列功能,但实际上我不想等待该过程,只需检查状态并继续。
我想要类似的东西
checkStatusOfProcess(&status);
if(status == WORKING) {
//do something
} else if(status == exited) {
//do something else
} else \\I dont care about other states
最佳答案
然后你想使用 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 */
}
关于c - 只需检查 c 中的状态过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4200373/