在外企做服务器开发, 目前是项目经理, 管理两个server开发的项目。不做嵌入式好久了。
全部博文(197)
分类: LINUX
2007-09-28 12:46:32
//声明一个链表 LIST_HEAD(bob_list); struct bob_jobs { unsigned long id; struct list_head list; }; #define this_job(p) list_entry(p, struct bob_jobs, list) static __init int chardev_init(void) { struct list_head *p = NULL; //begin struct bob_jobs job1 = { .id = 10UL, //.list = LIST_HEAD_INIT(list), 因为我们的结构体刚定义,list指针本身就是未知的,需要在list_add()才能对其进行操作,不用没法赋值。 }; struct bob_jobs job2 = { .id = 20UL, //.list = LIST_HEAD_INIT(list), 因为我们的结构体刚定义,list指针本身就是未知的,需要在list_add()才能对其进行操作,不用没法赋值。 }; Major = register_chrdev(0, DEVICE_NAME, &fops); if (Major < 0) { dbg("Registering the character device failed with %d\n", Major); return Major; } dbg("only debug the functions in list.h\n"); INIT_LIST_HEAD(&bob_list); //初始化链表 list_add(&(job1.list),&bob_list); list_add(&(job2.list),&bob_list); //print ,遍历一下 , list_for_each(p,&bob_list) { struct bob_jobs *job = NULL; job = this_job(p); dbg("id=%lu\n",job->id); } //试验完毕要删除链表, 但是这里面没有删除header ,因为header不是指针, 所以也就不用回收了。 dbg("will delete the bob_list double list\n"); while(bob_list.next != &bob_list) { struct bob_jobs *job = NULL; p = bob_list.next; job = this_job(p); //delete a entry after header // dbg("delete a entry after header , id = %lu\n",(this_job(p))->id); dbg("delete a entry after header , id = %lu\n",job->id); list_del(bob_list.next); } dbg("delete over \n"); return 0; } |