成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

聊一聊 .NET在Linux下的IO多路復用select和epoll

系統 Linux
在windows平臺上,相信很多人都知道.NET異步機制是借助了Windows自帶的?IO完成端口?實現的異步交互,那在 Linux 下.NET 又是怎么玩的呢?主要還是傳統的 select,poll,epoll 的IO多路復用,在 coreclr源代碼中我們都能找到它們的影子。

一、背景

1. 講故事

在windows平臺上,相信很多人都知道.NET異步機制是借助了Windows自帶的 IO完成端口 實現的異步交互,那在 Linux 下.NET 又是怎么玩的呢?主要還是傳統的 select,poll,epoll 的IO多路復用,在 coreclr源代碼中我們都能找到它們的影子。

select & poll

在平臺適配層的 pal.cpp 文件中,有這樣的一句話。

#if HAVE_POLL
#include <poll.h>
#else
#include "pal/fakepoll.h"
#endif  // HAVE_POLL

簡而言之就是在不支持 poll 的linux版本中使用 select(fakepoll) 模擬,參考代碼如下:

圖片圖片

  2. epoll

同樣的在 linux 中你也會發現很多,截圖如下:

圖片圖片

二、select IO多路復用

1. select 解讀

在沒有 select 之前,我們需要手工管理多句柄的收發,在使用select IO多路復用技術之后,這些多句柄管理就由用戶轉交給linux系統了,這個也可以從核心的 select 函數看出。

int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
  • readfds,writefds,exceptfds

這三個字段依次監視著哪些句柄已成可讀狀態,哪些句柄已成可寫狀態,哪些句柄已成異常狀態,那技術上是如何實現的呢?在libc 中定義了一個 bit 數組,剛好文件句柄fd值作為 bit數組的索引,linux 在內核中只需要掃描 __fds_bits 中哪些位為1 即可找到需要監控的句柄。

/* fd_set for select and pselect.  */
typedef struct
  {
    /* XPG4.2 requires this member name.  Otherwise avoid the name
       from the global namespace.  */
#ifdef __USE_XOPEN
    __fd_mask fds_bits[__FD_SETSIZE / __NFDBITS];
# define __FDS_BITS(set) ((set)->fds_bits)
#else
    __fd_mask __fds_bits[__FD_SETSIZE / __NFDBITS];
# define __FDS_BITS(set) ((set)->__fds_bits)
#endif
  } fd_set;
  •  nfds,timeout

為了減少掃描范圍,提高程序性能,需要用戶指定一個最大的掃描值到 nfds 上。后面的timeout即超時時間。

2. select 的一個小例子

說了再多還不如一個例子有說服力,我們使用 select 機制對 Console 控制臺句柄 (STDIN_FILENO) 進行監控,一旦有數據進來立馬輸出,參考代碼如下:

#include <stdio.h>
#include <sys/select.h>
#include <unistd.h>

int main()
{
    fd_set readfds;
    struct timeval timeout;
    char buf[256];

    printf("Enter text (press Ctrl+D to end):\n");

    while (1)
    {
        FD_ZERO(&readfds);
        FD_SET(STDIN_FILENO, &readfds);
        timeout.tv_sec = 5; // 5秒超時
        timeout.tv_usec = 0;

        int ready = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &timeout);

        if (ready == -1)
        {
            perror("select");
            break;
        }
        elseif (ready == 0)
        {
            printf("\nTimeout (5秒無輸入).\n");
            break;
        }
        elseif (FD_ISSET(STDIN_FILENO, &readfds))
        {
            // 使用 fgets 逐行讀取
            if (fgets(buf, sizeof(buf), stdin) != NULL)
            {
                printf("You entered: %s", buf); // 輸出整行(包含換行符)
            }
            else
            {
                printf("\nEnd of input (Ctrl+D pressed).\n");
                break;
            }
        }
    }

    return0;
}

圖片圖片

稍微解釋下代碼邏輯。

/* Standard file descriptors.  */
#define STDIN_FILENO 0 /* Standard input.  */
#define STDOUT_FILENO 1 /* Standard output.  */
#define STDERR_FILENO 2 /* Standard error output.  */
  • 將 STDIN_FILENO=0 塞入到可讀句柄監控 (readfds) 中。
  • 數據進來之后 select 被喚醒,執行后續邏輯。
  • 通過 FD_ISSET 判斷 bit=0 的位置(STDIN_FILENO)是否可用,可用的話讀取數據。

如果大家對 select 底層代碼感興趣,可以看下 linux 的 do_select 簡化實現,大量的遍歷邏輯(bit)。

static noinline_for_stack int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)
{
for (;;) {
unsignedlong *rinp, *routp, *rexp, *inp, *outp, *exp;
bool can_busy_loop = false;

  inp = fds->in; outp = fds->out; exp = fds->ex;
  rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;

for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
   in = *inp++; out = *outp++; ex = *exp++;
   all_bits = in | out | ex;

   for (j = 0; j < BITS_PER_LONG; ++j, ++i, bit <<= 1) {
    mask = select_poll_one(i, wait, in, out, bit,busy_flag);
    if ((mask & POLLIN_SET) && (in & bit)) {
     res_in |= bit;
     retval++;
     wait->_qproc = NULL;
    }
    if ((mask & POLLOUT_SET) && (out & bit)) {
     res_out |= bit;
     retval++;
     wait->_qproc = NULL;
    }
    if ((mask & POLLEX_SET) && (ex & bit)) {
     res_ex |= bit;
     retval++;
     wait->_qproc = NULL;
    }
   }
  }

if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE, to, slack))
   timed_out = 1;
 }

return retval;
}

三、epoll IO多路復用

1. epoll 解讀

現在主流的軟件(Redis,Nigix) 都是采用 epoll,它解決了select低效的遍歷,畢竟數組最多支持1024個bit位,一旦句柄過多會影響異步讀取的效率。epoll的底層借助了。

  • 紅黑樹:對句柄進行管理,復雜度為 O(logN)。
  • 就緒隊列:一旦句柄變得可讀或可寫,內核會直接將句柄送到就緒隊列。

libc中使用 epoll_wait 函數監視著就緒隊列,一旦有數據立即提取,復雜度 O(1),其實這個機制和 Windows 的IO完成端口 已經很靠近了,最后配一下參考代碼。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <errno.h>

#define MAX_EVENTS 10   // 最大監聽事件數
#define TIMEOUT_MS 5000 // epoll_wait 超時時間(毫秒)

int main()
{
    int epoll_fd, nfds;                        // epoll 文件描述符和返回的事件數
    struct epoll_event ev, events[MAX_EVENTS];// epoll 事件結構體
    char buf[256];

    // 創建 epoll 實例
    epoll_fd = epoll_create1(0);
    if (epoll_fd == -1)
    {
        perror("epoll_create1");
        exit(EXIT_FAILURE);
    }

    // 配置并添加標準輸入到 epoll 監聽
    ev.events = EPOLLIN;       // 監聽文件描述符的可讀事件(輸入)
    ev.data.fd = STDIN_FILENO; // 監聽標準輸入(文件描述符 0)

    if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
    {
        perror("epoll_ctl: STDIN_FILENO");
        exit(EXIT_FAILURE);
    }

    printf("Enter text line by line (press Ctrl+D to end):\n");

    // 主循環:監聽事件
    while (1)
    {
        // 等待事件發生或超時
        nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, TIMEOUT_MS);

        if (nfds == -1)
        {
            perror("epoll_wait");
            break;
        }
        elseif (nfds == 0)
        {
            printf("\nTimeout (5秒無輸入).\n");
            break;
        }

        // 處理所有觸發的事件
        for (int n = 0; n < nfds; ++n)
        {
            if (events[n].data.fd == STDIN_FILENO)
            {
                // 使用 fgets 逐行讀取輸入
                if (fgets(buf, sizeof(buf), stdin) != NULL)
                {
                    printf("You entered: %s", buf);
                }
                else
                {
                    // 輸入結束(用戶按下 Ctrl+D)
                    printf("\nEnd of input (Ctrl+D pressed).\n");
                    break;
                }
            }
        }
    }

    close(epoll_fd);
    return0;
}

圖片圖片

四、總結

說了這么多,文尾總結下目前主流的 epoll 和 iocp 各自的特點。

特性

epoll (Linux)

IOCP (Windows)

模型

事件驅動 (Reactor)

完成端口 (Proactor)

核心思想

通知可讀寫事件

通知I/O操作完成

適用場景

高并發網絡編程

高并發I/O操作

編程復雜度

較低

較高

網絡I/O性能

極佳(百萬級連接)

優秀

磁盤I/O支持

有限

完善

CPU利用率

內存開銷


責任編輯:武曉燕 來源: 一線碼農聊技術
相關推薦

2021-05-31 06:50:47

SelectPoll系統

2023-03-01 14:32:31

redisIOEpoll

2021-03-05 11:26:42

面試Java程序

2023-12-13 09:45:49

模型程序

2023-01-09 10:04:47

IO多路復用模型

2022-09-12 06:33:15

Select多路復用

2024-08-08 14:57:32

2023-12-06 07:16:31

Go語言語句

2021-07-16 11:48:26

模型 .NET微軟

2020-10-14 09:11:44

IO 多路復用實現機

2022-08-26 00:21:44

IO模型線程

2019-02-13 14:15:59

Linux版本Fedora

2020-06-28 09:30:37

Linux內存操作系統

2025-05-13 07:10:31

2023-11-07 08:19:35

IO多路復用磁盤、

2024-12-30 00:00:05

2021-01-26 05:06:24

LinuxXargs 命令

2021-01-04 08:09:07

Linux內核Watchdog

2021-08-26 09:31:40

Nacos配置注冊

2025-04-24 10:05:51

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 在线国产一区 | 久久国内精品 | 国产精品一区二区三区在线播放 | 亚洲一区二区三区免费观看 | 国产精品福利网 | 欧美一区二区三区,视频 | 青青久视频 | 国产精品成人一区二区三区 | www国产成人免费观看视频,深夜成人网 | 99re在线| 日韩在线视频播放 | 亚洲天堂一区 | 久久免费福利 | 成人精品毛片国产亚洲av十九禁 | 欧美一区二区在线免费观看 | 亚洲人成人一区二区在线观看 | 久久久久久久网 | 亚洲视频免费在线观看 | av黄色免费| 国产精品一区在线观看你懂的 | 国产精品亚洲精品日韩已方 | 色综合久久久久 | 激情 婷婷 | 欧美黄视频 | 日本亚洲欧美 | 日韩精品免费在线观看 | 成人精品国产一区二区4080 | 56pao在线 | 国产日产精品一区二区三区四区 | 一区中文字幕 | 免费性视频| 91在线精品视频 | 亚洲精品美女视频 | 欧洲一区二区在线 | 99精品一区二区三区 | 久久久久久综合 | av一区二区在线观看 | 天天操天天射综合网 | 中文字幕在线二区 | 亚洲精品一区二区三区在线 | 夜夜爽99久久国产综合精品女不卡 |