linux内核入门之List操作

声明: 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");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值