从管道读取时返回的数据不正确

Posted

技术标签:

【中文标题】从管道读取时返回的数据不正确【英文标题】:Incorrect data returned when reading from pipe 【发布时间】:2015-10-30 15:27:43 【问题描述】:

我正在使用管道将数据从发送方进程发送到接收方进程。在花了一些时间试图解决这个问题之后,仍然无法弄清楚。

写入管道有效,但从管道读取时,我得到“无”。下面是我的代码和输出。为简单起见,我只从管道中读出第一个字节(单个字母)。

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


void GenerateData();
void WriteData();
void ReadData();

int fildes[2];
char* buff; 
char*  alphab;
char* alphabt;              
int i, n;
int pid_rcv = -1;



main(int argc, char** argv)

    buff = (char*) malloc(26);

    pipe(fildes);               //pipe with two file descriptors (for write and read)

    printf("---IPC---\n");
    printf("Pipe_in descrp: %i\n", fildes[1]);
    printf("Pipe_out descrp: %i\n", fildes[0]);

    //Generate alphabets A - Z
    GenerateData();


    //Fork child (receiver). Parent will be sender
    pid_rcv = fork();

    if( pid_rcv < 0 )
    
        /* check for error while forking */
        fprintf(stderr, "Fork failed.\n");
        exit(-1);
    
    else if (pid_rcv == 0)
    
        /* this is the receiver process */
        printf("Receiver's PID: %i\n", getpid());

        close(fildes[1]);                               //close write end of pipe

        ReadData();                                     //read then print alphabet from pipe

        exit(0);                                        //exit receiver
    
    else 
    
        /* this is the sender process */
        printf("Sender's PID: %i\n", getpid());

        close(fildes[0]);                               //close read end of pipe

        WriteData();                                    //write alphabets to pipe at 1 sec intervals
        //close(fildes[1]);                                 //close write pipe


        wait(NULL);                                 //wait for receiver process to finish   
    


    //terminate
    free(buff);
    printf("End of IPC program.\n");
    return 0;



void GenerateData()

    //ASCII letters from A to Z. Buffer size of 26
    n = 65;
    for(i=0; i<26; i++)
    
        buff[i] = (char) n;
        n++;
    

    //display generated data
    printf("Buffer: ");
    for(i=0; i<26; i++)
    
        printf("%c", (char) buff[i]);
    
    printf("\n");
 

void WriteData()

    printf("Writing data to pipe...\n");    
    alphab = (char*) malloc(1); 

    for(i=25; i>=0; i--)                                    //reverse order
    
        alphab[0] = (char) buff[i];

        write(fildes[1], &alphab, sizeof(char));            //write an alphabet to pipe
        printf("%c", alphab[0]);
        sleep(1);                       
    
    printf("\nDone writing data to pipe.\n");

    free(alphab);


void ReadData()

    int numbBytes;
    printf("Attempting to read data from pipe...\n");
    alphabt = (char*) malloc(sizeof(char)); 

    numbBytes= read(fildes[0], alphabt, sizeof(char));

    printf("Number of bytes read=%d\n", numbBytes);
    printf("Read data: %c\n", (char) *alphabt);

    free(alphabt);  


---IPC---
Pipe_in descrp: 4
Pipe_out descrp: 3
Buffer: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Sender's PID: 5195
Writing data to pipe.
ZYXWVUTSRQPONMLKJIHGFEDCBA
Done writing data to pipe. 
Receiver's PID: 4842
Attempting to read data from pipe...
Number of bytes read=1
Read data: 0
End of IPC program.

【问题讨论】:

【参考方案1】:

这里的问题,在WriteData:

write(fildes[1], &alphab, sizeof(char));

alphab 是一个char *,而您传入​​的是该变量的地址,一个char **。只需传入alphab,就会写入你想要的字符。

write(fildes[1], alphab, sizeof(char));

输出:

---IPC---
Pipe_in descrp: 4
Pipe_out descrp: 3
Buffer: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Receiver's PID: 21523
Attempting to read data from pipe...
Sender's PID: 21522
Writing data to pipe...
Number of bytes read=1
Read data: Z

【讨论】:

谢谢!我犯了一个非常愚蠢的错误。再次感谢。现在的输出:ZYXWVUTSRQPONMLKJIHGFEDCBA

以上是关于从管道读取时返回的数据不正确的主要内容,如果未能解决你的问题,请参考以下文章

Linux-C:从管道读取返回写入它的第一个缓冲区

从命名管道、C 程序(编写器)和 Python(读取器)获取额外数据

使用异步管道从可观察对象中读取对象字段

Spark:Dataframe 管道分隔不返回正确的值

匿名管道的 ReadFile 函数

使用另一个命令通过管道传输时如何读取命令的返回码[重复]