我使用命令: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 ); }
这里没有问题。
pipe
有两端,每个都有自己的文件描述符。
所以, pipe
每一端都被视为一个违反限制的文件。
1024/2 = 512和510之间的细微差别是因为你的进程已经打开了文件stdin,stdout和stderr,这个数量超过了限制。
对于每个pipe()调用,您将获得两个文件描述符。 这就是为什么它结束于512。
man 2 pipe表示"pipefd[0] refersto the read end of the pipe. pipefd[1] refers to the write end of the pipe. "
表示管道"pipefd[0] refersto the read end of the pipe. pipefd[1] refers to the write end of the pipe. "