linux complete函数,Linux驱动中completion接口浅析(wait_for_complete例子,很好)

completion是一种轻量级的机制,它容许一个线程告诉另外一个线程工做已经完成。能够利用下面的宏静态建立completion:                          DECLARE_COMPLETION(my_completion);         linux

若是运行时建立completion,则必须采用如下方法动态建立和初始化:                          struct compltion my_completion;                           init_completion(&my_completion);redis

completion的相关定义包含在kernel/include/Linux/completion.h中:shell

struct completion {                                      unsigned int done;                                      wait_queue_head_t wait;                          };app

#define COMPLETION_INITIALIZER(work) /                                                            { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }less

#define DECLARE_COMPLETION(work) /                                                       struct completion work = COMPLETION_INITIALIZER(work)测试

static inline void init_completion(struct completion *x) {           x->done = 0;           init_waitqueue_head(&x->wait); }ui

要等待completion,可进行以下调用:                     void wait_for_completion(struct completion *c);this

触发completion事件,调用:                    void complete(struct completion *c);    //唤醒一个等待线程                    void complete_all(struct completion *c);//唤醒全部的等待线程线程

为说明completion的使用方法,将《Linux设备驱动程序》一书中的complete模块的代码摘抄以下: /* * complete.c -- the writers awake the readers * * Copyright (C) 2003 Alessandro Rubini and Jonathan Corbet * Copyright (C) 2003 O'Reilly & Associates * * The source code in this file can be freely used, adapted, * and redistributed in source or binary form, so long as an * acknowledgment appears in derived source files.    The citation * should list that the code comes from the book "Linux Device * Drivers" by Alessandro Rubini and Jonathan Corbet, published * by O'Reilly & Associates.     No warranty is attached; * we cannot take responsibility for errors or fitness for use. * * $Id: complete.c,v 1.2 2004/09/26 07:02:43 gregkh Exp $ */日志

#include #include

#include    /* current and everything */ #include  /* printk() */ #include       /* everything... */ #include    /* size_t */ #include

MODULE_LICENSE("Dual BSD/GPL");

static int complete_major = 253;//指定主设备号

DECLARE_COMPLETION(comp);

ssize_t complete_read (struct file *filp, char __user *buf, size_t count, loff_t *pos) {          printk(KERN_DEBUG "process %i (%s) going to sleep/n",          current->pid, current->comm);          wait_for_completion(&comp);          printk(KERN_DEBUG "awoken %i (%s)/n", current->pid, current->comm);          return 0; /* EOF */ }

ssize_t complete_write (struct file *filp, const char __user *buf, size_t count,     loff_t *pos) {          printk(KERN_DEBUG "process %i (%s) awakening the readers.../n",          current->pid, current->comm);          complete(&comp);          return count; /* succeed, to avoid retrial */ }

struct file_operations complete_fops = {          .owner = THIS_MODULE,          .read =    complete_read,          .write = complete_write, };

int complete_init(void) {          int result;

/*     * Register your major, and accept a dynamic number     */         result = register_chrdev(complete_major, "complete", &complete_fops);         if (result < 0)                 return result;         if (complete_major == 0)                 complete_major = result; /* dynamic */         return 0; }

void complete_cleanup(void) {          unregister_chrdev(complete_major, "complete"); }

module_init(complete_init); module_exit(complete_cleanup);

该模块定义了一个简单的completion设备:任何试图从该设备中读取的进程都将等待,直到其余设备写入该设备为止。编译此模块的Makefile以下: obj-m := complete.o KDIR := /lib/modules/$(Shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KDIR) M=$(PWD) modules clean: rm -f *.ko *.o *.mod.c

在linux终端中执行如下命令,编译生成模块,并进行动态加载。 #make #mknod completion c 253 0 #insmod complete.ko 再打开三个终端,一个用于读进程: #cat completion 一个用于写进程: #echo >completion 另外一个查看系统日志: #tail -f /var/log/messages

值得注意的是,当咱们使用的complete_all接口时,若是要重复使用一个completion结构,则必须执行 INIT_COMPLETION(struct completion c)来从新初始化它。能够在kernel/include/linux/completion.h中找到这个宏的定义:           #define INIT_COMPLETION(x) ((x).done = 0)

如下代码对书中原有的代码进行了一番变更,将唤醒接口由原来的complete换成了complete_all,而且为了重复利用completion结构,全部读进程都结束后就从新初始化completion结构,具体代码以下: #include #include

#include #include #include #include #include

MODULE_LICENSE("Dual BSD/GPL");

#undef KERN_DEBUG #define KERN_DEBUG "<1>"

static int complete_major=253; static int reader_count = 0;

DECLARE_COMPLETION(comp);

ssize_t complete_read (struct file *filp,char __user *buf,size_t count,loff_t *pos) {            printk(KERN_DEBUG "process %i (%s) going to sleep,waiting for writer/n",current->pid,current->comm);            reader_count++;            printk(KERN_DEBUG "In read ,before comletion: reader count = %d /n",reader_count);            wait_for_completion(&comp);            reader_count--;            printk(KERN_DEBUG "awoken %s (%i) /n",current->comm,current->pid);            printk(KERN_DEBUG "In read,after completion : reader count = %d /n",reader_count);

/*若是使用complete_all,则completion结构只能用一次,再次使用它时必须调用此宏进行从新初始化*/            if(reader_count == 0)                        INIT_COMPLETION(comp);

return 0; }

ssize_t complete_write(struct file *filp,const char __user *buf,size_t count,loff_t *pos) {            printk(KERN_DEBUG "process %i (%s) awoking the readers.../n",current->pid,current->comm);            printk(KERN_DEBUG "In write ,before do complete_all : reader count = %d /n",reader_count);

if(reader_count != 0)                      complete_all(&comp);

printk(KERN_DEBUG "In write ,after do complete_all : reader count = %d /n",reader_count);

return count; }

struct file_operations complete_fops={            .owner = THIS_MODULE,            .read = complete_read,            .write = complete_write, };

int complete_init(void) {            int result;

result=register_chrdev(complete_major,"complete",&complete_fops);            if(result<0)                     return result;            if(complete_major==0)                    complete_major =result;

printk(KERN_DEBUG    "complete driver test init! complete_major=%d/n",complete_major);            printk(KERN_DEBUG "静态初始化completion/n");

return 0; }

void complete_exit(void) {            unregister_chrdev(complete_major,"complete");            printk(KERN_DEBUG    "complete driver    is removed/n"); }

module_init(complete_init); module_exit(complete_exit);

这里测试步骤和上述同样,只不过须要多打开几个终端来执行多个进程同时读操做。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值