Working queue principle in Linux Kernel

Source: Internet
Author: User

 

 

It is divided into two parts. The first part is the kernel thread used to execute the function mounted on each node in the work queue, and the second part is the use of the work queue from the driver perspective.

Part 1 worker_thread kernel thread
A thread named worker_thread is created during the Linux system startup. After the thread is created, it is in sleep state. The so-called kernel thread is a scheduling process from the perspective of the scheduler. It is a function in the form of code. The worker_thread thread created by the system is based on a workqueue_struct structure variable (the name of the struct variable is "events ").

Part 2 Use of work queue
1. Only work queues managed by the system keventd are considered.
The driver calls schedule_work to submit a new work node to the work queue. schedule_work internally wakes up the worker_thread kernel thread (making the process status schedulable ). During the next process scheduling, worker_thread is scheduled to run. The main task is to call the function attached to each worker node in the work queue managed by worker_thread, after the call, the worker node is deleted from the task queue. After the function is called on all nodes, worker_thread continues to sleep until schedule_work is called again by a driver.
The difference between working columns created by the driver is that schedule_work calls queue_work (keventd_wq, work) internally ), when calling queue_work, the first parameter of the work queue created by the driver is the work queue created by the driver.

2. The driver uses the self-created work queue
In this case, the driver calls create_workqueue. The principle of this function is basically the same as that of 1, but another kernel process will be created. The kernel function name of this kernel process is still worker_thread, this worker_thread only works in the new working queue of the driver itself.
The usage is as follows:
A. Call create_workqueue to generate the work queue. struct workqueue_struct * my_workqueue = create_workqueue ("my_workqueue") of the driver ");
B. Call queue_work to register a work node in my_workqueue generated in Figure A. queue_work (my_workqueue, work)

In both cases, the kernel thread actually uses a kernel thread named kthreadd to create the worker_thread of the work queue, in essence, kthread_create_list is added to the global list with corresponding kernel thread function nodes. kthreadd is responsible for creating the process framework and then running the kernel thread functions on the nodes added to kthread_create_list.
When a kernel thread function node is added to the kthread_create_list, The kthreadd thread is awakened.

When the waiting queue is empty, worker_thread will sleep in the waiting queue. When the driver calls queue_work and adds a working node to the waiting queue, it will wake up the worker_thread thread sleeping in the waiting queue.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.