This is my first ever post here, so please excuse any formatting issues.
I have an interactive program which spawns external processes and monitors their IO. Things work fine until I spawn something off with "mpiexec", after which STDIN appears to break.
I realize this will be difficult to reproduce for most folks, but if anyone sees anything obvious or knows of this problem.... please help!
Here's a snippet:
int main( ... )
{
std::string choice;
while(std::getline(std::cin,choice)){
if(!choice.empty()){
if(choice == "Parallel"){
system("mpiexec ./aprogram");
}
if(choice == "Serial"){
system("./aprogram");
}
// Now the external process is done... so far, so good
std::cout << "Program is done. Press ENTER to continue."
<< std::endl;
// This next line *works* if the external process was serial
// But *fails* when "mpiexec" was invoked
std::getline(std::cin,choice);
if(std::cin.eof()){
std::cout << "STDIN has been closed." << std::endl;
exit(1);
}
}
}
}
I have tried lots of various things, e.g. pipes, explicit forking, meticulous descriptor management. The weirdest thing is that if I dup off and save stdin and then restore it after "mpiexec" returns, then I no longer get EOF on std::cin, but instead, std::getline(std::cin,...) no longer blocks! The program goes into an infinite loop reading zero bytes off std::cin in the std::readline call.
If, while the external process is running under mpiexec, I stack a bunch of data into std::cin (e.g. by typing), then subsequent calls to std::readline correctly parse the lines of data that I have stuck in there, but again... once it is done reading through that data, it just keeps going in an infinite loop (i.e. not blocking on std::readline(std::cin,..) even if there is no data to read! Ugh. So annoying.
Any help is deeply appreciated.
Cheers!