c - 只需检查 c 中的状态过程

标签 c process fork wait

我想知道进程的状态。我想我可以使用 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/

相关文章:

c - ‘main’ 通常是一个非静态函数 ERROR

c - 使用 keyfromString 函数切换字符串的大小写

c++ - 如何获取当前流程的工作对象(如果有)?

c# - 运行时主机到底是什么?

c++ - 如何在 C++ 中创建两个从单个父进程并行执行的子进程?

c - UNIX:recv 的内存问题 - malloc.c:3096:sSYSMALLOC:断言

c - 在 Xcode 中编译和运行 C 程序时如何添加命令行参数?

python - Jupyter Notebook CPU 使用率低

c - fork() 和 waitpid 可能的输出

c - 将Pthread转换为进程fork()