#include <linux/module.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include "ioctl_led.h"

#define DEV_NAME	"test-dev"
volatile bool empty = true;
//定义一个进程资源的指针变量
struct task_struct *task;

int test_open(struct inode *inode, struct file *filp)
{
	printk("test open\n");
	return 0;
}

int test_close(struct inode *inode, struct file *filp)
{
	printk("test close\n");
	return 0;
}

ssize_t test_read(struct file *filp, char __user *buf, size_t size, loff_t *off)
{
	int ret;
	//如果为真,那么就开始读
	while(empty)
	{
		//f_flags 指的是对应open调用的时候所指定的flag
		//O_NONBLOCK 非阻塞形式打开
		if(filp->f_flags & O_NONBLOCK)
		{
			return 0;
		}
		//current	(指向当前进程的task_struct)
		//(内核栈的底部thread_info.task)
		task = current;
		//设置当前进程的状态为TASK_INTERRUPTIBLE
		//TASK_INTERRUPTIBLE是阻塞态,进程当前正在等待除CPU外的其他系统资源,可以被信号唤醒.
		set_current_state(TASK_INTERRUPTIBLE);
		//通知调度器执行调度。
		schedule();
		if(signal_pending(current))
			return -ERESTARTSYS;
		//	return -EAGAIN;	

		printk("read: wake up\n");
	}

	ret = size;
	empty = true;
	return ret;
}

ssize_t test_write(struct file *filp, const char __user *buf, size_t size, loff_t *off)
{
	int ret;

	empty = false;
	wake_up_process(task);
	ret = size;

	return ret;
}

int major = 0;
struct file_operations fops = {
	.open = test_open,
	.release = test_close,
	.read = test_read,
	.write = test_write,
};

//模块;
int test_init(void)
{
	int ret;
	printk("test init\n");
	//注册一个字符设备驱动
	ret = register_chrdev(major, DEV_NAME, &fops);
	if(ret < 0)
		return ret;
	else
	{
		if(0 == major)
		{
			major = ret;
			printk("major = %d\n", major);
		}
	}

	return 0;
}

void test_exit(void)
{
	printk("test exit\n");
	//撤销字符设备
	unregister_chrdev(major, DEV_NAME);
}

module_init(test_init);
module_exit(test_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("yang.yx");
MODULE_VERSION("1.1");

Makefile

obj-m	+= test.o

ROOTFS = /rootfs
KERNEL_SRC	= /lib/modules/`uname -r`/build

all:
	make -C $(KERNEL_SRC) M=`pwd` modules

clean:
	make -C $(KERNEL_SRC) M=`pwd` clean
	rm -rf app

install:
	make -C $(KERNEL_SRC) M=`pwd` modules_install INSTALL_MOD_PATH=$(ROOTFS)

app:
	arm-linux-gcc app.c -o app

ioctl.c

#ifndef	__IOCTL_H__
#define __IOCTL_H__

#include <linux/ioctl.h>

#define LED_TYPE		0x1

#define LED_ALLON		_IO(LED_TYPE, 0)
#define LED_ALLOFF		_IO(LED_TYPE, 1)
#define LED_ON			_IOW(LED_TYPE, 2, int)
#define LED_OFF			_IOW(LED_TYPE, 3, int)

#endif

linux设备驱动--等待队列实现的更多相关文章

  1. linux设备驱动概述,王明学learn

    linux设备驱动学习-1 本章节主要学习有操作系统的设备驱动和无操作系统设备驱动的区别,以及对操作系统和设备驱动关系的认识. 一.设备驱动的作用 对设备驱动最通俗的解释就是“驱使硬件设备行动” .设 ...

  2. linux设备驱动归纳总结(七):1.时间管理与内核延时【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-100005.html linux设备驱动归纳总结(七):1.时间管理与内核延时 xxxxxxxxxxx ...

  3. linux设备驱动归纳总结(六):3.中断的上半部和下半部——tasklet【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-100005.html linux设备驱动归纳总结(六):3.中断的上半部和下半部——tasklet x ...

  4. linux设备驱动归纳总结(四):5.多处理器下的竞态和并发【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-67673.html linux设备驱动归纳总结(四):5.多处理器下的竞态和并发 xxxxxxxxxx ...

  5. linux设备驱动归纳总结(四):1.进程管理的相关概念【转】

    本文转载自;http://blog.chinaunix.net/uid-25014876-id-64866.html linux设备驱动归纳总结(四):1.进程管理的相关概念 xxxxxxxxxxxx ...

  6. linux设备驱动归纳总结(三):7.异步通知fasync【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-62725.html linux设备驱动归纳总结(三):7.异步通知fasync xxxxxxxxxxx ...

  7. linux设备驱动归纳总结(三):6.poll和sellct【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-61749.html linux设备驱动归纳总结(三):6.poll和sellct xxxxxxxxxx ...

  8. linux设备驱动归纳总结(三):5.阻塞型IO实现【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-60025.html linux设备驱动归纳总结(三):5.阻塞型IO实现 xxxxxxxxxxxxxx ...

  9. 《Linux设备驱动开发详解(第2版)》配套视频登录51cto教育频道

    http://edu.51cto.com/course/course_id-379-page-1.html http://edu.51cto.com/course/course_id-379-page ...

随机推荐

  1. The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required

    在Android 6.0(API 23)中,Google已经移除了移除了Apache HttpClient相关的类.HttpResponse类.缺失jar包使用HttpResponse等会报错: Th ...

  2. ECMAScript 6之Set和Map数据结构

    Set 基本用法 ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set本身是一个构造函数,用来生成Set数据结构. var s = new Set(); [2, ...

  3. 【IOS 开发】Object - C 面向对象 - 类 , 对象 , 成员变量 , 成员方法

    . 一. 类定义 类定义需要实现两部分 : -- 接口部分 : 定义类的成员变量和方法, 方法是抽象的, 在头文件中定义; -- 实现部分 : 引入接口部分的头文件, 实现抽象方法; 1. 接口部分定 ...

  4. 1. MariaDB简介

    作者: 铁锚 日期: 2013年9月21日 官方博客地址:https://mariadb.org/ 官网地址: https://mariadb.com/ 百度百科地址: http://baike.ba ...

  5. 【一天一道LeetCode】#56. Merge Intervals

    一天一道LeetCode系列 (一)题目 Given a collection of intervals, merge all overlapping intervals. For example, ...

  6. Leetcode_107_Binary Tree Level Order Traversal II

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41964067 Given a binary tree, r ...

  7. 深入理解 JSX

    JSX 是一个看起来很像 XML 的 JavaScript 语法扩展.React 可以用来做简单的 JSX 句法转换. 为什么要使用 JSX? 你不需要为了 React 使用 JSX,可以直接使用纯粹 ...

  8. 高通android开发摘要

    一部分是开源的,可以从codeaurora.org上下载,还有一部分是高通产权的,需要从高通的网站上下载. 将高通产权的代码放到:vendor/qcom/proprietary 1. 设置bms一些参 ...

  9. ffdshow 源代码分析 8: 视频解码器类(TvideoCodecDec)

    ===================================================== ffdshow源代码分析系列文章列表: ffdshow 源代码分析 1: 整体结构 ffds ...

  10. Ubuntu14.04安装Matlab2013a

    source url: http://blog.sina.com.cn/s/blog_ec5021d60102v3ky.html 1. 为方便操作,把Matlab镜像文件(iso)重命名为'Matla ...