linux complete同步机制 一

    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
               
               
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值