简体   繁体   English

关于Windows中的管道行为

[英]About pipe behavior in windows

    hPipe = CreateNamedPipe( 
          lpszPipename,             // 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 
          0,       

I have two questions about this: 我对此有两个问题:

  1. what if the above code is run twice,how many pipes will get created, 1 or 2 ? 如果上面的代码运行两次,将会创建多少个管道( 12呢?
  2. if 2 ,suppose one of the pipe get connected by A ,then B tries to connect lpszPipename , is it guaranteed that B will connect to the one that no one has connected? 如果2 ,假设其中一个管道被A连接,则B尝试连接lpszPipename ,是否可以确保B会连接到没有人连接的那个管道?

The second call to CreateNamedPipe with the same name fails, if FILE_FLAG_FIRST_PIPE_INSTANCE flag is used, or connects to the same pipe, if this flag is not used. 如果使用了FILE_FLAG_FIRST_PIPE_INSTANCE标志,则对具有相同名称的CreateNamedPipe的第二次调用失败,或者如果不使用此标志,则第二次调用将失败。 In the case the second CreateNamedPipe call succeeds, it returns another handle to the same kernel object. 如果第二个CreateNamedPipe调用成功,它将向同一内核对象返回另一个句柄。

In the fourth parameter of CreateNamedPipe function you can limit how many named pipe instances can be created. CreateNamedPipe函数的第四个参数中,您可以限制可以创建多少个命名管道实例。 If you set it to PIPE_UNLIMITED_INSTANCE as in your example and call the CreateNamedPipe function twice with the same parameters, two instances will be created (they'll share the same pipe name) and two clients will be able to connect to your named pipe server (each of them to one named pipe instance). 如果像示例中一样将其设置为PIPE_UNLIMITED_INSTANCE并使用相同的参数两次调用CreateNamedPipe函数,则将创建两个实例(它们将共享相同的管道名称),并且两个客户端将能够连接到您的命名管道服务器(它们中的每一个到一个命名管道实例)。

For more information look at http://msdn.microsoft.com/en-us/library/aa365594%28v=VS.85%29.aspx 有关更多信息,请参见http://msdn.microsoft.com/zh-cn/library/aa365594%28v=VS.85%29.aspx

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

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