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 <linux/module.h> #include <linux/init.h>
#include <linux/sched.h> /* current and everything */ #include <linux/kernel.h> /* printk() */ #include <linux/fs.h> /* everything... */ #include <linux/types.h> /* size_t */ #include <linux/completion.h>
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 <linux/module.h> #include <linux/init.h>
#include <linux/sched.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/types.h> #include <linux/completion.h>
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);
這裏測試步驟和上述同樣,只不過須要多打開幾個終端來執行多個進程同時讀操做。