linux 进程遍历,Linux内核编程遍历所有进程PID

#include

#include

#include

#include

#include

static __init int print_pid(void)

{

struct task_struct *task,*p;

struct list_head *pos;

int count=0;

printk("Hello,let begin\n");

task=&init_task;

list_for_each(pos,&task->tasks)

{

p=list_entry(pos,struct task_struct,tasks);

count++;

printk("%d---->%s\n",p->pid,p->comm);

}

printk("the number of process is:%d\n",count);

return 0;

}

static __exit void print_exit(void)

{

printk("<0>end!\n");

}

module_init(print_pid);

module_exit(print_exit);

我们写一个Makefile文件来编译模块,内容如下:

www.it165.net

obj-m:=print_pid.o //文件名

kernel_path=/usr/src/linux-headers-$(shell uname -r)

all:

make -c $(kernel_path)

clean:

make -c $(kernel_path) M=$(PWD)

最后,用make命令运行Makefile.

lwp@lwp-linux:~/kernel$ make

运行代码用:

lwp@lwp-linux:~/kernel$ sudo insmod print_pid.ko

从内核中移除该模块就可以看到退出时的信息:

lwp@lwp-linux:~/kernel$ sudo rmmod print_pid

查看模块是否正确写入到内核中去:

lwp@lwp-linux:~/kernel$ dmesg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值