How to get a return value from a CHILD PROCESS?

The program calculates the sum of numbers from 1 to N .. The child process calculates the sum of EVEN numbers. The parent process calculates the sum of the odd numbers. I want to get the return value of the child process in the parent process. How to do it

#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; } 

Tell me how can I get the return value of a child process?

+7
c gcc linux unix fork
source share
4 answers

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

+9
source share

Oops! This is not bash! Anyway...

The cause of the process is the continuation of the main thread, something in the meantime does not affect this. I browse 10k image directories and invert duplicates. I put the comparison code in the function and subprocess. It's very fast.

The way to return the value is to create a channel, direct the value to it, and then read the pipe: (Warning! The following code probably does not work, it just shows the working channel)

 mkfifo pipe moved=0 # Use imageMagick 'compare' to find files with zero difference: comPare () { if [[ ! $(compare -metric AE $1 $2) ]]; then mv $2 moved; echo 1 > pipe; else echo 0 > pipe fi } # For every file in the dir, compare it to every other one: for file1 in $( ls *.bmp | head -n $(( $( ls *.bmp | wc -l) - 1)); do for file2 in $( ls *.bmp | tail -n $(( $( ls *.bmp | wc -l) - 1)); do comPare file1 file2 & moved=$(( $(cat pipe) + 1 )) done done rm pipe echo "Moved $moved files" 

The only problem so far is that I am working on a USB drive, and permissions stop me from creating a channel. Besides...

+2
source share

Like dtmilano, you should use wait or waitpid, as needed.

But perhaps you need to split your program into two parts and execute your son with exec. When you do fork, your child process gets pid equal to 0, and I don't know if you will try to wait for process signals with pid 0, it will work fine.

In any case, you can go through, although this is not the best way, the value calculated by your child process through the output.

+1
source share

If you want to keep the return value for a success or failure message, you can call pipe(2) before forcing to configure a separate channel between the parent and child.

+1
source share

All Articles