Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1615460
  • 博文数量: 197
  • 博客积分: 10046
  • 博客等级: 上将
  • 技术积分: 1983
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-07 12:36
个人简介

在外企做服务器开发, 目前是项目经理, 管理两个server开发的项目。不做嵌入式好久了。

文章分类
文章存档

2011年(2)

2010年(6)

2009年(18)

2008年(30)

2007年(100)

2006年(41)

分类: LINUX

2007-09-28 12:49:06

【kernel进阶练习2】 查找进程的pid

注意2.4和2.6 kernel的struct task_struct 已经发生变化了。 原来2.4里面的struct task_struct *task_next 已经没有了, 变成了现在的struct list_head tasks 了。
所以编程时候要注意。



    /*
By process name , you can get task_struct for which wake up ,bob added
*/

int self_find_task_by_name(char *ps_name)
{
    pid_t pid = -1;
    // int counter = 0;


    //2.4

    //struct task_struct *p = current->next_task;

    //struct task_struct *p_query_head = current ;


    //2.6

    struct list_head *p = NULL;
    struct list_head *head = &current->tasks;

    //loop times depends on the number of the process in current system

    //2.4

    //while(p != NULL && p->pid != p_query_head->pid)

    //2.6 kernel

    list_for_each(p,head)
    {
        
#define this_task(p)    list_entry(p,struct task_struct, tasks) //我自己定义的一个宏 ,可以直接把list_head 指针的宿主指针找出来。


        struct task_struct *task = NULL;
        task = this_task(p);
        
        if( ! memcmp(task->comm,ps_name,strlen(ps_name)))
        {
            pid = task->pid;
            goto p_find;
        }
    }
    dbg("Can't find the process named %s,you should run it in advance\n",ps_name);
    return -1;

p_find:
    return pid;

}




阅读(2226) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~