Workqueue Mechanism Analysis in Linux

Source: Internet
Author: User

I have been in the Hall of Linux for more than a year. Here I want to analyze the various implementation mechanisms of Linux. On the one hand, it is also a warm story for myself, and on the other hand, it will promote communication between everyone, it is best to give you some inspiration. I have been working on hardware for many years. I have been developing specialized software for nearly two years. During this period, I have increasingly considered that software and hardware collaborative design is the mainstream of future development, the boundaries between software and hardware are becoming increasingly vague, and the design concepts of software and hardware are the same and the implementation
Methods vary, and the implementation results are also significantly different. Therefore, it is necessary to design software and hardware collaboratively. With this idea, I want to analyze the Linux I know, especially some mechanisms that I think are essential and important. In addition, during the discussion, I will insert some other OS implementation mechanisms for comparative analysis. I will classify this type of blog articles as "Linux Mechanism Analysis" and hope you will support it.

 

What is workqueue?

The workqueue mechanism in Linux aims to simplify the creation of kernel threads. You can create a kernel thread by calling the workqueue interface. In addition, the number of threads can be created based on the number of CPUs of the current system, so that the transaction processed by the thread can be parallel.

Workqueue is a simple and effective mechanism in the kernel. It obviously simplifies the creation of the kernel daemon and facilitates User Programming,

 

Workqueue mechanism implementation

The workqueue mechanism defines two important data structures. The analysis is as follows:

1,
Cpu_workqueue_struct structure. This structure binds the CPU and kernel threads. During the creation of workqueue, Linux creates cpu_workqueue_struct based on the number of CPUs of the current system. This structure mainly maintains a task queue and a waiting queue for Kernel threads to sleep. It also maintains a task context, that is, task_struct.

2,
The work_struct structure is an abstraction of a task. In this structure, you need to maintain the specific task method, data to be processed, and the task processing time. The structure is defined as follows:

Struct work_struct {


Unsigned long pending;


Struct list_head entry;/* mount the task to the mount point of the queue */


Void (* func) (void *);/* task Method */


Void * data;/* data processed by the task */


Void * wq_data;/* owner of work */


Strut timer_list timer;/* task delay processing timer */

};

When you call the workqueue initialization interface create_workqueue or create_singlethread_workqueue to initialize the workqueue queue, the kernel begins to assign a workqueue object to the user and link it to a global workqueue queue. Linux then allocates the cpu_workqueue_struct object with the same number of CPUs for the workqueue object based on the current CPU condition. Each cpu_workqueue_struct object will have a task queue. Next, Linux assigns a kernel thread for each cpu_workqueue_struct object, that is, the kernel daemon, to process tasks in each queue. At this point, the user calls the initialization interface to initialize the workqueue and returns the pointer of the workqueue.

 

During the initialization of workqueue, the kernel needs to initialize the kernel thread. The registered kernel thread is relatively simple, that is, it constantly scans the task queue corresponding to cpu_workqueue_struct and obtains a valid task from it, then execute the task. Therefore, if the task queue is empty, the kernel daemon will sleep in the wait queue in cpu_workqueue_struct until someone wakes up daemon to process the task queue.

 

After the workqueue Initialization is complete, the Context Environment for running the task is built, but there are no executable tasks, so you need to define a specific work_struct object. Then, add work_struct to the task queue. Linux will wake up daemon to process the task.

 

The workqueue kernel implementation principles described above can be described as follows:

In the workqueue mechanism, a default workqueue queue-keventd_wq is provided, which is created during Linux Initialization. You can directly initialize a work_struct object and schedule it in the queue for more convenient use.

 

Workqueue Programming Interface

Serial number

Interface functions

Description

1

Create_workqueue

Creates a workqueue queue and creates a kernel thread for each CPU in the system. Input parameters:

@ Name: name of workqueue

2

Create_singlethread_workqueue

Creates workqueue and only one kernel thread. Input parameters:

@ Name: workqueue name

3

Destroy_workqueue

Release the workqueue queue. Input parameters:

@ Workqueue_struct: pointer of the workqueue queue to be released

4

Schedule_work

Schedule and execute a specific task. The task will be attached to the workqueue -- keventd_wq provided by Linux system. Enter the following parameters:

@ Work_struct: task object pointer

5

Schedule_delayed_work

The function is similar to schedule_work. If a specific task is executed with a certain delay time, enter the following parameters:

@ Work_struct: task object pointer

@ Delay: Delay Time

6

Queue_work

Schedule and execute a task in the specified workqueue. Input parameters:

@ Workqueue_struct: Specifies the workqueue pointer.

@ Work_struct: task object pointer

7

Queue_delayed_work

Delayed scheduling executes a task in a specified workqueue. The function is similar to queue_work. The input parameter has a delay parameter.

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.