简体   繁体   English

命名管道问题

[英]Named Pipe Problem

Is there a function that returns when the other end successfully called ReadFile on the pipe. 当另一端在管道上成功调用ReadFile时,有没有返回的函数。 I have 2 application communicating on a named pipe, one sends a request on the pipe using WriteFile and expects an answer so it calls ReadFile. 我有2个应用程序在命名管道上通信,一个应用程序使用WriteFile在管道上发送请求,并期望得到答案,因此它调用ReadFile。 The problem is that the application is reading its own request as the answer since the other end did not remove it from the pipe yet. 问题在于应用程序正在读取自己的请求作为答案,因为另一端尚未将其从管道中删除。

Is there such a function? 有这样的功能吗?

I know an alternative would be to either use 2 unidirectional pipes or implement some kind or synchronization by having the other application signal the sender when it got its message but I'm just checking if there is a simpler way... 我知道一种替代方法是使用2个单向管道,或者通过让其他应用程序在收到消息时向发送方发送信号来实现某种或同步,但是我只是在检查是否有更简单的方法...

I think the method you are looking for is PeekNamedPipe . 我认为您正在寻找的方法是PeekNamedPipe This function will allow you to look at the pipe without removing the data. 使用此功能,您可以在不删除数据的情况下查看管道。 You can use this to check and see if the message you just sent is still in the pipe. 您可以使用它来检查并查看刚刚发送的消息是否仍在管道中。 If so then the other end hasn't read the message yet. 如果是这样,则另一端尚未读取该消息。

Overall though, I think you're better off going with a signal or two unidirectional pipes. 总的来说,我认为最好使用一个信号或两个单向管道。 They are better suited for 2 way communication. 它们更适合双向通讯。

Your design is inherently prone to race conditions and I would urge you to use two pipes instead. 您的设计天生就容易出现竞争状况,我强烈建议您改用两条管道。 In particular, have you considered what happens when two processes want to write to the same pipe? 特别是,您是否考虑过当两个进程要写入同一管道时会发生什么情况?

That said... You can potentially use an event to synchronize. 就是说...您可以潜在地使用事件进行同步。 After you're done with WriteFile(), you can wait for the event to be signalled. 在完成WriteFile()之后,您可以等待事件被通知。 When the other end is done with ReadFile(), it can SetEvent() . 当另一端使用ReadFile()完成时,它可以SetEvent() That might work, provided the two ends have a well defined contract and don't race with each other or deadlock one another. 如果两端之间的合同明确,并且彼此之间不争分夺秒或彼此陷入僵局,那可能会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM