Will Dyson on Tue, 25 Jun 2002 05:00:12 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] C++, FIFOs, getline, and O_NDELAY


Michael F. Robbins wrote:

[...]
are needed. I would have thought that checking for the POLLIN event
would have returned true since there were more data left to read. However, my guess is that the ifstream class has an internal buffer
which has the next command inside, so the poll() returns false.

Yeah, mixing iostreams with other io functions is quite the source of unhappyness. There is definatly a buffer (what you get a pointer to when you call rdbuf()).


FYI, I belive that the basic_filebuf class in libstdc++3 library (from g++ 3.0) does not have the fd() member. It is certainly not part of the ANSI C++ spec. You could probably implement your own filebuf class to take care of this. Just something to be aware of, should you ever consider switching/upgrading compilers.

Any ideas?  Right now I'm looking at setting O_NDELAY on the file
descriptor.  This would certainly allow a simple read() function to work
on it, but would the getline() work correctly?

You definatly don't want to set O_NDELAY on the fd behind the ifstream's back. I don't even see how you might do that, since O_NDELAY is a flag you pass to open(), and ifstream is opening the file for you.


couple approaches:

1) The basic_filebuf class has a public member in_avail(), which returns the number of charecters in the buffer. After poll tells you you have input waiting, loop on in_avail():

while(fifoIn.rdbuf()->in_avail()) {
	fifoIn.getline(...);
}

2) Set the ifstream to unbuffered ( fifoIn.rdbuf()->pubsetbuf(0,0) ). Not a huge performance hit, since you are never hitting the disk with a FIFO.

3) Do as Kyle Burton suggests and launch a thread to loop on getline() and dispatch commands. This is probably what I would do, but I learned to program on the BeOS, which probably warped my mind :)

--
Will Dyson
"Back off man, I'm a scientist!" -Dr. Peter Venkman


______________________________________________________________________ Philadelphia Linux Users Group - http://www.phillylinux.org Announcements-http://lists.phillylinux.org/mail/listinfo/plug-announce General Discussion - http://lists.phillylinux.org/mail/listinfo/plug