Chinaunix首页 | 论坛 | 博客
  • 博客访问: 230648
  • 博文数量: 48
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 548
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-28 19:18
文章分类

全部博文(48)

文章存档

2010年(6)

2009年(6)

2008年(36)

我的朋友

分类: LINUX

2008-12-16 10:13:52

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); <----------- complete(&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("<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
阅读(2084) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~