您的位置:首页 > 其它

文件异步读写

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: