[英]LINUX FIFO named pipe (IPC) stops writing/reading message at specific file descriptor
I'm communicating between two non-related process (not folked from the same parent) using Named Pipe (fifo mode).我正在使用命名管道(fifo 模式)在两个不相关的进程(不是来自同一个父进程)之间进行通信。 The program works.
该程序有效。 However, the pile file seem to be limited
不过堆文件好像有限制
The OUTPUT of the writer:作者的输出:
Wrote to file. fd = 3
Wrote to file. fd = 3
...... other output ...
Wrote to file. fd = 3
Wrote to file. fd = 3
The OUTPUT of the reader is:读取器的输出为:
fd: 3. Received buffer: hello
fd: 4. Received buffer: hello
...... other output ...
fd: 1021. Received buffer: hello
fd: 1022. Received buffer: hello
fd: 1023. Received buffer: hello
MY PROBLEM: At fd
1023, the reader seems to STOP READING from file (there is no fd
1024, 1025, etc.).我的问题:在
fd
1023 处,读者似乎停止从文件中读取(没有fd
1024、1025 等)。 When it happens, the writer STOP WRITING (log "Wrote to file. fd = 3" stops display)当它发生时,writer STOP WRITING(日志“Wrote to file.fd = 3”停止显示)
I try different msg
value ("hi", "where are you" ...), but the writer always stop at fd
1023我尝试了不同的
msg
值(“嗨”、“你在哪里”...),但作者总是停在fd
1023
I really don't understand why.我真的不明白为什么。 Could you explain for me?
你能帮我解释一下吗? Thanks you so much!
非常感谢!
Here is my code: The writer:这是我的代码: 作者:
#include <stdio.h>
#include <stdlib.h> /* exit functions */
#include <unistd.h> /* read, write, pipe, _exit */
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
char msg[] = "hello";
/* create the FIFO (named pipe) */
mkfifo(myfifo, 0666);
fd = open(myfifo, O_WRONLY);
if (fd < 0) {
return;
}
while(1) {
write(fd, msg, sizeof(msg));
printf("Wrote to file. fd = %d\n", fd);
sleep(1);
}
close(fd);
/* remove the FIFO */
unlink(myfifo);
return 0;
}
The reader:读者:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 500
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
while(1) {
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
if (fd != -1) {
if (read(fd, buf, sizeof(buf)) > 0) {
printf("fd: %d. Received buffer: %s\n", fd, buf);
}
}
sleep(0.2);
}
close(fd);
return 0;
}
I really don't understand why.
我真的不明白为什么。 Could you explain for me?
你能帮我解释一下吗?
There's a limit on the number of open file descriptors on your system.系统上打开的文件描述符的数量是有限制的。 The limit can be checked in shell with
ulimit -n
.可以使用
ulimit -n
在 shell 中检查限制。 It is configurable.它是可配置的。 See
help ulimit
and man limits.conf
.请参阅
help ulimit
和man limits.conf
。
On your system the maximum number of open file descriptors seems to be at 1024 (on mine too!) Opening more will return -1
with errno=EMFILE
.在您的系统上,打开的文件描述符的最大数量似乎是 1024(在我的也是如此!)打开更多将返回
-1
和errno=EMFILE
。
Your reader
program opens new file descriptors in a loop.您的
reader
程序会在循环中打开新的文件描述符。 After reaching the limit of open file descriptors, open
starts retuning -1
, which because of if (fd != -1) {
makes the program stop outputting anything.达到打开文件描述符的限制后,
open
开始重新调整-1
,因为if (fd != -1) {
使程序停止输出任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.