Linux信号异步通知应用和驱动编程

一:设备驱动层

 struct x_dev{

    ...

   struct fasync_struct *async;

};

/*fasync函数*/

static int x_fasync(int fd, struct file *filp, int mode)

{

    struct x_dev *dev = filp->private_data;

    return fasync_helper(fd, filp, mode, &dev->async);

}

 

/*添加到 file_operations*/

static const struct file_operations x_fops = {
     ...
    .fasync      =     x_fasync,
    ....
};

 

/*读写函数中释放信号*/

 struct x_dev *dev = filp->private_data;

if(dev->async)

    kill_fasync(&dev->async, SIGIO, POLL_IN(或者POLL_OUT));

 

/*release函数中删除*/

  x_fasync(-1, filp, 0 );

 

二:用户空间

static void signal_handler(int signum)

{

}

 

 void main(void)

{

    int fd, flag;

    fd = open("/dev/xx", O_RDWR, S_IRUSR | S_IWUSR);

    signal(SIGIO, signal_handler);

    fcntl(fd, F_SETOWN, getpid());

    flag = fcntl(fd, F_GETFL);

    fcntl(fd, F_SETFL, flag | FASYNC);

    while(1);

}

 

 

 

你可能感兴趣的:(linux驱动,c语言编程)