Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Getting started with Linux Interprocess Communication for 10 minutes

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Running a program in Linux environment, whether it is clicking an icon on the desktop or tapping a shell command under the command line, the Linux system will "package" our program into a process, and then schedule it to run: each process takes CPU for a period of time to execute, and when the time is up, the time slice rotates, as long as the rotation speed is fast enough. It will give users an illusion: we listen to music while typing on the computer, feeling that multiple programs are running at the same time. When different processes are running, according to the business needs, the processes will communicate with each other, such as transmitting data, sending signals and so on.

There are many tools for Inter-Process Communication (IPC) in Linux environment, such as unnamed pipe pipe, named pipe FIFO, message queue, shared memory, semaphore, signal, file lock, socket and so on. These IPC tools are provided to users in the form of system calls or library functions API: users can use these API to transfer data, synchronize processes, or send signals between different processes. For example, we can use the ctrl+C key combination to terminate a process, or use the shell command kill 3567 to kill a process with a pid of 3567. These are actually the process of sending a signal to the process, receiving the signal and processing it.

Different IPC tools are used in different situations and have their own advantages and disadvantages. In order to use them better, we should not only be proficient in the use of API interfaces, but also have a general understanding of their communication mechanism and kernel implementation principles. Only by mastering the underlying implementation principles, can we understand the advantages and disadvantages of each IPC communication tool, as well as where they are used. To really understand how Linux processes communicate with each other, we must first understand how different processes under Linux exist in memory when they are running, and how they interact with the Linux kernel. To understand this, we also need to have a general understanding of the compilation and execution of programs in the Linux environment.

1 compilation and execution of the program

When we click an icon on the desktop, or click a shell command under the command line to run, the Linux system will load these executable files into memory and encapsulate them into a process before participating in the scheduling and running of the operating system. So how is the operating system loaded?

First of all, the C source code we wrote is compiled into an executable file (ELF). The executable file is composed of different section: code segment, data segment, BSS segment and so on. Different code in our C program will be compiled into different segments: function implementations will be put into code segments; global variables, static local variables will be put into data segments; uninitialized global variables will be put into BSS segments.

The loader loader executes the program into memory in two steps: the first step is to use fork to create a child process, each with a 4G virtual address space. The second step is to read the header of the executable file: ELF header from the location of the software installation on disk, get the information of each segment, and then load different segments into different locations in the process space, as shown in the figure above.

In a computer system, there are usually multiple processes running at the same time, almost all of which are run through the above fork-exec mode. When there are more processes running, it is not enough for each process to occupy CPU and exclusive CPU,CPU resources, and then the operating system begins to debut. The operating system acts as a scheduler, coordinating each process to take turns to occupy the CPU operation.

As shown in the figure above, for different processes that users are running, there is a special data structure in kernel space: task_struct. This structure describes all kinds of information about processes. Different task_sruct structures are strung together through linked lists, and the kernel can manage these processes through linked lists. The operating system will have a core component called the scheduler, and every once in a while (usually milliseconds), the Linux scheduler will drive the running process off the CPU and let another process execute, over and over again. As long as the speed of CPU is fast enough and the frequency of rotation execution is high enough, for users, it feels like multiple programs are running at the same time.

2 address space of the process

Each process has a 4G-sized, independent virtual address space, which is then mapped to different locations of physical memory through page table mapping. When CPU executes different processes, it fetches instructions, translates instructions, and runs instructions one by one to its corresponding physical memory according to the mapping page table of each process.

Such as process An and process B in the figure above, they have the same 4G virtual address space in memory, but each process is mapped to a different location in physical memory through its own page table mapping. In other words, although the virtual address space of each process is the same, they are the same isolated and independent of each other in the physical memory space. In the 4G virtual address space of each process, this address space is unique to each process, while this space is occupied by the kernel, and the space of different processes is occupied by the kernel. When the kernel itself is running, it also has its own separate storage space on physical memory.

3 three methods of Linux inter-process communication

Through the above study, we can see that the different processes of user space are isolated and independent of each other in time and space, just like night and day, the sun and the moon, never meet each other. But everything is not absolute, if the various processes really want to communicate, there are ways, as shown in the following figure.

Although each process in user space is isolated and independent of each other in physical memory space, they can still communicate with each other through the shared area of kernel space. As long as the kernel is willing to provide some space, different processes can read and write data to this memory space to achieve the purpose of inter-process communication. The disk is also a common storage space, and different processes can also complete the communication between processes by reading and writing data to a specified file on the disk. In addition, if discussed in advance, different processes can also bypass the kernel, establish a piece of shared memory on physical memory through memory mapping, and communicate directly.

4 pipe communication mechanism of unnamed pipeline

Take Linux's unnamed pipeline pipe communication mechanism as an example: unnamed pipes are often used for communication between consanguineous processes. We can create a pipeline through pipe system calls:

Int pipe (int pipefd [2])

This function creates a pipe that has two file descriptors, one for reading and one for writing. Different processes can read and write to the pipe through the read-write descriptor for the purpose of inter-process communication.

The implementation of the anonymous pipe in the kernel is actually very simple, which is a buffer of the Linux kernel space. It is encapsulated into a file form through the pipefs mechanism, leaving a file read-write interface: the file descriptor is given to the user space process. Different processes in user space can read and write to the pipeline through this pair of read-write descriptors.

5 more tools for interprocess communication

In addition to unnamed pipes, Linux provides many tools for interprocess communication, such as named pipe FIFO, semaphores, message queues, shared memory, signal signal, socket, Dbus, and so on. Different IPC tools have their own advantages and disadvantages and situations of use. For example, unnamed pipe can only be used for inter-process communication of kinship, named pipe PIPE solves this limitation and supports communication between any two processes; message queue can support communication with data format, and shared memory is the most efficient, but it needs to be used in combination with semaphores, locks and other synchronous mechanisms; signals are mainly used for asynchronous communication between processes, and it is also the only asynchronous communication mechanism.

Each IPC communication tool has its own advantages and disadvantages, usage occasions and limitations. Only when we comprehensively understand and master the use of each IPC tool and know its advantages and disadvantages, can we choose the appropriate communication mechanism according to the needs in the actual work. In addition to these IPC tools defined by POSIX/system V standard interface, Linux system also extends some unique API, such as signalfd, timerfd, etc., to solve some defects of signal communication mechanism. To learn more about the use and implementation mechanism of these IPC tool interfaces, you can follow the tutorial: "Linux system programming" No. 05: interprocess Communication, which has been recorded and released at the 51CTO Institute. For more information, please click: Linux system programming issue 05: interprocess Communication.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report