提出问题:
如果还没有掌握基础模块编程技能,看看这个吧
https://blog.csdn.net/babybabyup/article/details/80537149
在目录/os/first_dir
下,用户名为root
编辑 first.c
#include
#include
#include
#include
static int first_init(void) {
struct task_struct *p;
p=NULL;
printk(KERN_ALERT"first start");
printk(KERN_ALERT"名称\t进程号\t状态\t优先级\t父进程PID");
for_each_process(p){
printk(KERN_ALERT"%s\t%d\t%d\t%d\t%d\n",p->comm,p->pid, p->state,p->prio,p->parent->pid);
}
return 0;
}
static void first_exit(void)
{
printk(KERN_ALERT"first exit");
}
module_init(first_init);
module_exit(first_exit);
MODULE_LICENSE("GPL");
在相同目录下编辑Makefile
obj-m :=fir.o
fir-objs:=first.o
KDIR:=/lib/modules/$(shell uname -r)/build
PWD:=$(shell pwd)
default:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
在目录/os/second_dir
,用户名为root
,编辑second.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/moduleparam.h>
#include <linux/list.h>
static int pid;
module_param(pid, int ,0644);
static int se_init(void) {
struct task_struct *ts;
struct pid *p;
p=find_get_pid(pid);
ts=pid_task(p,PIDTYPE_PID);
struct task_struct *parent;
printk(KERN_ALERT "second start!");
parent=ts->parent;
printk(KERN_ALERT"父名称\t进程号\t状态\t优先级\t\n");
printk(KERN_ALERT"%s\t%d\t%d\t%d\n",parent->comm,parent->pid,parent->state,parent->prio);
struct task_struct *list_sibling;
struct list_head *pos_sibling;
printk(KERN_ALERT"兄弟名称\t进程号\t状态\t优先级\t\n");
list_for_each(pos_sibling,&ts->sibling){
list_sibling=list_entry(pos_sibling,struct task_struct,sibling);
printk(KERN_ALERT"%s\t%d\t%d\t%d\n",list_sibling->comm,list_sibling->pid,list_sibling->state,list_sibling->prio);
}
struct task_struct *list_children;
struct list_head *pos_children;
printk(KERN_ALERT"孩子名称\t进程号\t状态\t优先级\t\n");
list_for_each(pos_children,&ts->children){
list_children=list_entry(pos_children,struct task_struct,sibling);
printk(KERN_ALERT"%s\t%d\t%d\t%d\n",list_children->comm,list_children->pid,list_children->state,list_children->prio);
}
return 0;
}
static void se_exit(void)
{
printk(KERN_ALERT "second exit\n");
}
module_init(se_init);
module_exit(se_exit);
MODULE_LICENSE("GPL");
以及MAkefile
obj-m :=sec.o
sec-objs:=second.o
KDIR:=/lib/modules/$(shell uname -r)/build
PWD:=$(shell pwd)
default:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
还是老套路,安装,需要注意的是,安装时要传参数:
ins mod sec.ko pid=pidvalue
可以通过
ps -A
我传pid=2884
吧
ins mod sec.ko pid=2884
卸载模块后
这是我实验课内容,如果有任何不对的地方,欢迎指正