Just check status process in c

丶灬走出姿态 提交于 2019-12-18 03:45:20

问题


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.

I would want something like

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

回答1:


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 */
}



回答2:


I think you want waitpid with WNOHANG.

waitpid(pid, &status, WNOHANG);



回答3:


Kill it with signal 0 and check return value.



来源:https://stackoverflow.com/questions/4200373/just-check-status-process-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!