Linux系统提供了一种比信号量更好的同步机制,即completion,它用于一个执行单元等待另一个执行单元执行完某事。Linux系统中与completion相关的操作主要有以下4种:
(1) 定义completion
struct completion my_completion;
(2) 初始化completion
init_completion(&my_completion);
对my_completion的定义和初始化可以通过如下快捷方式实现
DECLEARE_COMPLETION(my_completion);
(3) 等待completion
void wait_for_completion(struct completion *c);
(4) 唤醒completion
void complete(struct completion *c);
void complete_all(struct completion *c);
前者只唤醒一个等待的执行单元,后者唤醒所有等待同一completion的执行单元。
执行单元A 执行单元B
struct completion com;
init_completion(&com);
wake up
wait_for_completion(&com); <----------- complete(&com);
kernel_thread.c
--------------------------------------- #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/param.h> #include <linux/jiffies.h> #include <asm/system.h> #include <asm/processor.h> #include <asm/signal.h> #include <linux/completion.h> // for DECLARE_COMPLETION() #include <linux/sched.h> // for daemonize() and set_current_state() #include <linux/delay.h> // mdelay() static pid_t thread_id; static DECLARE_COMPLETION(my_completion); int my_fuction(void *arg) { printk("<1> in %s()/n", __FUNCTION__); daemonize("demo-thread"); allow_signal(SIGKILL); mdelay(2000); printk("<1> my_function complete()/n"); complete(&my_completion); // wake up wait_for_completion while (!signal_pending(current)) { // no signal printk("<1> jiffies is %lu/n", jiffies); set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(HZ * 5); } return 0; } static int __init init(void) { thread_id = kernel_thread(my_fuction, NULL, CLONE_FS | CLONE_FILES); printk("<1> init wait_for_completion()/n"); wait_for_completion(&my_completion); return 0; } static void __exit finish(void) { kill_proc(thread_id, SIGKILL, 1); printk("<1> Goodbye/n"); } module_init(init); module_exit(finish); MODULE_LICENSE("GPL");Makefile
---------------------------------------
KDIR=/usr/src/kernels/2.6.23.1-42.fc8-i686
obj-m += kernel_thread.o
all:
make -C $(KDIR) M=`pwd` modules
clean:
make -C $(KDIR) M=`pwd` clean