使用管道写入和读取Int数组 [英] Using a Pipe to Write and Read Array of Int

查看:477
本文介绍了使用管道写入和读取Int数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用管道制作一个简单的程序.程序必须在父级中请求一个整数数组,然后将其发送给子级,该子级必须对数组进行排序并将其发送回父级. 问题是在通过管道发送后,我不确定如何在子级中读取数组值.

I'm trying to make a simple program using the pipe. The program must request an array of integers in the parent, and send it to the child, which must sort the array and send it back to the parent. The problem is that I'm not sure how I read the array values in the child, after sending through the pipe.

遵循我的代码:

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

int main() {

int fd[2];
pid_t pid;

/* Verifica se ocorreu erro (-1) ao criar o PIPE */
if(pipe(fd) == -1){
    perror("pipe");
    exit(1);
}

/* Verifica se o fork foi realizado com sucesso > 0 */
if((pid = fork()) < 0){
    perror("fork");
    exit(1);
}

if(pid > 0){

    printf("-----------------------------------------------\n");
    printf("                PROCESSO PAI                   \n");
    printf("-----------------------------------------------\n");

    /* Como vamos ESCREVER do lado do PAI, fechamos a LEITURA */
    close(fd[0]);

    int numeros[5];

    for (int i = 0; i < 5; i++){
        printf("\nDigite o numero [%d]: ", i);
        scanf("%d", &numeros[i]);
    }

    /* Escrevendo o array no PIPE */
    write(fd[1], numeros, sizeof(numeros) + 1);
    printf("\nEnviando numeros para meu Filho...\n");
    exit(0);

}else{

    int numeros_recebidos[5];

    /* Como vamos LER do lado do FILHO, fechamos a ESCRITA*/
    close(fd[1]);

    /* Lendo a mensagem que foi enviada pelo FILHO */
    read(fd[0], numeros_recebidos, sizeof(numeros_recebidos));
    printf("\n-----------------------------------------------\n");
    printf("                PROCESSO FILHO                 \n");
    printf("-----------------------------------------------\n");

    printf("\nNumeros Recebidos, Ordenando...\n");


    for(int i = 0; i<5; i++){
        printf("%d \n", &numeros_recebidos[i]);
    }
}

return 0;
}

推荐答案

您的程序运行正常,除了少数错误,例如

your program is working fine except few bugs like

 for(int i = 0; i<5; i++){
        printf("%d \n", &numeros_recebidos[i]); // why &, remove it bcz you are printing data not scanning.

孩子和父母如何通过管道进行通信:

关于pipe()有一些重要的事情:

There are some important things about pipe() :

1)pipe()系统调用将创建两个名为fd[0]fd1[1]的端点.一个保留用于reading用途,另一个保留用于writing用途.

1)pipe() system call will create two ends called fd[0] and fd1[1]. one is reserved for reading purpose and another is reserved for writing purpose.

2)pipeuni-directional IPC,即一次您只能从/向管道的一端进行读取或写入,而这两者不可能同时进行.

2)pipe is a uni-directional IPC i.e at a time you can only read or write from/to one end of pipe, both not possible simultaneously.

3)只有与相关的过程(例如子级和父级)可以通过pipe()进行通信.

3)only related process (like child & parent ) can communicate through pipe().

父进程:父进程正在将整数数组写入fd[1].一旦完成,现在数据将写入pipe()的末尾.接下来的事情是从这里读取数据的子作业,让我们跳到子进程.

parent process : parent process is writing array of integers into fd[1]. once it'd done now data is there in writing end of pipe(). Next thing is child job's to read data from here so let's jump to child process.

                            pipe(uni-directional)
                       |---------------------------| 
parent is writing----->|                           |<---------
                       |---------------------------| 
                     fd[1]                        fd[0]

        write(fd[1], numeros, sizeof(numeros) + 1);

child process:父级将int数组放入fd [1]中,现在子级必须从fd [0]中读取并打印.

child process : parent has putted int array into fd[1] now child has to read from fd[0] and prints it.

          pipe(uni-directional)
      |---------------------------| 
----->|                           |<-----child process is reading data & storing in numeros_recebidos
      |---------------------------| 
     fd[1]                       fd[0]

  read(fd[0], numeros_recebidos, sizeof(numeros_recebidos));

我希望它有助于理解pipe()的工作.

I hope it helps to understand pipe() working.

这篇关于使用管道写入和读取Int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆