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

时间:2015-10-30 15:27:43

标签: c pipe fork ipc

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

写入管道工作,但从管道读取,我得到什么"没有"。下面是我的代码和输出。为简单起见,我只从管道中读出第一个字节(单个字母)。

#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 个答案:

答案 0 :(得分:2)

此处的问题,在WriteData

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

alphabchar *,您传递的是该变量的地址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