Linux下檔案描述符

來源:互聯網
上載者:User

檔案描述符是一個簡單的整數,用以標明每一個被進程所開啟的檔案和socket。第一個開啟的檔案是0,第二個是1,依此類推。Unix作業系統通常給每個進程能開啟的檔案數量強加一個限制。更甚的是,unix通常有一個系統級的限制。在UNIX/Linux平台上,對於控制台(Console)的標準輸入(0),標準輸出(1),標準錯誤(2)輸出也對應了三個檔案描述符。

 

對於squid,因為squid 的工作方式,檔案描述符的限制可能會極大的影響效能。當squid 用完所有的檔案描述符後,它不能接收使用者新的串連。也就是說,用完檔案描述符導致拒絕服務。直到一部分當前請求完成,相應的檔案和socket 被關閉,squid不能接收新請求。當squid發現檔案描述符短缺時,它會發布警告。

 

對於Apache,當使用了很多虛擬機器主機,而每個主機又使用了不同的記錄檔時,Apache可能會遭遇耗盡檔案描述符(有時也稱為file handles)的困境。 Apache使用的檔案描述符總數如下:每個不同的錯誤記錄檔檔案一個、 每個其他記錄檔指令一個、再加10~20個作為內部使用。Unix作業系統限制了每個進程可以使用的檔案描述符數量。典型上限是64個,但可以進行擴充,直至到達一個很大的硬限制為止(a large hard-limit)。

 

linux下最大檔案描述符的限制有兩個方面,一個是使用者級的限制,另外一個則是系統級限制。

以下是查看Linux檔案描述符的三種方式:

[root@localhost ~]# sysctl -a | grep -i file-max --color

fs.file-max = 392036

[root@localhost ~]# cat /proc/sys/fs/file-max

392036

[root@localhost ~]# ulimit -n

1024

[root@localhost ~]#

 

系統級限制:sysctl命令和proc檔案系統中查看到的數值是一樣的,這屬於系統級限制,它是限制所有使用者開啟檔案描述符的總和

使用者級限制:ulimit命令看到的是使用者級的最大檔案描述符限制,也就是說每一個使用者登入後執行的程式佔用檔案描述符的總數不能超過這個限制

 

如何修改檔案描述符的值?

1、修改使用者級限制

[root@localhost ~]# ulimit-SHn 10240

[root@localhost ~]# ulimit  -n

10240

[root@localhost ~]#

以上的修改只對當前會話起作用,是臨時性的,如果需要永久修改,則要修改如下:

[root@localhost ~]# grep -vE'^$|^#' /etc/security/limits.conf

*                hard nofile                  4096

[root@localhost ~]#

//預設設定檔中只有hard選項,soft 指的是當前系統生效的設定值,hard 表明系統中所能設定的最大值

[root@localhost ~]# grep -vE'^$|^#' /etc/security/limits.conf

*      hard         nofile       10240

*      soft         nofile      10240

[root@localhost ~]#

// soft<=hard soft的限制不能比hard限制高

 

2、修改系統限制

[root@localhost ~]# sysctl -wfs.file-max=400000

fs.file-max = 400000

[root@localhost ~]# echo350000 > /proc/sys/fs/file-max  //重啟後失效

[root@localhost ~]# cat /proc/sys/fs/file-max

350000

[root@localhost ~]#

//以上是臨時修改檔案描述符

//永久修改把fs.file-max=400000添加到/etc/sysctl.conf中,使用sysctl -p即可

 

下面是摘自kernel document中關於file-max和file-nr參數的說明

file-max & file-nr:

 

The kernel allocates file handles dynamically, but as yet it doesn't free them again.

核心可以動態分配檔案控制代碼,但到目前為止是不會釋放它們的

 

The value in file-max denotes the maximum number of file handles that the Linux kernel will allocate. When you get lots of error messages about running out of file handles, you might want to increase this limit.

file-max的值是linux核心可以分配的最大檔案控制代碼數。如果你看到了很多關於開啟檔案數已經達到了最大值的錯誤資訊,你可以試著增加該值的限制

 

Historically, the three values in file-nr denoted the number of allocated file handles, the number of allocated but unused file handles, and the maximum number of file handles. Linux 2.6 always reports 0 as the number of free file handles -- this is not an error, it just means that the number of allocated file handles exactly matches the number of used file handles.

在kernel 2.6之前的版本中,file-nr 中的值由三部分組成,分別為:1.已經分配的檔案控制代碼數,2.已經分配單沒有使用的檔案控制代碼數,3.最大檔案控制代碼數。但在kernel 2.6版本中第二項的值總為0,這並不是一個錯誤,它實際上意味著已經分配的檔案控制代碼無一浪費的都已經被使用了

 

參考文檔:維基百科 http://salogs.com/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.