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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

最高效的进(线)程间通信机制--eventfd

發(fā)布時間:2023/11/30 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最高效的进(线)程间通信机制--eventfd 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我們常用的進(jìn)程(線程)間通信機(jī)制有管道,信號,消息隊(duì)列,信號量,共享內(nèi)存,socket等等,其中主要作為進(jìn)程(線程)間通知/等待的有管道pipe和socketpair。線程還有特別的condition。

今天來看一個liunx較新的系統(tǒng)調(diào)用,它是從LINUX 2.6.27版本開始增加的,主要用于進(jìn)程或者線程間的通信(如通知/等待機(jī)制的實(shí)現(xiàn))。


首先來看一下函數(shù)原型:

[cpp]?view plain?copy
  • #include?<sys/eventfd.h>??
  • ??
  • ???????int?eventfd(unsigned?int?initval,?int?flags);??
  • 下面是它man手冊中的描述,我照著翻譯了一遍(我英語四級434,你們要是懷疑下文的話,可以直接去man eventfd ^^):


    eventfd()創(chuàng)建了一個"eventfd object",能在用戶態(tài)用做事件wait/notify機(jī)制,通過內(nèi)核取喚醒用戶態(tài)的事件。這個對象保存了一個內(nèi)核維護(hù)的uint64_t類型的整型counter。這個counter初始值被參數(shù)initval指定,一般初值設(shè)置為0。


    它的標(biāo)記可以有以下屬性:

    EFD_CLOECEX,EFD_NONBLOCK,EFD_SEMAPHORE。

    在linux直到版本2.6.26,這個flags參數(shù)是沒用的,必須指定為0。


    它返回了一個引用eventfd object的描述符。這個描述符可以支持以下操作:

    read:如果計(jì)數(shù)值counter的值不為0,讀取成功,獲得到該值。如果counter的值為0,非阻塞模式,會直接返回失敗,并把errno的值指紋EINVAL。如果為阻塞模式,一直會阻塞到counter為非0位置。

    write:會增加8字節(jié)的整數(shù)在計(jì)數(shù)器counter上,如果counter的值達(dá)到0xfffffffffffffffe時,就會阻塞。直到counter的值被read。阻塞和非阻塞情況同上面read一樣。

    close:這個操作不用說了。


    重點(diǎn)是支持這個:

    ?poll(2), select(2) (and similar)
    ? ? ? ? ? ? ? The returned file descriptor supports poll(2) (and analogously epoll(7)) and select(2), as follows:
    ? ? ? ? ? ? ? * ?The file descriptor is readable (the select(2) readfds argument; the poll(2) POLLIN flag) if the ?counter ?has ?a
    ? ? ? ? ? ? ? ? ?value greater than 0.
    ? ? ? ? ? ? ? * ?The ?file descriptor is writable (the select(2) writefds argument; the poll(2) POLLOUT flag) if it is possible to
    ? ? ? ? ? ? ? ? ?write a value of at least "1" without blocking.
    ? ? ? ? ? ? ? * ?If an overflow of the counter value was detected, then select(2) indicates the ?file ?descriptor ?as ?being ?both
    ? ? ? ? ? ? ? ? ?readable ?and ?writable, ?and ?poll(2) ?returns a POLLERR event. ?As noted above, write(2) can never overflow the
    ? ? ? ? ? ? ? ? ?counter. ?However an overflow can occur if 2^64 eventfd "signal posts" were performed by the KAIO subsystem (the‐
    ? ? ? ? ? ? ? ? ?oretically possible, but practically unlikely). ?If an overflow has occurred, then read(2) will return that maxi‐
    ? ? ? ? ? ? ? ? ?mum uint64_t value (i.e., 0xffffffffffffffff).
    ? ? ? ? ? ? ? The eventfd file descriptor also supports the other file-descriptor multiplexing APIs: pselect(2) and ppoll(2)
    .


    它的內(nèi)核代碼實(shí)現(xiàn)是這樣子的:

    [cpp]?view plain?copy
  • int?eventfd_signal(struct?eventfd_ctx?*ctx,?int?n)??
  • {??
  • ????unsigned?long?flags;??
  • ??
  • ????if?(n?<?0)??
  • ????????return?-EINVAL;??
  • ????spin_lock_irqsave(&ctx->wqh.lock,?flags);??
  • ????if?(ULLONG_MAX?-?ctx->count?<?n)??
  • ????????n?=?(int)?(ULLONG_MAX?-?ctx->count);??
  • ????ctx->count?+=?n;??
  • ????if?(waitqueue_active(&ctx->wqh))??
  • ????????wake_up_locked_poll(&ctx->wqh,?POLLIN);??
  • ????spin_unlock_irqrestore(&ctx->wqh.lock,?flags);??
  • ??
  • ????return?n;??
  • }??
  • 本質(zhì)就是做了一次喚醒,不用read,也不用write,與eventfd_write的區(qū)別是不用阻塞。


    說了這么多,我們來看一個例子,理解理解其中的含義:

    [cpp]?view plain?copy
  • #include?<sys/eventfd.h>??
  • #include?<unistd.h>??
  • #include?<stdlib.h>??
  • #include?<stdio.h>??
  • #include?<stdint.h>?????????????/*?Definition?of?uint64_t?*/??
  • ??
  • #define?handle_error(msg)?\??
  • ????do?{?perror(msg);?exit(EXIT_FAILURE);?}?while?(0)??
  • ??
  • int??
  • main(int?argc,?char?*argv[])??
  • {??
  • ????int?efd,?j;??
  • ????uint64_t?u;??
  • ????ssize_t?s;??
  • ??????
  • ????if?(argc?<?2)?{??
  • ????????fprintf(stderr,?"Usage:?%s?<num>...\n",?argv[0]);??
  • ????????exit(EXIT_FAILURE);??
  • ????}?????
  • ??
  • ????efd?=?eventfd(0,?0);???
  • ????if?(efd?==?-1)???
  • ????????handle_error("eventfd");??
  • ??????
  • ????switch?(fork())?{??
  • ????????case?0:??
  • ????????????for?(j?=?1;?j?<?argc;?j++)?{??
  • ????????????printf("Child?writing?%s?to?efd\n",?argv[j]);??
  • ????????????u?=?strtoull(argv[j],?NULL,?0);???
  • ????????????/*?strtoull()?allows?various?bases?*/??
  • ?????s?=?write(efd,?&u,?sizeof(uint64_t));??
  • ????????????if?(s?!=?sizeof(uint64_t))??
  • ????????????????handle_error("write");??
  • ????????????}??
  • ????????????printf("Child?completed?write?loop\n");??
  • ????????????exit(EXIT_SUCCESS);??
  • ????????default:??
  • ????????????sleep(2);??
  • ??
  • ????????????printf("Parent?about?to?read\n");??
  • ????????????s?=?read(efd,?&u,?sizeof(uint64_t));??
  • ????????????if?(s?!=?sizeof(uint64_t))??
  • ????????????????handle_error("read");??
  • ????????????printf("Parent?read?%llu?(0x%llx)?from?efd\n",??
  • ???????????????????(unsigned?long?long)?u,?(unsigned?long?long)?u);??
  • ????????????exit(EXIT_SUCCESS);??
  • ??
  • ????????case?-1:??
  • ????????????handle_error("fork");??
  • ??}??
  • }??
  • 輸出:

    $ ./a.out 1 2 4 7 14
    ? ? ? ? ? ?Child writing 1 to efd
    ? ? ? ? ? ?Child writing 2 to efd
    ? ? ? ? ? ?Child writing 4 to efd
    ? ? ? ? ? ?Child writing 7 to efd
    ? ? ? ? ? ?Child writing 14 to efd
    ? ? ? ? ? ?Child completed write loop
    ? ? ? ? ? ?Parent about to read
    ? ? ? ? ? ?Parent read 28 (0x1c) from efd

    注意:這里用了sleep(2)保證子進(jìn)程循環(huán)寫入完畢,得到的值就是綜合28。如果不用sleep(2)來保證時序,當(dāng)子進(jìn)程寫入一個值,父進(jìn)程會立馬從eventfd讀出該值。

    總結(jié)

    以上是生活随笔為你收集整理的最高效的进(线)程间通信机制--eventfd的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。