I
inkapyrite
Hi all. I'm using ifstream to read from a named pipe but
i've encountered an annoying problem. For some reason, the
program blocks on reading an ifstream's internal buffer that's
only half-filled. Only when the buffer becomes full does
it resume execution.
Here's my test code for reading from a pipe:
//(compiled with g++ -std=c++98)
//---------------------------------------------
#include <iostream>
#include <fstream>
using namespace std;
const int BUFF_SIZE = 20;
int main()
{
char buffer[BUFF_SIZE+1];
std::ifstream fin("npipe");
std::streambuf * pbuf;
pbuf = fin.rdbuf();
pbuf->pubsetbuf(buffer,BUFF_SIZE);
char c[200];
while(1){
if(pbuf->in_avail() > 0) {
cout << "stuff in buffer: " << pbuf->in_avail() << endl;
fin >> c; //program blocks HERE if buffer not full
cout << "read in stuff : " << c << endl;
cout << "---------------------------" << endl;
}
else
usleep(500000);
}
fin.close();
}
//---------------------------------------------
And for writing to the pipe, i just use:
cat > npipe
on the terminal.
So when i type something down the pipe and hit the return key,
the program does immediately recognise that there's data
available (cos pbuf->in_avail() immediately becomes > 0).
But when i try to read in the data with >> operator, it blocks if
the data being sent does not completely fill up the buffer. So
in the above case, the program blocks until BUFF_SIZE chars have
been sent down the pipe.
incidentally, i tried other read functions in place of >>
but it yield the same result.
//fin.read(c,pbuf->in_avail()); //none of these work either
//fin.readsome(c, 1);
//char test= pbuf->sbumpc();
It is unlikely that it's a pipe problem because i've written
a separate program that uses the C's FILE pointer for reading
where I don't encounter this problem at all.
My question is: doesn't in_avail() specify the number of
characters that are collected and can be read from ifstream's buffer
without blocking? Is there any way i can read a
half-filled streambuf without it blocking on me? Or have i
misunderstood the concept of in_avail() + the readability of
a buffer here?
As a temporary make-do solution, i've set pubsetbuf(buffer,1);
so that whenever a char is received the buffer becomes full, which
kinda resembles an unbuffered stream. (and yes, i've tried
pubsetbuf(0,0) & it doesnt' work. see earlier post on this by
someone else.
http://groups.google.co.nz/groups?h...coff=1&selm=3EBA9C5F.6EC7CC19%40gmx.de&rnum=1
)
It would be nice if there's a way where i can set streambuf's
buffer size to something > 1 & still be able to read from
the half filled buffer without blocking.
Any suggestions?
Thanks heaps in advance,
Des
i've encountered an annoying problem. For some reason, the
program blocks on reading an ifstream's internal buffer that's
only half-filled. Only when the buffer becomes full does
it resume execution.
Here's my test code for reading from a pipe:
//(compiled with g++ -std=c++98)
//---------------------------------------------
#include <iostream>
#include <fstream>
using namespace std;
const int BUFF_SIZE = 20;
int main()
{
char buffer[BUFF_SIZE+1];
std::ifstream fin("npipe");
std::streambuf * pbuf;
pbuf = fin.rdbuf();
pbuf->pubsetbuf(buffer,BUFF_SIZE);
char c[200];
while(1){
if(pbuf->in_avail() > 0) {
cout << "stuff in buffer: " << pbuf->in_avail() << endl;
fin >> c; //program blocks HERE if buffer not full
cout << "read in stuff : " << c << endl;
cout << "---------------------------" << endl;
}
else
usleep(500000);
}
fin.close();
}
//---------------------------------------------
And for writing to the pipe, i just use:
cat > npipe
on the terminal.
So when i type something down the pipe and hit the return key,
the program does immediately recognise that there's data
available (cos pbuf->in_avail() immediately becomes > 0).
But when i try to read in the data with >> operator, it blocks if
the data being sent does not completely fill up the buffer. So
in the above case, the program blocks until BUFF_SIZE chars have
been sent down the pipe.
incidentally, i tried other read functions in place of >>
but it yield the same result.
//fin.read(c,pbuf->in_avail()); //none of these work either
//fin.readsome(c, 1);
//char test= pbuf->sbumpc();
It is unlikely that it's a pipe problem because i've written
a separate program that uses the C's FILE pointer for reading
where I don't encounter this problem at all.
My question is: doesn't in_avail() specify the number of
characters that are collected and can be read from ifstream's buffer
without blocking? Is there any way i can read a
half-filled streambuf without it blocking on me? Or have i
misunderstood the concept of in_avail() + the readability of
a buffer here?
As a temporary make-do solution, i've set pubsetbuf(buffer,1);
so that whenever a char is received the buffer becomes full, which
kinda resembles an unbuffered stream. (and yes, i've tried
pubsetbuf(0,0) & it doesnt' work. see earlier post on this by
someone else.
http://groups.google.co.nz/groups?h...coff=1&selm=3EBA9C5F.6EC7CC19%40gmx.de&rnum=1
)
It would be nice if there's a way where i can set streambuf's
buffer size to something > 1 & still be able to read from
the half filled buffer without blocking.
Any suggestions?
Thanks heaps in advance,
Des