【问题标题】:Max number of open files per process in LinuxLinux中每个进程的最大打开文件数
【发布时间】:2015-05-07 17:48:28
【问题描述】:

我使用命令:ulimit -n 并取数字 1024,这是我系统中每个进程打开文件的最大数量。但是通过以下程序,我选择了 510 ...?怎么了

    #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 );
}

【问题讨论】:

  • 它并不是一个进程可以拥有的文件的最大数量,它是文件描述符的最大数量,可以是从管道到打开的网络套接字的任何东西。

标签: linux pipe limit ulimit


【解决方案1】:

这里没有问题。

pipe 有两端,每个端都有自己的文件描述符。

因此,pipe 的每一端都算作一个文件,不受限制。

1024/2 = 512和510之间的细微差别是因为你的进程已经打开了stdin、stdout和stderr文件,这算在限制内。

【讨论】:

    【解决方案2】:

    对于每个 pipe() 调用,您将获得两个文件描述符。这就是为什么它以 512 结束。

    man 2 pipe 说"pipefd[0] refersto the read end of the pipe. pipefd[1] refers to the write end of the pipe. "

    【讨论】:

      猜你喜欢
      • 2011-10-01
      • 2011-08-03
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      相关资源
      最近更新 更多