每次将数组的每一行传递给 child

Pass every time each row of array to child

我正在从用户那里读取 arr 的大小。我创建了 arr[row][col] 并用随机整数填充它。然后我想将 parent 进程中的每一行发送到 child,所以我创建了一个新的 arr2 来存储其中的当前行。我读取了 arr2 到 child 的过程并计算了行的总和。最后我想将总和发送到 parent 进程并打印出来。我在 for(i=0;i

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

int main(){

int gram, steiles;

printf("Dwse mou tis grammes tou pinaka\n");
scanf("%d", &gram);
printf("Dwse mou tis steiles tou pinaka\n");
scanf("%d", &steiles);

int arr[gram][steiles];
int arr2[gram];

srand(time(NULL));

for (int i = 0; i < gram; i++) {
  for (int j = 0; j < steiles; j++) {
    arr[i][j] = rand() % 20 + 1;
  }
}

for (int i = 0; i < gram; i++) {
  for (int j = 0; j < steiles; j++) {
    printf("%d", arr[i][j]);
    printf("\t");
  }
  printf("\n");
}
pid_t pid;
int fd[2], fd1[2], sum=0;

if (pipe(fd) == -1) {
  return 2;
}
if (pipe(fd1) == -1) {
  return 3;
}

    for (int i = 0; i < gram; i++) {
      pid = fork();
    
      if (pid<0) {
        return 1;
      }
    
      if (pid>0){   
        for (int j = 0; j < steiles; j++) {
          arr2[j]=arr[i][j];
        }
    
        close(fd[0]);
        write(fd[1], arr2, sizeof(int)*steiles);
        close(fd[1]);
    
        close(fd1[1]);
        read(fd1[0], &sum, sizeof(int));
        close(fd1[0]);
        printf("the sum of the row is: %d", sum);
    
    
      }else{ 
        wait(NULL);
        close(fd[1]);
        read(fd[0], arr2, sizeof(int)* steiles);
        close(fd[0]);
        for (int k = 0; k < steiles; k++) {
          // printf("%d\t", arr2[k]);
          sum+=arr2[k];
        }
        close(fd1[0]);
        write(fd1[1], &sum, sizeof(int));
        close(fd1[1]);
      }
}
}

我已经在上面的评论中概述了很多问题,包括:

  • 您不会错误检查您的 I/O 调用,但应该。
  • parent 进程关闭第一个 child 的管道;第二个和随后的 children 没有机会,可怜的东西。他们被剥夺了与 parent 的所有交流,你知道 children 从与他们的 parent 交谈中获益多少。
  • 在循环内部而不是外部创建管道。请注意,检查 I/O 调用的错误会告诉您这个问题。
  • 不要忘记用换行符结束消息。
  • 此外,请确保您的 child 进程在完成任务后退出。就目前而言,第一个 child 读取其数据,然后返回到循环 运行 一个新的 child (以及 parent 运行宁一个新的 child).

这里有一些代码仍然跳过 I/O 错误检查但工作正常:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

static void dump_vector(const char *tag, size_t size, const int *data);

int main(void)
{
    int gram, steiles;

    printf("Dwse mou tis grammes tou pinaka\n");
    scanf("%d", &gram);
    printf("Dwse mou tis steiles tou pinaka\n");
    scanf("%d", &steiles);

    int arr[gram][steiles];
    int arr2[gram];

    srand(time(NULL));

    for (int i = 0; i < gram; i++)
    {
        for (int j = 0; j < steiles; j++)
        {
            arr[i][j] = rand() % 20 + 1;
        }
    }

    for (int i = 0; i < gram; i++)
    {
        for (int j = 0; j < steiles; j++)
        {
            printf("%d", arr[i][j]);
            printf("\t");
        }
        printf("\n");
    }

    for (int i = 0; i < gram; i++)
    {
        pid_t pid;
        int fd1[2], fd2[2];

        if (pipe(fd1) == -1)
        {
            return 2;
        }
        if (pipe(fd2) == -1)
        {
            return 3;
        }
        pid = fork();

        if (pid < 0)
        {
            return 1;
        }

        if (pid > 0)
        {
            for (int j = 0; j < steiles; j++)
            {
                arr2[j] = arr[i][j];
            }

            dump_vector("arr2 - parent", steiles, arr2);

            close(fd1[0]);
            write(fd1[1], arr2, sizeof(int) * steiles);
            close(fd1[1]);

            int sum = 0;
            close(fd2[1]);
            read(fd2[0], &sum, sizeof(int));
            close(fd2[0]);
            printf("the sum of the row is: %d\n", sum);
        }
        else
        {
            close(fd1[1]);
            read(fd1[0], arr2, sizeof(int) * steiles);
            close(fd1[0]);
            dump_vector("arr2 - child", steiles, arr2);
            int sum = 0;
            for (int k = 0; k < steiles; k++)
            {
                // printf("%d\t", arr2[k]);
                sum += arr2[k];
            }
            printf("Child %d (%d): sum = %d\n", i, getpid(), sum);
            close(fd2[0]);
            write(fd2[1], &sum, sizeof(int));
            close(fd2[1]);
            exit(0);
        }
    }
    return 0;
}

static void dump_vector(const char *tag, size_t size, const int *data)
{
    int length = printf("%s (%zu):", tag, size);
    for (size_t i = 0; i < size; i++)
    {
        length += printf(" %2d", data[i]);
        if (length > 60)
        {
            length = 0;
            putchar('\n');
        }
    }
    if (length > 0)
        putchar('\n');
}

我发现像 dump_vector() 这样的函数非常有用;我为许多数据结构编写它们。有时我也接受 FILE *fp 参数,但标签很重要。对于数组,大小和数据是必需的;对于单个结构,当然只是结构。

示例输出

Dwse mou tis grammes tou pinaka
4
Dwse mou tis steiles tou pinaka
10
12  12  4   3   8   12  6   4   2   10  
10  13  14  13  13  1   7   12  4   8   
7   12  19  12  14  18  20  13  8   8   
5   14  7   11  19  16  4   12  14  18  
arr2 - parent (10): 12 12  4  3  8 12  6  4  2 10
arr2 - child (10): 12 12  4  3  8 12  6  4  2 10
Child 0 (15771): sum = 73
the sum of the row is: 73
arr2 - parent (10): 10 13 14 13 13  1  7 12  4  8
arr2 - child (10): 10 13 14 13 13  1  7 12  4  8
Child 1 (15772): sum = 95
the sum of the row is: 95
arr2 - parent (10):  7 12 19 12 14 18 20 13  8  8
arr2 - child (10):  7 12 19 12 14 18 20 13  8  8
Child 2 (15773): sum = 131
the sum of the row is: 131
arr2 - parent (10):  5 14  7 11 19 16  4 12 14 18
arr2 - child (10):  5 14  7 11 19 16  4 12 14 18
Child 3 (15774): sum = 120
the sum of the row is: 120