Triggor

Follow My Heart
  博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

命名管道(Named Pipes)

Posted on 2011-07-29 20:10  triggor  阅读(902)  评论(0编辑  收藏  举报

“命名管道”或“命名管线”(Named Pipes )是一种简单的进程间通信( I P C)机制,Microsoft Windows NT,Windows 2000、Windows 95以及Windows 98均提供了对它的支持(但不包括Windows CE)。命名管道可在同一台计算机的不同进程之间,或在跨越一个网络的不同计算机的不同进程之间,支持可靠的、单向或双向的数据通信。用命名管道来设计应用程序实际非常简单,并不需要事先深入掌握基层网络传送协议(如T C P / I P或I P X)的知识。这是由于命名管道利用了微软网络提供者( M S N P)重定向器,通过一个网络,在各进程间建立通信。这样一来,应用程序便不必关心网络协议的细节。之所以要用命名管道作为自己的网络通信方案,一项重要的原因是它们充分利用了Windows NT及Windows 2000内建的安全特性。
这里有一个可采纳命令管道的例子。假定我们要开发一个数据管理系统,只允许一个指定的用户组进行操作。想像在自己的办公室中,有一部计算机,其中保存着公司的秘密。我们要求只有公司的管理人员,才能访问及处理这些秘密。假定在自己的工作站机器上,公司内的每名员工都可看到网络上的这台计算机。然而,我们并不希望普通员工取得对机密材料的访问权。在这种情况下,命名管道可发挥出很好的作用,因为我们可开发一个服务器应用程序,令其以来自客户机的请求为准,对公司的秘密进行安全操作。服务器可将客户访问限制在管理人员身上,用Windows NT或新版Windows 2000自带的安全机制,便可非常轻松地做到这一点。
在此要记住的一个重点是,将命名管道作为一种网络编程方案使用时,它实际上建立一个简单的客户机/服务器数据通信体系,可在其中可靠地传输数据。


 Named Pipes In WikiPedia

In computing, a named pipe (also known as a FIFO for its behavior) is an extension to the traditional pipe concept on Unix and Unix-like systems, and is one of the methods of inter-process communication. The concept is also found in Microsoft Windows, although the semantics differ substantially. A traditional pipe is "unnamed" because it exists anonymously and persists only for as long as the process is running. A named pipe is system-persistent and exists beyond the life of the process and must be deleted once it is no longer being used. Processes generally attach to the named pipe (usually appearing as a file) to perform inter-process communication (IPC).

Contents

Named pipes in Unix

Instead of a conventional, unnamed, shell pipeline, a named pipeline makes use of the filesystem. It is explicitly created using mkfifo() or mknod(), and two separate processes can access the pipe by name — one process can open it as a reader, and the other as a writer.

For example, one can create a pipe and set up gzip to compress things piped to it:

mkfifo my_pipe
gzip -9 -c < my_pipe > out.gz

In a separate process shell, independently, one could send the data to be compressed:

cat file > my_pipe

The named pipe can be deleted just like any file:

rm my_pipe

A named pipe can be used to transfer information from one application to another without the use of an intermediate temporary file. For example you can pipe the output of gzip into a named pipe like so:

mkfifo --mode=0666 /tmp/namedPipe
gzip --stdout -d file.gz > /tmp/namedPipe

Then load the uncompressed data into a MySQL table[1] like so:

LOAD DATA INFILE '/tmp/namedPipe' INTO TABLE tableName;

Without this named pipe one would need to write out the entire uncompressed version of file.gz before loading it into MySQL. Writing the temporary file is both time consuming and results in more I/O and less space on the hard drive.

PostgreSQL's command line terminal, psql, also supports loading data from named pipes.[2]

Named pipes in Windows

In Windows, the design of named pipes is based towards client-server communication, and they work much like sockets, other than the usual read and write operations. Windows named pipes also support an explicit "passive(被动)" mode for server applications (compare: Unix domain sockets). Windows 95 supports named pipe clients, Windows NT based systems can also be servers.

The named pipe can be accessed much like a file. Win32 SDK functions such as CreateFile, ReadFile, WriteFile and CloseHandle can be used to open, read from, write to, and close a pipe. There is no command line interface like Unix.

Named pipes are not permanent and can not be created as special files on any writable filesystem, unlike in Unix, but are volatile names (freed after the last reference to them is closed) allocated in the root directory of the named pipe filesystem (NPFS), mounted under the special path \\.\pipe\ (that is, a pipe named "foo" would have a full path name of \\.\pipe\foo). Anonymous pipes used in pipelining are actually named pipes with a random name.

They are very rarely seen by users, but there are notable exceptions. The VMware Workstation PC hardware virtualization tool, for instance, can expose emulated serial ports to the host system as named pipes, and the WinDbg kernel mode debugger from Microsoft supports named pipes as a transport for debugging sessions (in fact, VMware and WinDbg can be coupled together - since WinDbg normally requires a serial connection to the target computer - letting driver developers do their development and testing on a single computer). Both programs require the user to enter names in the \\.\pipe\name form.

Windows NT Named Pipes can inherit a security context.

Summary of named pipes on Microsoft Windows:

  • Intermachine and Intramachine IPC
  • Half-duplex or full-duplex
  • Byte-oriented or Message-oriented
  • Reliable
  • Blocking or Nonblocking read and write (choosable)
  • Standard device I/O handles (FileRead, FileWrite)
  • Namespace used to create handles
  • Inefficient WAN traffic (explicit data transfer request, unlike e.g. TCP/IP sliding window, etc.)
  • Peekable reads (read without removing from pipe's input buffer)

The .NET Framework 3.5 has added named pipe support.[3]

Named pipes can also be used as an endpoint in Microsoft SQL Server.[4]

Named Pipes is also a networking protocol in the Server Message Block (SMB) suite, based on the use of a special Inter-process communication (IPC) share. SMB's IPC can seamlessly and transparently pass the authentication context of the user across to Named Pipes. Windows NT's entire NT Domain protocol suite of services are implemented as DCE/RPC services over Named Pipes, as are the Exchange 5.5 Administrative applications.