Linux设备驱动学习(3) 设备sleepy
来源:互联网 发布:高性能云计算 编辑:程序博客网 时间:2024/05/29 03:17
这个驱动程序可以在2.6.3x内核下编译通过。
sleepy是一个测试程序,当进程读设备时阻塞并进入等待队列,当写设备时则唤醒等待队列上的所有进程。
/* * sleepy.c -- the writers awake the readers * * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet * Copyright (C) 2001 O'Reilly & Associates * * The source code in this file can be freely used, adapted, * and redistributed in source or binary form, so long as an * acknowledgment appears in derived source files. The citation * should list that the code comes from the book "Linux Device * Drivers" by Alessandro Rubini and Jonathan Corbet, published * by O'Reilly & Associates. No warranty is attached; * we cannot take responsibility for errors or fitness for use. * * $Id: sleepy.c,v 1.7 2004/09/26 07:02:43 gregkh Exp $ */#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/wait.h>MODULE_LICENSE("Dual BSD/GPL");static int sleepy_major = 0;static DECLARE_WAIT_QUEUE_HEAD(wq);static int flag = 0;ssize_t sleepy_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_event_interruptible(wq, flag != 0);//若等待条件不为真则进入等待队列wq,并等待条件为真flag = 0;printk(KERN_DEBUG "awoken %i (%s)\n", current->pid, current->comm);return 0; /* EOF */}ssize_t sleepy_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);flag = 1;wake_up_interruptible(&wq);//唤醒等待队列wq上的所有进程return count; /* succeed, to avoid retrial */}struct file_operations sleepy_fops = {.owner = THIS_MODULE,.read = sleepy_read,.write = sleepy_write,};int sleepy_init(void){int result;/* * Register your major, and accept a dynamic number *///老式的字符设备注册函数,若sleepy_major为0,则动态分配主设备号和次设备号result = register_chrdev(sleepy_major, "sleepy", &sleepy_fops);if (result < 0)return result;if (sleepy_major == 0)sleepy_major = result; /* dynamic */return 0;}void sleepy_cleanup(void){unregister_chrdev(sleepy_major, "sleepy");}module_init(sleepy_init);module_exit(sleepy_cleanup);
测试方法
先查看主设备号major
#cat /proc/devices | grep sleepy
建立设备节点
#mknod /dev/sleepy c major 0
读设备
#cat > /dev/sleepy &
可以多运行几次。
写设备
#ls > /dev/sleepy
可以看到,每写一次设备,都会有一个读进程返回。
1.我一直在琢磨究竟wait_event_interruptible()中的可中断是什么意思。这里的中断是外部硬件中断?还是什么异常中断什么的,
因为之前看过x86的cpu中断,还有什么软中断什么的,在我印象中有中断的很多概念。
然后我把程序中的wait_event_interruptible()和wake_up_interruptible()改成了wait_event()和wake_up()试了一下。
唔。。读设备后进程确实无法用ctrl+C,ctrl+Z结束了,关闭终端窗口也无法结束进程。
明白了,这里的中断就是linux中信号产生的中断。
ps aux看一下发现进程的状态标志为D,man ps说是D Uninterruptible sleep。
2.我以次设备号为0 1 2 3 ....建立了设备节点,同样也可以使用,似乎次设备号可以随意值建立,应该是函数
register_chrdev的关系。
3.另外书上说,这个程序中是有竞态的,在重置flag标志为0之前,可能会有多个进程会发现flag==1,这种竞态可能会导致崩溃。。唔,有那么严重么。
书上也说了,用原子方式检查就可以避免这种状态。
- Linux设备驱动学习(3) 设备sleepy
- 《Linux设备设备驱动程序(第三版)》学习笔记之三:sleepy设备的使用
- 《Linux设备设备驱动程序(第三版)》学习笔记之三:sleepy设备的使用
- Linux设备驱动学习
- linux设备驱动学习
- linux设备驱动学习(11) linux设备模型3
- linux设备驱动学习(3) 字符设备驱动程序
- 【Linux设备驱动程序(第三版)】----简单休眠 Sleepy
- linux字符设备驱动学习笔记3
- Linux设备驱动开发学习(3个)
- 《Linux 设备驱动 Edition 3》学习笔记
- linux字符设备驱动学习
- Linux字符设备驱动学习
- 如何学习linux设备驱动
- 如何学习linux设备驱动
- Linux设备驱动学习笔记
- Linux设备驱动学习笔记
- Linux驱动学习字符设备
- 第04章_函数和递归
- StringUtils常用方法
- 怎样做符合用户预期的设计
- Oracle:表名、字段名、constraint名的限制
- 触发器(1)
- Linux设备驱动学习(3) 设备sleepy
- Hello World!
- 自己的web工程是根目录
- 设备管理系统设计和实现
- 正则表达式全集
- 开关键盘
- 第六周任务三
- delphi ADO连数据库学习实例(初级+高级)
- 找出一堆数据中最大或者最小的K个数