专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > C语言

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

发布时间:2011-06-28 13:30:01 文章来源:www.iduyao.cn 采编人员:星星草
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)
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: