Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2584408
  • 博文数量: 351
  • 博客积分: 76
  • 博客等级: 上将
  • 技术积分: 3555
  • 用 户 组: 普通用户
  • 注册时间: 2004-11-13 21:27
文章分类

全部博文(351)

文章存档

2013年(1)

2012年(4)

2011年(7)

2010年(16)

2009年(34)

2008年(34)

2007年(34)

2006年(68)

2005年(82)

2004年(71)

分类:

2005-11-29 11:35:29

最近看了lkd 2.0,根据process management那一章,写了一些小例子程序.
本文包括源代码,Makefile(Linux-2.6.14)和程序运行结果.

源代码 kps.c
/* kps.c - a simple ps in kernel mode,
 *         write after lkd(Linux Kernel Development Second Edition),
 *         chaper 3, process management
 *
 * Author:    zhou_weicheng at 163 dot com
 * Date:    2005/11/28
 */

#ifndef __KERNEL__
#define __KERNEL__
#endif

#ifndef MODULE
#define MODULE
#endif

#include
#include
#include
#include
#include

static int task_dep = 0;    /* depth of a task contrast INIT */

/* print all process */
static void get_task_info(void)
{
    struct task_struct *task;

    task = &init_task;
    for_each_process(task) {
        /* print name and pid of each process/task */
            printk("%s[%d] ", task->comm, task->pid);
    }

    return;
}

/* print dash with specified number */
static void print_dash(int n)
{
    for (; n > 0; n--)
        printk("-");
}

/* print sibling task of specified task */
/* a iterative routine */
static void get_task_tree(struct task_struct *task) {
    struct list_head *list = NULL;
    struct task_struct *sib = NULL;

    if (!task) return;
    task_dep++;

/*    printk("|%s[%d], %d ", task->comm, task->pid, task->parent->pid);*/
    printk("|%s[%d] ", task->comm, task->pid);

    list_for_each(list, &task->children) {
        sib = list_entry(list, struct task_struct, sibling);
        printk("|");
        print_dash(task_dep * 5);
        get_task_tree(sib);
    }
    task_dep--;
}
       
static int __init init(void)
{
    /* get all process */
    get_task_info();

    printk("--------------------------------------- ");

    /* get process tree */
    get_task_tree(&init_task);

    return 0;
}

static void __exit fini(void)
{
    return;
}

module_init(init);
module_exit(fini);

-----------------------------------------------------------------------------------------------
-Makefile
obj-m += kps.o

all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean


------------------------------------------------------------------------------------------------
运行结果(dmesg)
[17189877.288000] init[1]
[17189877.288000] ksoftirqd/0[2]
[17189877.288000] watchdog/0[3]
[17189877.288000] events/0[4]
[17189877.288000] khelper[5]
[17189877.288000] kthread[6]
[17189877.288000] kacpid[8]
[17189877.288000] kblockd/0[49]
[17189877.288000] pdflush[84]
[17189877.288000] pdflush[85]
[17189877.288000] aio/0[87]
[17189877.288000] kswapd0[86]
[17189877.288000] kseriod[670]
[17189877.288000] scsi_eh_0[1243]
[17189877.288000] shpchpd_event[1439]
[17189877.288000] khubd[1609]
[17189877.288000] dhclient[1729]
[17189877.288000] inetd[2078]
[17189877.288000] sshd[2114]
[17189877.288000] vsftpd[2118]
[17189877.288000] getty[2221]
[17189877.288000] getty[2227]
[17189877.288000] getty[2228]
[17189877.288000] getty[2229]
[17189877.288000] getty[2230]
[17189877.288000] getty[2231]
[17189877.288000] sshd[3829]
[17189877.288000] bash[3831]
[17189877.288000] sshd[3840]
[17189877.292000] bash[3842]
[17189877.292000] vi[4573]
[17189877.292000] insmod[4649]
[17189877.292000] ---------------------------------------
[17189877.292000] |swapper[0]
[17189877.292000] |-----|init[1]
[17189877.292000] |----------|ksoftirqd/0[2]
[17189877.292000] |----------|watchdog/0[3]
[17189877.292000] |----------|events/0[4]
[17189877.292000] |----------|khelper[5]
[17189877.292000] |----------|kthread[6]
[17189877.292000] |---------------|kacpid[8]
[17189877.292000] |---------------|kblockd/0[49]
[17189877.292000] |---------------|pdflush[84]
[17189877.292000] |---------------|pdflush[85]
[17189877.292000] |---------------|aio/0[87]
[17189877.292000] |---------------|kseriod[670]
[17189877.292000] |---------------|scsi_eh_0[1243]
[17189877.292000] |---------------|khubd[1609]
[17189877.292000] |----------|kswapd0[86]
[17189877.292000] |----------|shpchpd_event[1439]
[17189877.292000] |----------|dhclient[1729]
[17189877.292000] |----------|inetd[2078]
[17189877.292000] |----------|sshd[2114]
[17189877.292000] |---------------|sshd[3829]
[17189877.292000] |--------------------|bash[3831]
[17189877.292000] |-------------------------|insmod[4649]
[17189877.292000] |---------------|sshd[3840]
[17189877.292000] |--------------------|bash[3842]
[17189877.292000] |-------------------------|vi[4573]
[17189877.296000] |----------|vsftpd[2118]
[17189877.296000] |----------|getty[2221]
[17189877.296000] |----------|getty[2227]
[17189877.296000] |----------|getty[2228]
[17189877.296000] |----------|getty[2229]
[17189877.296000] |----------|getty[2230]
[17189877.296000] |----------|getty[2231]

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