我想知道進程的狀態。我想我可以使用等待家庭功能,但實際上我不想等待過程,只需檢查狀態並繼續。只需檢查c狀態進程
我希望像
checkStatusOfProcess(&status);
if(status == WORKING) {
//do something
} else if(status == exited) {
//do something else
} else \\I dont care about other states
我想知道進程的狀態。我想我可以使用等待家庭功能,但實際上我不想等待過程,只需檢查狀態並繼續。只需檢查c狀態進程
我希望像
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 */
}
我想你想waitpid
與WNOHANG
。
waitpid(pid, &status, WNOHANG);
與信號0殺了它,並檢查返回值。
那會繼續執行? – gvalero87 2010-11-17 00:20:32
@gvalero,對。 – 2010-11-17 00:21:35