我尝试使用strerror(errno)获取waitpid结果字符串 我在两种情况下都检查了状态字符串:
1) Child is running by adding sleep(30); in child process
2) Without sleep(30); in child
我从strerror获得了以下打印:
当孩子在跑步时:
WAITPID/WAITID returns: 0
WAITPID/WAITID childStatus: 0
waitpid: Success
终止儿童:
WAITPID/WAITID returns: -1
WAITPID/WAITID childStatus: 0
waitpid: No child processes
在waitpid之后,strerror(errno)的预期结果字符串应该是什么。你是什么意思"没有孩子过程"如果孩子正常退出/终止,它会成为有效的字符串吗?如果孩子正在运行/孩子被终止,那么应该从waitpid返回什么值?
在我的情况下,strerror返回:
"Success" : in case of child is running with sleep in child
"No child processes" : in case of child is terminated without sleep in child
使用的代码:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <signal.h>
int main(void)
{
pid_t Checksum_pid = fork();
printf("\nChecksum_pid : %d", Checksum_pid);
if (Checksum_pid < 0)
printf("Fork Failed\n");
else if (Checksum_pid == 0)
{
printf("\nInside Child Process before execl\n");
//execl("/bin/sleep", "/bin/sleep", "2", NULL);
execl("/bin/ls","ls",(char *)NULL) ;
//exit(EXIT_FAILURE);
printf("\nInside Child Process after execl\n");
exit(EXIT_FAILURE);
}
else
{
int childStatus = 0xFF00;
siginfo_t si;
printf("\nInside Parent Process");
printf("\nParent Sleep for 5 second");
sleep(5);
printf("\nchildStatus : %d", childStatus);
printf("\nstrerror : %s", strerror(errno));
pid_t returnValue = waitpid(Checksum_pid, &childStatus, WNOHANG);
//pid_t returnValue = waitid(P_PID, Checksum_pid, &si, WNOHANG | WEXITED);
printf("\nreturnValue : %d", returnValue);
printf("\nchildStatus : %d", childStatus);
printf("\nstrerror : %s", strerror(errno));
if (returnValue > 0)
{
if (WIFEXITED(childStatus))
printf("\nChild Exit Code: %d\n", WEXITSTATUS(childStatus));
else
printf("\nChild Exit Status: 0x%.4X\n", childStatus);
}
else if (returnValue == 0)
printf("\nChild process still running\n");
else
{
if (errno == ECHILD)
printf("\nError ECHILD!!\n");
else if (errno == EINTR)
printf("\nError EINTR!!\n");
else
printf("\nError EINVAL!!\n");
}
if (WIFEXITED(childStatus)) {
printf("exited, status=%d\n", WEXITSTATUS(childStatus));
} else if (WIFSIGNALED(childStatus)) {
printf("killed by signal %d\n", WTERMSIG(childStatus));
} else if (WIFSTOPPED(childStatus)) {
printf("stopped by signal %d\n", WSTOPSIG(childStatus));
} else if (WIFCONTINUED(childStatus)) {
printf("continued\n");
}
else{
perror("waitpid error");
}
printf("\nParent: I am about to finish\n");
}
return 0;
}
O / P:
Checksum_pid : 17459
Inside Parent Process
Checksum_pid : 0
Inside Child Process before execl
a.out ___waitpid_test1 waitpid_test1~ waitpid_test1.c waitpid_test1.c~ waitpid_test2.c waitpid_test2.c~
Parent Sleep for 5 second
childStatus : 65280
strerror : Success
returnValue : 17459
childStatus : 0
strerror : Success
Child Exit Code: 0
exited, status=0
Parent: I am about to finish
没有睡觉的O / P:
Checksum_pid : 17486
Inside Parent Process
Parent Sleep for 5 second
childStatus : 65280
strerror : Success
returnValue : 0
childStatus : 65280
strerror : Success
Child process still running
exited, status=255
Parent: I am about to finish
Checksum_pid : 0
Inside Child Process before execl
ubuntu:~/Test1$ a.out ___waitpid_test1 waitpid_test1~ waitpid_test1.c waitpid_test1.c~ waitpid_test2.c waitpid_test2.c~