日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

异步通知

發布時間:2024/9/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 异步通知 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

驅動程序與應用程序不能直接通信,如果設備已經準備好數據,可以采用異步通知的方式通知應用層來讀取,這樣應用程序就不需要一直查詢設備的狀態。要支持異步通知,需要實現設備驅動程序的fasync接口。當一個打開的文件的FASYNC標志變化時file_operations ->fasync()接口將被調用。file_operations ->fasync函數會調用fasync_helper從相關的進程列表中添加或去除異步通知關聯。

  • int?fasync_helper(int?fd,?struct?file?*filp,?int?mode,?struct?fasync_struct?**fa);?
  • 當數據到達時 kill_fasync函數將被用來通知相關的進程:
  • void?kill_fasync(struct?fasync_struct?**fa,?int?sig,?int?band);?
  • 例1.8? 異步通知實例

    代碼見光盤\src\1drivermodel\1-8fasync。驅動層代碼如下:

  • struct?simple_dev?*simple_devices; ?
  • static?unsigned?char?simple_inc=0; ?
  • static?struct?timer_list?simple_timer; ?
  • static?struct?fasync_struct?*fasync_queue=NULL; ?
  • int?simple_open(struct?inode?*inode,?struct?file?*filp) ?
  • { ?
  • ????struct?simple_dev?*dev; ?
  • ????dev?=?container_of(inode->i_cdev,?struct?simple_dev,?cdev); ?
  • ????filp->private_data?=?dev; ?
  • ????simple_timer.function?=?&simple_timer_handler; ?
  • ????simple_timer.expires?=?jiffies?+?2*HZ; ?
  • ????add_timer?(&simple_timer); ?
  • ????printk("add_timer...\n"); ?
  • ????return?0; ?
  • } ?
  • //異步通知處理函數 ?
  • static?int?simple_fasync(int?fd,?struct?file?*?filp,?int?mode)? ?
  • { ?
  • ????int?retval; ?
  • ????printk("simple_fasync...\n"); ?
  • ????retval=fasync_helper(fd,filp,mode,&fasync_queue); ?
  • ????if(retval<0) ?
  • ??????return?retval; ?
  • ????return?0; ?
  • } ?
  • int?simple_release(struct?inode?*inode,?struct?file?*filp) ?
  • { ?
  • ????simple_fasync(-1,?filp,?0); ?
  • ????return?0; ?
  • } ?
  • struct?file_operations?simple_fops?=?{ ?
  • ????.owner?=????THIS_MODULE, ?
  • ????.open?=?????simple_open, ?
  • ????.release=???simple_release, ?
  • ????.fasync=????simple_fasync, ?
  • }; ?
  • 當數據來臨時通知應用層:
  • static?void?simple_timer_handler(?unsigned?long?data) ?
  • { ?
  • ????printk("simple_timer_handler...\n"); ?
  • ????if?(fasync_queue) ?
  • ????{ ?
  • ??????//POLL_IN為可讀,POLL_OUT為可寫 ?
  • ??????kill_fasync(&fasync_queue,?SIGIO,?POLL_IN); ?
  • ??????printk("kill_fasync...\n"); ?
  • ????} ?
  • ????return?; ?
  • } ?
  • POLL_IN表示設備可讀,POLL_OUT表示設備可寫。應用層參考代碼如下:
  • int?fd; ?
  • void?fasync_handler(int?num) ?
  • { ?
  • ???printf("fasync_handler?entering\n"); ?
  • } ?
  • void?main() ?
  • { ?
  • ??int?i=2; ?
  • ??char?data[256]; ?
  • ??int?oflags=0; ?
  • ??int?retval; ?
  • ??signal(SIGIO,?fasync_handler);//注冊信號處理函數 ?
  • ??fd=open("/dev/fcn",O_RDWR); ?
  • ??if(fd==-1) ?
  • ??{ ?
  • ?????perror("error?open\n"); ?
  • ?????exit(-1); ?
  • ??} ?
  • ??printf("open?/dev/fcn?successfully\n"); ?
  • ??//使能了異步的通知到當前進程 ?
  • ??fcntl(fd,?F_SETOWN,?getpid()); ?
  • ??oflags=fcntl(fd,?F_GETFL); ?
  • ??fcntl(fd,?F_SETFL,?oflags?|?FASYNC);//修改文件標志 ?
  • ??while(1); ?
  • ??close(fd); ?
  • } ?
  • 運行結果如下:
  • [root@urbetter?/home]#?insmod?demo.ko ?
  • [root@urbetter?/home]#?mknod?/dev/fcn?c?226?0 ?
  • [root@urbetter?/home]#?./test ?
  • add_timer... ?
  • open?/dev/fcn?successfullysimple_fasync... ?
  • ?
  • simple_timer_handler... ?
  • kill_fasync... ?
  • fasync_handler?entering ?
  • 總結

    以上是生活随笔為你收集整理的异步通知的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。