1.test.c
#include linux/module.h
#include linux/init.h
#include linux/export.h
static DECLARE_WAIT_QUEUE_HEAD(test_wait);
static wait_queue_head_t * get_wait_queue(void)
{
return &test_wait;
}
EXPORT_SYMBOL(get_wait_queue);
static int flag = 0;
static int getflag(void)
{
return flag;
}
EXPORT_SYMBOL(getflag);
static void setflag(int i)
{
flag = i;
}
EXPORT_SYMBOL(setflag);
static int test_init(void)
{
printk("do nothing!\n");
return 0;
}
static void test_exit(void)
{
printk("do nothing!\n");
}
MODULE_AUTHOR("TEST");
MODULE_LICENSE("GPL");
module_init(test_init);
module_exit(test_exit);
2.testa.c
#include linux/module.h
#include linux/init.h
#include linux/sched.h
#include linux/wait.h
#include linux/sched/signal.h
extern void setflag(int i);
extern wait_queue_head_t * get_wait_queue(void);
int i = 0;
static int testb(void)
{
DECLARE_WAITQUEUE(wait, current);
add_wait_queue(get_wait_queue(), &wait);
for (;;) {
__set_current_state(TASK_INTERRUPTIBLE);
if(i == 1)
break;
if (signal_pending(current))
break;
schedule();
}
remove_wait_queue(get_wait_queue(), &wait);
set_current_state(TASK_RUNNING);
return 1;
}
static int testa_init(void)
{
printk("before testa funcitos!\n");
testb();
printk("after testa function");
return 0;
}
static void testa_exit(void)
{
i = 1;
setflag(1);
wake_up(get_wait_queue());
printk("testa do nothing!\n");
}
MODULE_AUTHOR("TESTA");
MODULE_LICENSE("GPL");
module_init(testa_init);
module_exit(testa_exit);
2.testb.c
#include linux/module.h
#include linux/init.h
#include linux/sched.h
#include linux/wait.h
#include linux/sched/signal.h
extern int getflag(void);
extern wait_queue_head_t * get_wait_queue(void);
static int testa(void)
{
DECLARE_WAITQUEUE(wait, current);
add_wait_queue(get_wait_queue(), &wait);
for (;;) {
__set_current_state(TASK_INTERRUPTIBLE);
if(getflag() == 1)
break;
if (signal_pending(current))
break;
schedule();
}
remove_wait_queue(get_wait_queue(), &wait);
set_current_state(TASK_RUNNING);
return 1;
}
static int testb_init(void)
{
printk("before testb funcitos!\n");
testa();
printk("after testb function");
return 0;
}
static void testb_exit(void)
{
printk("testb do nothing!\n");
}
MODULE_AUTHOR("TESTB");
MODULE_LICENSE("GPL");
module_init(testb_init);
module_exit(testb_exit);
4.Makefile
ifneq ($(KERNELRELEASE),)
obj-m:=test.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 *.symvers *.order *.mod
.PHONY: modules modules_install clean
endif
阅读(659) | 评论(0) | 转发(0) |