Linux下select呼叫的過程

2021-09-30 07:48:56 字數 1630 閱讀 1853

以下來自網路搜尋:

linux

下select呼叫的過程: 1.

使用者層應用程式呼叫select(),底層呼叫poll()) 2.

核心層呼叫sys_select() ------> do_select()

最終呼叫檔案描述符fd對應的struct file型別變數的struct file_operations *f_op的poll函式。

poll

指向的函式返回當前可否讀寫的資訊。 1)

如果當前可讀寫,返回讀寫資訊。 2)

如果當前不可讀寫,則阻塞程序,並等待驅動程式喚醒,重新呼叫poll函式,或超時返回。 3.

驅動需要實現poll函式。

當驅動發現有資料可以讀寫時,通知核心層,核心層重新呼叫poll指向的函式查詢資訊。

poll_wait(filp,&wait_q,wait) //

此處將當前程序加入到等待佇列中,但並不阻塞

在中斷中使用wake_up_interruptible(&wait_q)喚醒等待佇列

select(i/o多工機制)

表頭檔案

#include

#include

#include

定義函式

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

函式說明

select()用來等待檔案描述詞狀態的改變。引數n代表最大的檔案描述詞加1,引數readfds、writefds 和exceptfds 稱為描述片語,是用來回傳該描述詞的讀,寫或例外的狀況。底下的巨集提供了處理這三種描述片語的方式:

fd_clr(inr fd,fd_set* set);用來清除描述片語set中相關fd 的位

fd_isset(int fd,fd_set *set);用來測試描述片語set中相關fd 的位是否為真

fd_set(int fd,fd_set*set);用來設定描述片語set中相關fd的位

fd_zero(fd_set *set); 用來清除描述片語set的全部位

引數

timeout為結構timeval,用來設定select()的等待時間,其結構定義如下

struct timeval

;返回值

如果引數timeout設為null則表示select()沒有timeout。

錯誤**

執行成功則返回檔案描述詞狀態已改變的個數,如果返回0代表在描述詞狀態改變前已超過timeout時間,當有錯誤發生時則返回-1,錯誤原因存於errno,此時引數readfds,writefds,exceptfds和timeout的值變成不可**。

ebadf 檔案描述詞為無效的或該檔案已關閉

eintr 此呼叫被訊號所中斷

einval 引數n 為負值。

enomem 核心記憶體不足

範例

常見的程式片段:fs_set readset;

fd_zero(&readset);

fd_set(fd,&readset);

select(fd+1,&readset,null,null,null);

if(fd_isset(fd,readset)

Linux下select 系統呼叫

一 select 函式功能 select系統呼叫允許程式同時在多個底層檔案表述符上,等待輸入的到達或輸出的完成。二 函式意義 乙個伺服器可以同時在多個開啟的套接字等待請求到來的方法而處理多個客戶。只是具體應用的其中之一 自己的理解,但凡是程式在執行過程中會遇到阻塞 不到條件發生就不往下執行 的情況,...

linux下的Select函式

part1 fd set errorfds同上面兩個引數的意圖,用來監視檔案錯誤異常。struct timeval timeout是select的超時時間,這個引數至關重要,它可以使select處於三種狀態,第一,若將null以形參傳入,即不傳入時間結構,就是將select置於阻塞狀態,一定等到監視...

linux下select函式的使用

先看下列的例子程式 include include include include define stdin 0 define true 1 define false 0 define ulong unsigned long static struct timeval timelast static...