cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : UNIX/Linux Programming : pipe()/fork()/execl() functions do not r...
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

post  pipe()/fork()/execl() functions do not return output of external program

engintozal (2)
Hello,
I am writing a program which calls an external program and gets its output. I implemented this functionality into a method. When I called the method for the first time it successfully obtains the output, however, when I called multiple times in the main() it does not obtain the output of the external program after the first call.
Could anyone please help me.
engin

Below is the main structure of the method:
...::ally(){
allyString.clear();//to collect the output of external program
pid_t pid;
int commpipe[2];
int status;

if(pipe(commpipe)==-1){
cout<<"Error in opening pipe"<<endl;
exit(1);
}
pid=fork();
if(pid==-1){
cout<<"Error in forking"<<endl;
exit(1);
}
if(pid>0){//parent process
close(commpipe[1]);
dup2(commpipe[0],0)
;//replace stdin with read side of pipe
wait(&status);//wait for the child to finish
while(!cin.eof()){
char c=cin.get();
allyString.push_back(c);
}
close(commpipe[0]);
}else{//child process
close(commpipe[0]);
dup2(commpipe[1],1);
//replace stdout with write side of pipe
if(execl("/src/local/bin/ally", "ally","154.54.20.26", "154.54.20.26",(char *)0)==-1){//the external program
cout<<"Error in executing the external ally program "<<endl;
exit(1);
}
cout<<"Finished Child process";
close(commpipe[1]);
}
}
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us