先写family.c文件
代码如下:#include
#include
#include
#include
#include
static pid_t pid=1;
module_param(pid,int,0644);
static int family_init(void)
{
struct task_struct *p;
struct list_head *pp;
struct task_struct *psibling;
p = pid_task(find_vpid(pid), PIDTYPE_PID);
if(p->parent==NULL)
{
printk("No Parent\n");
}
else
{
printk("Parent : %d %s\n",p->parent->pid,p->parent->comm);
}
list_for_each(pp,&p->parent->children)
{
psibling=list_entry(pp,struct task_struct,sibling);
printk("sibling %d %s \n",psibling->pid,psibling->comm);
}
list_for_each(pp,&p->children)
{
psibling=list_entry(pp,struct task_struct,sibling);
printk("children %d %s \n",psibling->pid,psibling->comm);
}
return 0;
}
static void family_exit(void)
{
printk(KERN_ALERT "famliy end!\n");
}
module_init(family_init);//加载函数
module_exit(family_exit); //卸载函数
MODULE_LICENSE("GPL"); //许可证申明
MODULE_DESCRIPTION("family module");
MODULE_AUTHOR("fade");
##编写Makefile##ifneq ($(KERNELRELEASE),)
obj-m:=family.o
else
KDIR:= /lib/modules/$(shell uname -r)/build
PWD:= $(shell pwd)
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
endif
编译模块sudo make
添加模块sudo insmod family
输出日志dmesg
验证
1.父进程:ps aux | grep pid
该进程第三类即为父进程号
2.子进程:pstree -p pid
这个命令可以显示进程树
3.兄弟节点:pstree -p ppid
上面的ppid为第一个中获取到的父进程号,父亲的孩子就是兄弟~