0

I tried with strerror(errno) get the waitpid() result string.
I checked the status string in both conditions:

1)  Child is running by adding `sleep(30)` in child process
2)  Without `sleep(30)` in child

I got the below displays from strerror():

While Child is Running:

WAITPID/WAITID returns: 0
WAITPID/WAITID childStatus: 0
waitpid: Success 

Child Terminated:

WAITPID/WAITID returns: -1
WAITPID/WAITID childStatus: 0
waitpid: No child processes

What should be the expected result string from strerror(errno) after waitpid(). What do you mean be "No child Process" does it a valid string if child is exited/terminated normally? What should be expected return value from waitpid() if child is running / child terminated?

Here are my cases of strerror() returns:

"Success"               : in case of child is running with sleep in child
"No child processes"    : in case of child is terminated without sleep in child

Code used:

#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 without sleep:

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~
Rachid K.
  • 4,490
  • 3
  • 11
  • 30
kapilddit
  • 1,729
  • 4
  • 26
  • 51
  • 1
    Can you share your code snippet about how you are calling waitpid/fork in your code. waitpid() behavior depends on what is the input. – Mantosh Kumar Apr 10 '14 at 07:17
  • 3
    Note that `errno` is only meaningful if a function fails. That is when waitpid() returns -1, not if it returns anything else. The code you added is not enough. We need to see your calls to fork(), waitpid() too as well as variable declarations. ALL your code, not just 2 lines with printf/perror. – nos Apr 10 '14 at 07:30
  • @nos Please find code used in updated question. – kapilddit Apr 10 '14 at 13:20
  • It is correct that I should not read the errno status if it doesn't fail. but I check that in error codition (when waitpid returns -1 with ECHILD) then it is showing me "No child processes". I think that child process entry should be there till the time parent process do waitpid to get the status? then how it can happen? – kapilddit Apr 11 '14 at 05:43

0 Answers0