简体   繁体   English

unistd.h read()正在读取更多数据然后被写入

[英]unistd.h read() is reading more data then being written

I'm reading/writing data off of a named pipe. 我正在从命名管道读取/写入数据。 On the writing side it says that it's writing a constant 110 bytes. 在写方面,它表示正在写一个恒定的110字节。 On the Reading side for the majority of time it says that it's reading 110 bytes which is correct, but other times it says its reading 220 bytes or 330 bytes. 在大多数情况下,Reading表示读取的是110个字节,而其他时候表示读取的是220个字节或330个字节。 Which is right in the fact that when I print it out it's printing out the same message two or three times in a row within the same read(). 这是正确的,因为当我将其打印出来时,它会在同一read()中连续两次或两次打印出相同的消息。 In the code below for reading am I doing something wrong with the memset to clear the char? 在下面的阅读代码中,我是否使用memset清除char了吗? I can't think of any other way it's reading more then is being written unless something is left over in the buffer. 我想不出任何其他方式,除非缓冲区中遗留了一些东西,否则它正在被读取。

int fd1, numread;
char bufpipe[5000];

    while(1)
    {
        fd1 = open("/tmp/testPipe", O_RDONLY);
        numread = read(fd1,bufpipe, 5000);//->this should always be 110
        if(numread > 1)
        {
            printf("READ: %i", numread); 
            bufpipe[numread+1] = '\0';
            memset(bufpipe,'\0',5001);
            close(fd1);
        }
    }

This: 这个:

memset(bufpipe,'\0',5001);

is overwriting by one byte, because you have only 5000 bytes. 被一个字节覆盖,因为您只有5000个字节。

But the main "problem" is that read(..., 5000) will always read as much as it can up to 5000 bytes - you seem to be assuming that it will read only as much as was written in one go by the writer, which is not true. 但是主要的“问题”是read(..., 5000)始终会读取多达5000个字节的内容-您似乎假设它只能读取与编写者一次写入的内容一样多的内容。 ,这是不正确的。 If the writer writes two packets of 110 bytes between two reads, then it's quite correct that the reader reads 220 bytes. 如果写入器在两次读取之间写入两个110字节的数据包,那么读取器读取220字节是完全正确的。

If you need to read only a single packet at a time, you have to make your packets self-describing. 如果您一次只需要读取一个数据包,则必须使数据包自描述。 So for instance, the first four bytes contain the number of bytes to follow. 因此,例如,前四个字节包含要跟随的字节数。 Then you can read a single packet by reading four bytes, converting that to an integer, then reading that number of data bytes. 然后,您可以通过读取四个字节,将其转换为整数,然后读取该数量的数据字节来读取单个数据包。

Your assumption that a read will execute immediately after a write is not true. 您认为write后将立即执行read假设是不正确的。 The writer process might write to the pipe a couple of times before a read is encountered. 在遇到读取之前,编写器进程可能会多次写入管道。 Written data will be added to the end of buffer. 写入的数据将被添加到缓冲区的末尾。 Put it differently, read and write are not packet oriented. 换句话说,读取和写入不是面向数据包的。 They are stream oriented. 它们是面向流的。 It means that write just adds data to the buffer and read just gets anything available to it. 这意味着write只会将数据添加到缓冲区,而read只会获取可用的任何内容。

How are you synchronizing with the writer? 您如何与作者同步? You need to just read what your expecting (specify 110 to read()) 您只需要阅读您的期望(将110指定为read()即可)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM