Linux named pipes programming problem

A

abottchow

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::eek:ut);
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top