Table of Contents
Preface" >Preface
1. Communication between processes" >1. Communication between processes
2. Semaphore" >2. Semaphore
3. Mutex" >3. Mutex
4. Message Queue" >4. Message Queue
5. Shared memory" >5. Shared memory
6. Thread pool" >6. Thread pool
7. Kernel state synchronization mechanism" >7. Kernel state synchronization mechanism
8. Summary" >8. Summary
Home System Tutorial LINUX As an embedded development engineer, you have to know about the Linux kernel synchronization mechanism

As an embedded development engineer, you have to know about the Linux kernel synchronization mechanism

Feb 12, 2024 pm 02:50 PM
linux linux tutorial linux system linux command shell script Synchronization mechanism embeddedlinux Getting started with linux linux learning

作为嵌入式开发工程师,关于Linux kernel同步机制你不得不知道

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:

  1. 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.
  2. 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:

  1. Semaphore: It is used to operate locks. When a thread occupies a lock, it will send a semaphore to the system.
  2. 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.
  3. Mutex lock: This method mainly controls access to resources through priority.
  4. Mutexes and semaphores are both used to solve synchronization problems between processes.
  5. 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install software on Linux using the terminal? How to install software on Linux using the terminal? Aug 02, 2025 pm 12:58 PM

There are three main ways to install software on Linux: 1. Use a package manager, such as apt, dnf or pacman, and then execute the install command after updating the source, such as sudoaptininstallcurl; 2. For .deb or .rpm files, use dpkg or rpm commands to install, and repair dependencies when needed; 3. Use snap or flatpak to install applications across platforms, such as sudosnapinstall software name, which is suitable for users who are pursuing version updates. It is recommended to use the system's own package manager for better compatibility and performance.

The Ultimate Guide to High-Performance Gaming on Linux The Ultimate Guide to High-Performance Gaming on Linux Aug 03, 2025 am 05:51 AM

ChoosePop!_OS,Ubuntu,NobaraLinux,orArchLinuxforoptimalgamingperformancewithminimaloverhead.2.InstallofficialNVIDIAproprietarydriversforNVIDIAGPUs,ensureup-to-dateMesaandkernelversionsforAMDandIntelGPUs.3.EnabletheperformanceCPUgovernor,usealow-latenc

What are the main pros and cons of Linux vs. Windows? What are the main pros and cons of Linux vs. Windows? Aug 03, 2025 am 02:56 AM

Linux is suitable for old hardware, has high security and is customizable, but has weak software compatibility; Windows software is rich and easy to use, but has high resource utilization. 1. In terms of performance, Linux is lightweight and efficient, suitable for old devices; Windows has high hardware requirements. 2. In terms of software, Windows has wider compatibility, especially professional tools and games; Linux needs to use tools to run some software. 3. In terms of security, Linux permission management is stricter and updates are convenient; although Windows is protected, it is still vulnerable to attacks. 4. In terms of difficulty of use, the Linux learning curve is steep; Windows operation is intuitive. Choose according to requirements: choose Linux with performance and security, and choose Windows with compatibility and ease of use.

Understanding RAID Configurations on a Linux Server Understanding RAID Configurations on a Linux Server Aug 05, 2025 am 11:50 AM

RAIDimprovesstorageperformanceandreliabilityonLinuxserversthroughvariousconfigurations;RAID0offersspeedbutnoredundancy;RAID1providesmirroringforcriticaldatawith50�pacityloss;RAID5supportssingle-drivefailuretoleranceusingparityandrequiresatleastthre

Linux how to enable and disable services at boot Linux how to enable and disable services at boot Aug 08, 2025 am 10:23 AM

To manage the startup of Linux services, use the systemctl command. 1. Check the service status: systemctlstatus can check whether the service is running, enabled or disabled. 2. Enable the service startup: sudosystemctlenable, such as sudosystemctlenablenginx. If it is started at the same time, use sudosystemctlenable--nownginx. 3. Disable the service startup: sudosystemctldisable, such as sudosystemctldisablecups. If it is stopped at the same time, use sudosystemctldisabl

Linux how to list all running processes Linux how to list all running processes Aug 08, 2025 am 06:42 AM

Usepsauxforacompletesnapshotofallrunningprocesses,showingdetailedinformationlikeUSER,PID,CPU,andmemoryusage.2.Usetoporhtopforreal-timemonitoringofprocesseswithdynamicupdates,wherehtopoffersamoreintuitiveinterface.3.UsepgreporpidoftoquicklyfindthePIDs

How to clean up your Linux system How to clean up your Linux system Aug 22, 2025 am 07:42 AM

Removeunusedpackagesanddependencieswithsudoaptautoremove,cleanpackagecacheusingsudoaptcleanorautoclean,andremoveoldkernelsviasudoaptautoremove--purge.2.Clearsystemlogswithsudojournalctl--vacuum-time=7d,deletearchivedlogsin/var/log,andempty/tmpand/var

Linux how to view the contents of a file Linux how to view the contents of a file Aug 19, 2025 pm 06:44 PM

ToviewfilecontentsinLinux,usedifferentcommandsbasedonyourneeds:1.Forsmallfiles,usecattodisplaytheentirecontentatonce,withcat-ntoshowlinenumbers.2.Forlargefiles,uselesstoscrollpagebypageorlinebyline,searchwith/search_term,andquitwithq.3.Usemoreforbasi

See all articles