ITeye博客

kernel源码学习:进程

coderplay · 2007-08-09 10:19 · linux · Markdown

linux使用上百个双向链表来存储内核的各种数据结构, 其中包括进程描述符的task_struct结构.

cpp 代码

 

  1. struct list_head {
  2. struct list_head *next, *prev;
  3. };

在list.h中,形如__xxx的函数用来操作整个链表,而不是单个链表元素.

进程从pid得到进程描述符pd要一种高效的方法, 提到高效方法,那就用哈希表啦.  内核在这儿的哈希方法为一道宏, 表中有PIDHASH_SZ个元素(一般情况下PIDHASH_SZ=1024):

cpp 代码

 

  1. #define pid_hashfn(x)   ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))

哈希表解决冲突的方法以是采取双向链表. 这是数据结构中一种比较常见的手段. 因为数字比较大的pid很少用到, 这样的pid一般放在链表尾端。 而数字较小的pid经常用到, 而它们如果冲突也排在链表很前的位置, 所以采取这样的数据结构速度很快.
   内核提供了hash_pid()来加入一个元素到表中, unhash_pid()从表中删除一元素. 
   这个哈希表的主要目的是通过pid找到进程描述符, find_task_by_pid()提供了这项功能.

cpp 代码

 

  1. static inline void hash_pid(struct task_struct *p)
  2. {
  3. struct task_struct **htable = &pidhash[pid_hashfn(p->pid)];
  4. if((p->pidhash_next = *htable) != NULL)
  5. (*htable)->pidhash_pprev = &p->pidhash_next;
  6. *htable = p;
  7. p->pidhash_pprev = htable;
  8. }
  9. static inline void unhash_pid(struct task_struct *p)
  10. {
  11. if(p->pidhash_next)
  12. p->pidhash_next->pidhash_pprev = p->pidhash_pprev;
  13. *p->pidhash_pprev = p->pidhash_next;
  14. }
  15. static inline struct task_struct *find_task_by_pid(int pid)
  16. {
  17. struct task_struct *p, **htable = &pidhash[pid_hashfn(pid)];
  18. for(p = *htable; p && p->pid != pid; p = p->pidhash_next)
  19. ;
  20. return p;
  21. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。