Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 驅動開發中的異步通知機制

很多應用情境需要程式主動確認「驅動裡有沒有資料可讀」或「能不能寫入」。對單線程程式來說,這很容易變成阻塞;用 select 不斷輪詢雖然可行,卻不是好解法。更漂亮的做法是反過來——由驅動在狀態改變時主動通知應用程式。

異步通知的概念近似回呼:應用程式先向驅動註冊一個回呼函數,然後就可以不阻塞地去做別的事;當驅動認為已經可讀或可寫時,就呼叫那個註冊好的回呼,通知才因此完成。下面用一個最小範例說明。

這裡刻意用核心定時器超時作為觸發條件(實務上應依真正的時機觸發),驅動程式如下:

#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/moduleparam.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/poll.h>
#include <linux/fcntl.h>
#include <linux/signal.h>   /* for SIGIO, POLL_IN */
#include <linux/timer.h>
#include <linux/jiffies.h>

#define DEVICE_NAME   "sync"
#define DEVICE_MAJOR  228

/* 異步通知佇列 */
struct fasync_struct *async_queue;
/* 核心定時器 */
struct timer_list my_timer;

/* 定時器超時函數:送出 SIGIO,POLL_IN 表示可讀 */
static void timer_function(unsigned long arg)
{
    kill_fasync(&async_queue, SIGIO, POLL_IN);
}

/* 使用者空間呼叫 fcntl 時會走到這裡 */
static int async_fasync(int fd, struct file *filp, int mode)
{
    return fasync_helper(fd, filp, mode, &async_queue);
}

static int async_open(struct inode *node, struct file *flip)
{
    init_timer(&my_timer);
    my_timer.data = 0;
    my_timer.function = &timer_function;
    my_timer.expires = jiffies + 5 * 200;   /* 目前時間之後 1000 個時鐘滴答 */
    add_timer(&my_timer);
    return 0;
}

static struct file_operations async_fops = {
    .owner  = THIS_MODULE,
    .fasync = async_fasync,
    .open   = async_open,
};

static int __init dev_init(void)
{
    int ret;
    ret = register_chrdev(DEVICE_MAJOR, DEVICE_NAME, &async_fops);
    if (ret < 0) {
        printk(DEVICE_NAME " can't register\n");
        return ret;
    } else {
        printk(DEVICE_NAME " register successful\n");
        return 0;
    }
}

static void __exit dev_exit(void)
{
    unregister_chrdev(DEVICE_MAJOR, DEVICE_NAME);
    printk("unregister success \n");
}

module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("LKN@SCUT");

編譯後會產生 fasync.ko,先載入模組並建立裝置檔:

insmod fasync.ko
mknod /dev/sync c 228 1

接著寫應用程式:

#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>

int flag = 0;

/* 訊號處理函數,由核心觸發 */
void sig_handler(int signo)
{
    if (signo == SIGIO) {
        printf("receive successful!!!\n");
        flag = 1;
    }
}

int main()
{
    struct sigaction action;
    int async_fd;

    memset(&action, 0, sizeof(action));
    action.sa_handler = sig_handler;
    action.sa_flags = 0;
    sigaction(SIGIO, &action, NULL);

    async_fd = open("/dev/sync", O_RDONLY);
    if (async_fd < 0) {
        printf("can not open /dev/sync \n");
        return -1;
    }

    fcntl(async_fd, F_SETOWN, getpid());                       /* 告知驅動目前進程的 PID */
    fcntl(async_fd, F_SETFL, fcntl(async_fd, F_GETFL) | FASYNC); /* 開啟 FASYNC,啟用異步通知 */

    printf("waiting for receive...\n");
    while (!flag) {
    }

    close(async_fd);
    return 0;
}

編譯應用程式:

gcc -o fasync_test fasync_test.c

執行後,程式會先印出 waiting for receive...,等定時器超時、驅動送出 SIGIO,才會跳出 receive successful!!!。重點在於:整個等待過程中,應用程式沒有被鎖住,這正是異步通知有別於輪詢的價值。

Copyright © Linux教程網 All Rights Reserved