2013-08-01 37 views
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,那麼它根本不運行。思考?

+1

您的代碼甚至不申報'pipefd'。或者調用'pipe()'。發佈至少編譯的東西。 –

回答

0

read返回0(文件結束),因爲管道的子端已關閉。

你已經得到了DUP2以錯誤的方式,應該是

dup2(pipefd[1],STDOUT_FILENO) 

也刪除

close(STDOUT_FILENO) 
相關問題