#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. emysql add_poop() 超时出错

    emysql add_poop() 超时出错(金庆的专栏)sample/a_hello.erl 连接本机更改为连接局域网内的MySql服务器:    emysql:add_pool(hello_poo ...

  2. 利用ScrollView滑动属性实现点击查看更多

    利用ScrollView的滚动实现点击查看更多 效果图 更新内容布局 <ScrollView android:id="@+id/sv_des" android:layout_ ...

  3. spring @Qualifier注解使用

    @Autowired是根据类型进行自动装配的.如果当Spring上下文中存在多个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在U ...

  4. 【Netty源码解析】NioEventLoop

    上一篇博客[Netty源码学习]EventLoopGroup中我们介绍了EventLoopGroup,实际说来EventLoopGroup是EventLoop的一个集合,EventLoop是一个单线程 ...

  5. 【java虚拟机系列】从java虚拟机字节码执行引擎的执行过程来彻底理解java的多态性

    我们知道面向对象语言的三大特点之一就是多态性,而java作为一种面向对象的语言,自然也满足多态性,我们也知道java中的多态包括重载与重写,我们也知道在C++中动态多态是通过虚函数来实现的,而虚函数是 ...

  6. antlr v4 使用指南连载4——词法规则入门之黄金定律

    词法规则入门 黄金定律一二 若输入串能被多个词法规则匹配,那么声明在词法文件最前面的规则生效. parser parser grammar HelloParser; options { languag ...

  7. JQuery其他常用函数

    isArray(obj)                      检测obj否为一个数组对象 isFunction(obj)                 检测obj否为一个函数 isEmptyO ...

  8. dbcp连接池不合理的锁导致连接耗尽

    应用报错,表象来看是连接池爆满了. org.springframework.transaction.CannotCreateTransactionException: Could not open J ...

  9. 最简单的基于librtmp的示例:发布H.264(H.264通过RTMP发布)

    ===================================================== 最简单的基于libRTMP的示例系列文章列表: 最简单的基于librtmp的示例:接收(RT ...

  10. Activity之间的数据传递-android学习之旅(四十七)

    activity之间的数据传递主要有两种,一种是直接发送数据,另一种接受新启动的activity返回的数据,本质是一样的 使用Bundle传递数据 Intent使用Bundle在activity之间传 ...