dome:
taskc.ko中通过wake()函数唤醒taskb.ko中通过sleep()进行睡眠的进程
1.task.ko
#include linux/init.h
#include linux/module.h
#include linux/sched.h
#include linux/pid.h
struct pid * pidtask = NULL;
static int pause(void)
{
while (!signal_pending(current)) {
current->state = TASK_INTERRUPTIBLE;
schedule();
}
return -ERESTARTNOHAND;
}
void sleep(void)
{
printk(KERN_ALERT "task sleep now\n");
pidtask = get_task_pid(current, PIDTYPE_PID);
if(!pidtask){
printk(KERN_ALERT "get pid fail \n");
return ;
}
printk(KERN_ALERT "get pid OK \n");
pause();
return 0;
}
EXPORT_SYMBOL(sleep);
void wake(void)
{
struct task_struct * tsk;
if(!pidtask){
printk(KERN_ALERT "get pid fail \n");
return ;
}
tsk = get_pid_task(pidtask,PIDTYPE_PID);
if(!tsk){
printk(KERN_ALERT "get task fail\n");
return ;
}
wake_up_process(tsk);
printk(KERN_ALERT "task wake now \n");
}
EXPORT_SYMBOL(wake);
static int task_init(void)
{
printk(KERN_ALERT "hello word\n");
pidtask = alloc_pid(current->nsproxy->pid_ns_for_children);
if(!pidtask)
return ;
return 0;
}
static void task_exit(void)
{
printk(KERN_ALERT "bye bye task a!\n");
}
MODULE_LICENSE("GPL");
module_init(task_init);
module_exit(task_exit);
2.taskb.ko
#include linux/init.h
#include linux/module.h
#include linux/sched.h
#include linux/pid.h
extern void sleep(void);
extern void wake(void);
static int taskb_init(void)
{
printk(KERN_ALERT "hello task b\n");
return 0;
}
static void taskb_exit(void)
{
sleep();
printk(KERN_ALERT "bye bye task b !\n");
}
MODULE_LICENSE("GPL");
module_init(taskb_init);
module_exit(taskb_exit);
3.taskc.ko
#include linux/init.h
#include linux/module.h
#include linux/sched.h
#include linux/pid.h
extern void sleep(void);
extern void wake(void);
static int taskc_init(void)
{
printk(KERN_ALERT "hello task c\n");
return 0;
}
static void taskc_exit(void)
{
wake();
printk(KERN_ALERT "bye bye task c !\n");
}
MODULE_LICENSE("GPL");
module_init(taskc_init);
module_exit(taskc_exit);
4.Makefile
ifneq ($(KERNELRELEASE),)
obj-m:=task.o(名字需根据具体文档名进行更改)
else
KERNELDIR ?=/lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
endif
Note:由于taskb/taskc调用了task中的symbol,这边是把task中的Module.symvers拷贝到
taskb/taskc进行编译
阅读(1575) | 评论(0) | 转发(0) |