我正在研究一个简单的应用程序,它从另一个应用程序获取输出并将其写入套接字。 下面是我正在执行的应用程序的代码 此代码称为简单应用程序
int main( int argc, char * argv[])
{
int count = 0;
while(count < 5)
//Attempt fork
if((pid2 = fork()) < 0) //Failed to fork
{
printf("\n Failed to fork in Dump \n");
exit(1);
}
else if (pid2 == 0) //Child Code
{
dup2(STDOUT_FILENO,pipefd[1]);
close(pipefd[0]);
close(STDOUT_FILENO);
if(execvp("../App/App", dumpParam) < 0) //execute the app; returns -1 if failed
{
printf("\nFailed to execute App\n");
exit(1);
}
}
else //Parent
{
close(pipefd[1]);
memset(buff,'0',sizeof(buff));
printf("here\n");
while( (r = read(pipefd[0],buff,sizeof(buff))) >= 0)
{
printf("\nSuccess in read r = %d\n",r);
printf("\nBuff = %s\n",buff);
memset(buff,'0',sizeof(buff));
}
printf("nowHere\n");
}
return pipefd[0];
到目前为止,当我运行它时,它打印出的只有0吨。如果我设置阅读&gt; 0然后它根本不运行。想法?
答案 0 :(得分:0)
read
返回0(文件末尾),因为管道的子端已关闭。
你的dup2方向错了,应该是
dup2(pipefd[1],STDOUT_FILENO)
也删除
close(STDOUT_FILENO)