Linux Kernel chain List

Source: Internet
Author: User

Recently looking at the code of the Linux kernel, the first to know is the data structure used internally by Linux: Linked list.

In fact, the list of links used by the Linux kernel is a two-way circular list. Like a regular linked list, each of its nodes contains two domains: data fields and pointer fields, where the data fields are clear, that is, the user's own data, there is nothing to say, specifically to see what data the user wants to organize through the linked list. The pointer field is a struct as follows:

struct list_head{      struct list_head   *next, *prev;}
View Code

As can be seen from the above, this list is a doubly linked list because its pointer field contains two pointers, a pointer field pointing to the previous node, and a pointer field pointing to the latter node. This is very different from the normal list, the general list of the pointer field is directly to the other node of the data field, when we get to the node's address through the pointer field, we can directly obtain data. But the problem is that once a linked list node is created, its data field is fixed, so that when a user also wants to use the linked list, it must redefine the node data field, which makes the list of the generality of the decline. The list of the Linux kernels is used to define the linked list in order to make the list more generic: The pointer field of the node points to the pointer field of the other node, and the data field can be defined by the user. As the following definition:

struct student{    char name[];     int 中文版;     int Math;     int Chinese;     struct list_head list;};
View Code

In the definition above, the data field is the student's data, and the pointer field is a struct, which points to the next and previous node's pointer fields, respectively. In this case, the data domain can be defined by the user, only need to join the pointer field.
But it's not easy for a user to use a linked list to get the data for a node. Because the pointer field points to a data field that is not another node. But don't worry, the kernel provides functions that allow us to get the data through the pointer field. The following is a list of functions that are commonly used by the kernel to manipulate lists:

Init_list_head (): Create a linked list;

void List_add (); Insert a node in the linked list header

List_add_tail (); Inserts a node at the end of the list

List_add_tail (); Delete node

List_entry (); Remove node

List_for_each (); Traversing a linked list

The above just gives the name of the operation function, not the specific function header. You can find specific function definitions and implementations in the kernel code. These functions are defined in include/linux/list.h.

Linux Kernel chain List

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.