Inter-process communication pipeline
An unknown pipeline:
Features: kinship-related inter-process communication, but not just between parent and child processes.
(1) Create an unknown MPs queue
Int pipe (int pipefd
Parameters:
The first address of the pipefd array.
Return Value:
0 is returned for success and-1 is returned for failure.
Note:
The unknown MPs queue exists in the kernel space. If it is successfully created, two file descriptors are provided to the user space. fd [0]: Read MPs queue fd [1]: Write MPs queue.
Think: Why can't an unknown pipeline only be used for inter-kinship process communication?
Because only unrelated processes have data copies [copy file descriptors]
II. Famous Pipelines
Features:
(1) communication between any process
(2) file names exist in the file system.
<1> Create a famous MPs queue File
Int mkfifo (const char * pathname, mode_t mode );
Parameters:
@ Pathname: File Name
@ Mode: the specified permission.
Return Value:
0 is returned for success and-1 is returned for failure.
<2> open a famous Pipeline File
Open
Note:
When a process opens the MPs queue file in read-only mode, it is blocked until another process opens the MPs queue file in write mode.
When a process opens the MPs queue file in write-only mode, it is blocked until the other process opens the MPs queue file in Read mode.
<3> read/write Pipeline
Read/write
The following is an example of communication between pipelines:
Reads data from pipelines
<Pre name = "code" class = "cpp"> <pre name = "code" class = "cpp"> # include
Write Data to the MPs queue
# Include
Inter-process communication (MPS) in linux
If (write (pipe_fd [1], "first string", 10 )! =-1)
How do you know that the length of the first string of text is 10? If your file is UTF-8 encoded, it will be 15 characters long. If you give the file a length of 10, only the last three words and one single byte will be passed.
C ++ inter-process communication (pipeline and shared memory respectively), give a simple code, package comments
# Include <stdio. h> # include <windows. h> int main (int argc, char * argv []) {if (argv [1] = 0) {// if the main process is HANDLE hPipeW, hPipeR; // read and write pipelines STARTUPINFOA si; PROCESS_INFORMATION pi; char str [128]; char param [1024]; CreatePipe (& hPipeR, & hPipeW, NULL, 0 ); setHandleInformation (hPipeW, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); // enables the sub-process to inherit this handle ZeroMemory (& si, sizeof (si); si. cb = sizeof (si); sprintf (param ,"\ "% S \" % x ", argv [0], hPipeW); // if (CreateProcessA (argv [0], param, 0, 0, TRUE, 0, 0, 0, & si, & pi )! = FALSE) {char * pstr = str; CloseHandle (hPipeW); // closes the input of the pipeline because the subprocess has used the input of CloseHandle (pi. hProcess); CloseHandle (pi. hThread); for (;) {DWORD r; ReadFile (hPipeR, pstr, 128, & r, 0); // read data from the pipeline if (r> 0) pstr + = r; else break;} CloseHandle (hPipeR); puts (str);} return 0;} else {// if it is a sub-process char str [] = "Hello! "; HANDLE hPipeW; DWORD r; sscanf (argv [1]," % x ", & hPipeW); // obtain the write HANDLE WriteFile (hPipeW, str, sizeof (str), & r, 0); // write data to the pipe CloseHandle (hPipeW); return 0 ;}# include <stdio. h> # include <string. h> # include <windows. h> int main (int argc, char * argv []) {if (argv [1] = 0) {// if HANDLE hShmem is run as the main process; char param [1024]; STARTUPINFOA si; PROCESS_INFORMATION pi; char * pstr; hShmem = CreateFileMapping (INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0,256, 0 );/&...... remaining full text>