


As an embedded development engineer, you have to know about the Linux kernel synchronization mechanism
Preface
Synchronization is the interaction between processes and between processes and system resources. Since the Linux kernel uses multitasking, there must be a synchronization mechanism between multiple processes to ensure coordination.
There are many synchronization mechanisms in the Linux kernel. Today we will focus on the asynchronous and synchronous mechanisms in the kernel, focusing on the asynchronous mechanism in the kernel. The asynchronous mechanism in the kernel is divided into two types: one is the synchronization mechanism of the application layer, that is, the communication between application layer threads; the other is the synchronization mechanism of the kernel.
When a thread enters the kernel state, it can communicate directly with the kernel. There are two threads in the kernel: one is thread A. After it enters the kernel state, it will communicate directly with the kernel and tell it what to do. When it is completed, it will notify the kernel. (We call this operation semi) When a thread enters the kernel state, it will first communicate with the kernel once, and then it can be executed directly.
The synchronization mechanism in the kernel is essentially a communication mechanism between threads, and communication between them is achieved through the synchronization mechanism.
1. Communication between processes
In order to ensure the correctness and consistency of the system, the Linux kernel will use blocking queues to handle inter-process communication during the process of inter-process communication. A blocking queue means that an element in the message queue is created when a message is sent, but not all messages will be sent out. Only when the waiting queue for a certain message is full will it be sent out. If there is no message in the receiver's waiting queue, the notification will be received. If there is a message in the receiver's waiting queue, no notification will be received.
In the kernel, the blocking queue is abstracted, that is, when a process sends a message, it is blocked. Therefore, blocking queue is actually a synchronization mechanism. The blocking queue creates a new object through a specific function, which contains a waiting queue pointer (Push/Pop). When the waiting queue is full, the system will use the object pointed to by the waiting queue pointer as the thread of the first process to issue a notification. That is, the process will be notified before it can continue to perform its tasks.
2. Semaphore
Semaphores can be used to send or receive messages. When a process owns a semaphore, it means that it already owns a semaphore of its own, which is a private variable of its own. This private variable cannot be obtained by other processes. Semaphores are used to represent the number of semaphores owned by a process. When this process owns the semaphore, it can send messages to other processes. This private variable is only allowed to be used by this process itself, and cannot be taken to the processes of other processes.
When a thread has its own semaphore, it can communicate with other threads through shared variables. Shared variables are also used in other threads, and other threads use shared variables to communicate with themselves.
3. Mutex
Mutex is mainly for system resources. Mutexes in the Linux kernel can be divided into two types: shared resources and global mutex resources.
Shared resources are shared between processes. For example, if a process has multiple threads, then each thread can access this shared memory space. Global mutually exclusive resources mean that processes and threads can only access the global memory space where they are located. In a system, mutexes can be used to allow multiple processes to execute in memory at the same time. But if you want to implement multiple processes executing at the same time, you need to use a synchronization mechanism to ensure that all processes can run in the same memory. Using a mutex, a process can only access the global memory space where it is located, but cannot access other memory spaces. But a mutex has a big advantage, that is, there will be no process blocking.
4. Message Queue
The emergence of message queues has greatly expanded inter-process communication. In the kernel, in addition to the synchronization mechanism, there is another asynchronous mechanism, which is the message queue. We all know that the Linux kernel supports message queues. Although there is detailed information about message queues in the kernel, since the kernel does not support user-mode message queues, we still have to start with the application layer to understand message queues.
First of all, let’s understand what a message queue is?
The message queue is a special queue that can meet the synchronization needs between multiple application threads. Message queues are used to provide asynchronous communication between applications and other processes or threads. If we need to communicate asynchronously, we can do it using a message queue. For example, when we call the clear() function, we can directly use a registered message queue.
So how to create a message queue? When we use ext2.json, we can create a message queue using the semaphore command in JAR.json.clear().
5. Shared memory
In shared memory, we use shared locks, but because shared locks share memory with a certain process, when you want to acquire a shared lock, you need to request it from other processes.
Like in the above example, we access shared memory through the volatile keyword. At this time, you are not requesting from other processes, so when you want to acquire this shared lock, you only need to request from other processes. This avoids competition between the two processes and achieves data synchronization.
Since the shared lock shares memory with a process, you must request the process to access its address. The simplest solution for this situation is to use a thread pool.
There is an object called "byte" in the thread pool, which is also a shared lock. When you want to acquire this lock, you only need to send a request to the byte object. At this time, the byte object will send your request to the queue of the thread. When the thread receives the request, it will return a response message to you.
6. Thread pool
The thread pool is a very good thread management tool, which allows multiple threads to run at the same time and can also reduce deadlocks and conflicts between threads. One of its most important features is that it can effectively utilize the system's memory to improve efficiency.
The use of thread pools is very simple, which is to allocate the tasks to be executed to the corresponding thread pools. When the task to be executed is assigned to the corresponding thread pool, it can be executed. Using a thread pool will bring us many benefits:
- Tasks can be completed faster. When a task is assigned to the thread pool, it will be executed in a loop until the execution is completed or the thread is re-created.
- Improved resource utilization efficiency. When a task requires many resources, such as CPU, memory, etc., we can use a thread pool to manage these resources so that each thread has enough CPU and memory to perform the task.
7. Kernel state synchronization mechanism
Two synchronization mechanisms are introduced above, so let’s take a look at the synchronization mechanism in the kernel state. There are four synchronization methods in the kernel state:
- Semaphore: It is used to operate locks. When a thread occupies a lock, it will send a semaphore to the system.
- Semaphore transfer: This mechanism is provided by the operating system. After calling the sys_thread function, it will enter the interrupt state. At this time, when the sys_thread function is called, the semaphore will be passed to the system.
- Mutex lock: This method mainly controls access to resources through priority.
- Mutexes and semaphores are both used to solve synchronization problems between processes.
- Another way to synchronize is through the thread pool. In this process, the thread pool will create a thread to interact with other threads through this thread to achieve synchronization effect.
8. Summary
Through the above analysis, we understand that synchronization is a complex issue. How is synchronization completed in the kernel state?
First of all, there are three processes in the kernel state: these three processes can access each other's resources, and can also synchronize when resources are requested by other processes.
When a process is blocked, all its child processes will take out a child process (or other child process) from the waiting queue and add it to the blocking queue. When all child processes are blocked, there are no child processes in the blocking queue. At this time, other child processes in the waiting queue will add the current thread to the waiting queue. These three processes will not affect each other during the waiting process. The three threads can synchronize with other threads by setting their own priorities.
The above is the detailed content of As an embedded development engineer, you have to know about the Linux kernel synchronization mechanism. For more information, please follow other related articles on the PHP Chinese website!

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

Introduction: Securing the Digital Frontier with Linux-Based Ethical Hacking In our increasingly interconnected world, cybersecurity is paramount. Ethical hacking and penetration testing are vital for proactively identifying and mitigating vulnerabi

The methods for basic Linux learning from scratch include: 1. Understand the file system and command line interface, 2. Master basic commands such as ls, cd, mkdir, 3. Learn file operations, such as creating and editing files, 4. Explore advanced usage such as pipelines and grep commands, 5. Master debugging skills and performance optimization, 6. Continuously improve skills through practice and exploration.

Linux is widely used in servers, embedded systems and desktop environments. 1) In the server field, Linux has become an ideal choice for hosting websites, databases and applications due to its stability and security. 2) In embedded systems, Linux is popular for its high customization and efficiency. 3) In the desktop environment, Linux provides a variety of desktop environments to meet the needs of different users.

The disadvantages of Linux include user experience, software compatibility, hardware support, and learning curve. 1. The user experience is not as friendly as Windows or macOS, and it relies on the command line interface. 2. The software compatibility is not as good as other systems and lacks native versions of many commercial software. 3. Hardware support is not as comprehensive as Windows, and drivers may be compiled manually. 4. The learning curve is steep, and mastering command line operations requires time and patience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.