473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

I/O buffers

It was my understanding that doing something like
'cin.rdbuf()->pubsetbuf(NULL, 0)' would cause any buffering troubles to
stop. I am not sure where the problem resides, but I am experiencing
difficulties in this area.

Basically what I am trying to do is an interface/engine setup where the
engine communicates through stdin/out and the interface communicates
with the engine through pipes to these handles. I thought I had it
working correctly until I used a certain library (gnustep) to create an
interface and now I am running into what looks like a buffer issue.

When the interface sends two commands (separated by \n) to the engine
quickly the engine gets the first but then fails to read the second or
following. Later when the same set of commands is sent again the engine
recieves what it had not before and then the first of the current set.
I also tried something like 'cat | engine' and got similar results. Not
sure if cat is buffering or not. I am also unsure in which part the
problem resides, engine or interface.

I shut of cin's buffer with 'cin.rdbuf()->pubsetbuf(NULL,0)' and then
read lines with this code:

while (Interface::readReady())
{
cerr << "Input ready.\n";
string input = Interface::readLine();
if (!CommandDispatcher::interpretAndDispatch(input))
Interface::printRaw(string("Error (unknown command): ") + input
+ "\n");
}
readLine() is:
string final;

int c;
while (cin && (c = cin.get()) != '\n') // Read up to '\n'
final += c;

cerr << "Data Read: *" << final << "*" << endl;

return final;

Of course everything in Interface::readReady() is off-topic and
unfortunately this is what is failing. However, all this function does
is poll stdin (using select()) to see if something is available and
return true if so. I am wondering if the problem is actually elsewhere,
like the above snippets for instance.

Unfortunately I am also getting no responce for the readReady() function
from any on-topic group :P That is why I am really hoping that the
error is somewhere else.

NR

Jul 22 '05 #1
2 1777
On Mon, 15 Dec 2003 20:30:10 -0800, Noah Roberts
<nr******@dontemailme.com> wrote:
It was my understanding that doing something like
'cin.rdbuf()->pubsetbuf(NULL, 0)' would cause any buffering troubles to
stop. I am not sure where the problem resides, but I am experiencing
difficulties in this area.
I think that buffering of stdin might be causing your problems, but it
isn't necessarily the case.
Basically what I am trying to do is an interface/engine setup where the
engine communicates through stdin/out and the interface communicates
with the engine through pipes to these handles. I thought I had it
working correctly until I used a certain library (gnustep) to create an
interface and now I am running into what looks like a buffer issue.
I've been there, done that, ended up rewriting to use shared memory
and native messaging for performance reasons, but don't let that stop
you (I was writing a realtime system). I did get it working before the
rewrite though, so read on...
When the interface sends two commands (separated by \n) to the engine
quickly the engine gets the first but then fails to read the second or
following. Later when the same set of commands is sent again the engine
recieves what it had not before and then the first of the current set.
I also tried something like 'cat | engine' and got similar results. Not
sure if cat is buffering or not. I am also unsure in which part the
problem resides, engine or interface.

I shut of cin's buffer with 'cin.rdbuf()->pubsetbuf(NULL,0)' and then
read lines with this code:
For a start, I'd remove the unbuffering call - the code can be fixed
so that you don't need it.

Have you set stdin to non-blocking? blocking is generally the default,
and might work best for you here (it simplifies the code a bit) if the
writer should be writing whole lines.
while (Interface::readReady())
{
cerr << "Input ready.\n";
string input = Interface::readLine();
if (!CommandDispatcher::interpretAndDispatch(input))
Interface::printRaw(string("Error (unknown command): ") + input
+ "\n");
}
readLine() is:
string final;

int c;
while (cin && (c = cin.get()) != '\n') // Read up to '\n'
final += c;
Replace the above with the briefer and faster:
std::getline(std::cin, final);
cerr << "Data Read: *" << final << "*" << endl;

return final;

Of course everything in Interface::readReady() is off-topic and
unfortunately this is what is failing. However, all this function does
is poll stdin (using select()) to see if something is available and
return true if so. I am wondering if the problem is actually elsewhere,
like the above snippets for instance.


readReady needs to be fixed by adding this to the start:

if (std::cin.in_avail() > 0)
return true;

If std::cin has any characters buffered, return immediately. Hopefully
that will fix your problem.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
readReady needs to be fixed by adding this to the start:

if (std::cin.in_avail() > 0)
return true;

If std::cin has any characters buffered, return immediately. Hopefully
that will fix your problem.


I will try that again, but last time I did it did not work as expected.
I don't recall which way it went, but in_avail returned the same value
no matter what conditions on certain systems.

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: doodle4 | last post by:
Hello, I need to create 6 buffers in python and keep track of it. I need to pass this buffer, say buffer 1 as an index to a test app. Has any one tried to do this. Any help with buffer...
18
by: JG | last post by:
Does anyone know a standard (or supported on Linux, Mac, Win32) way to clear a read stream buffer (standard ANSI C file stream)? I would even settle for a platform specific way of doing it. ...
2
by: ldawson | last post by:
From the same C++ source code, I'm attempting to generate both an unmanaged DLL and a managed assembly. This will eliminate interop as the calling code is slowly migrated to .NET. However, C++...
3
by: yuvalif | last post by:
I want to get rid of my "sprintf" call, so I wrote the following example, in which I have two problems: (1) why do I need to inherit from "streambuf", and not just use it as a local in my "ToBuff"...
3
by: Sally Sally | last post by:
I have a very basic question on the two parameters shared buffers and effective cache size. I have read articles on what each is about etc. But I still think I don't quite grasp what these settings...
2
by: | last post by:
Hi, we are planning to rewrite an extisting C++ image processing application/library in C#. Now several question arouse where I hope you can help me: So far we allocated a block of memory as...
2
by: Alex Vinokur | last post by:
I need a container of char-buffers, for instance, vector of char-buffers. vector<string> v; // It is not what I need to char a; memset (&a, 0, sizeof (a)); v.push_back(a);
0
by: Sam Durai | last post by:
Hello, A particular select query took unusually long time to execute hence I took an app.snap to find out what happens internally and I found out that tablequeue buffers are overflowing on a...
3
by: darren | last post by:
Hi there Im working on an assignment that has me store data in a buffer to be sent over the network. I'm ignorant about how C++ stores data in an array, and types in general. If i declare an...
2
by: chenxinleo | last post by:
Hi, When i use some standard library functions and fields,which return char* type(like ctime in time.h, optarg in getopt.h)and do not have to be freed after calling,i always worry about memory...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.