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

全部博文(87)

文章存档

2012年(49)

2011年(7)

2010年(26)

2009年(5)

分类: LINUX

2010-04-26 11:27:52

源码
 

/* simple_task_info.c
   2.6.18-8.e15
 */

#ifndef __KERNEL__
  #define __KERNEL__
#endif
#ifndef MODULE
  #define MODULE
#endif

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/version.h>

MODULE_LICENSE("GPL");
MODULE_VERSION("1.0");

#define MODULE_NAME "simple-task-info"

unsigned int module_major = 0;

static inline void read_task_info()
{
    struct task_struct *task;
    struct task_struct *p;
    struct list_head *head;
    int count = 0;

    printk("Read All Tasks Info from Kernel");
    task = &init_task;//内核的进程列表表头

    list_for_each(head, &task->tasks)
    {
        p = list_entry(head, struct task_struct, tasks);
        count++;
        printk(KERN_ALTER "%d---->%s",p->pid,p->comm);
    }
    printk(KERN_ALERT "All Tasks is [%d]\n",count);
}

static int simple_task_read(struct file *fd, char *buff, size_t len, loff_t *offset)
{
    size_t nRet = 0;
    read_tasks_info();
    return nRet;
}

struct file_operations simple_tasks_fops = {
    .owner = THIS_MODULE,
    .read = simple_task_read,
    .write = NULL,
    .ioctl = NULL,
    .open = NULL,
    .release = NULL,
};

static int __init simple_task_info_init(void)
{
    int nRet;
    
    if( 0 > (nRet = register_chrdev(0 ,MODULE_NAME, simple_tasks_fops)) )
        printk(KERN_INFO "Can't get major device number\n");
    else if (module_major == 0) module_major = nRet;
    
    return nRet;
}

static void __exit simple_task_info_exit(void)
{
    unregister_chrdev(module_major, MODULE_NAME);
}    

module_init(simple_task_info_init);
module_exit(simple_task_info_exit);


 

makefile可用最简单例子中的,只要修改一下模块名称即可。

 

使用:

可在程序中用open方式打开设备文件/dev/simple-task-info

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