Kernel Chain list Design

Source: Internet
Author: User

1, write a kernel module, in the module to complete the creation of the kernel chain list, insert, delete, traverse and other operations.

Background knowledge

1. Example analysis of kernel chain list

The largest feature of the kernel list is its versatility, which does not have to be designed as a separate set of methods for manipulating linked lists because of the different data fields in the structure.

The Linux kernel defines the basic structure of the kernel common list list_head type in the linux/lish.h file:

struct List_head

{struct List_head *next,*prev;}

The common use of a kernel list is to string a data structure itself into a linked list, or to link some linked lists to a data structure, and the following examples illustrate the process of stringing a data structure itself into a linked list.

1) Join List_head structure members

Suppose that a example_struct structure needs to be connected to a linked list, so that its structure, with its list_head members, forms a list of structures, such as:

struct example_struct{

struct List_head list;

int priority;

.../* Other Members */

}

A list member in the EXAMPLE_STRUCT structure that is used to string the EXAMPLE_STRUCT structure into a linked list. The load that can be understood as list_head "piggyback" is the example_struct structure.

2) Create List_head structure

Before use, you must apply the link header and initialize the linked header with the Init_list_head macro, which can be used in two ways:

struct List_head example_list;

Init_list_head (&example_list);

Or

List_head (example_list);

Of these, the two macro include/linux/list.h are defined as follows:

#define LIST_HEAD (name) struct List_head name=list_head_init (name)

#define Init_list_head (PTR) do{(PTR)->next= (PTR);(p tr)->pre= (PTR);} while (0)

The macro definition Init_list_head initializes the linked list, which refers to the forward and backward pointers to the linked header. In this way, a example_list-linked header is initialized, and the list element can be added to the list later.

3) Link list and user structure connection

The List_entry macro joins the example_list linked list with the EXAMPLE_STRUCT structure type.

The following line of code is the EXAMPLE_STRUCT structure pointer for the node that is obtained from the Example_list list, where PTR is a pointer in the Example_list list, such as Ptr=example_list->next.

struct Example_struct *node=list_entry (ptr,struct example_struct,list);

The macro definition in the preceding line of code list_entry maps a LIST_HEAD structure pointer back to a pointer to the struct example_struct, which is the host structure of the list_head. Its macro is defined in Include/linux/list.h

#define List_entry (Ptr,type,member) container_of (Ptr,type,member)

The function of List_entry is to get the structure of the node in the linked list, and its parameter means:

PTR is a struct list_head structure element pointer in a linked list. Type is a user-defined struct type, which contains struct list_head struct members. Member a struct list_head struct member name in a user-defined structure.

4) traversing the linked list

The following uses the List_entry macro to traverse the linked list to get the linked list pointer, and then mapping back from the linked list pointer to the corresponding structure example_struct pointer, and then to its member priority operation, function example_add_ The function of entry is to add new struct members to the list.

Mylist.c

Makefile

After installing the module Insmod Mylist.ko

.........................

The module initialization function Mylist_init is called, in the module load function, creates a kernel linked list, the data field of the list is name and school number, the pointer field is List_head, then uses List_add to insert the node, using List_for_each to traverse the entire list, Use List_entry to find data structure pointers. Since the List_add insertion node uses the head interpolation method, which means that each time it is inserted at the head node, the first inserted node is printed after the duration.

The kernel list is one of the data structures that we have to master in kernel learning, which can be said to be the most widely used structure in the kernel. In addition to being able to use the kernel list correctly, the understanding of the various operational methods in the kernel list is what we need to do. By using source insight to view the kernel source code under Include/linux/list.h analysis of the kernel linked list initialization, insertion, deletion, removal, merging and traversal of the specific implementation methods. If you really understand the kernel list, it will not only help you learn the kernel, it will continue to be used in your application, but also make your program more robust.

Another function:

Kernel Chain list Design

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.