void test()
{
int pipeDes[2], pipeDesErr[2];
if ( pipe(pipeDes) == -1 || pipe(pipeDesErr) == -1 )
{
cout << "pipe create failed" << endl;
return;
}
int childPID = fork();
/// 0 이면 자식프로세
if ( childPID == 0 )
{
cout << "child process" << endl;
dup2(pipeDes[1], 1);
dup2(pipeDesErr[1], 2);
close(pipeDes[0]);
close(pipeDes[1]);
close(pipeDesErr[0]);
close(pipeDesErr[1]);
execl("./test", "./test", "", (char*)0);
exit(0);
}
else if ( childPID > 0 )
{
cout << "parent process" << endl;
/*
int secondChildPID = fork();
if ( secondChildPID == 0 )
{
cout << "second child process" << endl;
dup2(pipeDesErr[0], 0);
close(pipeDes[0]);
close(pipeDes[1]);
close(pipeDesErr[0]);
close(pipeDesErr[1]);
}
*/
close(pipeDes[1]);
close(pipeDesErr[1]);
cout << "|start|" << endl;
stringstream strSTDOUT, strSTDERR;
while ( true )
{
char szBuff[1024] = { 0 };
int ret = read(pipeDes[0], szBuff, sizeof(szBuff));
if ( ret <= 0 ) break;
cout << szBuff << endl;
}
while ( true )
{
char szBuff[1024] = { 0 };
int ret = read(pipeDesErr[0], szBuff, sizeof(szBuff));
if ( ret <= 0 ) break;
cout << szBuff << endl;
}
cout << "|end|" << endl;
}
}