#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. java中的interface接口

    接口:java接口是一些方法表征的集合,但是却不会在接口里实现具体的方法. java接口的特点如下: 1.java接口不能被实例化 2.java接口中声明的成员自动被设置为public,所以不存在pr ...

  2. Servlet之cookie处理

    Cookies 通常设置在 HTTP 头信息中(虽然JavaScript 也可以直接在浏览器上设置一个 Cookie).设置 Cookie 的 Servlet 会发送如下的头信息: HTTP/1.1 ...

  3. ubuntu安装水星MW150US无线网卡8188eu驱动

    买了一个无线网卡插在ubuntu系统的电脑上,却不能识别出来.lsusb,可以看到下面的结果: Bus 002 Device 002: ID 0bda:8179 Realtek Semiconduct ...

  4. 开源项目——小Q聊天机器人V1.0

    小Q聊天机器人V1.0 http://blog.csdn.net/baiyuliang2013/article/details/51386281 小Q聊天机器人V1.1 http://blog.csd ...

  5. java中hashCode()与equals()详解

    首先之所以会将hashCode()与equals()放到一起是因为它们具备一个相同的作用:用来比较某个东西.其中hashCode()主要是用在hash表中提高 查找效率,而equals()则相对而言使 ...

  6. SSH深度历险(七) 剖析SSH核心原理(一)

    接触SSH有一段时间了,但是对于其原理,之前说不出来莫模模糊糊(不能使用自己的语言描述出来的就是没有掌握),在视频和GXPT学习,主要是实现了代码,一些原理性的内容还是欠缺的,这几天我自己也一直在反问 ...

  7. Qualcomm平台camera调试移植入门

    1  camera基本代码架构 高通平台对于camera的代码组织,大体上还是遵循Android的框架:即上层应用和HAL层交互,高通平台在HAL层里面实现自己的一套管理策略:在kernel中实现se ...

  8. How to Use Kdiff3 as a 3-way Merge Tool With Mercurial, Git, and Tower.app

    How to Use Kdiff3 as a 3-way Merge Tool With Mercurial, Git, and Tower.app Jan 12th, 2012            ...

  9. SWING中文件的上传

    <pre name="code" class="java">import java.io.File; import java.io.FileInpu ...

  10. Unity 简易的UI背景昼夜轮替效果

    在UI背景上实现一个简易的有光影照射的昼夜轮替效果,往往比一个死板的UI背景看起来更加形象生动,比较传统的方式是多图轮流替换的序列帧动画,不过要达到整个UI背景大图的所有地方都产生光影效果,那么务必每 ...