当前位置:   article > 正文

linux内核入门之List操作_list_add queue

list_add queue

声明: static LIST_HEAD(my_list);

加到尾部 list_add_tail(&a0.list, &my_list);

加到头部 list_add(&a0.list, &my_list);

遍历 list_for_each_entry(pos, &my_list2, list)

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

#include <asm-generic/errno-base.h>
#include <uapi/asm-generic/errno.h>

static LIST_HEAD(my_list);

static LIST_HEAD(my_list2);

struct struct1 {
	struct list_head list;
	char name[20];
};

void inline queues01(void) {
	struct struct1 a0 = { .name = "a0", };
	struct struct1 a1 = { .name = "a1", };
	struct struct1 a2 = { .name = "a2", };
	struct struct1 a3 = { .name = "a3", };
	struct struct1 *pos;
	struct list_head;

	list_add_tail(&a0.list, &my_list);
	list_add_tail(&a1.list, &my_list);
	list_add_tail(&a2.list, &my_list);
	list_add_tail(&a3.list, &my_list);

	list_for_each_entry(pos, &my_list, list)
	{
		pr_err("name = %s\n", pos->name);
	}

}

void inline queues01_move(void) {
	struct struct1 a0 = { .name = "a0", };
	struct struct1 a1 = { .name = "a1", };
	struct struct1 a2 = { .name = "a2", };
	struct struct1 a3 = { .name = "a3", };

	struct struct1 a4 = { .name = "a4", };
	struct struct1 a5 = { .name = "a5", };
	struct struct1 a6 = { .name = "a7", };
	struct struct1 a7 = { .name = "a7", };

	struct struct1 *pos;
	struct list_head;

	list_add_tail(&a0.list, &my_list);
	list_add_tail(&a1.list, &my_list);
	list_add_tail(&a2.list, &my_list);
	list_add_tail(&a3.list, &my_list);

	list_add_tail(&a4.list, &my_list2);
	list_add_tail(&a5.list, &my_list2);
	list_add_tail(&a6.list, &my_list2);
	list_add_tail(&a7.list, &my_list2);

	list_for_each_entry(pos, &my_list, list)
	{
		pr_err("my_list1 name = %s\n", pos->name);
	}

	pr_err("#####################\n");

	list_for_each_entry(pos, &my_list2, list)
	{
		pr_err("my_list2 name = %s\n", pos->name);
	}

	list_move(&a2.list, &my_list2);

	pr_err("after move\n");

	list_for_each_entry(pos, &my_list, list)
	{
		pr_err("my_list1 name = %s\n", pos->name);
	}

	pr_err("#####################\n");

	list_for_each_entry(pos, &my_list2, list)
	{
		pr_err("my_list2 name = %s\n", pos->name);
	}

}

void inline queues01_del(void) {
	struct struct1 a0 = { .name = "a0", };
	struct struct1 a1 = { .name = "a1", };
	struct struct1 a2 = { .name = "a2", };
	struct struct1 a3 = { .name = "a3", };
	struct struct1 *pos;
	struct list_head;

	list_add_tail(&a0.list, &my_list);
	list_add_tail(&a1.list, &my_list);
	list_add_tail(&a2.list, &my_list);
	list_add_tail(&a3.list, &my_list);

	list_del(&a2.list);

	list_for_each_entry(pos, &my_list, list)
	{
		pr_err("name = %s\n", pos->name);
	}

	pr_err("list_empty = %d\n", list_empty(&my_list));
}

void inline stack01(void) {
	struct struct1 a0 = { .name = "a4", };
	struct struct1 a1 = { .name = "a5", };
	struct struct1 a2 = { .name = "a6", };
	struct struct1 a3 = { .name = "a7", };
	struct struct1 *pos;
	struct list_head;

	list_add(&a0.list, &my_list);
	list_add(&a1.list, &my_list);
	list_add(&a2.list, &my_list);
	list_add(&a3.list, &my_list);

	list_for_each_entry(pos, &my_list, list)
	{
		pr_err("name = %s\n", pos->name);
	}
}

int myinit(void) {
//	queues01();
//	stack01();
//	queues01_del();
	queues01_move();
	return 0;
}

static __init int my_init(void) {
	return  myinit();

}

static void __exit my_exit(void) {

}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("A sample c  driver");

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/128105
推荐阅读
相关标签
  

闽ICP备14008679号