【发布时间】:2016-01-12 14:48:17
【问题描述】:
我在尝试读取多个 fifo 时遇到了一个非常烦人的问题。我有 1 个进程等待来自 fifo 的结构,并且很少有进程向他发送这些结构的信号。第一次阅读后,我无法从任何信号中读取更多内容。看起来程序冻结了。
发送进程在 main 中有这个
myfifo = '/tmp/myfifo{0}' //{0} is a number that every process has individual.
mkfifo(myfifo, 0666);
fd = open(myfifo, O_WRONLY);
write(fd, &demon1 , sizeof(demon1));
close(fd);
while (1)
{
}
这在 signal_handler 中
void signal_handler(int signum)
{
if (signum == SIGUSR1)
{
//some declarations here
mkfifo(myfifo, 0666);
fd = open(myfifo, O_WRONLY | O_NONBLOCK);
write(fd, &demon1 , sizeof(demon1));
}
}
虽然阅读过程有
myfifo[i] = /tmp/myfifo{0} // {0} is i which is the number of process that sends.
while(1)
{
for(i=0;i<n;i++)
{
fd = open(myfifo[i], O_RDONLY | O_NONBLOCK);
r = read(fd, &demon1, sizeof(demon1));
if(r > 1)
{
//printf struct elements
}
}
}
【问题讨论】:
-
请发送一个最小的工作示例。例如,您是否在读取过程中打开后再次关闭每个fd?否则你很快就会用完文件描述符
-
这在很大程度上是其他东西正在打印和设置目录的所有内容。
-
我建议阅读
man 7 fifo的输出,特别是关于在非阻塞模式下打开FIFO的部分。我认为当 FIFO 的另一端未打开时,您的只写、非阻塞opens 将失败并显示 errnoENXIO。
标签: c linux named-pipes