Hey,

I just discovered a very interesting phenomenon.

I have 2 programs.

m.cpp

#include <iostream>
#include<time.h>
using namespace std;

int main()
{
int a=34,b=40;
while(1)
{
usleep(400000);
cout<<a<<" "<<b<<"\n";
}
}

and

n.cpp

#include<iostream>
#include<stdlib.h>

using namespace std;

int main()
{
int a,b;

while(1)
{
cin>>a>>b;

cout<<a<<b;
}
}

Now ,when I do ./m | ./n
nothing is printed


Can anyone please explain why?

Recommended Answers

All 8 Replies

There are two problems:

(1) m must terminate before n can use its output as input.
(2) n doesn't terminate.

These should work ok:

m.cpp

#include <iostream>

using namespace std;

int main()
{
    int a=34, b=40;
    int count = 0;

    while(1)
    {
        if (++count > 10) break;

        cout << a << " " << b << endl;
    }
}

n.cpp

#include<iostream>

using namespace std;

int main()
{
    int a, b;

    while(1)
    {
        cin >> a >> b;

        if (!cin) break;

        cout << a << " " << b << endl;
    }
}

There are two problems:

(1) m must terminate before n can use its output as input.
(2) n doesn't terminate.

These should work ok:

m.cpp

#include <iostream>

using namespace std;

int main()
{
    int a=34, b=40;
    int count = 0;

    while(1)
    {
        if (++count > 10) break;

        cout << a << " " << b << endl;
    }
}

n.cpp

#include<iostream>

using namespace std;

int main()
{
    int a, b;

    while(1)
    {
        cin >> a >> b;

        if (!cin) break;

        cout << a << " " << b << endl;
    }
}

But what if I want to run both programs simultaneously?........Is it not possible to do that.........these problems that u hav mentioned go away if u remove the usleep statement from m.cpp.......in that case.....all values are printed when I do
./m | ./n

and also m is not terminating before n is using m's output as input...and n never terminates....

so the reason why the code in my post doesnt work has to be something different.........

Indeed, my bad. Try adding a cin.clear(); call before cin>>a>>b; (use your programs, not mine) and see if it works.

EDIT:

Ok, these here should work ok:

m.cpp

#include <iostream>
#include<time.h>

using namespace std;

int main()
{
    int a=34, b=40;

    while(1)
    {
        usleep(400000);

        cout << a << " " << b << endl;
    }
}

n.cpp

#include<iostream>

using namespace std;

int main()
{
    int a, b;

    while(1)
    {
        cin.clear();

        cin >> a >> b;

        if (!cin) continue;

        cout << a << " " << b << endl;
    }
}

The (real) problem is that m generates numbers slower than n can consume them,
which causes some input operations to fail. Clearing the stream solves the problem.

Indeed, my bad. Try adding a cin.clear(); call before cin>>a>>b; (use your programs, not mine) and see if it works.

Ok, these here should work ok:

m.cpp

#include <iostream>
#include<time.h>

using namespace std;

int main()
{
    int a=34, b=40;

    while(1)
    {
        usleep(400000);

        cout << a << " " << b << endl;
    }
}

n.cpp

#include<iostream>

using namespace std;

int main()
{
    int a, b;

    while(1)
    {
        cin.clear();

        cin >> a >> b;

        if (!cin) continue;

        cout << a << " " << b << endl;
    }
}

The (real) problem is that m generates numbers slower than n can consume them,
which causes some input operations to fail. Clearing the stream solves the problem.

It does not solve the problem.....did you try this on your computer..........it still does not work.........even if m generates numbers slower than n can consume them...........n's cin is supposed to wait for input to come....rite.?

Your programs work fine (you may observe that by decreasing an usleep() timeout to a reasonable value). The problem is that a pipeline IO is fully buffered, and for n to obtain any input, m shall produce 4K of data.
Be patient... data will come... slowly...

Indeed, my bad. Try adding a cin.clear(); call before cin>>a>>b; (use your programs, not mine) and see if it works.

Ok, these here should work ok:

m.cpp

#include <iostream>
#include<time.h>

using namespace std;

int main()
{
    int a=34, b=40;

    while(1)
    {
        usleep(400000);

        cout << a << " " << b << endl;
    }
}

n.cpp

#include<iostream>

using namespace std;

int main()
{
    int a, b;

    while(1)
    {
        cin.clear();

        cin >> a >> b;

        if (!cin) continue;

        cout << a << " " << b << endl;
    }
}

The (real) problem is that m generates numbers slower than n can consume them,
which causes some input operations to fail. Clearing the stream solves the problem.

yeah...got it...thanks

Hmmm... I'm not sure then. Yes, I tried it, but I use Sleep(1000); , as I'm on Windows.
It works perfectly fine for me. Are you sure you don't make usleep wait too much time?

EDIT:

yeah...got it...thanks

Ok.

EDIT2: Well, turns out the clear calls aren't necessary either.

To avoid the buffering effects on output streams you can use std::flush . For example:

std::cout << a << ' ' << b << '\n' << std::flush

If I recall correctly, std::endl will do the same thing so if you replace '\n' with std::endl it should also work.

N.B. That last part is untested.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.