fork()从子进程的退出状态进行更多诊断

时间:2017-05-29 10:36:18

标签: c fork

当使用fork创建的子进程失败并有一些退出状态时,有没有办法获得更多的数字诊断信息。下面是列出问题的示例代码(我们希望获得更多信息的评论)。

#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>

int main()
{
    pid_t pid;
    pid = fork();
    int exitstatus = 0;
    int bSuccess = 1;
    char argv[2][20] = { {0 , 0} };
    if(pid == -1)
    {
        printf("Failed to create Forked process\n");
        bSuccess = 0;
    }
    else if(pid == 0)
    {
        // code for child
        if(-1 == execlp("perl", "perl", "/home/test/test.pl", "-b", "/opt/test/test", "-c", "64", "-t", "Imm", "-v", "a.b.c.d", argv[0], argv[1], (char*)0))
        {
            return;
        }
    }
    else
    {
        int retCode = waitpid(pid, &exitstatus, WUNTRACED);
        if(retCode != pid)
        {
            printf("Child process is not running\n");
            bSuccess = 0;
        }
        else if (WIFEXITED(exitstatus))
        {
            int statusCode = WEXITSTATUS(exitstatus);
            if (statusCode != 0)
            {
                // How to get more information what the status code means. Is there a way
                // to get more information?
                printf("Test.pl execution failed with exit status <%d>\n", statusCode);
                bSuccess = 0;
            }
        }
        else if (WIFSIGNALED(exitstatus))
        {
            // How to get more information what the status code means. Is there a way
            // to get more information?
            printf("Test.pl terminated due to uncaught signal <%d>\n", WTERMSIG(exitstatus));
            bSuccess = 0;
        }
        else if (WCOREDUMP(exitstatus))
        {
            printf("Test.pl terminated and process is crashed\n");
            bSuccess = 0;
        }
        else if (WIFSTOPPED(exitstatus))
        {
            // How to get more information what the status code means. Is there a way
            // to get more information?
            printf("Test.pl has stopped due to signal <%d>\n", WIFSTOPPED(exitstatus));
            bSuccess = 0;
        }
    }
    printf("Success %d\n", bSuccess);
}

现在当子进程退出并带有退出代码时,我们希望获得更多信息。如下所示是孩子失败时的样本

  

Test.pl执行失败,退出状态为&lt; 13&gt;

现在我无法理解这个状态代码13的含义,因为手册页也没有列出这样的错误代码。因此,我希望获得更多信息,说明为什么Test.pl的执行会从C程序中失败。当我们从shell运行具有相同参数的脚本时,它运行时没有任何错误。

所以想到如何为执行失败提供更多信息。

0 个答案:

没有答案