0

我正在开发一个简单的应用程序,该应用程序从另一个应用程序获取输出并将其写入套接字。下面是我正在执行的应用程序的代码,此代码称为简单应用程序

    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。如果我设置 read > 0 那么它根本不会运行。想法?

4

1 回答 1

0

正在返回 0(文件结尾),因为管道的read子端已关闭。

你把 dup2 弄错了,应该是

dup2(pipefd[1],STDOUT_FILENO)

也删除

close(STDOUT_FILENO)
于 2013-08-05T12:43:42.493 回答