【问题标题】:How to get return value from child process to parent?如何从子进程获取返回值到父进程?
【发布时间】:2018-09-09 21:41:28
【问题描述】:

我应该将斐波那契数列的前 12 个项的总和从子进程返回给父进程,但不是377,父进程得到30976。

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

int main(int argc, char *argv[])
{
    pid_t childpid;
    int i, fib_sum=0, fib1=1, fib2=1, temp, status;

    childpid=fork();

    if(childpid!=0)
    {
        wait(&status);
        fprintf(stderr, "%d\n", status);
    }
    else
    {
        for(i=1; i<=12; i++)
        {
            temp=fib1;
            fib_sum=fib1+fib2;
            fib1=fib_sum;
            fib2=temp;
        }
        fprintf(stderr, "%d\n", fib_sum);
        return fib_sum;
    }
}

我做错了什么?

【问题讨论】:

  • 使用管道在进程之间传递数据。
  • 锻炼是如何阻止你使用管道的?
  • 如果需要从child获取数据,可以使用child更新的shared全局变量,当parent检测到child完成时,会将sum存储在shared中全球。
  • 您将如何共享该全局变量? @bruceg。他必须创造一个共享的记忆。
  • 来自man 2 wait: "WEXITSTATUS(wstatus) 返回子进程的退出状态。这由子进程在退出调用中指定的状态参数的最低有效 8 位组成(3) 或 _exit(2) 或作为 main() 中的 return 语句的参数。"

标签: c fork parent-child ipc child-process


【解决方案1】:

如果您不能use pipes,这将是这里的最佳解决方案,您可以将结果保存到父级将从中读取的文件中。传递文件的名称以将结果保存到从父级到子级。在您的子进程中,您会这样做:

int main(int argc, char *argv[])
{
    int fib_sum=0;
    if (argc <= 1)
    {
        print_usage();
        return 1;
    }
    //... calculate fib_sum
    FILE *f = fopen(argv[1], "w");
    if (f == NULL)
    {
        printf("Error opening file!\n");
        return 1;
    }
    fprintf(f, "%d", fib_sum);
    return 0;
}

然后在你的父进程中:

int n = 0;
FILE* f;
//... spawn child and wait
FILE *f = fopen(file_name, "r");
fscanf(f, "%d", &n);

【讨论】:

    【解决方案2】:

    我应该返回斐波那契数列前 12 项的总和 从子进程到父进程,但有 377,父进程得到 30976.

    进程退出状态的值是有限的,因此它不是在子级和父级之间传达值的最佳方式。

    解决方案之一是使用管道传递计算值。

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int main(int argc, char *argv[])
    {
        pid_t childpid;
        int i, fib_sum=0, fib1=1, fib2=1, temp, status;
    
        int fd[2];
        int val = 0;
    
        // create pipe descriptors
        pipe(fd);
    
        childpid = fork();
        if(childpid != 0)  // parent
        {
            close(fd[1]);
            // read the data (blocking operation)
            read(fd[0], &val, sizeof(val));
    
            printf("Parent received value: %d\n", val);
            // close the read-descriptor
            close(fd[0]);
        }
        else  // child
        {
            // writing only, no need for read-descriptor:
            close(fd[0]);
    
            for(i=1; i<=12; i++)
            {
                temp = fib1;
                fib_sum = fib1+fib2;
                fib1 = fib_sum;
                fib2 = temp;
            }
    
            // send the value on the write-descriptor:
            write(fd[1], &fib_sum, sizeof(fib_sum)); 
            printf("Child send value: %d\n", fib_sum);
    
            // close the write descriptor:
            close(fd[1]);
    
            return fib_sum;
        }
    }
    

    测试:

    Child send value: 377                                                                                                                         
    Parent received value: 377
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-05
      • 2013-07-18
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      相关资源
      最近更新 更多