Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > lkm内核进程统计源代码kps.c
【标  题】:lkm内核进程统计源代码kps.c
【关键字】:lkm,kps.c
【来  源】:http://blog.chinaunix.net/article.php?articleId=59203&blogId=2108

lkm内核进程统计源代码kps.c

Your Ad Here 最近看了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 <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/version.h>
#include <linux/sched.h>

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]

UNIX 萤幕导向程式的发展利器 - curses(一) ZT:【上一篇】
informix 和db2 数据库如何根据日期获取星期几:【下一篇】
【相关文章】
  • lkm内核编程代码-proc
  • Linux内核编程指导(lkm)
  • lkm内核编程源代码-char devices
  • 索爱首款Walkman手机发布
  • 后门技术和Linux LKM Rootkit
  • 用LKM更改linux缺省安全等级
  • 逃避kstat的检测的lkm程序的实现方法
  • 关于SLKM隐含目录的bug
  • 利用LLKM处理网络通信---对抗IDS、Firewall
  • 兰陵邮件快递(LLbulkmailer V1.1)
  • 【随机文章】
  • WEB2.0介绍
  • LSA LSA分类分类
  • 美女PS素描法(1)
  • 24口10/100M可堆叠交换机
  • MFC -> COM\ActiveX\more...
  • windows与unix体系结构图
  • 李纳斯·托沃兹:Linux之父
  • MySQL 4.1 字符集支持的原理
  • IBM 纯光纤高性能全冗余 SAN 存储及自动备份解决方案
  • 堆排序heapsortC语言实现
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.