preface
Interprocess communication and interthread communication are often asked during interviews
summary
Process communication can be divided into the following categories
-
The pipe
-
The message queue
-
The Shared memory
-
A semaphore
-
signal
-
socket
The pipe
Anonymous pipe
ps auxf | grep mysql
Copy the code
Command line on the vertical bar “|” is a pipe, its function is to the output of a command (ps auxf) before and after as a command input (grep mysql), from the functional description, it can be seen that pipeline is one-way transmission data, if you want to communicate with each other, we need to create two pipes.
A named pipe
- Named pipes are files
$ mkfifo myPipe
Copy the code
- MyPipe is the name of the pipe, based on the Linux concept that everything is a file, so the pipe also exists as a file. We can use ls to see that the type of the file is P, which means pipe
$ ls -l\
prw-r--r--. 1 root root 0 Jul 17 02:45 myPipe
Copy the code
- We write data to the pipe myPipe:
$ echo "hello" > myPipe // Write data to pipe \
// 停住了 ...
Copy the code
Open another thread to read data
$ cat < myPipe // Read the data in the pipe \
hello
Copy the code
Realize the principle of
- Anonymous pipes are created through the following system call:
int pipe(int fd[2])
Copy the code
-
This means that an anonymous pipe is created and two descriptors are returned, one is the pipe’s read-side descriptor fd[0] and the other is the pipe’s writer-side descriptor fd[1]. Note that this anonymous pipe is a special file that exists only in memory, not in the file system.
-
A pipeline is a string of caches in the kernel. The data that is written from one end of the pipe is actually cached in the kernel, and the data that is read from the other end is read from the kernel. In addition, the data transported by the pipe is unformatted stream and is limited in size.
-
You may be wondering, since both of these descriptors are in the same process and do not serve the purpose of interprocess communication, how to make the pipe span two processes?
-
We can use fork to create a child process that copies the file descriptor of the parent process, so that each process has two “fd[0] and fd[1]”, and both processes can communicate across processes by writing and reading from the same pipe file using their FDS.
- Execute in the shell
A | B
When A command is executed, process A and process B are both child processes created by the shell. There is no parent relationship between process A and process B. The parent processes of both processes are shell.
-
So, in the shell through “|” anonymous pipe connect multiple commands together, in fact, that is, to create a more child processes, so when we write a shell script, can use a pipe fix things, don’t use a pipe, so that we can reduce the child process creation overhead.
-
We can know that for an anonymous pipe, its communication scope is the process that has a parent-child relationship. Since the pipe has no entity, it has no pipe file, so it can only be used by copying the parent fd file descriptor through fork.
-
In addition, with named pipes, it is possible for unrelated processes to communicate with each other. Because of the command pipe, a device file of type pipe is created in advance. As long as the process uses this device file, it can communicate with each other.
reference
- Mp.weixin.qq.com/s/mblyh6XrL…