linux进程家族,linux内核根据pid,列出家族信息中的程序名和PID号

先写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

4fcaddef65e22a4beca9a86cc2066047.png

验证

1.父进程:ps aux | grep pid

该进程第三类即为父进程号

2.子进程:pstree -p pid

这个命令可以显示进程树

3.兄弟节点:pstree -p ppid

上面的ppid为第一个中获取到的父进程号,父亲的孩子就是兄弟~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值