C程序从FIFO读取数据
我有两个程序来写和读FIFO。一个是读(O_RDONLY)FIFO。另一种方法是将数据写入FIFO。这是代码: Read:可执行文件名为ReadC程序从FIFO读取数据,c,fifo,C,Fifo,我有两个程序来写和读FIFO。一个是读(O_RDONLY)FIFO。另一种方法是将数据写入FIFO。这是代码: Read:可执行文件名为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
#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;
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
int fd,nread;
const char*pipe_file=“/tmp/test_fifo”;
字符buf[0x100];
fd=打开(仅管道文件);
而(1){
printf(“\n”);/**/
memset(buf,01100);
nread=读取(fd,buf,0x100);
printf(“数据是%s”,buf);
睡眠(1);
}
返回0;
}
Write:可执行文件名为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;
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
int-fd;
int num=1234;
char*s=“长治”;
const char*pipe_file=“/tmp/test_fifo”;
fd=打开(管道文件,仅限O_文件);
写入(fd、s、strlen);
返回0;
}
名为/tmp/test\u FIFO
的FIFO已存在。当我运行read
读取FIFO并运行write
写入FIFO时,一切正常但是,读取
在没有printf(“\n”)时无法读取数据代码>在读取代码中。我不知道为什么。有人能帮我吗?非常感谢 第二个printf()
被缓冲,直到写入\n
。读取未被阻止:-)
重写循环:
while(1) {
memset(buf, 0, 100);
nread = read(fd, buf, 0x100);
printf("data is %s\n", buf);
sleep(1);
}
使用严格编译选项下的干净编译代码:
$ 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]
,但避免了关于argc
未使用的警告(错误原因是-Werror
)
read.c
样本输出
如果FIFO不存在,程序会敏感地报告错误。请注意,系统提供了名为read
和write
的命令。你确定你没有意外地使用其中一个吗?(例如,如果使用/read
和/write
,您就可以了。)在代码中,您不检查打开是否成功;您没有报告您的打开成功;您没有检查读或写操作是否成功;写入成功时不报告。您需要并行运行程序:/read&/write
或/write&/read
。如果数据中没有换行符,则调用printf()
,循环返回顶部,然后在睡眠(1)
引起的1秒延迟后执行printf(“\n”)
。但是你是对的,这样做是…一种常规,我们可以说吗?你能解释一下缓冲区和阻塞区的细节吗?谢谢!输出默认为行缓冲;printf()
的输出只有在有换行符时才会显示,然后显示换行符之前的数据。换行符之后的任何内容都将等待直到出现换行符。因此,第二个printf()
,不会强制输出数据。但是,经过1秒钟的睡眠后,循环返回顶部,并且printf(“\n”)代码>强制输出“数据为…”
”材质。在原始代码中,循环是无限的,直到您中断程序,因为您没有测试EOF或读取错误。对于“阻塞”,直到FIFO中有一些数据要返回,或者直到FIFO打开时没有写入程序,read()
才会返回EOF(读取0字节)。您的代码忽略此返回代码,因此循环将无限期地继续。open();在每个程序中,只有在FIFO打开的情况下有另一个(另一个)进程时,才会完成。谢谢您的回答。但是我仍然有一些关于代码的问题。当我注释printf(“\n”)代码>在读取程序中。缓冲区是否有问题导致读取操作被阻止?您可以看到@cadrian answer。当我删除printf(“\n”)代码>行从read.c
,程序运行良好-只需少打印一行新行(这可以说是一种改进)。您是否逐字复制了代码,或者您是否尝试将我的更改改装到您的代码中?你在哪个站台工作?我正在使用GCC4.9.0在Mac OS X 10.9.2 Mavericks上进行测试,尽管我希望代码能够在当前千年的任何POSIX ish平台上使用任何C编译器都能正常工作(当然,您需要调整非GCC编译器的编译标志,或者真正的GCC旧版本)。我正在CentOS 6.4上进行测试,gcc版本是4.4.7 20120313(Red Hat 4.4.7-4)(gcc)。我当然不希望CentOS 6.4和gcc 4.4.7会有任何问题。你把我的答案一字不差地抄下来了吗?你用我展示的选项编译了吗?我用GCC4.8.2检查了Ubuntu14.04上编译的代码,无论是否使用printf(“\n”)代码>行。
#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;
}
#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
$