2008年07月29日 星期二 21:46 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); kernel_thread.c --------------------------------------- #include #include #include #include #include #include #include #include #include // for DECLARE_COMPLETION() #include // for daemonize() and set_current_state() #include // mdelay() static pid_t thread_id; static DECLARE_COMPLETION(my_completion); int my_fuction(void *arg) { printk(" in %s()\n", __FUNCTION__); daemonize("demo-thread"); allow_signal(SIGKILL); mdelay(2000); printk(" my_function complete()\n"); complete(&my_completion); // wake up wait_for_completion while (!signal_pending(current)) { // no signal printk(" 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(" init wait_for_completion()\n"); wait_for_completion(&my_completion); return 0; } static void __exit finish(void) { kill_proc(thread_id, SIGKILL, 1); printk(" 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 |
|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )
GMT+8, 2024-9-30 07:28 , Processed in 0.094961 second(s), 12 queries , Gzip On, MemCache On.
Powered by Discuz! X3.5
© 2001-2023 Discuz! Team.