概述
completions是Linux内核中轻量级的同步机制,使用它完成内核中不同线程之间的同步,允许一个线程告诉另一个线程工作已经完成。
接口
包含内核头文件
<include/completion.h>
创建一个completion可以使用
DECLARE_COMPLETION(my_completion);
也可以使用
struct completion my_completion;
init_completion(&my_completion);
等待completion可以使用
void wait_for_completion(struct completion *c);
注意这个函数将会进行一个不可打断的等待。如果你的程序调用了wait_for_completion
,然后没有人完成这个任务, 则会成为一个不可杀死的进程。
当进程完成了动作,需要同步给其它进程可以调用
void complete(struct completion *c);
void complete_all(struct completion *c); //可以用于唤醒多个线程
实例
#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 = 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\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);
insmod安装模块之后,读取/proc/devices文件可以找到complete的主设备号,这样就可以使用"mknod /dev/complete c 主设备号 次设备号"创建对应的设备。
可以通过cat/echo
命令读写/proc/devices文件来测试上面的程序。
欢迎访问我的博客