尝试从多个FIFO中读取时,我遇到了一个非常恼人的问题。我有1个进程等待来自fifo的结构,很少有进程将这些结构发送给信号。在第一次阅读后,我无法从任何信号中读出更多。它看起来像程序冻结。无法从多个FIFO中读取
发送进程具有这主要与
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
}
}
}
请发送一个简单的工作示例。例如,您是否在打开_reading process_后再次关闭每个fd?否则,你将很快用完文件描述符 – Ctx
,这大部分是其他事物正在打印和设置目录的一切。 – Thomas
我建议读取'man 7 fifo'的输出,特别是关于在非阻塞模式下打开FIFO的部分。我认为当FIFO的另一端没有打开时,你的只写,非阻塞的'open'将会失败,并带有errno'ENXIO'。 –