我正在尝试使用 github/torvalds/linux/blob/master/include/linux/list.h 在用户空间中,其用户空间实现可在 gist.github/roychen/1710968
I am trying to use linux kernel doubly linked-list implementation mentioned in github/torvalds/linux/blob/master/include/linux/list.h in user-space which its user-space implementation can be found in gist.github/roychen/1710968
以下是我最初使用的代码,它可以正常工作:)
following is the code which I used at first and it works fine :)
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include "list.h" struct Node { int data; char name[10]; struct list_head mylist; }; int main() { LIST_HEAD(plist); struct Node node1 = {.data = 10, .name = "node1", .mylist = LIST_HEAD_INIT(node1.mylist)}; struct Node node2; node2.data = 20; strcpy(node2.name, "node2"); INIT_LIST_HEAD(&node2.mylist); list_add_tail(&node1.mylist, &plist); list_add_tail(&node2.mylist, &plist); struct Node* iter; list_for_each_entry(iter, &plist, mylist) { printf("name = %s, data = %d\n", iter->name, iter->data); } return 0; }以上代码的输出为
name = node1, data = 10 name = node2, data = 20这是预期的.
现在假设我要添加node1 两次
方案1:
list_add_tail(&node1.mylist, &plist); list_add_tail(&node1.mylist, &plist);输出1:
name = node1, data = 10 name = node1, data = 10 name = node1, data = 10 name = node1, data = 10 name = node1, data = 10 name = node1, data = 10 name = node1, data = 10 name = node1, data = 10 name = node1, data = 10 name = node1, data = 10 name = node1, data = 10 name = node1, data = 10 name = node1, data = 10 name = node1, data = 10 ... -> non-stopping loop (to infinity)方案2:
list_add_tail(&node1.mylist, &plist); list_add_tail(&node2.mylist, &plist); list_add_tail(&node1.mylist, &plist);输出2:
name = node1, data = 10 (-> just one node is added to the list instead of 3 nodes)以上输出显示,至少在其功能宏之一中,list.h实现存在错误.
The above outputs show that the list.h implementation has bug, at least in one of its function macros.
我不知道我们无法在链接列表中添加节点两次的错误在哪里.
I don't know where is the bug which we cannot add a node twice in the linked list.
有什么主意吗?! :|
Any idea?! :|
***** 编辑 ***** 方案3:
***** EDIT ***** Scenario 3:
list_add_tail(&node1.mylist, &plist); list_add_tail(&node2.mylist, &plist); list_add_tail(&node1.mylist, &plist); struct Node* iter; list_for_each_entry_reverse(iter, &plist, mylist) { printf("name = %s, data = %d\n", iter->name, iter->data); }输出3:
name = node2, data = 20 name = node1, data = 10 name = node2, data = 20 name = node1, data = 10 name = node2, data = 20 name = node1, data = 10 name = node2, data = 20 name = node1, data = 10 name = node2, data = 20 name = node1, data = 10 ... -> non-stopping loop (to infinity)推荐答案
Linux链表不支持多次添加节点.您不能将其两次添加到同一列表中,也不能将其添加到两个不同的列表中.
The Linux linked list does not support adding a node more than once. You can't add it twice to the same list, and you can't add it to two different lists.
更多推荐
将元素两次添加到Linux内核双链表
发布评论