yjcdoc 发表于 2013-03-15 10:49

请教!!!unix环境编程实例 双输出

#include "apue.h"
#include "my_err.h"
#include <sys/wait.h>
int main()
{
      char buf;
      pid_t pid;
      int status;

      printf("%% ");
      while(fgets(buf,MAXLINE,stdin) != NULL)
      {
                if(buf=='\n')
                buf=0;

                if(pid=fork()<0)
                {err_sys("fork error");}
                else if(pid==0){
                        execlp(buf,buf,(char *)0);
                        err_ret("couldnot execute:%s",buf);
                        exit(127);
                }

                if(pid=waitpid(pid,&status,0))
                        err_sys("waitpid error");
                printf("%% ");

      }
}
# ./a.out
% date
三3月 13 14:04:26 CST 2013
三3月 13 14:04:26 CST 2013

txgc_wm 发表于 2013-03-17 17:52

if(pid=fork()<0)   //错误的根源正确的写法:if((pid=fork()) < 0)

yjcdoc 发表于 2013-03-19 21:21

首先非常感谢,但是每次运行完命令之后直接结束进程,退出?回复 2# txgc_wm


   

yjcdoc 发表于 2013-03-19 21:24

知道原因了 谢谢!回复 2# txgc_wm


   
页: [1]
查看完整版本: 请教!!!unix环境编程实例 双输出