如何 return 从子进程到父进程的价值?

how to return value from children process to parent?

我知道已经有一些关于此的问题,但我不明白我在这里做错了什么。 我正在从文件中读取数字,并将它们存储到数组中。 现在我想创建父进程读取 10 个数字,然后子进程读取其他数字,将它们加在一起并发送给父进程,但由于某种原因,我得到的 return 值是错误的。我在这里缺少什么? 我添加了图像来演示

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

int my_read(char *arg1);

int main(int argc, char *argv[])
{
  my_read("numbers.txt");
}

int my_read(char *arg1)
{
  int status;
  int avg = 0;
  int p, i;
  int arr[26];

  FILE *file = fopen(arg1, "r");

  if (NULL == file)
  {
    printf("file can't be opened \n");
  }

  fscanf(file, "%d", &i);
  while (!feof(file))
  {
    arr[p] = i;
    p++;
    fscanf(file, "%d", &i);
  }
  arr[25] = i;

  // fork
  printf("I am the parent: %d\n", (int)getpid());

  pid_t pid = fork();

  if (pid < 0)
  { /* error occurred */
    perror("Fork failed");
  }
  if (pid == 0)
  { /* child process */
    printf("I am the child with pid %d\n", (int)getpid());
    for (i = 10; i < 26; i++)
    {
      printf("%d\n", arr[i]);
      avg += arr[i];
    }
    exit(avg);//send to parent
    
  }

  /* parent process */

  for (i = 0; i < 10; i++)
  {
    printf("%d\n", arr[i]);
  }

  wait(&status);
  printf("I am the parent: %d\n", (int)getpid());
  printf("%d\n", status);

  fclose(file);
  return (0);
}

这段代码解决了我评论中列出的大部分问题。它解决的一个区域没有在注释中突出显示,那就是原始代码的脆弱性——它假设文件中正好有 26 个数字。此代码确保仅读取 26 个数字。

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

int my_read(char *arg1);

int main(void)
{
  my_read("numbers.txt");
  return 0;
}

int my_read(char *arg1)
{
  int status;
  int avg = 0;
  int p, i;
  int arr[26];

  FILE *file = fopen(arg1, "r");

  if (NULL == file)
  {
    fprintf(stderr, "file '%s' can't be opened for reading\n", arg1);
    exit(EXIT_FAILURE);
  }

  p = 0;
  while (fscanf(file, "%d", &i) == 1 && p < 26)
    arr[p++] = i;

  fclose(file);

  /* p contains the number of values */

  // fork
  printf("I am the parent: %d\n", (int)getpid());

  pid_t pid = fork();

  if (pid < 0)
  { /* error occurred */
    perror("Fork failed");
  }
  if (pid == 0)
  { /* child process */
    printf("I am the child with pid %d\n", (int)getpid());
    for (i = 10; i < p; i++)
    {
      printf("%d\n", arr[i]);
      avg += arr[i];
    }
    printf("Sum of values: %d\n", avg);
    avg /= (26 - 10);
    printf("Avg of values: %d (0x%.2X)\n", avg, avg);
    exit(avg);//send to parent
  }

  /* parent process */

  for (i = 0; i < 10; i++)
  {
    printf("%d\n", arr[i]);
  }

  wait(&status);
  printf("I am the parent: %d\n", (int)getpid());
  printf("Raw: 0x%.4X\n", status);
  printf("Value: %d\n", WEXITSTATUS(status));

  return (0);
}

给定输入文件(有 4 个额外素数):

1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
101 103 107 109

它仍然可以正常工作 — 并产生如下输出:

I am the parent: 18797
1
2
3
5
7
11
13
17
19
23
I am the child with pid 18798
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
Sum of values: 960
Avg of values: 60 (0x3C)
I am the parent: 18797
Raw: 0x3C00
Value: 60

注意最好不要包含纯文本的截图;在问题中将其显示为文本即可。