只需检查 c 中的状态进程 [英] Just check status process in c
本文介绍了只需检查 c 中的状态进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道进程的状态.我想我可以使用等待系列功能,但实际上我不想等待过程,只需检查状态并继续.
I want to know the status of a process. I think I can use the wait family functions but actually I don't want to wait for the process, just check the status and go on.
我想要类似的东西
checkStatusOfProcess(&status);
if(status == WORKING) {
//do something
} else if(status == exited) {
//do something else
} else \I dont care about other states
推荐答案
然后你想使用带有 WNOHANG
选项的 waitpid
函数:
Then you want to use the waitpid
function with the WNOHANG
option:
#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 中的状态进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文