首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

unix环境编程书中的一个有关问题

2012-09-06 
unix环境编程书中的一个问题unix环境编程书中一道例题,#include stdio.h#include sys/types.h#include

unix环境编程书中的一个问题
unix环境编程书中一道例题,
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <strings.h>
#include <unistd.h>

int main()
{
  char buf[1024];
  pid_t pid;
  int status;

  printf("%%");
  while(fgets(buf,1024,stdin) != NULL){
  if(buf[strlen(buf) - 1] == '\n')
  buf[strlen(buf) - 1] =0;
  if(pid = fork() < 0)
  {
  printf("fork error!\n");
  exit(0);
  }
  else if(pid == 0)
  {
  execlp(buf,buf,(char *)0);
  exit(127);
  }

  if((pid = (waitpid (pid,&status,0))) < 0)
  {
  printf("wait error!\n");
  exit(2);
  }
  printf("%%");
  }
  exit(0);

}
可以运行,输入date等参数,也有输出结果,但是不能循环,一次就结束了,而且while语句结束处的打印也没有,请各位指点一下,谢谢!

[解决办法]
if((pid = fork()) < 0)

热点排行