c - `write()` 和 `read()` 无法正常工作

标签 c pipe

我基本上必须做的:我有一个包含 10 个随机整数的数组,我需要有两个 child ;一个将计算偶数指数的乘积,另一个将处理赔率。完成后,父进程会将这两个值相乘并打印出来。

这是我的代码:

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

int main()
{
  int ans[2];
  int array[10];                                //main array
  int i;
  srand (time(NULL));                           //for not the same numbers

  for(i=0;i<10;i++)
  {                                             //array filling
    array[i]= ( rand() % 10 + 1);               //from 1 to 10 random numbers
    printf("Array: %d \n",array[i]);
  }
  int children = 2;                             //number of child processes
  int *product=0;                                // var to store the product sums

  int fd[2];
  pipe(fd);

  int arraymax[2];                              //the two products

  for(i=0;i<children;i++)
  {                                             //for 10 children
                                                //child processes
    if(fork() == 0)
    {
      printf("checkpoint 1\n");
        close(fd[0]);                                //close reading

        for(int j=i;j<sizeof(array)-1;j+=2)
        {
            product+= (array[j]*array[j+2]);
        }
        write(fd[1], &product, sizeof(*product));     //write the max number into the pipe
        printf("the product is: %d\n", *product);
        close(fd[1]);                                //close write
        wait(NULL);
        exit(0);
    }                                               //parent process
    else
    {
      printf("checkpoint 2\n");
        wait(NULL);
        close(fd[1]);                                //close write
        /* read pipe */
        for(int j=0; j<2; j++)
        {
          read(fd[0], &ans , sizeof(*product));
        }
        close(fd[0]);                                //close read
    }
  }
  printf("Total answer is: %d \n", (ans[0]*ans[1]));
  return 0;
}

输出结果如下:

Array: 10 
Array: 7 
Array: 9 
Array: 5 
Array: 4 
Array: 4 
Array: 8 
Array: 5 
Array: 7 
Array: 1 
checkpoint 2
checkpoint 1
checkpoint 2
checkpoint 1
Total answer is: -432399864 

答案不应该接近它给我的那个数字。任何帮助表示赞赏:)

最佳答案

希望对您有所帮助。 (这里不涉及奇数部分和偶数部分,希望你能搞清楚)

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

int main()
{
  int ans;
  int total = 1;
  int array[10];                                //main array
  int i;
  srand (time(NULL));                           //for not the same numbers

  for(i=0;i<10;i++)
  {                                             //array filling
    array[i]= ( rand() % 10 + 1);               //from 1 to 10 random numbers
    printf("Array: %d \n",array[i]);
  }

  int children = 2;                             //number of child processes
  int product=0;                                // var to store the product sums

  int fd[2];
  pipe(fd);

  int arraymax[2];                              //the two products


  for(i=0;i<children;i++)
  {                                             //for 10 children
                                                //child processes
    if(fork() == 0)
    {
      printf("checkpoint 1\n");
        close(fd[0]);                                //close reading

        for(int j=i;j<sizeof(array)/sizeof(int)-2;j+=2)
        {
            product+= (array[j]*array[j+2]);
        }

    printf("the product: %d\n", product);
        write(fd[1], &product, sizeof(product));     //write the max number into the pipe
        //printf("the product is: %d\n", product);
        close(fd[1]);                                //close write
        wait(NULL);
        exit(0);
    }                                               //parent process
    else
    {
      printf("checkpoint 2\n");
        wait(NULL);
        close(fd[1]);                                //close write
        /* read pipe */
        for(int j=0; j<2; j++)
        {
          read(fd[0], &ans , sizeof(product));
        }
    total *= ans;
        close(fd[0]);                                //close read
    }
  }
  printf("Total answer is: %d \n", (total));
  return 0;
}

您的指针有一些错误,我已经更正了。为了得到两个 child 的结果,我使用了一个单独的变量(总计)。

这里的循环越界了

for(int j=i;j<sizeof(array)-1;j+=2)
    {
        product+= (array[j]*array[j+2]);
    }

应该是这样的

for(int j=i;j<sizeof(array)/sizeof(int)-2;j+=2)
    {
        product+= (array[j]*array[j+2]);
    }

如果您有任何问题,请随时提出。

关于c - `write()` 和 `read()` 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46575684/

相关文章:

c - 执行 msp430x 指令但 mcu 不是 msp430x

c - 如何创建指向列表中每个成员的指针

C 版本的 strpos 和 substr?

node.js - .pipe() 是否在 node.js 中执行 memcpy?

c - 如何查找 read() 返回的错误?

c - 为什么 sem_open 在没有共享内存的情况下与 fork() 一起工作?

c - 如何将图像转换为 WORD (uint16) 数组?

bash - POSIX 兼容的 shell 相当于 Bash "while read -d $'\0' ..."?

bash - 终止 shell 脚本而不等待管道的早期部分

node.js - 未知方法 process.openStdin()