2010-11-17 78 views
0

我想知道進程的狀態。我想我可以使用等待家庭功能,但實際上我不想等待過程,只需檢查狀態並繼續。只需檢查c狀態進程

我希望像

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

回答

7

然後你想使用的功能waitpidWNOHANG選項:

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

我想你想waitpidWNOHANG

waitpid(pid, &status, WNOHANG); 
+0

那會繼續執行? – gvalero87 2010-11-17 00:20:32

+1

@gvalero,對。 – 2010-11-17 00:21:35

1

與信號0殺了它,並檢查返回值。