免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1019 | 回复: 0
打印 上一主题 下一主题

在Linux内核中遍历当前进程的方法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-02 10:39 |只看该作者 |倒序浏览
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)

通过这种方式实现对当前进程的遍历。





本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/83134/showart_1848440.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP