Table of contents
Introduction to Interprocess Communication
Purpose of interprocess communication
Inter-process communication development
Classification of interprocess communication
The difference between anonymous pipes and named pipes
Schematic diagram of shared memory
Introduction to Interprocess Communication
Purpose of interprocess communication
Data transfer: One process needs to send its data to another process
Resource sharing: The same resource is shared between multiple processes.
Notification event: A process needs to send a message to another process or a group of processes, informing it (they) that some event has occurred (such as notifying the parent process when the process terminates).
Process control: Some processes want to completely control the execution of another process (such as Debug process). At this time, the control process hopes to be able to intercept all traps and exceptions of another process, and to be able to know its state changes in time.
Inter-process communication development
pipelineSystem V interprocess communication
POSIX interprocess communication
Classification of interprocess communication
pipeline
anonymous pipe
named pipe
System V IPC
System V message queues
System V shared memory
System V semaphores
POSIX IPC
message queue
shared memory semaphore
mutex
condition
variable
read-write lock
pipeline
what is a pipeline
Pipes are the oldest form of interprocess communication in Unix.
We call a stream of data connected from one process to another a "pipe".
anonymous pipe
#include <unistd.h>
Function: create an unnamed pipe
prototype int pipe(int fd[2]);
parameter
fd: file descriptor array, where fd[0] represents the read end, fd[1] represents
the return value:Success returns 0, failure returns an error code
Pipeline read and write rules
when there is no data to read
- O_NONBLOCK disable: The read call is blocked, that is, the process suspends execution and waits until data arrives.
- O_NONBLOCK enable: The read call returns -1, and the errno value is EAGAIN.
when the pipe is full
- O_NONBLOCK disable: The write call is blocked until a process reads the data
- O_NONBLOCK enable: the call returns -1, and the errno value is EAGAIN
If the file descriptors corresponding to all pipe write
ends are closed, read returns 0 If the file descriptors corresponding to all pipe read ends are closed, the write operation will generate a signal SIGPIPE , which may cause the write process to exit
when the data to be written When the amount is not greater than PIPE_BUF, linux will guarantee the atomicity of writing.
When the amount than PIPE_BUF , Linux will no longer guarantee the atomicity of the write.
Pipeline Features
- It can only be used to communicate between processes with a common ancestor (processes with kinship); usually, a pipe is created by a process, and then the process calls fork, and the pipe can be used between the parent and child processes.
- Pipelines provide streaming services
Generally speaking, when the process exits, the pipeline is released, so the life cycle of the pipeline follows the process.
Generally speaking, the kernel will synchronize and mutually exclusive the pipeline operations.
- The pipeline is half-duplex , and data can only flow in one direction ; when two parties need to communicate, two pipelines need to be established
named pipe
- A limitation of pipes is that they can only communicate between processes that share a common ancestor (affinity).
- If we want to exchange data between unrelated processes, we can use FIFO files to do the job, which are often called named pipes.
- Named pipes are a special type of file
Create a named pipe
Named pipes can be created from the command line. The command line method is to use the following command:
mkfifo filename
Named pipes can also be created from the program, the related functions are
int mkfifo(const char *filename file name, mode_t mode file permission);
The difference between anonymous pipes and named pipes
- Anonymous pipes are created and opened by the pipe function.
- Named pipes are created by the mkfifo function and opened with open
- The only difference between FIFOs (named pipes) and pipes (anonymous pipes) is how they are created and opened, but once the work is done, they have the same semantics.
Opening rules for named pipes
- If the current open operation is opening the FIFO for reading
O_NONBLOCK disable: block until a corresponding process opens the FIFO for writing
O_NONBLOCK enable: return success immediately
- If the current open operation is opening the FIFO for writing
O_NONBLOCK disable: block until a corresponding process opens the FIFO for reading
O_NONBLOCK enable: return failure immediately, the error code is ENXIO
system V shared memory
Shared memory areas are the fastest form of IPC. Once such memory is mapped into the address space of the processes sharing it, these inter-process data transfers no longer involve the kernel, in other words processes no longer pass data to each other by executing system calls into the kernel
Schematic diagram of shared memory
shared memory data structure
struct shmid_ds {
struct ipc_perm shm_perm; /* operation perms */
int shm_segsz; /* size of segment (bytes) */
__kernel_time_t shm_atime; /* last attach time */
__kernel_time_t shm_dtime; /* last detach time */
__kernel_time_t shm_ctime; /* last change time */
__kernel_ipc_pid_t shm_cpid; /* pid of creator */
__kernel_ipc_pid_t shm_lpid; /* pid of last operator */
unsigned short shm_nattch; /* no. of current attaches */
unsigned short shm_unused; /* compatibility */
void *shm_unused2; /* ditto - used by DIPC */
void *shm_unused3; /* unused */
}
shared memory function
shmget function
Function: used to create shared memory
Prototype
int shmget(key_t key, size_t size, int shmflg);
parameter
key: name of this shared memory segment
size: size of shared memory
shmflg: composed of nine permission flags, their usage and when creating a file The mode flags used are the same.
Return value: successfully returns a non-negative integer, which is the identification code of the shared memory segment; fails and returns -1
shmat function
Function: Connect the shared memory segment to the process address space
Prototype
void *shmat(int shmid, const void *shmaddr, int shmflg);
Parameter
shmid: Shared memory identifier
shmaddr: Specifies the address of the connection
shmflg: Its two possible values are SHM_RND and SHM_RDONLY
return value: successfully return a pointer to the first section of shared memory; fail to return -1
shmaddr is NULL, the core automatically selects an address if
shmaddr is not NULL and shmflg has no SHM_RND flag, then use shmaddr as the connection address.
If shmaddr is not NULL and shmflg sets the SHM_RND flag, the connected address will be automatically adjusted down to an integer multiple of SHMLBA. Formula: shmaddr -
(shmaddr % SHMLBA)
shmflg=SHM_RDONLY, indicating that the connection operation is used to read-only shared memory
shmdt function
Function: separate the shared memory segment from the current process
Prototype
int shmdt(const void *shmaddr);
parameter
shmaddr: pointer returned by shmat
Return value: return 0 if successful; return -1 if failed
Note: separate the shared memory segment from the current process Not equal to deleting the shared memory segment
shmctl function
Function: used to control shared memory
Prototype
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
parameter
shmid: shared memory identification code returned by shmget
cmd: action to be taken (there are three possible values)
buf: points to A data structure that saves the mode status and access rights of the shared memory
Return value: returns 0 on success; returns -1 on failure
Note: shared memory is not synchronized and mutually exclusive
process mutual exclusion
- Since each process requires shared resources, and some resources need to be used mutually exclusive, so the processes compete to use these resources, the relationship between processes is mutual exclusion of processes
- Certain resources in the system are only allowed to be used by one process at a time, and such resources are called critical resources or mutually exclusive resources.
- A program segment involving mutually exclusive resources in a process is called a critical section
- In terms of characteristics: IPC resources must be deleted, otherwise they will not be cleared automatically unless restarted, so the life cycle of system V IPC resources follows the kernel