#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. 有奖试读—Windows PowerShell实战指南(第2版)

    为什么要学PowerShell? Windows用户都已习惯于使用图形化界面去完成工作,因为GUI总能轻易地实现很多功能,并且不需要记住很多命令.使得短时间学会一种工具成为可能. 但是不幸的是,GUI ...

  2. Erlang 集群互连测试

    Erlang 集群互连测试Erlang节点相同cookie全互联成为一个集群(cluster).如果2个集群不同cookie, 然后其中有节点连接到对方集群的节点,这2个集群会合并成一个集群吗?连接到 ...

  3. 由源代码编译SpriteBuilder最新版本1.5.0搭配最新的Cocos2D 3.4.9

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 大家知道SpriteBuilder版本停留在1.4.9已经很久 ...

  4. Tomcat性能优化及常用命令整理

    1汤姆猫性能优化 1.1连接参数 1.1.1默认连接配置 默认连接器采用阻塞式 IO,默认最大线程数为200,配置如下: <Connector port="8080" pro ...

  5. Android进阶(三)android httpClient 支持HTTPS的访问方式

    项目中Android https请求地址遇到了这个异常(无终端认证): javax.net.ssl.SSLPeerUnverifiedException: No peer certificate 是S ...

  6. x264 n-th pass编码时候Stats文件的含义

    x264 n-th pass(一般是2pass)编码时所用的文件包括下述x264参数生成.stats文件 options: 1280x816 fps=2997/125 timebase=125/299 ...

  7. 1020. Tree Traversals (25) -BFS

    题目如下: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...

  8. SharePoint 2013: The "New Web Application" button is disabled is the central administration

    安装完sharepoint foundation2013后,直接通过url访问管理中心,想进application management去新建的话会发现新建按钮是灰色的,即使你用的是系统管理员账号并且 ...

  9. DB 查询分析器 方便地创建DB2自定义函数

    DB 查询分析器 方便地创建DB2自定义函数                           马根峰            (广东联合电子服务股份有限公司, 广州 510300) 摘要       ...

  10. JAVA之旅(十)——异常的概述,Try-Catch,异常声明Throws,多异常处理,自定义异常,Throw和Throws的区别

    JAVA之旅(十)--异常的概述,Try-Catch,异常声明Throws,多异常处理,自定义异常,Throw和Throws的区别 不知不觉,JAVA之旅这个系列已经更新到第十篇了,感觉如梦如幻,时间 ...