首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

请看这段代码解决方案

2012-02-28 
请看这段代码#includeunistd.h#includestdio.h#includestdlib.hint main(){int fd[2]char str[256]

请看这段代码
#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值会使用未占用的最小值

[解决办法]
楼上正解

热点排行