核心連結串列的使用demo
1 /*1. 標頭檔案 */ 2 #include <linux/init.h> 3 #include <linux/module.h> 4 #include <linux/moduleparam.h> 5 #include <linux/kernel.h> 6 #include <linux/list.h> 7 8 struct score{ 9 int num; 10 int Englist; 11 int math; 12 struct list_head list; 13 }; 14 15 struct list_head score_head; 16 17 struct score stu1,stu2,stu3; 18 struct list_head *pos; 19 struct score *tmp; 20 21 22 MODULE_PARM_DESC(ivar, "module kernel list"); 23 24 25 26 27 28 /* 2.模組的載入函式*/ 29 static int __init hello_init(void) 30 { 31 int ret = 0; 32 33 INIT_LIST_HEAD(&score_head); 34 35 stu1.num = 1; 36 stu1.Englist =59; 37 stu1.math = 99; 38 39 list_add_tail(&(stu1.list),&score_head); 40 41 stu2.num = 2; 42 stu2.Englist = 69; 43 stu3.math = 98; 44 45 list_add_tail(&(stu2.list),&score_head); 46 47 stu3.num = 3; 48 stu3.Englist = 89; 49 stu3.math = 97; 50 51 list_add_tail(&(stu3.list),&score_head); 52 53 list_for_each(pos,&score_head){ 54 tmp = list_entry(pos,struct score,list); 55 printk(KERN_ERR"num:%d\tEnglist: %d\tmath: %d\r\n",tmp->num,tmp->Englist,tmp->math); 56 } 57 58 59 return ret; 60 } 61 62 /* 3.模組的解除安裝函式*/ 63 static void __exit hello_exit(void) 64 { 65 list_del(&(stu3.list)); 66 list_del(&(stu2.list)); 67 list_del(&(stu1.list)); 68 69 printk("%s()[%s:%d]\n", __func__, __FILE__, __LINE__); 70 printk("Bye, drivers!\n"); 71 } 72 73 /*4. 模組的入口和出口*/ 74 module_init(hello_init); 75 module_exit(hello_exit); 76 77 /*5.模組的許可宣告*/ 78 MODULE_LICENSE("GPL");