Linux C語言鏈表深度解析與實戰技巧
- 一、鏈表基礎概念與內核鏈表優勢
- 1.1 為什么使用鏈表?
- 1.2 Linux 內核鏈表與用戶態鏈表的區別
- 二、內核鏈表結構與宏解析
- 常用宏/函數
- 三、內核鏈表的優點
- 四、用戶態鏈表示例
- 五、雙向循環鏈表在內核中的實現優勢
- 5.1 插入效率
- 5.2 安全遍歷刪除
- 六、典型應用場景
- 七、調試技巧與常見陷阱
- 7.1 打印鏈表內容
- 7.2 常見錯誤
- 八、實戰案例:Linux 內核模塊中的鏈表使用
- 8.1 模塊源碼
- 8.2 Makefile 編譯模塊
- 8.3 插入與卸載模塊
- 九、總結:開發建議
本文系統講解 Linux 下 C 語言鏈表的使用原理、最佳實踐及內核模塊中的實戰示例,適合嵌入式開發者、驅動工程師和系統軟件開發者。
一、鏈表基礎概念與內核鏈表優勢
1.1 為什么使用鏈表?
- 動態插入/刪除效率高(相比數組不需要整體移動元素)
- 內存利用率高(按需分配)
- 常用于隊列、任務調度、資源池、驅動設備列表等場景
1.2 Linux 內核鏈表與用戶態鏈表的區別
項目 | 用戶態實現 | 內核態實現 |
---|---|---|
指針結構 | 自定義指針結構 | 使用 struct list_head |
安全性 | 程序員自行維護 | 提供安全宏/內聯函數 |
插入/刪除API | 手動實現 | 提供統一接口如 list_add |
迭代方式 | 手動循環 | 宏如 list_for_each_entry |
二、內核鏈表結構與宏解析
struct list_head {struct list_head *next, *prev;
};
常用宏/函數
INIT_LIST_HEAD(ptr)
list_add(new, head)
:頭插法list_add_tail(new, head)
:尾插法list_del(entry)
list_empty(head)
list_for_each_entry(pos, head, member)
list_for_each_entry_safe(pos, n, head, member)
三、內核鏈表的優點
- 雙向循環結構:從任意節點出發都能遍歷完整鏈表
- 插入刪除不涉及內容拷貝:僅修改指針
- 接口統一、安全可靠:可結合
container_of
獲取真實結構體指針
四、用戶態鏈表示例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>typedef struct student {int id;char name[20];struct student *next;
} student_t;void add_student(student_t **head, int id, const char *name) {student_t *new_node = malloc(sizeof(student_t));new_node->id = id;strncpy(new_node->name, name, sizeof(new_node->name));new_node->next = *head;*head = new_node;
}void print_students(student_t *head) {while (head) {printf("ID: %d, Name: %s\n", head->id, head->name);head = head->next;}
}
五、雙向循環鏈表在內核中的實現優勢
5.1 插入效率
在頭部插入只需兩次指針操作:
list_add(&new_node->list, &head);
5.2 安全遍歷刪除
list_for_each_entry_safe(ptr, tmp, &head, list) {list_del(&ptr->list);kfree(ptr);
}
六、典型應用場景
場景 | 描述 |
---|---|
設備驅動管理 | 管理設備結構體(如 net_device) |
定時器鏈表 | 內核定時器統一調度 |
slab 分配器鏈表 | 管理對象緩存池 |
進程調度/等待隊列 | 管理 PCB / wait queue |
協議棧管理 | TCP/UDP 的 socket 鏈表 |
七、調試技巧與常見陷阱
7.1 打印鏈表內容
#define print_list(head) \list_for_each_entry(ptr, head, list) \printk(KERN_INFO "Node: %s\n", ptr->name);
7.2 常見錯誤
- 忘記初始化
INIT_LIST_HEAD
- 使用已釋放節點(UAF)
- 非安全刪除遍歷(未使用
list_for_each_entry_safe
)
八、實戰案例:Linux 內核模塊中的鏈表使用
8.1 模塊源碼
// mylist_module.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/list.h>MODULE_LICENSE("GPL");struct student {int id;char name[20];struct list_head list;
};static LIST_HEAD(student_list);static int __init mylist_init(void)
{int i;struct student *stu;printk(KERN_INFO "Loading student list module...\n");for (i = 1; i <= 5; ++i) {stu = kmalloc(sizeof(*stu), GFP_KERNEL);stu->id = i;snprintf(stu->name, sizeof(stu->name), "Student%d", i);INIT_LIST_HEAD(&stu->list);list_add_tail(&stu->list, &student_list);}return 0;
}static void __exit mylist_exit(void)
{struct student *stu, *tmp;printk(KERN_INFO "Unloading student list module...\n");list_for_each_entry_safe(stu, tmp, &student_list, list) {list_del(&stu->list);kfree(stu);}
}module_init(mylist_init);
module_exit(mylist_exit);
8.2 Makefile 編譯模塊
obj-m += mylist_module.oall:make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modulesclean:make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
8.3 插入與卸載模塊
$ make
$ sudo insmod mylist_module.ko
$ dmesg | tail -n 10
$ sudo rmmod mylist_module
$ dmesg | tail -n 10
九、總結:開發建議
建議項 | 內容 |
---|---|
一定初始化鏈表頭 | 使用 INIT_LIST_HEAD 初始化 |
刪除節點用安全宏 | list_for_each_entry_safe 防止遍歷時刪除崩潰 |
內存管理責任明確 | kmalloc/kfree 成對使用 |
多線程環境加鎖 | 必要時配合 spinlock 或 mutex |
定位 bug 用 printk | 輸出 prev , next , 數據字段調試鏈表結構 |
本文涵蓋了從用戶態鏈表構造到 Linux 內核模塊鏈表的實戰應用,幫助你在驅動開發和內核開發中熟練掌握鏈表的構造與使用。