将元素两次添加到Linux内核双链表

编程入门 行业动态 更新时间:2024-10-24 22:18:31
本文介绍了将元素两次添加到Linux内核双链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 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内核双链表

本文发布于:2023-11-10 21:53:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1576563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   内核   元素   链表   Linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!