[IPC process Communication 2] pipeline Pipe

Source: Internet
Author: User

IPC inter-process communication + pipeline PipeIPC (Inter-Process Communication ).MPs queues are used to share data between processes. The essence is shared memory, one of the commonly used IPC. Pipelines can be used not only for communication between local processes, but also for cross-network process communication. Like Socket communication, pipelines also encapsulate the underlying network implementation of computers and provide a good API interface.1. Pipeline (Pipe ):
Pipelines are divided into anonymous pipelines and named pipelines.An anonymous pipeline can only be used for communication between parent and child processes. It cannot communicate across networks and is unidirectional.A named pipe can be used for communication between any process and supports cross-network communication. The communication is bidirectional and any segment can receive and send data. The naming pipeline is the focus of learning.A named pipeline communication process consists of a server and a client. The server creates a Pipe. After the client connects to the Pipe through the pipeline name, both parties can send and receive data through the Pipe.2. mail slot name:Local format: \ ServerName \ Pipe \ nameFor example: \. \ Pipe \ my_pipe
3. Basic API functions:Create a named pipe:HANDLE CreateNamedPipe (
Lptstr lpName, // pointer to the pipe name
DWORD dwOpenMode, // pipeline open mode
DWORD dwPipeMode, // pipe mode
DWORD nMaxInstances, // maximum number of instances
DWORD nOutBufferSize, // output cache size
DWORD nInBufferSize, // enter the cache size
DWORD ndefatimetimeout, // timeout setting
LPSECURITY_ATTRIBUTES lpSecurityAttributes // Security Attribute pointer
);
This function is used to create named pipes on the server side.Waiting for client connection:BOOL ConnectNamedPipe (
HANDLE hNamedPipe, // MPs queue HANDLE
LPOVERLAPPED lpOverlapped // set to NULL
);
This function is used by the server to wait for the client to connect to the MPs queue.Check the named pipe:BOOL WaitNamedPipe (
Lptstr lpNamedPipeName, // MPs queue name
DWORD nTimeOut // timeout
);
This function is used by the client to check whether a pipeline Pipe exists.Open the named pipe:BOOL CreateFile (Lptstr lpFileName,
DWORD dwDesiredAccess,
DWORD dw1_mode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);The first parameter is the pipe name pointer.The second parameter is access mode.Third parameter: Shared Mode.Fourth parameter: Security Attribute pointer.Fifth parameter: Create option.Sixth parameter: file attributes.Seventh parameter: used to copy the file handle.This function is used to open an MPS queue and return the MPs queue handle.Read pipeline:BOOLReadFile (
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDOWRD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped );
The first parameter is the MPs queue handle.The second parameter is the cache address.The third parameter is the message length.Fourth parameter: the actual read length.Fifth parameter: generally set to NULL.This function is used to read data in the MPs queue.Write pipeline:BOOL WriteFile (
HANDLE hFile,
LPCVOID lpBuffer,
DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped );
The first parameter is the MPs queue handle.The second parameter is the data buffer pointer.Third parameter: number of bytes written.Fourth parameter: returns the actual number of bytes written.Fifth parameter: struct pointer, usually NULL.This function is used by the client to write data to the pipeline.
4. Cow test:Run the server program in VC6.0 and then the client program.
Running effect:
Server program:Client Program:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.