Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get return value from CHILD PROCESS?

Tags:

c

linux

fork

unix

gcc

Program calculates sum of numbers from 1 to N.. Child process calculates sum of EVEN numbers. Parent process calculates sum of odd numbers. I want to get the return value of child process in parent process. How do i do that

#include<stdio.h>
#include<errno.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>

int main()
{
    int N;
    int id;
    int fd_result;;

    printf("Enter N till which you want the sum: \n");
    scanf("%d",&N);
    if ((fd_result=creat("result", 600))== -1)
    {
        perror("Error creating file");
        exit(1);
    }
    if ((fd_result=open("result",O_TRUNC | O_RDWR)) == -1)
    {
        perror("Error Opening file");
        exit(1);
    }
    if ((id=fork())<0)
    {
        perror("Error occurred forking....!");
        exit(errno);
    }
    if (id == 0)
    {
        int i;
        int sum=0;
        for (i=0;i<=N;i=i+2)
            sum+=i;
        printf("Child sum: %d",sum);
        if (write(fd_result,&sum,sizeof(int))==-1) perror("Error writing to file");
        _exit(0);
    }


    if (id > 0)
    {
        int i;
        int sum=0;
        int sum_odd;
        for (i=1;i<=N;i=i+2)
            sum+=i;
        lseek(fd_result,0,SEEK_SET);
        read(fd_result,&sum_odd,sizeof(int));
        printf("\nThe sum is: %d",sum+(int)sum_odd);
    }

    close(fd_result);
    return 0;
}

Pls tell me how do I get the return value of child process?

like image 965
Abhijeet Rastogi Avatar asked Jan 27 '10 11:01

Abhijeet Rastogi


People also ask

How can I get my child's return value?

To obtain the return value from another program run as a child process your code must fork. The child fork (pid zero) contains the execl() function to “execute and leave” another program. The parent fork contains the wait() function, which suspends execution until the child process completes its task.

What is the return value of fork () for a child process?

Upon successful completion, fork() returns 0 to the child process and returns the process ID of the child process to the parent process.

What happens when a child process returns?

Child process increments val prints it and returns. Once it returns parent process resumes and executes further by incrementing var , printing its value and then returning from it from the main() .

How do you find the PID of all child processes?

Using the /proc File System It contains information about the kernel, system, and processes. We can find the PIDs of the child processes of a parent process in the children files located in the /proc/[pid]/task/[tid] directories.


2 Answers

TLDR

int wstatus;
waitpid(<pid>, &wstatus, 0); // Store proc info into wstatus
int return_value = WEXITSTATUS(wstatus); // Extract return value from wstatus

Reference

The manpage for waitpid shows its type signature is:

pid_t waitpid(pid_t pid, int *stat_loc, int options);

It also states that we can store process information with the stat_loc argument:

if the value of the argument stat_loc is not a null pointer, information shall be stored in the location pointed to by stat_loc

the stat_val argument is the integer value pointed to by stat_loc.

We then use WEXITSTATUS(wstatus) to extract our return value from the process.

WEXITSTATUS(stat_val): If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().

like image 157
Noel Kwan Avatar answered Oct 04 '22 02:10

Noel Kwan


What you are looking for is wait() or waitpid().

like image 29
Diego Torres Milano Avatar answered Oct 04 '22 02:10

Diego Torres Milano