Chinaunix首页 | 论坛 | 博客
  • 博客访问: 137297
  • 博文数量: 27
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-23 13:28
文章分类

全部博文(27)

文章存档

2011年(1)

2009年(15)

2008年(11)

我的朋友

分类: LINUX

2009-03-02 10:39:48

Linux内核中,进程描述符关于当前进程集合的定义如下:
struct task_struct {
 
    volatile long state; 当前进程状态
    ....
    struct list_head tasks;
}
list_head的定义如下:
struct list_head {
 
    struct list_head *next,*prev;
}
下面来看一下具体的过程:
struct task_struct *p = current; //current是内核中指向当前task的指针
由此我们可以通过 p->task.next或是p->task.prev来遍历list_head。
如何从list_head再获得task_struct的指针,需要list_entry()函数来处理。
 
list_entry的定义
/**
*list_entry get the struct of 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_entry((p)->tasks.next,struct task_struct,tasks)
 
通过这种方式实现对当前进程的遍历。
 
 
 
 
阅读(3097) | 评论(0) | 转发(0) |
0

上一篇:const用法简介

下一篇:mysql的中文编码问题

给主人留下些什么吧!~~