//set the output to the pipe
if (dup2 (pipe_id[1], 2) == -1) {//重定向输出
cout << "(Child Failed to duplicate) " << endl;
closedir (rdir);
close (pipe_id[1]);
_exit (-1);
}
close (pipe_id[1]);
//run a command
execvp ("ls", NULL);
}
}
else if (pid == -1) {//error
cerr << "Failed to creat a child process" << endl;
_exit (-1);
}
else {//parent
cout << "parent running" << endl;
string buff;
//wait for the child run
close (pipe_id[1]);
//get the chlid's output
char ct[256];
memset (ct, 0, 256);
//wait for output "EOF"
int len = 0;
while (1) {
len = read (pipe_id[0], ct , 256);//在这读通道,就是ls的输出
if (len > 0) {
//do with the output
cout << "(Pipe: " << ct << ')' << endl;
ct[len] = 0;
buff += ct;
if (buff.find ("EOF") != string::npos) {
sleep (WAIT_FOR_TERMINATE);
kill (pid, SIGTERM);
int term;
waitpid (pid, &term, 0);
delete[] vlc_argv;
remove (rfilename);
break;
}
}
else
break;
}