By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Linux named pipes programming problem

P: n/a
Hi all,

I'm new to the group and am seeking your advice on my Linux programming
problem.

Two programs are involved. One is myProgram.cc, which reads user's
input from keyboard and prints output 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 the 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<<i<<i<<i<<i<<i<<i<<i<<i<<i<<i<<i<<i<<" 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 line "<<i<<" is: "<<str<<"\n";
std::getline(fin,str,'\n');
std::cout<<"\nThe content of line "<<i<<" 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 program 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

Dec 13 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.