linux kernel中的completion

概述

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文件来测试上面的程序。


欢迎访问我的博客

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一章 设备驱动程序的简介 处于上层应用与底层硬件设备的软件层 区分机制和策略是Linux最好的思想之一,机制指的...
    xiaozi63阅读 836评论 0 0
  • feisky云计算、虚拟化与Linux技术笔记posts - 1014, comments - 298, trac...
    不排版阅读 3,916评论 0 5
  • 大学的时候,帮朋友写的操作系统调研的作业,最近整理过去的文档时候偶然发现,遂作为博客发出来。 从串口驱动到Linu...
    free_will阅读 7,441评论 7 59
  • 本文通过对Linux下串口驱动的分析。由最上层的C库。到操作系统系统调用层的封装。再到tty子系统的核心。再到一系...
    Leon_Geo阅读 947评论 0 3
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 126,028评论 2 7