insmod moduleone.ko
dmesg
rmmod moduleone
dmesg
第(1)小题运行结果
insmod module_two.ko pid=10
dmesg
rmmod module_two
dmesg
第(2)小题运行结果
实验(1)代码:
#include
#include
#include
#include
#include
static int moduleone_init(void)
{
struct task_struct *p;
printk(KERN_ALERT"Name \t\t PID \t State \t Prio\t");
for_each_process(p)
{
if(p->mm == NULL)
printk(KERN_ALERT"%s\t%d\t%d\t%d\n",p->comm,p->pid, p->state,p->static_prio);
}
return 0;
}
static void moduleone_exit(void)
{
printk(KERN_ALERT "MooduleOne has exit successfully!\n");
}
module_init(moduleone_init);
module_exit(moduleone_exit);
MODULE_LICENSE("GPL");
实验(1)makefile文件:
obj-m:=moduleone.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
实验(2)代码:
#include
#include
#include
#include
#include
static pid_t pid;
module_param(pid,int,0644);
static int module_two_init(void)
{
struct task_struct *p;
struct list_head *pp;
struct task_struct *psibling;
p = pid_task(find_vpid(pid), PIDTYPE_PID);//根据pid值,找到task
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)//遍历p的父亲节点的孩子节点(p的兄弟节点)
{
psibling=list_entry(pp,struct task_struct,sibling);
printk("sibling %d %s \n",psibling->pid,psibling->comm);
}
list_for_each(pp,&p->children)//遍历p的孩子节点
{
psibling=list_entry(pp,struct task_struct,sibling);
printk("children %d %s \n",psibling->pid,psibling->comm);
}
return 0;
}
static void module_two_exit(void)
{
printk(KERN_ALERT "ModuleTwo has exit successfully!\n");
}
module_init(module_two_init);
module_exit(module_two_exit);
MODULE_LICENSE("GPL");
实验(2)makefile文件:
obj-m:=module_two.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