blackdaemon
blackdaemon

Reputation: 755

Obtain this exit status of a child process and from the parent process and print it in the parent.

and I was exploring the processes flow in c. I want to update the following program in a way that, the child terminates with an exit status (such as 10). Obtain this exit status from the parent process and print it in the parent. I have tried the following way, but the status am getting from the parent is wrong. Where i went wrong? Please help me on it guys. Thanks in advance.

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

int main()
{
    int pid;
    int ppid;
    int status;
    pid=vfork();
    pid_t return_pid = waitpid(pid, &status, WNOHANG);
    //printf("%d\n", pid); --> value = 0

    if (pid < 0) {
        printf("\n Error ");
        exit(1);
    } else if (pid==0) {
        printf("\n Hello I am the child process\n");
        printf("\n My pid is:  %d\n", getpid());
        printf("\n My parent pid is:  %d\n", getppid());
        printf("\n My return_pid is:  %d\n", return_pid);
        printf("_______________________________________\n");
        int es = WEXITSTATUS(status);
        printf ("\n Child exit status:  %d\n", es);
        status = es;
        exit(0);
    } else {
        printf("The child has terminated!\n");
        printf("_______________________________________\n");
        printf("\n Hello I am the parent process\n");
        printf("\n My actual pid is %d\n" , getpid());
        printf("\n My parent pid is:  %d\n", getppid());
        printf("\n My return_pid is:  %d\n", return_pid);
        printf("_______________________________________\n");
        printf ("\n Child's Exit staus from parent:  %d\n", status);
        exit(1);
    }
}

Upvotes: 1

Views: 3027

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 755044

This code, using fork() and not vfork(), does something close to what you're after. You want to wait for the child to die — don't use the option that says "Don't wait for the child to die if it isn't already dead". And it would be better to only wait in the parent. This code also sometimes kills the child with an interrupt signal — mainly so you can see how exit statuses are coded.

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

int main(void)
{
    pid_t pid = fork();

    if (pid < 0)
    {
        perror("fork()");
        exit(1);
    }
    else if (pid == 0)
    {
        printf("Hello I am the child process\n");
        printf("My pid is:  %d\n", (int)getpid());
        printf("My parent pid is:  %d\n", (int)getppid());
        int status = getpid() % 256;
        printf("I am about to exit with status 0x%.2X (%d)\n", status, status);
        printf("_______________________________________\n");
        exit(status);
    }
    else
    {
        int status;
        if (pid % 10 == 0)
            kill(pid, SIGINT);
        pid_t return_pid = waitpid(pid, &status, 0);
        printf("The child has terminated!\n");
        printf("Hello I am the parent process\n");
        printf("My actual pid is %d\n", (int)getpid());
        printf("My parent pid is:  %d\n", (int)getppid());
        printf("My return_pid is:  %d\n", (int)return_pid);
        printf("Child's raw exit status:  0x%.4X (%d)\n", status, status);
        if (WIFEXITED(status))
            printf("Child's exit status: 0x%.2X (%d)\n",
                   WEXITSTATUS(status), WEXITSTATUS(status));
        if (WIFSIGNALED(status))
            printf("Child exited because of signal %d\n", WTERMSIG(status));
        printf("_______________________________________\n");
    }
    return 0;
}

Sample outputs:

Normal run:

Hello I am the child process
My pid is:  40377
My parent pid is:  40376
I am about to exit with status 0xB9 (185)
_______________________________________
The child has terminated!
Hello I am the parent process
My actual pid is 40376
My parent pid is:  847
My return_pid is:  40377
Child's raw exit status:  0xB900 (47360)
Child's exit status: 0xB9 (185)
_______________________________________

Signal run:

The child has terminated!
Hello I am the parent process
My actual pid is 40379
My parent pid is:  847
My return_pid is:  40380
Child's raw exit status:  0x0002 (2)
Child exited because of signal 2
_______________________________________

Upvotes: 2

Related Questions