问题
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.
follow my code:
#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;
}
回答1:
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.
How child and parent are communicating through pipe :
There are some important things about pipe() :
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)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)only related process (like child & parent ) can communicate through 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 : 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));
I hope it helps to understand pipe() working.
来源:https://stackoverflow.com/questions/47371735/using-a-pipe-to-write-and-read-array-of-int