linux嵌入式系统驱动程序的阻塞与异步
2013-12-17 19:51
423 查看
对于那些需要进程独占的设备,需要使用linux提供的阻塞编程。步骤如下:
1.在设备驱动程序中定义该设备的进程等待列多,并将其初始化
对于异步驱动编程,需要做的步骤如下:
1.定义设备驱动程序的异步通知列队
1.在设备驱动程序中定义该设备的进程等待列多,并将其初始化
static wait_queue_head_t wait_queue;
init_waitqueue_head(&wait_queue);
2.在设备驱动程序的读操作中,调用函数wait_event 实现阻塞访问init_waitqueue_head(&wait_queue);
int mixled_read(struct file * filp,char__user * buffer,size_t size,loff_t * ppos)
{
wait_event(wait_queue,Elmixled_dev->full_flag!=0);
elmixled_dev->full_flag = 0;
if(copy_to_user(buffer,Elmixled_dev->buf,read_lea))
{
printk(“copy to user err!”);
}
return lead_len;
}
在写操作中,调用wake_up函数唤醒该设备等待进程列队上的进程{
wait_event(wait_queue,Elmixled_dev->full_flag!=0);
elmixled_dev->full_flag = 0;
if(copy_to_user(buffer,Elmixled_dev->buf,read_lea))
{
printk(“copy to user err!”);
}
return lead_len;
}
int mixled_write(struct file * filp, const char__user * buffer,size_t size,loff_t * ppos)
{
int write_len =size;
if(copy_from_user(Elmixled_dev->buf,buffer,write_len))
{
printk(“copy from user err”);
}
elmixled_dev->full_flag = 1;
wake_up(&wait_queue);
return write_len;
}
{
int write_len =size;
if(copy_from_user(Elmixled_dev->buf,buffer,write_len))
{
printk(“copy from user err”);
}
elmixled_dev->full_flag = 1;
wake_up(&wait_queue);
return write_len;
}
对于异步驱动编程,需要做的步骤如下:
1.定义设备驱动程序的异步通知列队
struct fasync_struct * fasync_queue;
实现设备驱动程序的异步操作函数fasync,并在异步操作函数fasync中调用fasync_helper函数将当前进程添加到设备驱动程序的异步通知列队 int mixled_fasync(int fd,struct file * file ,int on)
{
printk(“enter mixled_fasync function”);
return fasync_helper(fd,file,on,&Elmixled_dev->fasync_queue);
}
struct file_operations mixled_ops={
…
.fasync = mixled_fasync
}
3.在设备驱动程序中,当检测到设备状态信息发生变化时,如数据到达或者按键被按下就需要通知应用程序,调用函数kill_fasync想应用程序发送异步通知消息{
printk(“enter mixled_fasync function”);
return fasync_helper(fd,file,on,&Elmixled_dev->fasync_queue);
}
struct file_operations mixled_ops={
…
.fasync = mixled_fasync
}
irqeturn_t mixled_isr(int irq,void * dev_id)
{
printk(“key interrupt take place”);
if(Elmixled_dev-》fasync_queue)
{
kill_fasync(&Elmixled_dev->fasync_queue,SIGIO,POLLMSG)
}
return IRQ_HANDLED;
}
4.在设备驱动的close函数中,调用mixled_fasync函数将当前进程从设备的异步通知列队中移除{
printk(“key interrupt take place”);
if(Elmixled_dev-》fasync_queue)
{
kill_fasync(&Elmixled_dev->fasync_queue,SIGIO,POLLMSG)
}
return IRQ_HANDLED;
}
mixled_fasync(-1,file,0)
相关文章推荐
- 07-S3C2440驱动学习(一)嵌入式linux字符设备驱动-按键驱动程序之异步通知机制+原子操作+互斥信号量+阻塞与非阻塞+定时器去抖
- 嵌入式Linux系统下I2C设备驱动程序的开发
- 嵌入式Linux系统下I2C设备驱动程序的开发
- 基于嵌入式Linux系统设备驱动程序的开发
- 实验题目:实现嵌入式Linux系统下的字符设备驱动程序(报告)
- 嵌入式Linux系统下I2C设备驱动程序的开发
- 嵌入式linux系统中设备驱动程序
- 嵌入式Linux驱动学习之路(九)Linux系统调用、驱动程序框架
- 嵌入式Linux系统之I.MX6触摸屏驱动程序TSC2007.C的分析、移植与校准
- linux及win系统关于阻塞,非阻塞,同步I/O,异步I/O概念的统一解析
- 嵌入式Linux系统下I2C设备驱动程序的开发
- linux嵌入式开发中,由串口日志中需要输入选择而导致的系统阻塞处理方法
- 嵌入式Linux系统下I2C设备驱动程序的开发
- 嵌入式Linux2.6 根文件系统制作
- 单片机和嵌入式系统linux的区别
- 嵌入式Linux系统的GDB远程调试的实现
- 嵌入式Linux常用文件系统
- 学习嵌入式Linux系统开发基础 第二课(Linux常用命令)
- (转载)嵌入式设备上的 Linux 系统开发
- 嵌入式Linux之我行——内核、文件系统、应用移植开发篇