2015-05-07 21 views
2

我使用的命令是:ulimit -n,我得到的數字是1024,這是我係統中每個進程打開文件的最大數量。但是通過下面的程序我可以看到數字510 ......?出了什麼問題Linux中每個進程打開的文件的最大數量

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


    int main(void) 

{ 
     int pipe_ext = 0, pfd[ 2 ], counter = 0; 

     while (1) 
     { 
       pipe_ext = pipe(pfd); 
       //errno = 0; 

       if (pipe_ext == 0) 
       { 
         write(pfd[ 1 ], "R", 1); 

         counter = counter + 1; 
         printf("Counter = %d\n", counter); 
       } 
       else 
       { 
         perror("pipe()"); 
         printf("errno = %d\n", errno); 
         exit(1); 
       } 

     } 

     return(0); 
} 
+0

它不是一個真正的文件,進程的最大數目,它的文件描述符的最大數量,這可能是從管什麼開放網絡套接字。 – John

回答

3

這裏沒有問題。

A pipe有兩端,每個都有它自己的文件描述符。

因此,pipe的每一端都會被視爲違反限制的文件。

1024/2 = 512和510之間的細微差別是因爲您的進程已經打開stdin,stdout和stderr文件,這些文件會超出限制。

1

對於每個pipe()調用,您將獲得兩個文件描述符。這就是爲什麼它在512

人結束了2管說"pipefd[0] refersto the read end of the pipe. pipefd[1] refers to the write end of the pipe. "

相關問題