编程知识 cdmana.com

Linux interprocess communication

There are three ways to communicate between processes : 1. Anonymous pipeline –pipe; 2. name pipes –fifo; 3. Memory mapping –mmap;

The pipeline is in the kernel , Processes share kernel regions

1. pipe The Conduit

	#include <unistd.h>	
	int pipe(int pipefd[2]);
	
	#define _GNU_SOURCE             /* See feature_test_macros(7) */
	#include <fcntl.h>              /* Obtain O_* constant definitions */
	#include <unistd.h>	
	int pipe2(int pipefd[2], int flags);
	 If it works , return 0, Otherwise return to -1 And set up errno, pipefd[2] It's the outgoing parameter 
	
	 Pipes act between related processes ,  adopt fork To pass on 
	 call pipe Function to open up a buffer in the kernel ( It's called a pipe ) For communication , It has a read side and a write side ,  then 
	 adopt pipefd Parameters are passed out to the user program, two file descriptors , pipefd[0] Point to the read end of the pipe , pipefd[1] Pointing to the pipe 
	 Write end , So the pipeline looks like an open file in the user program , adopt read(pipefd[0]) perhaps write(pipefd[1])
		1. Parent process call pipe Open up a pipe ,  Get two file descriptors pointing to both ends of the pipe 
		2. Parent process call fork Create child process ,  Then the child process also has two file descriptors pointing to the same pipe 
		3. The parent process closes the pipe reader ,  The child process closes the pipe writer ,  The parent process can write , Subprocesses can read from the pipeline , The pipe is made of 
		 Ring queue implementation , Data flows in from the write side and flows out from the read side , This enables interprocess communication 
	** Precautions for pipeline use :**
		1. If the write end is off ,  Read the contents of the pipeline until the end of reading ,  When reading again , return 0,  It's equivalent to reading EOF Read end off 
		2. If the terminal is not closed ,  But there is no data on the write side ,  When the reader finishes reading the data in the pipeline ,  Read it again ,  The reader will block 
		3. If the reader is off ,  The write side writes data to the pipe ,  Will produce SIGPIPE The signal ,  Causes the write process to terminate 
		4. If the reader does not read data from the pipe ,  The write end writes data all the way to the pipe ,  Until writing is full. ,  When the writer writes again , The writer will block 
	fpathconf(fd[0], _PC_PIPE_BUF)
	 You can test the pipeline buffer size , fcntl Function can be used to set the pipe non blocking 

2.fifo

	 command  : mkfifo fifoname		 Create a file called fifoname Pipeline files for 
	fifo Famous pipeline ,  It is mainly used in the process communication without blood relationship 
	
	#include <sys/types.h>
	#include <sys/stat.h>
	int mkfifo(const char *pathname, mode_t mode);
	 Successfully returns 0,  Failure to return -1  And set up errno

3.mmap/munmap Memory sharing mapping / Unmapping memory sharing

	mmap Part of the disk file can be mapped directly to memory , In this way, the location in the file directly has the corresponding address ,  Reading and writing files can be done directly with the pointer without the need for read/write function 
	#include <sys/mman.h>
	void *mmap(void *addr, size_t length, int prot, int flags,
				int fd, off_t offset);
			addr :  Specify the location of the mapping , It's usually passed in NULL
			length :  The length of the requested memory 
			prot :  Set page properties  ( page  : Pages, 4096 byte ,  Allocate memory with   page   Assign to units )
				PROT_EXEC				 Whether the page applied by the user has Execution Authority ,  Shared library through mmap Mapping to memory , And allocate this memory exec Executive authority 
				PROT_READ				 Whether the page applied by the user has read permission  
				PROT_WRITE				 Whether the page applied by the user has write permission 
				PROT_NONE				 The page requested by the user is not allowed to be accessed 
			flags : 
				MAP_SHARED			 When the contents of the memory address are changed , Disk files are also modified synchronously , vice versa 
				MAP_PRIVATE			 When the contents of the memory address are changed , The disk file has not changed , vice versa 
			fd :  File descriptor (
			offset :  Offset ,  The offset must be 4096 Integer multiple ( Press   page   The offset )
	int munmap(void *addr, size_t length);
	mmap If it works ,mmap() Returns a pointer to the mapped area , Return if failed MAP_FAILED( namely (void *) -1),  And set up errno
	munmap If successful return 0,  Failure to return -1,  And set up errno
	 Be careful :
		 When it is used for inter process communication, it is generally set as structure 
		 Files for communication ,  Set to temporary file or non temporary file according to requirement 
		 When a bus error is reported ,  Check whether there is a size of 0 Shared files for 

版权声明
本文为[Ran1366]所创,转载请带上原文链接,感谢

Scroll to Top