日期:2014-05-16  浏览次数:20658 次

请看这段代码
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
  int fd[2];
  char str[256];
  if(pipe(fd)<0) //创建管道
{perror("error");
exit(1);}
  write(fd[1],"create the pipe successfully\n",31); //向管道中写入数据
  read(fd[0],str,sizeof(str)); //从管道中读取数据到字符数组中
  printf("%s",str);
  printf("pipe file descriptors are %d,%d\n",fd[0],fd[1]);
  close(fd[0]); //关闭管道的读端
  close(fd[1]); //关闭管道的写入端
  return 0;
}
为什么结果是:pipe file descriptors are 3,4
其中结果为什么是3,4?谢谢各位回答!

------解决方案--------------------
因为0 1 2 fd被 stdin stdout stderr占用了

------解决方案--------------------
fd值会使用未占用的最小值

------解决方案--------------------
楼上正解