C语言 如何从 CHILD PROCESS 获取返回值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2146602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get return value from CHILD PROCESS?
提问by Abhijeet Rastogi
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
程序计算从 1 到 N 的数字总和。 子进程计算偶数的总和。父进程计算奇数的总和。我想在父进程中获取子进程的返回值。我怎么做
#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?
请告诉我如何获得子进程的返回值?
回答by gryphonB
Oops! This isn't bash! Anyway...
哎呀!这不是bash!反正...
The reason for spawning a process is to continue the main flow, doing something meanwhile that doesn't affect that. I'm running through directories of 10k images, & moving out duplicates. I put the compare code in a function & subprocess that. It's very fast.
产生进程的原因是继续主流程,同时做一些不影响它的事情。我正在浏览 10k 个图像的目录,并移出重复项。我将比较代码放在一个函数和子进程中。它非常快。
The way to get a value back is to create a pipe, echo a value onto that then read the pipe: ( Warning! the following code probably doesn't work, it just shows a working pipe)
取回值的方法是创建一个管道,将一个值回显到该管道上,然后读取管道:(警告!以下代码可能不起作用,它只是显示了一个工作管道)
mkfifo pipe
moved=0
# Use imageMagick 'compare' to find files with zero difference:
comPare () {
if [[ ! $(compare -metric AE ) ]]; then
mv 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'm working on a usb drive, & permissions are stopping me from creating the pipe. Apart from that ...
到目前为止唯一的问题是我正在使用 USB 驱动器,并且权限阻止我创建管道。除此之外 ...
回答by Rafael
Like dtmilano said you should use wait or waitpid, depending or need.
就像 dtmilano 说的那样,您应该根据或需要使用 wait 或 waitpid。
But maybe you should split your program in two parts and execute the son with exec. When you make fork your child process receive pid equal 0 and I don't know if you try to wait signals of process with pid 0 will work fine.
但也许你应该把你的程序分成两部分,然后用 exec 执行儿子。当您创建 fork 时,您的子进程会收到等于 0 的 pid,我不知道您是否尝试使用 pid 0 等待进程的信号会正常工作。
Anyway you can pass, though it's not the best way, the value calculated by your child process through exit.
无论如何,您可以通过,尽管这不是最好的方法,但您的子进程通过退出计算出的值。
回答by pra
If you want to preserve the return value for signaling a successful or failure exit status, you can call pipe(2)before forking to set up a separate channel between parent and child.
如果您想保留返回值以表示成功或失败退出状态,您可以pipe(2)在分叉之前调用以在父子之间建立单独的通道。

