如何在打开/写入功能中实现超时

时间:2021-05-11 23:33:18

I want to use named fifo channel and I want to implement a timeout when I write in this fifo.

我想使用命名的fifo通道,我想在写这个fifo时实现超时。

fd = open(pipe, O_WRONLY);
write(fd, msg, len);

Program is blocked by function open, so using the function select will not work. Thanks.

程序被打开的功能阻止,因此使用功能选择将不起作用。谢谢。

2 个解决方案

#1


1  

Read pipe(7), fifo(7), poll(2)

读管(7),fifo(7),民意调查(2)

You might setup a timer or or alarm with a signal handler (see time(7) & signal(7)) before your call to open(2) - but I won't do that - or you could use the O_NONBLOCK flag, since fifo(7) says:

在调用open(2)之前,您可以使用信号处理程序设置计时器或警报(请参阅时间(7)和信号(7)) - 但我不会这样做 - 或者您可以使用O_NONBLOCK标志,因为fifo(7)说:

 A process can open a FIFO in nonblocking mode.  In this case, opening
for read-only will succeed even if no-one has opened on the write
side yet, opening for write-only will fail with ENXIO (no such device
or address) unless the other end has already been opened.

However, you need something (some other process reading) on the other side of the FIFO or pipe.

但是,您需要在FIFO或管道的另一端(某些其他过程读取)。

Perhaps you should consider using unix(7) sockets, i.e. the AF_UNIX address family. It looks more relevant to your case: change your code above (trying to open for writing a FIFO) to a AF_UNIX socket on the client side (with a connect), and change the other process to become an AF_UNIX socket server.

也许你应该考虑使用unix(7)套接字,即AF_UNIX地址族。它看起来与您的情况更相关:将上面的代码(尝试打开以编写FIFO)更改为客户端上的AF_UNIX套接字(使用连接),并将其他进程更改为AF_UNIX套接字服务器。

As 5gon12eder commented, you might also look into inotify(7). Or even perhaps D-bus !

正如5gon12eder所评论的那样,您也可以查看inotify(7)。甚至可能是D-bus!

I'm guessing that FIFOs or pipes are not the right solution in your situation. You should explain more and give a broader picture of your concerns and goals.

我猜测在您的情况下,FIFO或管道不是正确的解决方案。您应该解释更多,并更全面地了解您的关注点和目标。

#2


0  

use select() and its timeout argument.

使用select()及其超时参数。

#1


1  

Read pipe(7), fifo(7), poll(2)

读管(7),fifo(7),民意调查(2)

You might setup a timer or or alarm with a signal handler (see time(7) & signal(7)) before your call to open(2) - but I won't do that - or you could use the O_NONBLOCK flag, since fifo(7) says:

在调用open(2)之前,您可以使用信号处理程序设置计时器或警报(请参阅时间(7)和信号(7)) - 但我不会这样做 - 或者您可以使用O_NONBLOCK标志,因为fifo(7)说:

 A process can open a FIFO in nonblocking mode.  In this case, opening
for read-only will succeed even if no-one has opened on the write
side yet, opening for write-only will fail with ENXIO (no such device
or address) unless the other end has already been opened.

However, you need something (some other process reading) on the other side of the FIFO or pipe.

但是,您需要在FIFO或管道的另一端(某些其他过程读取)。

Perhaps you should consider using unix(7) sockets, i.e. the AF_UNIX address family. It looks more relevant to your case: change your code above (trying to open for writing a FIFO) to a AF_UNIX socket on the client side (with a connect), and change the other process to become an AF_UNIX socket server.

也许你应该考虑使用unix(7)套接字,即AF_UNIX地址族。它看起来与您的情况更相关:将上面的代码(尝试打开以编写FIFO)更改为客户端上的AF_UNIX套接字(使用连接),并将其他进程更改为AF_UNIX套接字服务器。

As 5gon12eder commented, you might also look into inotify(7). Or even perhaps D-bus !

正如5gon12eder所评论的那样,您也可以查看inotify(7)。甚至可能是D-bus!

I'm guessing that FIFOs or pipes are not the right solution in your situation. You should explain more and give a broader picture of your concerns and goals.

我猜测在您的情况下,FIFO或管道不是正确的解决方案。您应该解释更多,并更全面地了解您的关注点和目标。

#2


0  

use select() and its timeout argument.

使用select()及其超时参数。