Linux下檔案描述符
檔案描述符是一個簡單的整數,用以標明每一個被程式所開啟的檔案和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/
相關文章
- Linux檔案描述符Linux
- 檔案包含之包含了Linux檔案描述符Linux
- Linux 檔案描述符和重定向Linux
- Linux--檔案描述符、檔案指標、索引節點Linux指標索引
- Linux下利用檔案描述符恢復的成功失敗實驗Linux
- 檔案描述符和檔案系統
- 檔案描述符、開啟檔案表以及inode
- linux系統程式設計之檔案與IO(一):檔案描述符、open,closeLinux程式設計
- windows 檔案描述符 _open_osfhandleWindows
- linux一切皆檔案之Unix domain socket描述符(二)LinuxAI
- linux下core檔案Linux
- 基於檔案描述符的恢復
- dup函式:複製檔案描述符函式
- linux下解壓.gz檔案、.cpio檔案、zip檔案Linux
- 使用檔案描述符恢復誤刪除的資料檔案
- 檔案描述符恢復rm掉的dbf
- Linux下合併檔案Linux
- LINUX下的檔案加密Linux加密
- LINUX下建立空檔案Linux
- linux下主要檔案(轉)Linux
- Linux下把sra檔案轉成fastq檔案LinuxAST
- Linux下的檔案及檔案字尾名Linux
- 【MySQL】檔案描述符導致報警一則MySql
- 如何在 Linux 伺服器上設定 ulimit 和檔案描述符數限制Linux伺服器MIT
- linux系統程式設計之檔案與IO(八):檔案描述符相關操作-dup,dup2,fcntlLinux程式設計
- Linux下安裝whl檔案Linux
- linux下檢視hosts檔案Linux
- linux下的檔案結構Linux
- Linux下輸出excel檔案LinuxExcel
- linux下解壓rar檔案Linux
- linux下檔案的校驗Linux
- 在Linux下建立大檔案Linux
- linux下的檔案共享(轉)Linux
- day58:Linux:BashShell&linux檔案管理&linux檔案下載上傳Linux
- 一文幫你搞懂 Android 檔案描述符Android
- APUE 2-17 確定檔案描述符個數
- 在父子程式間用管道傳遞檔案描述符
- Linux下批量將md檔案轉換為html檔案LinuxHTML