#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/pid_namespace.h>
#include <linux/sched.h>
#include <linux/pid.h>
#include <linux/rculist.h>
#include <linux/init_task.h>
#include <linux/syscalls.h>
#include <linux/slab.h>
#include <linux/bootmem.h>
#include <linux/pid_namespace.h>
#include <linux/list.h>
static pid_t pid;
module_param(pid,int,S_IRUGO);
static int __init hello_init(void)
{
struct task_struct *cur_task;
cur_task=pid_task(find_get_pid(pid),PIDTYPE_PID); //获取pid进程块
struct task_struct *task=NULL;
struct list_head *list;
task = cur_task;
printk("遍历进程号为%d的父进程如下:\n",pid);
while(task->parent->pid) {
task = task->parent;
if (task) {
printk(KERN_ALERT"comm=%s\tpid=%u\tstate=%ld\n",task->comm,task->pid,task->state);
}
}
printk("遍历进程号为%d的子进程如下:\n",pid);
list_for_each(list, &(cur_task->children)) {
task = list_entry(list, struct task_struct,children);
printk(KERN_ALERT"comm=%s\tpid=%u\tstate=%ld\n",task->comm,task->pid,task->state);
}
printk("遍历进程号为%d的兄弟进程如下:\n",pid);
list_for_each(list, &(cur_task->sibling)) {
task = list_entry(list, struct task_struct,sibling);
printk(KERN_ALERT"comm=%s\tpid=%u\tstate=%ld\n",task->comm,task->pid,task->state);
}
printk("测试进程cur_task的pid :%u\n",cur_task -> pid);
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_INFO "Leaving the kernel!Byebye!\n");
}
MODULE_LICENSE("GPL");//Check if the code support the protocol
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("email:ceaglechina@gmail.com");
|