我在子进程中执行此操作:execlp ("wc", "wc" ,filename,"-l", NULL)
并将管道中的输出重定向到从父进程中读取。
All working good but when wc
option doesn't find the specified file name it cause an infinite loop. 与查找选项相同。
我如何检查输出execlp
或者我应该怎么做才能不进入这个无限循环?
这是使用 fork 创建的 child 的代码:
close(1);
if (dup (pipeCom[1]) != 1)
{
fprintf (stderr, "dup - 1\n");
exit (1);
}
execlp ("wc", "wc" ,filename,"-l", NULL);`
这是来自父进程的代码:
wait();
if ((num = read(pipeCom[0],&out,200))==0)
perror("pipe error");
else {
out[num] = '\0';
}
printf("%s",out);