C程序从FIFO读取数据

时间:2014-05-12 06:10:35

标签: c fifo

我有两个程序来写和读FIFO。一个是读(O_RDONLY)FIFO。另一种是将数据写入该FIFO。这是代码:

读取:可执行文件名为read。

#include<errno.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<string.h>

int main(int argc, char *argv[])
{
    int fd, nread;
    const char *pipe_file = "/tmp/test_fifo";

    char buf[0x100] ;

    fd = open(pipe_file, O_RDONLY);

    while(1) {
        printf("\n"); /*  */
        memset(buf, 0, 100);
        nread = read(fd, buf, 0x100);
        printf("data is %s", buf);
        sleep(1);
    }

    return 0;
}

写:可执行文件名为write。

#include<errno.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<string.h>

int main(int argc, char *argv[])
{
    int fd;
    int num = 1234;
    char *s = "changzhi";
    const char *pipe_file = "/tmp/test_fifo";


    fd = open(pipe_file, O_WRONLY);
    write(fd, s, strlen(s));

    return 0;
}

名为/tmp/test_fifo的FIFO已存在。当我运行read读取FIFO并运行write来写入FIFO时,一切正常。 但是read在读取代码中没有printf("\n");时无法读取数据。我不知道为什么。有人能帮助我吗?非常感谢!

2 个答案:

答案 0 :(得分:2)

第二个printf()被缓冲,直到\n被写入。读取未被阻止: - )

重写你的循环:

while(1) {
    memset(buf, 0, 100);
    nread = read(fd, buf, 0x100);
    printf("data is %s\n", buf);
    sleep(1);
}

答案 1 :(得分:0)

在严格的编译选项下干净地编译代码:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
>     -Wold-style-definition -Werror write.c -o write
$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
>     -Wold-style-definition -Werror read.c -o read
$

代码基本上适合我。我添加了错误检查。略微奇怪的argv[argc-argc]打印argv[0],但避免因未使用-Werror而发出警告(因argc而导致错误)。

read.c

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    int fd, nread;
    const char *pipe_file = "/tmp/test_fifo";

    char buf[0x100];

    if ((fd = open(pipe_file, O_RDONLY)) < 0)
    {
        fprintf(stderr, "%s: Failed to open FIFO %s\n", argv[argc-argc], pipe_file);
        return 1;
    }
    printf("%s: open successful\n", argv[0]);

    while(1) {
        printf("\n"); /*  */
        memset(buf, 0, 100);
        nread = read(fd, buf, 0x100-1);
        if (nread <= 0)
            break;
        printf("data is %s\n", buf);
        sleep(1);
    }

    return 0;
}

write.c

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int fd;
    char *s = "changzhi";
    const char *pipe_file = "/tmp/test_fifo";

    if ((fd = open(pipe_file, O_WRONLY)) < 0)
    {
        fprintf(stderr, "%s: failed to open FIFO %s\n", argv[argc-argc], pipe_file);
        return 1;
    }
    printf("%s: open successful\n", argv[0]);
    if (write(fd, s, strlen(s)) <= 0)
    {
        fprintf(stderr, "%s: failed to open FIFO %s\n", argv[argc-argc], pipe_file);
        return 1;
    }
    printf("%s: write successful\n", argv[0]);

    return 0;
}

示例输出

$ ./read & ./write
[1] 85542
./write: open successful
./write: write successful
./read: open successful

data is changzhi
$ 

[1]+  Done                    ./read
$ ./write & ./read
[1] 85544
./write: open successful
./read: open successful

./write: write successful
data is changzhi

[1]+  Done                    ./write
$

如果FIFO不存在,程序会报告错误。