我有一個簡單的 C HTTP 服務器。我關閉了fd由 回傳的磁盤檔案和新連接的檔案描述符accept(...),但我注意到我得到的新檔案描述符編號比以前的編號大:例如,accept 回傳的檔案描述符以 4 開頭,然后是 5,然后又是 4依此類推,直到檔案描述符達到系統上的最大打開檔案描述符。
我已10,000在我的系統上將該值設定為,但我不確定為什么檔案描述符編號會跳轉到最大值。而且我很確定我的程式正在關閉檔案描述符。
所以我想知道如果沒有數千個連接,那么檔案描述符的新數量如何定期增加:在大約 24 小時內我收到 message accept: too many open files。這是什么訊息?
此外,ulimit -n number值是否會在不重啟系統的情況下自動重置?
如答案中所述。的輸出_2$ ps aux | grep lh是
dr-x------ 2 fawad fawad 0 Oct 11 11:15 .
dr-xr-xr-x 9 fawad fawad 0 Oct 11 11:15 ..
lrwx------ 1 fawad fawad 64 Oct 11 11:15 0 -> /dev/pts/3
lrwx------ 1 fawad fawad 64 Oct 11 11:15 1 -> /dev/pts/3
lrwx------ 1 fawad fawad 64 Oct 11 11:15 2 -> /dev/pts/3
lrwx------ 1 fawad fawad 64 Oct 11 11:25 255 -> /dev/pts/3
和輸出ls -la /proc/$$/fd是
root 49855 0.5 5.4 4930756 322328 ? Sl Oct09 15:58 /usr/share/atom/atom --executed-from=/home/fawad/Desktop/C -work/lhparse --pid=49844 --no-sandbox
root 80901 0.0 0.0 25360 5952 pts/4 S 09:32 0:00 sudo ./lh
root 80902 0.0 0.0 1100852 2812 pts/4 S 09:32 0:00 ./lh
fawad 83419 0.0 0.0 19976 916 pts/3 S 11:27 0:00 grep --color=auto lh
我想知道什么是 pts/4 等列。這是檔案描述符編號嗎?
uj5u.com熱心網友回復:
檔案描述符表示的套接字很可能處于 close_wait 或 time_wait 狀態。這意味著 TCP 堆疊將 fd 打開一段時間。因此,在這種情況下,您將無法立即重用它。
一旦套接字完全完成,檔案描述符編號將可在您的程式中重復使用。
請參閱:https : //en.m.wikipedia.org/wiki/Transmission_Control_Protocol
協議操作,特別是等待狀態。
要查看哪些檔案仍處于打開狀態,您可以運行
ls -la /proc/$$/fd
此輸出也將有所幫助。
ss -tan | head -5
LISTEN 0 511 *:80 *:*
SYN-RECV 0 0 192.0.2.145:80 203.0.113.5:35449
SYN-RECV 0 0 192.0.2.145:80 203.0.113.27:53599
ESTAB 0 0 192.0.2.145:80 203.0.113.27:33605
TIME-WAIT 0 0 192.0.2.145:80 203.0.113.47:50685
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/312472.html
