转自:http://blog.csdn.net/Deep_l_zh/article/details/48392935

版权声明:本文为博主原创文章,未经博主允许不得转载。摘要:将内核链表移植到应用程序中,实现创建,添加节点,遍历,删除的操作。首先复习一下内核链表中经常使用的几个函数,在/include/Linux/list.h中。创建链表:[html] view plain copy<span style="font-size:18px;">INIT_LIST_HEAD()  staticinlinevoid INIT_LIST_HEAD(struct list_head *list)  {  list->next =list;  list->prev =list;  }</span>插入节点:[objc] view plain copy<span style="font-size:18px;">list_add()在链表头插入  list_add_tail()在链表尾插入  staticinlinevoid list_add(struct list_head *new, struct list_head *head)  {  __list_add(new, head, head->next);  }  staticinlinevoid list_add_tail(struct list_head *new, struct list_head *head)  {  __list_add(new, head->prev, head);  }</span>删除节点:[objc] view plain copy<span style="font-size:18px;">list_del()  staticinlinevoid list_del(struct list_head *entry)  {  __list_del(entry->prev, entry->next);  entry->next =LIST_POISON1;  entry->prev =LIST_POISON2;  }</span>遍历链表:[objc] view plain copy<span style="font-size:18px;">list_for_each()  #definelist_for_each(pos, head) \for(pos = (head)->next; prefetch(pos->next), pos !=(head); \  pos= pos->next)</span>取出节点:[objc] view plain copy<span style="font-size:18px;">list_entry()  #definelist_entry(ptr, type, member) \  container_of(ptr,type, member)</span>移植过程中用到的其他函数:1.malloc函数原型:extern void *malloc(unsigned intnum_bytes);功能:分配字节长度为num_bytes内存,如果成功则返回指向内存起始地址的指针,否则返回null。说明:这里声明为void*表示未确定类型的指针,这样使用的时候就可以强制转换为其他我们需要的任何类型的指针。2.memset函数原型:void *memset(void *s,intch,seze_t n);功能:将s指向的某一块内存中的前n个字节的内容全部填充为ch。一般用来对新申请的内存做初始化工作,ch一般都是填充0。我们在使用较大的结构体和数组的时候,都会使用其对分配到的内存清零。3.sprintf函数原型:int sprintf(char *buffer,const char *format,[arugument]…);功能:把格式化的数据写入某个字符串中,返回值是字符串的长度。移植步骤:1.创建list.h因为我们要写成一个app,里面用到很多内核链表的函数,都在list.h里面声明的,一开始这里我就偷懒把内核里面的list.h拷贝一份,放到我当前的工作目录下,命名为list.h,后来编译的时候提示找不到list.h里面加进去的那三个头文件,于是我又把position.h,这三个头文件注释掉了,但是提示LIST_POSITION1和LIST_POSITION2没有定义还有别的错误,于是利用grep查找,到源码目录下,把这部分拷贝到我们的list.h前面部分里面来就可以了。完整的list.c附在最后。[objc] view plain copy<span style="font-size:18px;">#ifndef _LINUX_LIST_H#define _LINUX_LIST_H  #include<linux/stddef.h>#ifndef ARCH_HAS_PREFETCH#define ARCH_HAS_PREFETCH  static inline void prefetch(const voidvoid *x){;}#endif  #define LIST_POISON1 ((void *) 0x0)   #define LIST_POISON2 ((void *) 0x0)  #define container_of(ptr ,type,member)({              \  const typeof( ((type *)0)->member ) *__mptr =(ptr);     \  (type*)( (charchar *)__mptr - offsetof(type,member) );})</span>  2.创建listapp.c添加头文件这里我命名为listapp.c,因为我们要用到很多头文件,这里都添加进去,我添加的如下;[objc] view plain copy<span style="font-size:18px;">#include"list.h"//内核链表操作函数#include<malloc.h>//使用malloc分配内存#include<stdio.h>//sprintf和printf#include<string.h>//memset</span><span style="font-size:14px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">                </span>3.创建球员信息结构体[objc] view plain copy<span style="font-size:18px;">structmember  {  charname[10];  intnum;  intscore;  intassists;  structlist_head list;  };</span>  4.main函数主要思想是创建链表,分配内存,插入节点,遍历输出,删除节点。编译成功后运行出现如下信息;可以看到我们的链表操作是成功了,输出信息也与期望值一样,但是最后free的时候出现了core dump,这个问题查了下有几种解释,这里大概是数组操作越界,或者我们修改了mem区的指针信息,导致free释放内存的时候,释放到别的地方去了,这里不做深究了,留待之后结局。最后附上list.h和listapp.c的代码,结束,如有不正确的地方还请指出,大家共同进步。list.h如下
[objc] view plain copy<span style="font-size:14px;">#ifndef _LINUX_LIST_H#define _LINUX_LIST_H  #include<linux/stddef.h>#ifndef ARCH_HAS_PREFETCH#define ARCH_HAS_PREFETCH  static inline void prefetch(const voidvoid *x) {;}#endif  #define LIST_POISON1 ((void *) 0x0)    #define LIST_POISON2 ((void *) 0x0)  #define container_of(ptr ,type,member) ({              \  const typeof( ((type *)0)->member ) *__mptr =(ptr);     \  (type*)( (charchar *)__mptr -offsetof(type,member) );})/** Simple doubly linked list implementation. * * Some of the internal functions ("__xxx") are useful when * manipulating whole lists rather than single entries, as * sometimes we already know the next/prev entries and we can * generate better code by using them directly rather than * using the generic single-entry routines.*/  structlist_head {struct list_head *next, *prev;  };#define LIST_HEAD_INIT(name) { &(name), &(name) }  #define LIST_HEAD(name) \  struct list_head name =LIST_HEAD_INIT(name)static inline void INIT_LIST_HEAD(struct list_head *list)  {  list->next =list;  list->prev =list;  }/** Insert a new entry between two known consecutive entries. * * This is only for internal list manipulation where we know * the prev/next entries already!*/#ifndef CONFIG_DEBUG_LISTstatic inline void __list_add(struct list_head *new,struct list_head *prev,struct list_head *next)  {  next->prev = new;new->next =next;new->prev =prev;  prev->next = new;  }#else  extern void __list_add(struct list_head *new,struct list_head *prev,struct list_head *next);#endif  /** * list_add - add a new entry * @new: new entry to be added * @head: list head to add it after * * Insert a new entry after the specified head. * This is good for implementing stacks.*/  static inline void list_add(struct list_head *new, struct list_head *head)  {  __list_add(new, head, head->next);  }/** * list_add_tail - add a new entry * @new: new entry to be added * @head: list head to add it before * * Insert a new entry before the specified head. * This is useful for implementing queues.*/  static inline void list_add_tail(struct list_head *new, struct list_head *head)  {  __list_add(new, head->prev, head);  }/** Delete a list entry by making the prev/next entries * point to each other. * * This is only for internal list manipulation where we know * the prev/next entries already!*/  static inline void __list_del(struct list_head * prev, struct list_head *next)  {  next->prev =prev;  prev->next =next;  }/** * list_del - deletes entry from list. * @entry: the element to delete from the list. * Note: list_empty() on entry does not return true after this, the entry is * in an undefined state.*/#ifndef CONFIG_DEBUG_LISTstatic inline void list_del(struct list_head *entry)  {  __list_del(entry->prev, entry->next);  entry->next =LIST_POISON1;  entry->prev =LIST_POISON2;  }#else  extern void list_del(struct list_head *entry);#endif  /** * list_replace - replace old entry by new one * @old : the element to be replaced * @new : the new element to insert * * If @old was empty, it will be overwritten.*/  static inline void list_replace(struct list_head *old,struct list_head *new)  {new->next = old->next;new->next->prev = new;new->prev = old->prev;new->prev->next = new;  }static inline void list_replace_init(struct list_head *old,struct list_head *new)  {  list_replace(old,new);  INIT_LIST_HEAD(old);  }/** * list_del_init - deletes entry from list and reinitialize it. * @entry: the element to delete from the list.*/  static inline void list_del_init(struct list_head *entry)  {  __list_del(entry->prev, entry->next);  INIT_LIST_HEAD(entry);  }/** * list_move - delete from one list and add as another's head * @list: the entry to move * @head: the head that will precede our entry*/  static inline void list_move(struct list_head *list, struct list_head *head)  {  __list_del(list->prev, list->next);  list_add(list, head);  }/** * list_move_tail - delete from one list and add as another's tail * @list: the entry to move * @head: the head that will follow our entry*/  static inline void list_move_tail(struct list_head *list,struct list_head *head)  {  __list_del(list->prev, list->next);  list_add_tail(list, head);  }/** * list_is_last - tests whether @list is the last entry in list @head * @list: the entry to test * @head: the head of the list*/  static inline int list_is_last(const struct list_head *list,const struct list_head *head)  {return list->next ==head;  }/** * list_empty - tests whether a list is empty * @head: the list to test.*/  static inline int list_empty(const struct list_head *head)  {return head->next ==head;  }/** * list_empty_careful - tests whether a list is empty and not being modified * @head: the list to test * * Description: * tests whether a list is empty _and_ checks that no other CPU might be * in the process of modifying either member (next or prev) * * NOTE: using list_empty_careful() without synchronization * can only be safe if the only activity that can happen * to the list entry is list_del_init(). Eg. it cannot be used * if another CPU could re-list_add() it.*/  static inline int list_empty_careful(const struct list_head *head)  {struct list_head *next = head->next;return (next == head) && (next == head->prev);  }/** * list_is_singular - tests whether a list has just one entry. * @head: the list to test.*/  static inline int list_is_singular(const struct list_head *head)  {return !list_empty(head) && (head->next == head->prev);  }static inline void __list_cut_position(struct list_head *list,struct list_head *head, struct list_head *entry)  {struct list_head *new_first = entry->next;  list->next = head->next;  list->next->prev =list;  list->prev =entry;  entry->next =list;  head->next =new_first;  new_first->prev =head;  }/** * list_cut_position - cut a list into two * @list: a new list to add all removed entries * @head: a list with entries * @entry: an entry within head, could be the head itself *  and if so we won't cut the list * * This helper moves the initial part of @head, up to and * including @entry, from @head to @list. You should * pass on @entry an element you know is on @head. @list * should be an empty list or a list you do not care about * losing its data. **/  static inline void list_cut_position(struct list_head *list,struct list_head *head, struct list_head *entry)  {if(list_empty(head))return;if (list_is_singular(head) &&(head->next != entry && head !=entry))return;if (entry ==head)  INIT_LIST_HEAD(list);else__list_cut_position(list, head, entry);  }static inline void __list_splice(const struct list_head *list,struct list_head *prev,struct list_head *next)  {struct list_head *first = list->next;struct list_head *last = list->prev;  first->prev =prev;  prev->next =first;  last->next =next;  next->prev =last;  }/** * list_splice - join two lists, this is designed for stacks * @list: the new list to add. * @head: the place to add it in the first list.*/  static inline void list_splice(const struct list_head *list,struct list_head *head)  {if (!list_empty(list))  __list_splice(list, head, head->next);  }/** * list_splice_tail - join two lists, each list being a queue * @list: the new list to add. * @head: the place to add it in the first list.*/  static inline void list_splice_tail(struct list_head *list,struct list_head *head)  {if (!list_empty(list))  __list_splice(list, head->prev, head);  }/** * list_splice_init - join two lists and reinitialise the emptied list. * @list: the new list to add. * @head: the place to add it in the first list. * * The list at @list is reinitialised*/  static inline void list_splice_init(struct list_head *list,struct list_head *head)  {if (!list_empty(list)) {  __list_splice(list, head, head->next);  INIT_LIST_HEAD(list);  }  }/** * list_splice_tail_init - join two lists and reinitialise the emptied list * @list: the new list to add. * @head: the place to add it in the first list. * * Each of the lists is a queue. * The list at @list is reinitialised*/  static inline void list_splice_tail_init(struct list_head *list,struct list_head *head)  {if (!list_empty(list)) {  __list_splice(list, head->prev, head);  INIT_LIST_HEAD(list);  }  }/** * list_entry - get the struct for this entry * @ptr:    the &struct list_head pointer. * @type:   the type of the struct this is embedded in. * @member: the name of the list_struct within the struct.*/  #define list_entry(ptr, type, member) \  container_of(ptr, type, member)/** * list_first_entry - get the first element from a list * @ptr:    the list head to take the element from. * @type:   the type of the struct this is embedded in. * @member: the name of the list_struct within the struct. * * Note, that list is expected to be not empty.*/  #define list_first_entry(ptr, type, member) \  list_entry((ptr)->next, type, member)/** * list_for_each    -   iterate over a list * @pos:    the &struct list_head to use as a loop cursor. * @head:   the head for your list.*/  #define list_for_each(pos, head) \  for (pos = (head)->next; prefetch(pos->next), pos !=(head); \  pos= pos->next)/** * __list_for_each  -   iterate over a list * @pos:    the &struct list_head to use as a loop cursor. * @head:   the head for your list. * * This variant differs from list_for_each() in that it's the * simplest possible list iteration code, no prefetching is done. * Use this for code that knows the list to be very short (empty * or 1 entry) most of the time.*/  #define __list_for_each(pos, head) \  for (pos = (head)->next; pos != (head); pos = pos->next)/** * list_for_each_prev   -   iterate over a list backwards * @pos:    the &struct list_head to use as a loop cursor. * @head:   the head for your list.*/  #define list_for_each_prev(pos, head) \  for (pos = (head)->prev; prefetch(pos->prev), pos !=(head); \  pos= pos->prev)/** * list_for_each_safe - iterate over a list safe against removal of list entry * @pos:    the &struct list_head to use as a loop cursor. * @n:      another &struct list_head to use as temporary storage * @head:   the head for your list.*/  #define list_for_each_safe(pos, n, head) \  for (pos = (head)->next, n = pos->next; pos !=(head); \  pos= n, n = pos->next)/** * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry * @pos:    the &struct list_head to use as a loop cursor. * @n:      another &struct list_head to use as temporary storage * @head:   the head for your list.*/  #define list_for_each_prev_safe(pos, n, head) \  for (pos = (head)->prev, n = pos->prev; \  prefetch(pos->prev), pos !=(head); \  pos= n, n = pos->prev)/** * list_for_each_entry  -   iterate over list of given type * @pos:    the type * to use as a loop cursor. * @head:   the head for your list. * @member: the name of the list_struct within the struct.*/  #define list_for_each_entry(pos, head, member)              \  for (pos = list_entry((head)->next, typeof(*pos), member);   \  prefetch(pos->member.next), &pos->member !=(head);  \  pos= list_entry(pos->member.next, typeof(*pos), member))/** * list_for_each_entry_reverse - iterate backwards over list of given type. * @pos:    the type * to use as a loop cursor. * @head:   the head for your list. * @member: the name of the list_struct within the struct.*/  #define list_for_each_entry_reverse(pos, head, member)          \  for (pos = list_entry((head)->prev, typeof(*pos), member);   \  prefetch(pos->member.prev), &pos->member !=(head);  \  pos= list_entry(pos->member.prev, typeof(*pos), member))/** * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() * @pos:    the type * to use as a start point * @head:   the head of the list * @member: the name of the list_struct within the struct. * * Prepares a pos entry for use as a start point in list_for_each_entry_continue().*/  #define list_prepare_entry(pos, head, member) \  ((pos)? : list_entry(head, typeof(*pos), member))/** * list_for_each_entry_continue - continue iteration over list of given type * @pos:    the type * to use as a loop cursor. * @head:   the head for your list. * @member: the name of the list_struct within the struct. * * Continue to iterate over list of given type, continuing after * the current position.*/  #define list_for_each_entry_continue(pos, head, member)         \  for (pos = list_entry(pos->member.next, typeof(*pos), member);   \  prefetch(pos->member.next), &pos->member !=(head);  \  pos= list_entry(pos->member.next, typeof(*pos), member))/** * list_for_each_entry_continue_reverse - iterate backwards from the given point * @pos:    the type * to use as a loop cursor. * @head:   the head for your list. * @member: the name of the list_struct within the struct. * * Start to iterate over list of given type backwards, continuing after * the current position.*/  #define list_for_each_entry_continue_reverse(pos, head, member)     \  for (pos = list_entry(pos->member.prev, typeof(*pos), member);   \  prefetch(pos->member.prev), &pos->member !=(head);  \  pos= list_entry(pos->member.prev, typeof(*pos), member))/** * list_for_each_entry_from - iterate over list of given type from the current point * @pos:    the type * to use as a loop cursor. * @head:   the head for your list. * @member: the name of the list_struct within the struct. * * Iterate over list of given type, continuing from current position.*/  #define list_for_each_entry_from(pos, head, member)             \  for (; prefetch(pos->member.next), &pos->member !=(head);    \  pos= list_entry(pos->member.next, typeof(*pos), member))/** * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry * @pos:    the type * to use as a loop cursor. * @n:      another type * to use as temporary storage * @head:   the head for your list. * @member: the name of the list_struct within the struct.*/  #define list_for_each_entry_safe(pos, n, head, member)          \  for (pos = list_entry((head)->next, typeof(*pos), member),   \  n= list_entry(pos->member.next, typeof(*pos), member);  \&pos->member !=(head);                     \  pos= n, n = list_entry(n->member.next, typeof(*n), member))/** * list_for_each_entry_safe_continue * @pos:    the type * to use as a loop cursor. * @n:      another type * to use as temporary storage * @head:   the head for your list. * @member: the name of the list_struct within the struct. * * Iterate over list of given type, continuing after current point, * safe against removal of list entry.*/  #define list_for_each_entry_safe_continue(pos, n, head, member)         \  for (pos = list_entry(pos->member.next, typeof(*pos), member),       \  n= list_entry(pos->member.next, typeof(*pos), member);      \&pos->member !=(head);                     \  pos= n, n = list_entry(n->member.next, typeof(*n), member))/** * list_for_each_entry_safe_from * @pos:    the type * to use as a loop cursor. * @n:      another type * to use as temporary storage * @head:   the head for your list. * @member: the name of the list_struct within the struct. * * Iterate over list of given type from current point, safe against * removal of list entry.*/  #define list_for_each_entry_safe_from(pos, n, head, member)             \  for (n = list_entry(pos->member.next, typeof(*pos), member);     \&pos->member !=(head);                     \  pos= n, n = list_entry(n->member.next, typeof(*n), member))/** * list_for_each_entry_safe_reverse * @pos:    the type * to use as a loop cursor. * @n:      another type * to use as temporary storage * @head:   the head for your list. * @member: the name of the list_struct within the struct. * * Iterate backwards over list of given type, safe against removal * of list entry.*/  #define list_for_each_entry_safe_reverse(pos, n, head, member)      \  for (pos = list_entry((head)->prev, typeof(*pos), member),   \  n= list_entry(pos->member.prev, typeof(*pos), member);  \&pos->member !=(head);                     \  pos= n, n = list_entry(n->member.prev, typeof(*n), member))/** Double linked lists with a single pointer list head. * Mostly useful for hash tables where the two pointer list head is * too wasteful. * You lose the ability to access the tail in O(1).*/  structhlist_head {struct hlist_node *first;  };structhlist_node {struct hlist_node *next, **pprev;  };#define HLIST_HEAD_INIT { .first = NULL }  #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }  #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)  static inline void INIT_HLIST_NODE(struct hlist_node *h)  {  h->next =NULL;  h->pprev =NULL;  }static inline int hlist_unhashed(const struct hlist_node *h)  {return !h->pprev;  }static inline int hlist_empty(const struct hlist_head *h)  {return !h->first;  }static inline void __hlist_del(struct hlist_node *n)  {struct hlist_node *next = n->next;struct hlist_node **pprev = n->pprev;*pprev =next;if(next)  next->pprev =pprev;  }static inline void hlist_del(struct hlist_node *n)  {  __hlist_del(n);  n->next =LIST_POISON1;  n->pprev =LIST_POISON2;  }static inline void hlist_del_init(struct hlist_node *n)  {if (!hlist_unhashed(n)) {  __hlist_del(n);  INIT_HLIST_NODE(n);  }  }static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)  {struct hlist_node *first = h->first;  n->next =first;if(first)  first->pprev = &n->next;  h->first =n;  n->pprev = &h->first;  }/*next must be != NULL*/  static inline void hlist_add_before(struct hlist_node *n,struct hlist_node *next)  {  n->pprev = next->pprev;  n->next =next;  next->pprev = &n->next;*(n->pprev) =n;  }static inline void hlist_add_after(struct hlist_node *n,struct hlist_node *next)  {  next->next = n->next;  n->next =next;  next->pprev = &n->next;if(next->next)  next->next->pprev  = &next->next;  }/** Move a list from one list head to another. Fixup the pprev * reference of the first entry if it exists.*/  static inline void hlist_move_list(struct hlist_head *old,struct hlist_head *new)  {new->first = old->first;if (new->first)new->first->pprev = &new->first;  old->first =NULL;  }#define hlist_entry(ptr, type, member) container_of(ptr,type,member)  #define hlist_for_each(pos, head) \  for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \  pos= pos->next)#define hlist_for_each_safe(pos, n, head) \  for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \  pos=n)/** * hlist_for_each_entry - iterate over list of given type * @tpos:   the type * to use as a loop cursor. * @pos:    the &struct hlist_node to use as a loop cursor. * @head:   the head for your list. * @member: the name of the hlist_node within the struct.*/  #define hlist_for_each_entry(tpos, pos, head, member)            \  for (pos = (head)->first;                     \  pos&& ({ prefetch(pos->next); 1;}) &&\  ({ tpos= hlist_entry(pos, typeof(*tpos), member); 1;}); \  pos= pos->next)/** * hlist_for_each_entry_continue - iterate over a hlist continuing after current point * @tpos:   the type * to use as a loop cursor. * @pos:    the &struct hlist_node to use as a loop cursor. * @member: the name of the hlist_node within the struct.*/  #define hlist_for_each_entry_continue(tpos, pos, member)         \  for (pos = (pos)->next;                       \  pos&& ({ prefetch(pos->next); 1;}) &&\  ({ tpos= hlist_entry(pos, typeof(*tpos), member); 1;}); \  pos= pos->next)/** * hlist_for_each_entry_from - iterate over a hlist continuing from current point * @tpos:   the type * to use as a loop cursor. * @pos:    the &struct hlist_node to use as a loop cursor. * @member: the name of the hlist_node within the struct.*/  #define hlist_for_each_entry_from(tpos, pos, member)             \  for (; pos && ({ prefetch(pos->next); 1;}) &&\  ({ tpos= hlist_entry(pos, typeof(*tpos), member); 1;}); \  pos= pos->next)/** * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry * @tpos:   the type * to use as a loop cursor. * @pos:    the &struct hlist_node to use as a loop cursor. * @n:      another &struct hlist_node to use as temporary storage * @head:   the head for your list. * @member: the name of the hlist_node within the struct.*/  #define hlist_for_each_entry_safe(tpos, pos, n, head, member)        \  for (pos = (head)->first;                     \  pos&& ({ n = pos->next; 1; }) &&\  ({ tpos= hlist_entry(pos, typeof(*tpos), member); 1;}); \  pos=n)#endif</span>  listapp.c如下[objc] view plain copy<span style="font-size:14px;">#include"list.h"//内核链表操作函数#include<malloc.h>//使用malloc分配内存#include<stdio.h>//sprintf和printf#include<string.h>//memsetstructmember  {char name[100];intnum;intscore;structlist_head list;  };struct list_head *pos;//遍历指针的pos,不断地指向链表中节点的指针域,需要是list_head指针类型struct list_head member_list;//名为menber_list的链表struct member *tmp;//存放遍历结果,为struct member类型struct member *pmember;//member的成员int main(void)  {  unsignedint i = 0;            //循环变量的声明
INIT_LIST_HEAD(&member_list); //创建一个链表头,使其前向和后继指针都指向自己,传入参数必须为指针类型,所以取地址
pmember=malloc(sizeof(struct member)*4);  memset(pmember,0,sizeof(struct member)*4);//为member成员分配内存,这里分配四个成员,并且对分配到的内存清零/*给球员成员命名,编号,进球数*/sprintf(pmember[1].name,"player %s","xu");  sprintf(pmember[2].name,"player %s","zeng");  sprintf(pmember[3].name,"player %s","le");  sprintf(pmember[4].name,"player %s","suo");  pmember[1].num=9;  pmember[2].num=21;  pmember[3].num=10;  pmember[4].num=66;  pmember[1].score=2;  pmember[2].score=0;  pmember[3].score=1;   pmember[4].score=5;/*插入节点,list_add第一个参数是成员内部list的指针,第二个是刚才创建的链表头,这样就插入进去了*/  for(i=0;i<4;i++)  {  list_add(&(pmember[i+1].list),&member_list);  printf("###num %d player add sucess!###\n",i+1);  }/*遍历链表,并开始输出球员信息*/printf("###start list_for_each player information###\n");  list_for_each(pos,&member_list)  {  tmp=list_entry(pos,struct member,list);//第一个参数为pos,第二个要给进去我们定义的球员信息结构体,最后是结构内部的list名printf("play %d name %s score %d\n",tmp->num,tmp->name,tmp->score);  }/*最后删除节点*/  for(i=0;i<4;i++)  {  list_del(&(pmember[i+1].list));  printf("### num %d has deleted###\n",i+1);  }/*释放分配得内存*/  free(pmember);  }</span>  

转载于:https://www.cnblogs.com/sky-heaven/p/7133261.html

Linux2.6.32内核笔记(5)在应用程序中移植使用内核链表【转】相关推荐

  1. PJSIP学习笔记15 -- PJSUA应用程序中的会议桥

    注: 上面的tpport,是我为了帮助理解对应关系,为app_config增加的成员变量, 源码中并无这个成员. 可以使用PJSUA程序控制台命令cl来查看系统中的conference_port 未拨 ...

  2. linux内核自旋锁解释,LINUX内核笔记:自旋锁

    目录 1.自旋锁作用与基本使用方法? 与其他锁一样,自旋锁也用于保护临界区,但是自旋锁主要是用于在SMP上保护临界区.在SMP上,自旋锁最多只能被一个可执行线程持有,如果一个线程尝试获得一个被争用的自 ...

  3. ASM:《X86汇编语言-从实模式到保护模式》第13章:保护模式下内核的加载,程序的动态加载和执行...

    ★PART1:32位保护模式下内核简易模型 1. 内核的结构,功能和加载 每个内核的主引导程序都会有所不同,因为内核都会有不同的结构.有时候主引导程序的一些段和内核段是可以共用的(事实上加载完内核以后 ...

  4. linux-2.6.32.2内核在mini2440上的移植,Linux2.6.32.2移植到Mini2440

    1.移植内核的准备工作 (1)使用的环境 操作系统:Fedora 10 交叉编译工具使用:arm-linux-gcc-4.3.2 (2)获取内核 有很多方式可以获取 Linux 内核源代码,如果你的 ...

  5. Linux-2.6.32.2内核在mini2440上的移植(四)---根文件系统制作(1)

    移植环境(红色粗字体字为修改后内容,蓝色粗体字为特别注意内容) 1,主机环境:VMare下CentOS 5.5 ,1G内存. 2,集成开发环境:Elipse IDE 3,编译编译环境:arm-linu ...

  6. linux内核添加usb键盘驱动,配置USB外设 - linux-2.6.32在mini2440开发板上移植_Linux编程_Linux公社-Linux系统门户网站...

    linux-2.6.32在mini2440开发板上移植 配置USB外设 [日期:2013-04-08] 来源:Linux社区 作者:ssdsafsdsd [字体:大 中 小] 编者:因为LINUX内核 ...

  7. linux 内核配置lcd,Linux-2.6.32.2内核在mini2440上的移植---添加LCD背光驱动

    移植环境(红色粗字体字为修改后内容,蓝色粗体字为特别注意内容) 1,主机环境:VMare下CentOS 5.5 ,1G内存. 2,集成开发环境:Elipse IDE 3,编译编译环境:arm-linu ...

  8. mini2440 linux移植开发实战指南,Linux-2.6.32.2内核在mini2440上的移植---移植SD卡驱动...

    移植环境(红色粗字体字为修改后内容,蓝色粗体字为特别注意内容) 1,主机环境:VMare下CentOS 5.5 ,1G内存. 2,集成开发环境:Elipse IDE 3,编译编译环境:arm-linu ...

  9. linux内核(二)内核移植(DM365-DM368开发攻略——linux-2.6.32的移植)

    一.介绍linux-2.6.32: Linux-2.6.32的网上介绍:增添了虚拟化内存 de-duplicacion.重写了 writeback 代码.改进了 Btrfs 文件系统.添加了 ATI ...

最新文章

  1. mysql5.6热升级_mysql 5.6 后热数据的加载
  2. 2013-7-12学习笔记
  3. Repeater 嵌套 绑定数据,嵌套的Repeater无法绑定的问题
  4. 吴恩达:AI是时候从大数据转向「小数据」了
  5. java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener错误解决方案
  6. Android小項目之---吃飯選哪?--》選擇對話框(附源碼)
  7. 襄阳汽车职业学院计算机专业,襄阳汽车职业技术学院毕业设计模板.docx
  8. 超详细简单解决git的上传和下载
  9. wordpress 手动更新方法记录
  10. Spring Boot整合Druid的使用以及步骤
  11. python3解密栅栏密码的正确方法
  12. 全息过山车:带你释放压力,体验激情
  13. ILSVRC-2015 ILSVRC-2017VID数据集下载
  14. [BZOJ 1135][POI2009]Lyz
  15. matlab画红色爱心(心形图)
  16. AndroidStudio很卡怎么办?
  17. 使用负载均衡技术建设高负载的网络站点(经典文章)
  18. Excel凑数:从一堆数据中凑出指定数值的操作
  19. 企业服务总线(ESB)
  20. win10软件拒绝访问删不掉_Win10文件夹无法访问拒绝访问怎么解决?

热门文章

  1. CALLBACKS IN C++ USING TEMPLATE FUNCTORS
  2. 操作系统双语阅读 - Schedulers调度器2
  3. 前端入门-day2(常见css问题及解答)
  4. 微信测试公众号-jssdk基本配置和使用-thinkphp
  5. 《Greenplum5.0 最佳实践》 内存与资源队列 (四)
  6. Java对象锁和类锁全面解析(多线程synchronized关键字)
  7. Redis内存缓存系统入门
  8. EXCEL 制作万年历
  9. [导入]在asp.net中实现观察者模式,或有更好的方法(续)
  10. 使用java9的uuid生成方式,让uuid生成速度提升一个档次