文件异步读写
2010-11-26 10:28
1021 查看
文件异步读写在应用程序侧,使用SIGIO的信号来处理。
1. 设置接受SIGIO信号的函数。
2. F_SETOWN
3. FASYNC
在驱动侧,需要fasync函数来处理。
1. F_SETOWN时,驱动不需要做什么
2. FASYNC时,驱动需要将这个进程吧,加入到一个fasync_struct的队列里
3. 当收到数据时,用kill_fasync()通知所有在fasync_struce队列中的进程。
需要注意的一点是,在文件close,也就是驱动中的release中,需要将本进程从fasync_struct队列中移除。
/*
* =====================================================================================
*
* Filename: async.c
*
* Description: this program test the async notificatioin
*
* Version: 1.0
* Created: 11/26/2010 09:49:33 AM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Company:
*
* =====================================================================================
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
static void sig_sigio(int signo)
{
printf("received data/n");
}
int main()
{
pid_t pid;
struct sigaction action, save;
int fd;
/* signal handler for SIGIO */
action.sa_handler = sig_sigio;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
if (sigaction(SIGIO, &action, &save) < 0)
return -1;
fd = open("/dev/scullpipe0", O_RDWR);
if (fd < 0)
{
perror("open()");
return -1;
}
printf("the fd is %d/n", fd);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | FASYNC);
while(1)
{
sleep(50);
}
return 1;
}
1. 设置接受SIGIO信号的函数。
2. F_SETOWN
3. FASYNC
在驱动侧,需要fasync函数来处理。
1. F_SETOWN时,驱动不需要做什么
2. FASYNC时,驱动需要将这个进程吧,加入到一个fasync_struct的队列里
3. 当收到数据时,用kill_fasync()通知所有在fasync_struce队列中的进程。
需要注意的一点是,在文件close,也就是驱动中的release中,需要将本进程从fasync_struct队列中移除。
/*
* =====================================================================================
*
* Filename: async.c
*
* Description: this program test the async notificatioin
*
* Version: 1.0
* Created: 11/26/2010 09:49:33 AM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Company:
*
* =====================================================================================
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
static void sig_sigio(int signo)
{
printf("received data/n");
}
int main()
{
pid_t pid;
struct sigaction action, save;
int fd;
/* signal handler for SIGIO */
action.sa_handler = sig_sigio;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
if (sigaction(SIGIO, &action, &save) < 0)
return -1;
fd = open("/dev/scullpipe0", O_RDWR);
if (fd < 0)
{
perror("open()");
return -1;
}
printf("the fd is %d/n", fd);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | FASYNC);
while(1)
{
sleep(50);
}
return 1;
}
相关文章推荐
- libuv异步文件读写
- VC文件的读写操作中的同步和异步
- 异步读写文件
- 【C#】62. 异步读写文件的几种方法: Task.Factory.FromAsync,WriteAsync
- 文件操作:模拟cp复制功能源码&&文件异步读写
- IO与文件读写---同步/异步与阻塞/非阻塞的区别(转) _适用于TI dsp驱动
- c/c++ 文件的异步读写
- Python笔记-IO同步和异步、 读写文件、StringIO和BytesIO
- fs模块基于async/await异步读写文件
- Asp.net异步文件读写并发处理总结
- C#.net 异步读写文件
- 实现文件异步读写、暂停、继续、停止功能的类
- 【C#】62. 异步读写文件的几种方法: Task.Factory.FromAsync,WriteAsync
- 读写文件精华 (转)
- VC++结构文件的读写操作
- Android入门(5) 读写文件
- C++文件读写
- python 读写文件时,r、rt、rb、r+的区别
- 使用双向文件流来对文件同时读写
- C#读写文件总结