简体   繁体   English

我如何 PIPE 文件输入或 output 缓冲区刷新?

[英]How can i PIPE file input or output buffer flush?

today my code is had problem to input & output buffer flush problem.今天我的代码输入有问题 & output 缓冲区刷新问题。

this problem is这个问题是

first Process(#1) 's code....第一个 Process(#1) 的代码....

int sign_sender(char* command)
{
    int f , len;

    f=open(PIPE_NAME,O_RDWR);

    if(f<0)
    {
        printf("open fail : %s\n",strerror(errno));
        exit(1);
    }

    while(1)
    {
        len=strlen(command);
        if(len<=0)
        {
            continue;
        }
        write(f,command,len);
        printf("write...\n");
        sleep(1);
    }
    return 1;
}

this is write string non blocking...这是写字符串非阻塞...

and.和。

Second process(#2) is...第二个过程(#2)是......

int sign_receiver()
{
    int f , len;
    char temp_buffer[128]={0};

    f=open(PIPE_NAME,O_RDWR|O_NONBLOCK);
    if(f<0)
    {
        printf("open fail \n");
        exit(1);
    }

    while(1)
    {
        if((len=read(f,temp_buffer,sizeof(temp_buffer)))<0)
        {
            printf("read error. len = %d\n",len);
        }
        printf("read...\n");
        if(len>0)
        {
            printf("len : %d , sign process recv : [%s]\n",len,temp_buffer);
        }
        sleep(1);
    }

    close(f);
    unlink(PIPE_NAME);

    return 1;
} 

like this.像这样。 process(#1) is first run.进程(#1)首先运行。 and 5 sec after process(#2) is run..和进程(#2)运行后5秒..

process(#2) 's printf....进程(#2)的 printf ....

read...
len : 15 , sign process recv : [hellohellohello]
read...
len : 5 , sign process recv : [hellohellohello]
read...
len : 5 , sign process recv : [hellohellohello]
read...
len : 5 , sign process recv : [hellohellohello]
read...
len : 5 , sign process recv : [hellohellohello]
read...
len : 5 , sign process recv : [hellohellohello]

i want this printf.我想要这个 printf。

read...
len : 5 , sign process recv : [hello]
read...
len : 5 , sign process recv : [hello]
read...
len : 5 , sign process recv : [hello]
read...
len : 5 , sign process recv : [hello]

i dont know why.... i'm so noob of C...我不知道为什么....我对 C 很陌生...

The reason why [hellohellohello] keeps getting written after the first read, is because you do not clear your buffer in-between each read operation. [hellohellohello]在第一次读取后一直被写入的原因是因为您没有在每次读取操作之间清除缓冲区。 So what you actually see on the next lines where it reads:因此,您在接下来的几行中实际看到的是:

len: 5, sign process recv: [hellohellohello] len: 5, 签名过程recv: [hellohellohello]

Is the message hello that was just read, and that overrided the 5 first letters contained in your buffer's previous state, followed by what is remaining in the buffer, hence the repetition of the message.是刚刚读取的消息hello ,并且覆盖了缓冲区先前 state 中包含的 5 个首字母,然后是缓冲区中剩余的内容,因此重复了消息。

Here is an example of how you can clear the buffer:以下是如何清除缓冲区的示例:

int sign_receiver()
{
    int f , len;
    char temp_buffer[128]={0};

    f=open(PIPE_NAME,O_RDWR|O_NONBLOCK);
    if(f<0)
    {
        printf("open fail \n");
        exit(1);
    }

    while(1)
    {
        if((len=read(f,temp_buffer,sizeof(temp_buffer)))<0)
        {
            printf("read error. len = %d\n",len);
        }
        printf("read...\n");
        if(len>0)
        {
            printf("len : %d , sign process recv : [%s]\n",len,temp_buffer);
            memset(temp_buffer, 0, 128); <- Adding this line
        }
        sleep(1);
    }

    close(f);
    unlink(PIPE_NAME);

    return 1;
}

But that won't get you rid of the first [hellohellohello] , and that is because you are writing on the named pipe several times before reading it (3 times in your case).但这不会让你摆脱第一个[hellohellohello] ,那是因为你在阅读之前在命名 pipe 上写了几次(在你的情况下是 3 次)。 These messages are buffered in a system buffer, until it is retrieved by the read calls.这些消息被缓冲在系统缓冲区中,直到被读取调用检索。

Your Non-Blocking read doesn't mean that it will discard the piled up data accumulated in the system buffer, it just means that its call will return immediately, even if no data has been read.您的非阻塞读取并不意味着它会丢弃积累在系统缓冲区中的堆积数据,它只是意味着它的调用将立即返回,即使没有读取任何数据。

I'd suggest inverting the order in which you call the two processes, running executable 2 before executable 1, if you don't want messages to add up on the first read.如果您不想在第一次读取时添加消息,我建议您颠倒调用这两个进程的顺序,在可执行文件 1 之前运行可执行文件 2。

You could also add an extra definition in your messages, with a header/footer, or a delimiter that would let you parse messages inside the payload.您还可以在消息中添加额外的定义,使用页眉/页脚或分隔符,让您在有效负载中解析消息。

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

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