C fork()从子进程的退出状态进行更多诊断
当使用fork get创建的子进程失败并处于某种退出状态时,是否有方法获取更多数字信息。下面是列出问题的示例代码(我们希望获得更多信息的注释)C fork()从子进程的退出状态进行更多诊断,c,fork,C,Fork,当使用fork get创建的子进程失败并处于某种退出状态时,是否有方法获取更多数字信息。下面是列出问题的示例代码(我们希望获得更多信息的注释) #包括 #包括 #包括 #包括 #包括 int main() { pid_t pid; pid=fork(); int exitstatus=0; int bSuccess=1; charargv[2][20]={{0,0}; 如果(pid==-1) { printf(“未能创建分叉进程\n”); b成功率=0; } 否则如果(pid==0) { //儿
#包括
#包括
#包括
#包括
#包括
int main()
{
pid_t pid;
pid=fork();
int exitstatus=0;
int bSuccess=1;
charargv[2][20]={{0,0};
如果(pid==-1)
{
printf(“未能创建分叉进程\n”);
b成功率=0;
}
否则如果(pid==0)
{
//儿童代码
如果(-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))
{
返回;
}
}
其他的
{
int-retCode=waitpid(pid,&exitstatus,WUNTRACED);
如果(retCode!=pid)
{
printf(“子进程未运行\n”);
b成功率=0;
}
如果(妻子退出(退出状态))
{
int statusCode=WEXITSTATUS(exitstatus);
如果(状态代码!=0)
{
//如何获取更多信息状态码的含义。有什么方法吗
//要获得更多信息?
printf(“Test.pl执行失败,退出状态\n”,状态代码);
b成功率=0;
}
}
else if(WIFSIGNALED(exitstatus))
{
//如何获取更多信息状态码的含义。有什么方法吗
//要获得更多信息?
printf(“Test.pl因未捕获信号而终止”,WTERMSIG(exitstatus));
b成功率=0;
}
否则如果(WCOREDUMP(exitstatus))
{
printf(“Test.pl终止,进程崩溃\n”);
b成功率=0;
}
否则如果(WIFSTOPPED(exitstatus))
{
//如何获取更多信息状态码的含义。有什么方法吗
//要获得更多信息?
printf(“Test.pl由于信号\n而停止”,WIFSTOPPED(exitstatus));
b成功率=0;
}
}
printf(“成功%d\n”,b成功);
}
现在,当子进程使用退出代码退出时,我们希望获得更多信息。下面是孩子失败时的示例
Test.pl执行失败,状态为退出
现在我无法理解状态代码13的含义,因为手册页也没有列出此类错误代码。因此,我希望从C程序中获得更多关于Test.pl执行失败的信息。就像我们使用shell中相同的参数运行脚本一样,它运行时没有任何错误
因此,任何关于如何为执行失败获取更多信息的想法。退出代码可以由退出程序任意设置(即,
exit()
-调用的参数值),因此该值的语义完全取决于退出程序。大多数程序都遵循惯例,0表示“成功”,其他任何出现错误的程序,但是应该在某个地方记录该值的确切含义(Test.pl源代码中最坏的情况)如果您查看man waitpid
页面,则每个exitstatus
都有其含义的描述,使用此状态,您可以了解您必须执行的操作。如果你想知道更多的信息,你必须自己编写代码。
#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);
}