linux - Linux 中每个进程的最大打开文件数

标签 linux pipe limit ulimit

我使用命令: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 );
}

最佳答案

这里没有问题。

管道有两端,每一端都有自己的文件描述符。

因此,管道的每一端都算作一个超出限制的文件。

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

关于linux - Linux 中每个进程的最大打开文件数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30108288/

相关文章:

mysql - 按长度排序(城市) ASC 限制 1;在 MYSQL 中不显示第一项

c++ - 创建大量文件后,fopen失败并显示EFBIG “File too large”

Linux shell脚本运行多个shell脚本选项卡

linux - cut、colrm、awk 和 sed : fail to cut characters from a pipe stream 的奇怪问题

c++ - 在 C/C++ 中,我想多次写入同一个管道

perl - 当从 Perl 向执行 xargs 的子进程打开 n 个管道时,n-1 个进程收到一个空行

c - 使用写函数时出现未初始化字节的 valgrind 错误

python - django 1.8.3 python manage.py 迁移 Linux

linux - perl + 将 json 转换为 csv 文件

mysql - 选择每列值有限制的数据