当FIFO被从程序打开检测 [英] Detect when a fifo is opened from a program

查看:122
本文介绍了当FIFO被从程序打开检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我需要检查,如果一个FIFO对方已经打开它,但是我不能用一个开放的,否则程序将启动做的东西。

I have a situation where I need to check if the other side of a fifo has opened it, however I can't use a open because otherwise the program will start doing stuff.

我为什么要做这样的:我有一个计划(监视器),会启动服务器程序(由我都创建)。该显示器采用这种FIFO沟通东阳显示器可以关闭/重新开放,而服务器已启动。

Why I have to do this: I have a program (a monitor) that launches a server program (both created by me). The monitor uses this fifo to communicate beacause the monitor can be closed/reopened while the server is already started.

问题是,当显示器启动服务器:在这种情况下,我必须以某种方式等待要创建的FIFO,然后打开它们。
其实我使用的检查创建的FIFO时,在监视器上一段时间,但通过这种方式它打开之前,FIFO中的 服务器可以做到这一点(即使mkfifo子后的指令实际上是开放!)。

The problem is when the monitor starts the server: in this case I have to wait in some way for fifos to be created and then open them. Actually I'm using a while on the monitor that checks when the fifos are created, however in this way it opens the fifos before the server could do it (even if the instruction after the mkfifo is actually the open!!!).

您可能会说,我是在开放的顺序错误的FIFO的显示器(在读FIFO之前我开写FIFO(WRONLY))上,问题是,因为它的要求我不能恢复这个命令服务器将等待客户端的开放(RDONLY)FIFO。

You may say that I'm opening the fifos in wrong order on the monitor (I'm opening the Write fifo(WRONLY) before the read fifo), the problem is that I can't revert this order because it's required that the server will wait for clients on the open (RDONLY) fifo.

如何避免这种竞争情况的任何建议?
实际上,我使用显示屏睡眠检查,如果创建的FIFO后,此obviusly解决了这个问题,但我认为肯定是不正确的。

Any suggestion on how to avoid this race condition? I'm actually using a sleep in the monitor after checking if fifos are created, this obviusly solves the problem but I think is definitely not correct.

谢谢大家。

编辑1:

这是怎么会事都做什么工作的。

This is how things are working at the moment

服务器

mkfifo(fifo1)
mkfifo(fifo2)
open(fifo1 O_RDONLY)
open(fifo2 O_WRONLY)

显示器

while (fifo1 doesn't exists && fifo2 doesn't exists);
open(fifo1 O_WRONLY)
open(fifo2 O_RDONLY)

我觉得竞争状态现在很明确,要注意FIFO被阻塞(仅RDONLY是阻止这一点很重要,如果没有对对方的人=> 这是一个WRONLY甚至不会阻止UNIX行为,而不是由我设计)。

编辑2:

竞争状态发生在第一FIFO开放水平。 我必须打开监视器会之前在服务器上的第一个FIFO。

The race condition happens at first fifo open level. I must open the first fifo on the server before the monitor does it.

推荐答案

至少我发现了什么至今让我明白的没有如果FIFO被打开可检测的方式,除非你打开它了。

At least what I found 'till now makes me understand that there isn't a way to detect if a fifo is opened, except if you open it too.

编辑1:

由于贾森说,有两种方法(两者在我的作业允许然而):

As Jason stated, there are two ways (both not allowed in my homework however):

1)*你可以做通过搜索的/ proc / PID 的/ FD(带有一个数字的进程ID替换PID),看看有什么客户进程已经打开了你的FIFO *
2)的另一种办法是打电话给你的FIFO

1) *You could do a search through /proc/PID/fd (replace PID with a numerical process ID) to see what client processes had already opened your FIFO* 2) Another option would be to call fuser on your FIFO

然而,第一个需要的东西,老师不希望:看proc /进程/ FD内。第二个我heared需要root权限,而这又是我的东西不能做。希望这会帮助别人,将来别人。

However, the first one requires something that teachers don't want: watching inside proc/PID/fd. The second one I heared requires root privileges, and this is again something I can't do. Hopefully this will help someone else in the future.

这篇关于当FIFO被从程序打开检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆