源码
/* 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
阅读(1875) | 评论(0) | 转发(0) |