如何获得子进程的退出状态?

问题描述 投票:0回答:1

两个示例输出(由我的教授提供)是(这些输入是在Linux终端中输入的:]

 ibrahim@ibrahim-latech:~$ ./prog2 .
 Current working directory: /home/ibrahim
 Executing ls . --all -l --human-readable
 total 24M
 drwxr-xr-x 74 ibrahim ibrahim 20K Oct 26 16:08 .
 drwxr-xr-x 6 root root 4.0K Apr 10 2014 ..
 -rw-r--r-- 1 ibrahim ibrahim 4.1K Sep 13 12:09 .bashrc
 drwxr-xr-x 7 ibrahim ibrahim 4.0K Oct 11 14:51 Desktop/
 ...snip...
 Exit status: 0

因此,如果代码有效,则退出状态为0,但此示例:

 ibrahim@ibrahim-latech:~$ ./prog2 /root
 Current working directory: /home/ibrahim
 Executing ls /root --all -l --human-readable
 Can't chdir to /root
 Exit status: 1 

退出状态为1,因为它为false。

这是我的代码:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
    pid_t pid;
    pid = fork();
    char cwd[255];
    if(pid == 0){
        printf("Current working directory: %s\n",getcwd(cwd, sizeof(cwd)));
        printf("Executing ls %s --all -l --human-readable\n",argv[1]);
        if(chdir(argv[1])!=0){
            printf("this is not a valid directory");
        }
        else{
            execl("/bin/ls","ls","--all","-l","--human-readable", NULL);
        }
    }
    else{
        wait(NULL):
        printf("Exit status:");// not sure how to put the 1 or 0 in this
    }
}

退出状态必须从父进程输出。我不知道如何使退出状态正常工作。

c fork exec
1个回答
0
投票

将评论中的关键信息转移到答案中。

wait()函数会告诉您PID和退出状态-您只需要捕获它们,而不必忽略它们。您需要使用wait()WIFEXITED(如果WEXITSTATUS报告为false,则还有其他宏可用于信号和核心转储等)。

修改您的代码:

WIFEXITED

我在不使用GNU #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <unistd.h> #ifndef LS_PATH #define LS_PATH "/bin/ls" #endif int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: %s directory\n", argv[0]); return 1; } pid_t pid = fork(); if (pid == 0) { char cwd[255]; printf("Current working directory: %s\n", getcwd(cwd, sizeof(cwd))); printf("Executing 'ls -a -l' in %s\n", argv[1]); if (chdir(argv[1]) != 0) { fprintf(stderr, "%s: %s is not a valid directory\n", argv[0], argv[1]); return 1; } //execl(LS_PATH, "ls", "--all", "-l", "--human-readable", NULL); execl(LS_PATH, "ls", "-a", "-l", NULL); fprintf(stderr, "Failed to execute %s\n", LS_PATH); return 1; } else { int status; int corpse = wait(&status); if (corpse < 0) printf("Failed to wait for process %d (errno = %d)\n", (int)pid, errno); else if (corpse != pid) printf("Got corpse of process %d (status 0x%.4X) when expecting PID %d\n", corpse, status, (int)pid); else if (WIFEXITED(status)) printf("Process %d exited with normal status 0x%.4X (status %d = 0x%.2X)\n", corpse, status, WEXITSTATUS(status), WEXITSTATUS(status)); else if (WIFSIGNALED(status)) printf("Process %d exited because of a signal 0x%.4X (signal %d = 0x%.2X)\n", corpse, status, WTERMSIG(status), WTERMSIG(status)); else printf("Process %d exited with status 0x%.4X which is %s\n", corpse, status, "neither a normal exit nor the result of a signal"); } return 0; } 的Mac上工作,所以我已经将代码的编译时可配置为ls的位置,并且它仅使用可移植的选项(ls-a而不是双破折号选项)。我更改了消息传递并检测到更多错误。像您的代码一样,这假设-l不会失败-它也应该在那里检查错误。

样本输出(缩写)—来自从fork()编译的程序es61

es61.c
© www.soinside.com 2019 - 2024. All rights reserved.