WriteFile/ReadFile 发生死锁

发布于 2024-12-09 22:41:21 字数 1863 浏览 1 评论 0原文

我正在使用管道,并且在 WriteFile/ReadFile 上遇到了某种死锁。这是我的代码:

hProbePipeRet = CreateNamedPipe( 
          "\\\\.\\pipe\\probePipeRet", // pipe name 
          PIPE_ACCESS_DUPLEX,       // read/write access 
          PIPE_TYPE_MESSAGE |       // message type pipe 
          PIPE_READMODE_MESSAGE |   // message-read mode 
          PIPE_WAIT,                // blocking mode 
          PIPE_UNLIMITED_INSTANCES, // max. instances  
          BUFSIZE,                  // output buffer size 
          BUFSIZE,                  // input buffer size 
          5,                        // client time-out 
          NULL);                    // default security attribute 

首先我创建我的管道,然后在另一个应用程序中像这样使用它:

WriteFile( 
            hProbePipeRet,        // handle to pipe 
            msg.c_str(),     // buffer to write from 
            msg.size(), // number of bytes to write 
            &dwBytesWritten,   // number of bytes written 
            NULL);        // not overlapped I/O 

然后我收到它:

        fSuccess = ReadFile( 
            myInst->hProbePipeRet,        // handle to pipe 
            buf,    // buffer to receive data 
            BUFSIZE, // size of buffer 
            &dwBytesRead, // number of bytes read 
            NULL);        // not overlapped I/O 

这是非常基本的,我还有两个管道可以做完全相同的事情,唯一的区别是它们位于不同的线程中,但我只需要这个线程来进行基本的消息事务。

第一次尝试时,管道上的信息被成功读取,但第二次尝试时,如果我不发送至少 BUFSIZE 的数据,则 WriteFile 和 ReadFile 都会阻塞。正如我所说,我还有两个管道执行相同的操作,具有相同的功能,并且我不需要发送 BUFSIZE 数据即可成功通信。

编辑:附加信息

执行如下:通过pipe1将消息发送到服务器,接收该消息,然后在我有问题的代码中使用hProbePipeRet返回数据。数据由客户端读取并打印到屏幕上。

使用 pipeline1 发送另一条消息,收到后结果再次进入 hProbePipeRet,客户端正在等待至少 BUFSIZE 的信息,我不知道服务器在做什么,但它在 WriteFile 处被阻止。

这种情况与我的其他管道相同,但我没有将 hProbePipeRet 放在单独的线程中来读取它。我这样做是因为我在发送消息后需要立即得到答复。

I'm using pipes and I got a kind of deadlock on WriteFile/ReadFile. Here is my code :

hProbePipeRet = CreateNamedPipe( 
          "\\\\.\\pipe\\probePipeRet", // pipe name 
          PIPE_ACCESS_DUPLEX,       // read/write access 
          PIPE_TYPE_MESSAGE |       // message type pipe 
          PIPE_READMODE_MESSAGE |   // message-read mode 
          PIPE_WAIT,                // blocking mode 
          PIPE_UNLIMITED_INSTANCES, // max. instances  
          BUFSIZE,                  // output buffer size 
          BUFSIZE,                  // input buffer size 
          5,                        // client time-out 
          NULL);                    // default security attribute 

First I create my pipe, then I use it like this in another application:

WriteFile( 
            hProbePipeRet,        // handle to pipe 
            msg.c_str(),     // buffer to write from 
            msg.size(), // number of bytes to write 
            &dwBytesWritten,   // number of bytes written 
            NULL);        // not overlapped I/O 

And I receive it back with :

        fSuccess = ReadFile( 
            myInst->hProbePipeRet,        // handle to pipe 
            buf,    // buffer to receive data 
            BUFSIZE, // size of buffer 
            &dwBytesRead, // number of bytes read 
            NULL);        // not overlapped I/O 

This is very basic and I have two more pipes that do EXACLY the same thing, the only difference is that they are in a different thread, but I need this one only for basic transactions of message.

On the first try, the informations on the pipes are read successfully, but on the second try, if I don't send at least BUFSIZE of data, both WriteFile and ReadFile will block. As I said, I have two more pipes that do the same thing, with the same functions and I don't need to send BUFSIZE of data to have a successful communication.

EDIT : Additionnal infos

The execution goes as follow : A message is sent to the server by pipe1, the message is received then it returns data with hProbePipeRet in my problematic code. The data is read by the client, printed to the screen.

Another message is dispatched using pipe1, received and the result goes again in hProbePipeRet, the client is waiting for at least BUFSIZE of information and I don't know what the server is doing but it's blocked at WriteFile.

This scenario is the same as my others pipes but I don't put hProbePipeRet in a seperate thread to read from it. I'm doing this because I need an answer right after I dispatched the message.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别理我 2024-12-16 22:41:21

也许您遇到了使用阻塞 IO 的问题。对 ReadFile 的调用会阻塞,直到有内容可供读取。如果您有一个调用 write 然后 read 的循环,它可能会在第二次调用中阻塞。
也许你应该考虑使用 async io。您可以通过事件调用 readFile。当有东西要读时,事件就会被设置。所以不需要创建多个线程。

Perhaps you have the problem that you use blocking IO. The call to ReadFile blocks until there is something to read. If you have a loop that calls write and then read it may block in the second call.
Perhaps you should consider using async io. You call the readFile with a event. The event gets set when there is something to read. So there is no need to create multiple threads.

赴月观长安 2024-12-16 22:41:21

使用 PIPE_TYPE_BYTEPIPE_READMODE_BYTE 而不是 MESSAGE 计数器部分。此外,在任何客户端连接之前,服务器不得执行任何阻塞读取操作。

请参阅 http://msdn.microsoft .com/en-us/library/windows/desktop/aa365150(v=vs.85).aspx

编辑:对于“不得执行任何阻止”读取操作':根据文档,这可能会导致竞争条件,这实际上可能是您的情况,但是如果不查看更多代码,就很难判断。

use PIPE_TYPE_BYTE and PIPE_READMODE_BYTE instead of the MESSAGE counter parts. Also the server must not perform any blocking read operations before any client has connected.

See http://msdn.microsoft.com/en-us/library/windows/desktop/aa365150(v=vs.85).aspx

Edit: For the 'must not perform any blocking read operations': This can, according the the documentation lead to a race condition which actually might be your case, however it is hard to tell without seeing more of your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文