Interprocess communication
Why do processes need to communicate?
Shared data, data transmission, message notification, process control
What are the types of communication between processes?
First of all, according to the previous knowledge, the user address space between processes is independent of each other and cannot be accessed to each other. However, the kernel space is shared, so the communication between processes should pass through the kernel.
Taking Linux as an example, this paper introduces several common communication modes between Linux processes: shared memory, pipeline, message queue, semaphore and signal.
The Conduit
If you have studied linux commands, you must have seen this vertical bar.
$ ps auxf | grep mysql
The vertical line | in the above command is a pipe. Its function here is to take the output of the previous command (ps auxf) as the input of the next command (grep mysql).
It can be seen that pipeline data transmission is one-way. If we want to communicate with each other, we need at least two pipes.
The pipeline in the above example has no name, so we call it an anonymous pipeline, which will be destroyed when used up.
Another kind of pipeline is named pipeline, also known as FIFO, which means first in first out.
Before using the named pipe, you need to create it with the mkfifo command and add the name of the pipe.
$ mkfifo myPipe
The pipe name is myPipe, which also exists in the form of a file in Linux:
w-rw-r--. 1 iron2222 iron2222 0 Dec 4 22:20 myPipe
We try to write data to this pipeline:
$ echo "hello" > myPipe //Write the data in //You'll find it stopped and didn't respond
Because the command can exit normally only after the data in the pipeline is read:
$ cat < myPipe hello
Note that you must input the command on another terminal and keep the original terminal interface unchanged. In this way, it will be obvious that the original terminal command can exit normally after executing the command.
It can be seen from here that this way of pipeline is inefficient and not suitable for frequent data exchange.
Message queue
Communication mode of message queue: process A sends messages to process B. process A can directly return the messages after putting them in the corresponding message queue. Process B can read them when needed.
The essence of a message queue is a linked list of messages stored in the kernel. The sender and receiver should agree on the data type and size of the message body. After reading, the kernel will delete the message body.
Bad news: in the process of message queue communication, there is data copy overhead between user state and kernel state.
Shared memory
The mechanism of shared memory is to take out a virtual address space and map it to the same physical memory. High reading efficiency.
The disadvantage is that if two processes write data to a physical address at the same time, it will cause overwrite.
Semaphore
In order to prevent multiple processes competing for shared resources like shared memory, a protection mechanism is needed so that shared resources can only be accessed by one process at any time.
Semaphore is actually an integer counter, which is mainly used to realize mutual exclusion and synchronization between processes, rather than to cache the data of inter process communication.
The initial value is 1. If process A wants to use it, it becomes 0. If process B wants to use it, it becomes - 1, blocking process B.
It is equivalent to a flag that tells the process whether the shared resource is free.
signal
Signal is generally used for the communication between processes under abnormal conditions. It is an asynchronous signal, that is, a number.
For example, in Linux, dozens of signals are provided for different events, which represent different meanings:
$ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX
Take a common example:
In the shell terminal, we can use some shortcut keys to send signals to the process:
- Ctrl+C will generate SIGINT signal to terminate the process;
- Ctrl+Z will generate SIGTSTP signal to stop the process, but the process is not over.
You can also use the kill Command, but only if you know the PID number of the process.
- kill -9 1050, which means sending 9) SIGKILL signal to the process with PID 1050 and ending the process immediately.
The only asynchronous communication mechanism. The process needs to set corresponding listening and processing for the signal and perform relevant operations.
Socket
If you want to communicate with processes on different hosts across the network, you need to use Socket.