What is a Named Pipe?

"Named Pipes", also known as "Named Pipes", is a simple inter-process communication (IPC) mechanism. Most Microsoft Windows provides support for it (but not including Windows CE). Named pipes support reliable, one-way or two-way data communication between different processes on the same computer or between different processes on different computers across a network. An important reason for recommending named pipes as a process communication scheme is that they take full advantage of Windows' built-in security features (ACLs, etc.).

"Named Pipes", also known as "Named Pipes", is a simple inter-process communication (IPC) mechanism. Most Microsoft Windows provides support for it (but not including Windows CE). Named pipes support reliable, one-way or two-way data communication between different processes on the same computer or between different processes on different computers across a network. An important reason for recommending named pipes as a process communication scheme is that they take full advantage of Windows' built-in security features (ACLs, etc.).
Using named pipes to design cross-computer applications is actually very simple, and does not require prior knowledge of the underlying network transmission protocols (such as TCP, UDP, IP, IPX). This is because named pipes use the Microsoft Network Provider (MSNP) redirector to establish communication between processes over the same network, so that applications do not need to care about the details of the network protocol.
Chinese name
Named pipes
Foreign name
Named Pipes
Supported platforms
Windows, Linux, Unix, etc.

Introduction to named pipes

A named pipe (NamedPipe) is a one-way or two-way pipe for communication between a server process and one or more client processes. What is different from anonymous pipes is that named pipes can be used between unrelated processes and between different computers. When the server establishes a named pipe, it is assigned a name, and any process can open the other end of the pipe by this name. The specified permissions communicate with the server process. Named pipes provide a relatively simple programming interface, making data transmission over the network no more difficult than communicating between two processes on the same computer, but it can't handle it if you want to communicate with multiple processes at the same time. [1]

Named pipe role

In computer programming, named pipes are a type of process that uses kernel objects to transfer information from one process to another. Different from general pipes, named pipes can be called by different processes in different ways (can cross permissions, cross languages, cross platforms). As long as the program knows the name of the named pipe, the information sent to the named pipe can be read by all programs that have the specified authorization, but not for those who have the specified authorization. A named pipe is a FIFO (First-In First-Out) object. [2]

Application direction and learning method of named pipes

Assuming a server holds company secrets, we require that only company managers can access or edit these secrets (high-privilege information). On the work network, every employee in the company can see this computer on the network (low-privileged access). However, we do not want ordinary employees (low-privilege groups) to gain access to confidential materials. The company asked us to develop a data management system that allowed only a specified user group (high-privilege group) to operate.
In this case, data communication systems containing ACLs such as named pipes can work. Because we can use ACLs, only users with special privileges (high-privileged groups) can send control information to designated servers to operate company secrets. An important point to remember here is that when using named pipes as a network programming solution, it actually establishes a simple client / server data communication system (usually TCP / IP, and the TCP protocol has good stability) And data security).
To learn to develop a set of named pipe applications (which may be different threads of a process, different instances of the same executable file, or completely different programs), you must first understand the naming conventions (naming protocols) of named pipes, and then understand the basics The pipeline type, then implement a simple set of server applications and a client application. Then use it as a basis to study advanced server programming techniques and understand more complex communication systems and simple communication protocols.

Linux Named Pipes Linux instance

1 Named pipe instance 1

Create and use named pipes in one program.
  #include <sys / types.h>


 #include <sys / stat.h>


 #include <unistd.h>


 #include <fcntl.h>


 int main (
 void)


 {


 char buf [80];


 int fd;



 unlink ("zieckey_fifo");



 mkfifo ("zieckey_fifo", 0777);


 if (
 fork ()> 0)


 {


 chars [] = "Hello! \ n";


 fd = open ("zieckey_fifo", O_WRONLY);


 write (fd, s, sizeof (s));


 // close (fd);


 }


 else


 {


 fd = open ("zieckey_fifo", O_RDONLY);


 read (fd, buf, sizeof (buf));



 printf ("The message from the pipes:% s \ n", buf);


 // close (fd);


 }


 return0;


 }


 / * The execution result is The message from the pipes: Hello!
 And can generate pipeline file zieckey_fifo in the program execution directory
 * / 

2 Named pipe instance 2

This sample code is intended to reflect the difference between named pipes and ordinary pipes. Named pipes come in the form of an ordinary file and include three file operations: create named pipe, write pipe, and read pipe.
Create a named pipe

  #include <sys / types.h>

 #include <sys / stat.h>

 #include <unistd.h>

 #include <fcntl.h>

 int main (void)

 {

 char buf [80];

 int fd;

 unlink ("zieckey_fifo");

 mkfifo ("zieckey_fifo", 0777);

 }
Write named pipe code










 #include <sys / types.h>

 #include <sys / stat.h>

 #include <unistd.h>

 #include <fcntl.h>

 int main (void)

 {

 int fd;

 chars [] = "Hello! \ n";

 fd = open ("zieckey_fifo", O_WRONLY);


 while
 (1)

 {

 write (fd, s, sizeof (s));

 sleep (1);

 }

 return 0;

 }
Read named pipe code
  #include <sys / types.h>

 #include <sys / stat.h>

 #include <unistd.h>

 #include <fcntl.h>

 int main (void)

 {

 int fd;

 char buf [80];

 fd = open ("zieckey_fifo", O_RDONLY);

 while (1)

 {

 read (fd, buf, sizeof (buf));

 printf ("% s \ n", buf);

 sleep (1);

 }

 return0;

 } 

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?