Chinaunix首页 | 论坛 | 博客
  • 博客访问: 391089
  • 博文数量: 87
  • 博客积分: 2571
  • 博客等级: 少校
  • 技术积分: 920
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-29 13:10
文章分类

全部博文(87)

文章存档

2012年(49)

2011年(7)

2010年(26)

2009年(5)

分类: LINUX

2009-12-29 13:22:05

遍历所有的进程
 
for_each_process,这个函数本身就可以遍历所有的进程,代码:
 
struct task_struct *p;
char buff[1024];
 
if(offset>0)
 return 0;
//加锁(SMP)
read_lock(&tasklist_lock);
for_each_process(p)
{
  sprintf(buff,"%d\t%d\t\t%s\n",p->pid,p->parent->pid,p->comm);
}
//解锁
read_unlock(&tasklist_lock);
 
 
for_each_process是在linux/sched.h中的宏定义:
#define for_each_process(p)   for (p = &init_task ; (p = next_task(p)) != &init_task ; )
作用是从init_task遍历所有的进程描述符。
 
 
当前进程
 
linux内核是如何获取当前进程的任务结构指针的,以下代码均参照linux内核2.4.0的源码。
在include\asm-i386\ current.h中
 
#ifndef _I386_CURRENT_H
#define _I386_CURRENT_H
 
struct task_struct;
 
static inline struct task_struct * get_current(void)
{
       struct task_struct *current;
       __asm__("andl %%esp,%0; ":"=r" (current) : "0" (~8191UL));
       return current;
 }
#define current get_current()
 
#endif /* !(_I386_CURRENT_H) */
 

 
阅读(1555) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:[linux] proc文件系统

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