How processes communicate

The process communication method under linux is basically inherited from the process communication method on the Unix platform.

The two major contributors to the development of Unix, AT&T's Bell Labs and BSD (Berkeley Software Distribution Center at the University of California, Berkeley), have different emphases on interprocess communication. The former system improves and expands the early inter-process communication methods of Unix, forming "system V IPC", and the communication process is limited to a single computer; the latter skips this limitation and forms a socket-based interface (socket) interprocess communication mechanism. Linux inherits both, as shown in the figure:

write picture description here
1. Pipes and named pipes: Pipes can be used for communication between processes with affinity. Named pipes overcome the limitation that pipes have no names. Therefore, in addition to the functions of pipes, it also allows no Communication between affinity processes;

2. Signal: Signal is a relatively complex communication method, used to notify the receiving process that some event has occurred. In addition to being used for inter-process communication, the process can also send signals to the process itself; linux supports Unix early signal semantics In addition to the function sigal, it also supports the signal function sigaction whose semantics conform to the Posix.1 standard (in fact, this function is based on BSD. In order to achieve a reliable signal mechanism, BSD can unify the external interface and re-implement the signal function with the sigaction function);

3. Message queue (message queue): message queue is a linked list of messages, including Posix message queue system V message queue. Processes with sufficient permissions can add messages to the queue, and processes granted read permissions can read messages from the queue. The message queue overcomes the shortcomings of the signal carrying a small amount of information, the pipeline can only carry unformatted byte streams, and the buffer size is limited.

4. Shared memory: enables multiple processes to access the same memory space, which is the fastest form of IPC available. It is designed for the inefficient operation of other communication mechanisms. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes.
Semaphore: It is mainly used as a means of synchronization between processes and between different threads of the same process.

5. Socket: A more general inter-process communication mechanism, which can be used for inter-process communication between different machines. Originally developed by the BSD fork of Unix systems, it is now generally portable to other Unix-like systems: both Linux and System V variants support sockets.

Details of the pipeline

A primitive way of IPC is that all processes communicate through a file. For example I write my name and age on a piece of paper (document). Another person reading this piece of paper will know my name and age. He can also write his message on the same piece of paper, and when I read the piece of paper, I can also know the message of others. However, because the hard disk read and write is relatively slow, so this method is very inefficient. So, can we put this piece of paper into memory to increase read and write speed?

In Linux Text Streaming, we have explained how to use pipes to connect multiple processes in the shell.

Similarly, in many programming languages, there are commands to implement similar mechanisms, such as using Popen and PIPE in Python subprocesses, and there are also popen library functions in C language to implement pipes (the pipes in the shell are written according to this) . A pipe is a buffer managed by the kernel, which is equivalent to a note we put into memory. One end of a pipe connects the output of a process. This process will put information into the pipe. The other end of the pipe is connected to the input of a process that takes out the information put into the pipe. A buffer doesn't need to be very large, it is designed as a ring data structure so that pipes can be recycled. When there is no information in the pipe, the process reading from the pipe waits until the process on the other end puts information. When the pipe is full of messages, the process trying to put the message will wait until the process on the other end gets the message. When both processes are terminated, the pipe also automatically disappears.


First, let's talk about what happened after fork.

由fork创建的新进程被称为子进程(child process)。该函数被调用一次,但返回两次。两次返回的区别是子进程的返回值是0,而父进程的返回值则是新进程(子进程)的进程 id。将子进程id返回给父进程的理由是:因为一个进程的子进程可以多于一个,没有一个函数使一个进程可以获得其所有子进程的进程id。对子进程来说,之所以fork返回0给它,是因为它随时可以调用getpid()来获取自己的pid;也可以调用getppid()来获取父进程的id。(进程id 0总是由交换进程使用,所以一个子进程的进程id不可能为0 )。

fork之后,操作系统会复制一个与父进程完全相同的子进程,虽说是父子关系,但是在操作系统看来,他们更像兄弟关系,这2个进程共享代码空间,但是数据空间是互相独立的,子进程数据空间中的内容是父进程的完整拷贝,指令指针也完全相同,子进程拥有父进程当前运行到的位置两进程的程序计数器pc值相同,也就是说,子进程是从fork返回处开始执行的),但有一点不同,如果fork成功,子进程中fork的返回值是0,父进程中fork的返回值是子进程的进程号,如果fork不成功,父进程会返回错误。
可以这样想象,2个进程一直同时运行,而且步调一致,在fork之后,他们分别作不同的工作,也就是分岔了。这也是fork为什么叫fork的原因

至于那一个最先运行,可能与操作系统(调度算法)有关,而且这个问题在实际应用中并不重要,如果需要父子进程协同,可以通过原语的办法解决。


 

常见的通信方式:

1. Pipe: Pipe is a half-duplex communication method, data can only flow in one direction, and can only be used between processes with affinity. The affinity of a process usually refers to a parent-child process relationship.
2. Named pipe FIFO: Named pipe is also a half-duplex communication method, but it allows communication between unrelated processes.
4. Message queue MessageQueue: The message queue is a linked list of messages, stored in the kernel and identified by the message queue identifier. Message Queuing overcomes the shortcomings of less information in signaling, pipes can only carry unformatted byte streams, and limited buffer size.
5. Shared storage SharedMemory: Shared memory is to map a piece of memory that can be accessed by other processes. This shared memory is created by one process, but can be accessed by multiple processes. Shared memory is the fastest way of IPC, and it's designed for the inefficiencies of other interprocess communication methods. It is often used in conjunction with other communication mechanisms, such as signal two, to achieve synchronization and communication between processes.
6. Semaphore Semaphore: A semaphore is a counter that can be used to control access to shared resources by multiple processes. It is often used as a locking mechanism to prevent other processes from accessing a shared resource while a process is accessing the resource. Therefore, it is mainly used as a synchronization method between processes and between different threads in the same process.
7. Socket: Socket is also an inter-process communication mechanism. Unlike other communication mechanisms, it can be used for different and inter-process communication.
8. Signal (sinal): A signal is a relatively complex communication method used to notify the receiving process that an event has occurred.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325681497&siteId=291194637