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

麻烦大家帮看一个管道的问题
程序名: testmore.c

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char * argv[])
{
  char buf[4096];
  pid_t child;
  int fds[2];
   
  if (pipe(fds) != 0)
  {
  exit(0);
  }

  if ((child = fork()) < 0)
  {
  exit (0);
  }
  else if(child == 0)
  {
  close(fds[1]);
  if (fds[0] != STDIN_FILENO)
  {
  dup2(fds[0], STDIN_FILENO);
  }
  close(fds[0]);

  execl("/bin/more", "more", 0);

  perror("exec:");
  }

  close(fds[0]);
   
  while (fgets(buf, sizeof(buf)-1, stdin) != NULL)
  {
  write(fds[1], buf, strlen(buf)); 
  } 

  buf[0] = EOF;
  write(fds[1], buf, strlen(buf));

  wait(NULL);

  return 0;
}

照着UNIX高级编程弄的代码
执行 ./testmore < testmore.c
程序会一直卡在wait(NULL)那里.
并且会有SIGTTOU信号.为什么会有SIGTTOU信号呢, 假如我自己写个写标准输入的程序, 子进程去execl执行, 却没有SIGTTOU
高手帮忙哦.


------解决方案--------------------

自己多调试一下

------解决方案--------------------
顶一下,我也想知道~