0
我正在研究一個簡單的應用程序,它從另一個輸出並將其寫入套接字。 下面是,我execvp'ing 這段代碼的應用程序的代碼被稱爲簡單的應用程序將子stdout發送到父標準輸出
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的。如果我設置讀> 0,那麼它根本不運行。思考?
您的代碼甚至不申報'pipefd'。或者調用'pipe()'。發佈至少編譯的東西。 –