[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Named pipes problem



Hi all,

I'm new to the group and am seeking your advice on my Debian Linux C++
programming problem with named pipes.

Two programs are involved. One is myProgram.cc, which reads user's
input from keyboard and prints to the screen. The other program is
main.cc, which wants to communicate with myProgram in real time
using input/output redirection and two named pipes.

Now the problem is that main.cc works fine in one machine (sun4u, OS
version 5.9, sparc, Ultra-60) but hangs in another machine running
Debian Linux--the execution falls into infinite waiting at the first
std::getline statement in main.cc. Could anyone suggest what the
problem might be? The two programs are as follows:


/*****************************************
myProgram.cc
*****************************************/
#include <iostream>
#include <fstream>
#include <string>

main(){
std::string strIn;
std::cout<<"Starting myProgram.\n";
std::cout.flush();
for(int i=1;i<=10;i++){
   std::cout<<"Iteration "<<i<<" out of 10. Please enter: \n";
   std::cout.flush();
   std::getline(std::cin,strIn,'\n');
   std::cout<<"Your input "<<i<<" was: "<<strIn<<"\n";
   std::cout.flush();
}
std::cout<<"\nEnd of myProgram.\n";
std::cout.flush();
return 2;
}

/*****************************************
main.cc
*****************************************/
#include <iostream>
#include <fstream>
#include <string>

std::fstream fin,fout;
std::string path,f1,f2,cmd,str, cmd1;

main(){
system("rm -rf sicsin");
system("rm -rf sicsout");
system("mknod sicsin p");
system("mknod sicsout p");

char *mycmd="./myProgram <sicsin  2>sicsout >sicsout &";
system(mycmd);
usleep(3);

fout.open("sicsin",std::ios::out);
if(!fout.is_open()) {
   std::cout<<"\nsicsin not found.\n";
   return 0;
}
std::cout<<"\nsicsin is opened.\n";

fin.open("sicsout",std::ios::in);
if(!fin.is_open()) {
   std::cout<<"\nsicsout not found.\n";
   return 0;
}
std::cout<<"\nsicsout is opened.\n";

int i;

for(i=1; i<=10; i++){
  fout<<" from main.cc"<<std::endl;
  fout.flush();
// /*Line A*/      }
// /*Line B*/     for(i=1; i<=10; i++){
  std::getline(fin,str,'\n');
  std::cout<<"\nThe content of the line is: "<<str<<"\n";
  std::getline(fin,str,'\n');
  std::cout<<"\nThe content of the line is: "<<str<<"\n";
}

std::getline(fin,str,'\n');
std::cout<<"\nThe content of the next line is: "<<str<<"\n";

std::getline(fin,str,'\n');
std::cout<<"\nThe content of the next line is: "<<str<<"\n";

std::getline(fin,str,'\n');
std::cout<<"\nThe content of the next line is: "<<str<<"\n";


std::cout<<"\nThis is the end of main.cc.\n";
return 2;
}
------------------------------------------------------------------------------------

The above main.cc falls into infinite waiting at the first
std::getline statement when running on Debian Linux. However, if I
remove the "//" in Line A and Line B in main.cc, it will work fine. But
this solution is not what I want because it performs one-off write to
the pipe file rather than real time interactive write and read.

Thanks very much for any suggestion from you!
George



Reply to: