나는 명령 1의 관련 오류 코드, 오류가 반환됩니다 명령 줄에서 실행할 때 일부 입력이 있습니다오류 조건에 대해 올바른 WEXITSTATUS를 반환() waitpid를 얻을 수 없음
$ foo bar
[some useful error message...]
$ echo $?
1
을
나는 waitpid()
으로이 오류 코드를 잡으려고 노력하고 있어요 :
...
char *proc_cmd = "foo bar"
pid_t proc = popen4(proc_cmd, in_fd, out_fd, err_fd, POPEN4_FLAG_NONE);
...
if (waitpid(proc, &global_foo_status, WNOHANG | WUNTRACED) == -1) {
/* process failed */
}
...
pthread_create(&proc_thread, NULL, perform_foo_function, bar_data);
pthread_join(proc_thread, (void **) NULL);
...
프로세스가 실패 할 때까지로 인해 데이터에 오류가 bar_data
더 이상 처리하지 않는 것입니다, 또는 때까지 perform_foo_function()
를 실행 내 스레드 :
static void * perform_foo_function (data *bar_data) {
/* check before */
if (WIFEXITED(global_foo_status)) {
int exit_status = WEXITSTATUS(global_foo_status);
if (exit_status != 0)
/* process failed */
}
/* do stuff with bar_data */
while (bar_data) {
/* causes error ... */
}
/* check after */
if (WIFEXITED(global_foo_status)) {
int exit_status = WEXITSTATUS(global_foo_status);
if (exit_status != 0)
/* process failed */
}
pthread_exit(NULL);
}
제 질문은이 프로세스의 오류 상태를 잡는 방법입니까? 디버깅 과정에서 의도적으로 오류 상황을 만들거나 합법적 인 입력을 제공하든 WEXITSTATUS
은 항상 0입니다.
waitpid()
에 대한 오해와 관련된 상태 코드 확인 및이를 작동시키기 위해 어떤 변경을해야합니까?
다음 코드는 차단하지 않고, 작동하는 것 같다
후속 :
...
char *proc_cmd = "foo bar"
pid_t global_foo_pid = popen4(proc_cmd, in_fd, out_fd, err_fd, POPEN4_FLAG_NONE);
...
if (waitpid(global_foo_pid, &global_foo_status, WNOHANG | WUNTRACED) == -1) {
/* process failed */
}
...
pthread_create(&proc_thread, NULL, perform_foo_function, bar_data);
pthread_join(proc_thread, (void **) NULL);
...
static void * perform_foo_function (data *bar_data)
{
/* do stuff with bar_data */
while (bar_data) {
/* causes error ... */
}
/* check after */
if (WIFEXITED(global_foo_status)) {
waitpid(global_foo_pid, &global_foo_status, WUNTRACED);
int exit_status = WEXITSTATUS(global_foo_status);
if (exit_status != 0)
/* process failed */
}
pthread_exit(NULL);
}
을 나는 과정이 있기 때문에 waitpid()
전화가 응답하지 않습니다 "후 확인"같은데요 이 단계에서 이미 종료되었습니다.