Inter-process communication 10 minutes Linux Quick Start

In the Linux environment to run the program, whether it is an icon on the desktop, or a knock on the command line shell command, Linux systems will put our program "packaging" in the form of a process, and is scheduled to run: each process in turn take up CPU time to execute, it is time to give other processes, round-robin, as long as the rotation speed was fast enough, it will give the user an illusion: we listen to songs on the computer while typing, I feel more programs in the run concurrently. During operation of different processes, according to business needs, will communicate with each other processes: for example the transmission data, the transmission signal and the like.

Interprocess communication under the Linux environment (Inter-Process Communication, referred to as IPC) has a variety of tools may be used, such as: unknown conduit pipe, the FIFO named pipes, message queues, shared memory, semaphores, signals, file locking, socket and the like. These tools IPC system call or in the form of the API library functions available to users: the user data can be transmitted using the API, the synchronization process between different processes or transmit signals. For example, we can use ctrl + C key combination to terminate a process or use the shell command kill 3567 to kill a process pid for the process of 3567, these are actually sending a signal to the process, and process the received signal processing process.

IPC different tools, different occasions, advantages and disadvantages. In order to make better use of them, we must not only master the use of the API interface, but also their communication mechanism, the kernel implementation principles have a general understanding. Only with the implementation of the principles underlying, we can understand the advantages and disadvantages of each IPC communication tools, as well as their usage scenarios. In the end you want to really understand how the communication between the Linux process, different processes at first wants to see Linux during operation, the memory is present in what form, and how between the Linux kernel and is interactive . Want to understand this, we also need to compile the program under the Linux environment, the implementation process have a rough idea.



Compile and execute a program

When we click on an icon on the desktop, or tap a shell command when run from the command line, Linux system will these executable file is loaded into memory, and packaged into a process before you can participate in the operating system scheduler to run. That operating system is how to load it?

1564654740303.png



First of all, we have written in the C language source code is compiled into an executable file (ELF). Executable by the various sub-section (section): the code segments, data segments, and so the BSS section. Our different C program code is compiled into a different segment: function implementation is put into the code segment; global variables, static local variables are stored in the data segment; uninitialized global variables are placed in the BSS segment ... ...

loader loads into memory execution, usually two steps: the first step will be the first to use fork to create a child process, each child has a 4G virtual address space. The second step, from a position on the disk of the software installation, the head of the executable file to read: ELF header, obtaining the individual segments, and then were loaded into different segments at different positions of the process space, as shown in FIG.

In a computer system, usually have multiple processes running simultaneously, each process through which almost all the way above the fork-exec to run. When running processes and more, each process wants to occupy the CPU, exclusive CPU, CPU resource is not enough, this time the operating system starts debut. Operating system acts as a scheduler to coordinate the various processes take turns running CPU-intensive.

1564656367916.png



如上图所示,对于用户运行的不同进程,在内核空间,会有一个专门的数据结构来表示:task_struct。这个结构体描述了进程的各种信息,不同的task_sruct结构体通过链表串起来,内核通过链表就可以对这些进程进行管理。操作系统会有一个叫调度器的核心组件,每隔一段时间(一般是毫秒级)会有一个定时器中断,Linux调度器就会把正在运行的进程从CPU上赶下来,接着让另一个进程去执行,如此反复,周而复始。只要CPU的速度足够快、轮流执行的频率足够高,对于用户来说,就感觉多个程序同时运行。




2 进程的地址空间

每一个进程,都有一个4G大小、独立的虚拟地址空间,然后通过页表映射,映射到物理内存的不同位置上。CPU执行不同的进程时,根据每个进程的映射页表,就会到其对应的物理内存上一条一条地取指令、翻译指令、运行指令。

1564656641206.png



如上图中的进程A和进程B,它们在内存中有相同的4G虚拟地址空间,但是每个进程通过各自的页表映射,就映射到了物理内存中的不同位置。也就是说,每个进程的虚拟地址空间虽然是相同的,但是它们在物理内存空间上却是相同隔离的、相互独立的。在每个进程的4G虚拟地址空间中,[0,3G]这段地址空间是每个进程独有的,而[3G,4G]这段空间是被内核占用的,不同进程的[3G,4G]这段空间都被内核占用。内核本身在运行时,在物理内存上也会有自己单独的存储空间。

1564657079518.png






3 Linux进程间通信的三种方法


通过上面的学习我们可以看到,用户空间的不同进程,它们在时空上是相互隔离、相互独立的,如同黑夜和白天,太阳和月亮,永远不会见面,老死不相往来。但万事没有绝对,各个进程之间如果真想通信,还是有方法的,如下图所示。

1564658539689.png



用户空间的每个进程虽说在物理内存空间上是相互隔离、相互独立的,但通过内核空间这一共享区域,它们还是可以相互通信的。只要内核愿意、提供一些空间,不同的进程之间就可以对这块内存空间读写数据,达到进程间通信的目的。磁盘也是公共存储空间,不同进程也可以通过往磁盘上某个指定的文件读写数据完成进程间的通信。除此之外,不同的进程之间,如果事先商量好,也可以绕过内核,通过内存映射,在物理内存上建立一片共享内存,直接进行通信。




4 无名管道pipe通信机制


以Linux的无名管道pipe通信机制为例:无名管道常用于有血缘关系的进程之间的通信,我们可以通过pipe系统调用去创建一个管道:
int pipe (int pipefd[2]);

该函数会创建一个管道,这个管道有两个文件描述符,一个用来读,一个用来写,不同进程可以通过读写描述符对这个管道进行读写,达到进程间通信的目的。


1564660602383.png



无名管道在内核中的实现其实很简单,就是Linux内核空间的一片缓冲区,通过pipefs机制把它封装成一个文件的形式,留出文件的读写接口:文件描述符给用户空间进程。用户空间的不同进程通过这一对读写描述符就可以对管道进行读写。

1564660740725.png






Inter-process communication means 5 more


except unnamed pipe, Linux offers a lot of inter-process communications means can be used, such as: the FIFO named pipes, semaphores, message queues, shared memory, signals signal, socket, Dbus like. IPC different tools have their own advantages and disadvantages, using the occasion. For example, anonymous pipes can only be used for interprocess communication kinship, Named Pipes PIPE solve this limitation, supports communication between any two processes; message queues can support communication data format, the maximum shared memory efficiency, but we need to talk use of semaphores, binding lock synchronization mechanism; signal is mainly used for asynchronous inter-process communication, and only an asynchronous communication mechanism.

Each IPC communication tool has its own advantages and disadvantages, and limitations of using the occasion, we can only fully understand and master the use of various IPC tools, know their strengths and weaknesses, to the actual work needed to select the appropriate communication mechanism . In addition to these POSIX / system V IPC standard interface definition tool, Linux system also extends the API own unique number, such as signalfd, timerfd etc., to solve the defect signal communication mechanism. To learn more about the use and implementation of these IPC mechanisms tool interfaces, can follow the tutorial: "Linux System Programming" on page 05: inter-process communication, has now finished recording, released in 51CTO College, please click: Linux System Programming 05: inter-process communication


Guess you like

Origin blog.51cto.com/zhaixue/2426327