繁体   English   中英

C无法从FIFO(命名管道)读取

[英]C can't read from fifo (named pipe)

我创建了一个fifo,但无法读取。 有什么问题? 这是代码和输出。 如果我在没有O_NONBLOCK程序的情况下使用O_RDONLY,请稍候。

pid_t p;
int fd;
char str[]="sample";

mkfifo("myfifo", S_IRUSR | S_IWUSR);

fd = open("myfifo", O_RDWR);

printf("write %d byte\n", write(fd, str, 5));

close(fd);

fd = open("myfifo", O_RDONLY | O_NONBLOCK);

printf("read %d byte\n",read(fd, str, 6));

close(fd);

unlink("myfifo");

输出:

write 5 byte
read 0 byte
  1. 问题是您关闭了文件,

您可以尝试此操作或派生另一个进程并读取文件,这主要是为什么将名为fifos用于进程间通信的原因

该链接详细说明了如何使用管道在两个程序之间发送简单字符串?

pid_t p;
int fd;
char str[]="sample";

mkfifo("myfifo", S_IRUSR | S_IWUSR);

fd = open("myfifo", O_RDWR);

printf("write %d byte\n", write(fd, str, 5));

printf("read %d byte\n",read(fd, str, 6));

close(fd);

unlink("myfifo"); 

暂无
暂无

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

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