读书人

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

发布时间: 2012-09-06 10:37:01 作者: rapoo

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)

读书人网 >C语言

热点推荐