How do I flush a pipe ?

Code, algorithms, languages, construction...
User avatar
Matthias Gemuh
Posts: 295
Joined: Wed Jun 09, 2010 2:48 pm
Contact:

How do I flush a pipe ?

Post by Matthias Gemuh » Sat Aug 21, 2010 9:55 pm

How do I flush a pipe under Windows ?

My GUI talks to child processes using pipes. I write into them using WriteFile(Handle, ...);
How can I flush such output pipes ?

Matthias.
Aided by engines, GMs can be very strong.
http://www.hylogic.de

User avatar
kingliveson
Posts: 1388
Joined: Thu Jun 10, 2010 1:22 am
Real Name: Franklin Titus
Location: 28°32'1"N 81°22'33"W

Re: How do I flush a pipe ?

Post by kingliveson » Sat Aug 21, 2010 11:11 pm

Have you explored fflush?
PAWN : Knight >> Bishop >> Rook >>Queen

User avatar
Matthias Gemuh
Posts: 295
Joined: Wed Jun 09, 2010 2:48 pm
Contact:

Re: How do I flush a pipe ?

Post by Matthias Gemuh » Sun Aug 22, 2010 10:47 am

kingliveson wrote:Have you explored fflush?
I casted a handle to a stream pointer to use with fflush(), but it did not work.

Anyway, HGM says a pipe cannot be flushed.

Matthias.
Aided by engines, GMs can be very strong.
http://www.hylogic.de

hyatt
Posts: 1242
Joined: Thu Jun 10, 2010 2:13 am
Real Name: Bob Hyatt (Robert M. Hyatt)
Location: University of Alabama at Birmingham
Contact:

Re: How do I flush a pipe ?

Post by hyatt » Sun Aug 22, 2010 4:39 pm

Matthias Gemuh wrote:
kingliveson wrote:Have you explored fflush?
I casted a handle to a stream pointer to use with fflush(), but it did not work.

Anyway, HGM says a pipe cannot be flushed.

Matthias.
The "pipe" part is irrelevant. The "buffer" is in the C library code. That's where you get into trouble. Easiest way to deal with this is to simply use read()/write() as I do in Crafty. They are, by standard, unbuffered. Which means that as soon as you do a write, the data is "on its way" to the final destination. It doesn't sit in a C library buffer until the buffer is filled, before it is sent. The engine-intf.html document explains that in detail.

User avatar
kingliveson
Posts: 1388
Joined: Thu Jun 10, 2010 1:22 am
Real Name: Franklin Titus
Location: 28°32'1"N 81°22'33"W

Re: How do I flush a pipe ?

Post by kingliveson » Sun Aug 22, 2010 5:22 pm

hyatt wrote:
Matthias Gemuh wrote:
kingliveson wrote:Have you explored fflush?
I casted a handle to a stream pointer to use with fflush(), but it did not work.

Anyway, HGM says a pipe cannot be flushed.

Matthias.
The "pipe" part is irrelevant. The "buffer" is in the C library code. That's where you get into trouble. Easiest way to deal with this is to simply use read()/write() as I do in Crafty. They are, by standard, unbuffered. Which means that as soon as you do a write, the data is "on its way" to the final destination. It doesn't sit in a C library buffer until the buffer is filled, before it is sent. The engine-intf.html document explains that in detail.
Ok, I just read what HGM said and don't think he is saying a pipe cannot be flushed. I think as Prof. Hyatt pointed out, you are not targeting the key area, the buffer. Using read()/write() is probably best trying to redirect output in this case, as it is more efficient -- if I understand correctly what you're trying to do . See MSDN FlushFileBuffers note below:
Typically the WriteFile and WriteFileEx functions write data to an internal buffer that the operating system writes to a disk or communication pipe on a regular basis. The FlushFileBuffers function writes all the buffered information for a specified file to the device or pipe.

Due to disk caching interactions within the system, the FlushFileBuffers function can be inefficient when used after every write to a disk drive device when many writes are being performed separately. If an application is performing multiple writes to disk and also needs to ensure critical data is written to persistent media, the application should use unbuffered I/O instead of frequently calling FlushFileBuffers. To open a file for unbuffered I/O, call the CreateFile function with the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags. This prevents the file contents from being cached and flushes the metadata to disk with each write. For more information, see CreateFile.
PAWN : Knight >> Bishop >> Rook >>Queen

User avatar
Matthias Gemuh
Posts: 295
Joined: Wed Jun 09, 2010 2:48 pm
Contact:

Re: How do I flush a pipe ?

Post by Matthias Gemuh » Sun Aug 22, 2010 7:09 pm

kingliveson wrote: Typically the WriteFile and WriteFileEx functions write data to an internal buffer that the operating system writes to a disk or communication pipe on a regular basis. The FlushFileBuffers function writes all the buffered information for a specified file to the device or pipe.

Due to disk caching interactions within the system, the FlushFileBuffers function can be inefficient when used after every write to a disk drive device when many writes are being performed separately. If an application is performing multiple writes to disk and also needs to ensure critical data is written to persistent media, the application should use unbuffered I/O instead of frequently calling FlushFileBuffers. To open a file for unbuffered I/O, call the CreateFile function with the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags. This prevents the file contents from being cached and flushes the metadata to disk with each write. For more information, see CreateFile.
I have been using WriteFile() without knowing that it buffers.
That is why I was suspecting the buffer to be in the pipe. :evil:
Aided by engines, GMs can be very strong.
http://www.hylogic.de

kranium
Posts: 55
Joined: Mon Aug 02, 2010 10:49 pm
Real Name: Norman Schmidt

Re: How do I flush a pipe ?

Post by kranium » Mon Aug 23, 2010 9:22 pm

1st and fore all: try the good old fashioned plunger! (this age-old remedy can often do wonders)

Drano (and/or Mr. Plumber) is quite good...but excercise caution and read the directions carefully if you have PVC.

otherwise: i highly recommend the help of an experienced professional...especially if the pipe is lead.

hope this helps...
Norm

User avatar
Matthias Gemuh
Posts: 295
Joined: Wed Jun 09, 2010 2:48 pm
Contact:

Re: How do I flush a pipe ?

Post by Matthias Gemuh » Mon Aug 23, 2010 9:40 pm

kranium wrote:1st and fore all: try the good old fashioned plunger! (this age-old remedy can often do wonders)

Drano (and/or Mr. Plumber) is quite good...but excercise caution and read the directions carefully if you have PVC.

otherwise: i highly recommend the help of an experienced professional...especially if the pipe is lead.

hope this helps...
Norm
What has a plumber got to do with what I smoke ? :P

Matthias.
Aided by engines, GMs can be very strong.
http://www.hylogic.de

benstoker
Posts: 110
Joined: Thu Jun 10, 2010 7:32 pm
Real Name: Ben Stoker

Re: How do I flush a pipe ?

Post by benstoker » Mon Aug 23, 2010 10:27 pm

Matthias Gemuh wrote:How do I flush a pipe under Windows ?

My GUI talks to child processes using pipes. I write into them using WriteFile(Handle, ...);
How can I flush such output pipes ?

Matthias.
http://www.pipesandcigars.com/pipecleaners.html

User avatar
kingliveson
Posts: 1388
Joined: Thu Jun 10, 2010 1:22 am
Real Name: Franklin Titus
Location: 28°32'1"N 81°22'33"W

Re: How do I flush a pipe ?

Post by kingliveson » Thu Aug 26, 2010 9:45 pm

From CCC:
Matthias Gemuh wrote:One last trial to explain it.

Windows offers only WriteFile() to write into a pipe.
(Obselete write functions do exist that work like WriteFile())
WriteFile() has an internal buffer that makes WriteFile() non-blocking.
Windows flushes that buffer depending on how full it is or how long data has been standing in it. This efficiency decision effectively steals time from any engine waiting at the other end of the pipe.
The internal WriteFile() buffer can be forcefully flushed with FlushFileBuffers() to eliminate delays, but that is blocking
You can accomplish read()/write() effect using FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags with WriteFile().
PAWN : Knight >> Bishop >> Rook >>Queen

Post Reply