文件异步读写

文件异步读写在应用程序侧,使用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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值