Linux named pipes programming problem

Discussion in 'C Programming' started by abottchow@yahoo.com, Dec 13, 2005.

  1. Guest

    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
     
    , Dec 13, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. piyush
    Replies:
    0
    Views:
    1,869
    piyush
    Jul 14, 2004
  2. Joe Estock

    Re: Linux named pipes programming problem

    Joe Estock, Dec 13, 2005, in forum: C Programming
    Replies:
    1
    Views:
    443
  3. Tomek

    Python and named pipes problem

    Tomek, Feb 26, 2008, in forum: Python
    Replies:
    0
    Views:
    506
    Tomek
    Feb 26, 2008
  4. Replies:
    17
    Views:
    4,785
    CBFalconer
    Jan 13, 2009
  5. Phil Tomson

    Bidirectional named pipes on Linux

    Phil Tomson, Jun 16, 2004, in forum: Ruby
    Replies:
    11
    Views:
    1,147
    John Carter
    Jun 17, 2004
Loading...

Share This Page