I found an article which is related to what you are trying to do



----------------------------------
http://tim-mann.org/xboard/engine-intf.html#7

Beware of using buffered I/O in your chess engine. The C stdio library, C++
streams, and the I/O packages in most other languages use buffering both on
input and output. That means two things. First, when your engine tries to
write some characters to xboard, the library stashes them in an internal
buffer and does not actually write them to the pipe connected to xboard
until either the buffer fills up or you call a special library routine
asking for it to be flushed. (In C stdio, this routine is named fflush.)
Second, when your engine tries to read some characters from xboard, the
library does not read just the characters you asked for -- it reads all the
characters that are currently available (up to some limit) and stashes any
characters you are not yet ready for in an internal buffer. The next time
you ask to read, you get the characters from the buffer (if any) before the
library tries to read more data from the actual pipe.

Why does this cause problems? First, on the output side, remember that your
engine produces output in small quantities (say, a few characters for a
move, or a line or two giving the current analysis), and that data always
needs to be delivered to xboard/WinBoard for display immediately. If you use
buffered output, the data you print will sit in a buffer in your own address
space instead of being delivered.

You can usually fix the output buffering problem by asking for the buffering
to be turned off. In C stdio, you do this by calling setbuf(stdout, NULL). A
more laborious and error-prone method is to carefully call
fflush(stdout)after every line you output; I don't recommend this. In
C++, you can try
cout.setf(ios::unitbuf), which is documented in current editions of "The C++
Programming Language," but not older ones. Another C++ method that might
work is cout.rdbuf()->setbuf(NULL, 0). Alternatively, you can carefully call
cout.flush() after every line you output; again, I don't recommend this.

Another way to fix the problem is to use unbuffered operating system calls
to write directly to the file descriptor for standard output. On Unix, this
means write(1, ...) -- see the man page for write(2). On Win32, you can use
either the Unix-like _write(1, ...) or Win32 native routines like WriteFile.


Second, on the input side, you are likely to want to poll during your search
and stop it if new input has come in. If you implement pondering, you'll
need this so that pondering stops when the user makes a move. You should
also poll during normal thinking on your move, so that you can implement the
"?" (move now) command, and so that you can respond promptly to a "result",
"force", or "quit" command if xboard wants to end the game or terminate your
engine. Buffered input makes polling more complicated -- when you poll, you
must stop your search if there are *either* characters in the buffer
*or*characters available from the underlying file descriptor.

The most direct way to fix this problem is to use unbuffered operating
system calls to read (and poll) the underlying file descriptor directly. On
Unix, use read(0, ...) to read from standard input, and use select() to poll
it. See the man pages read(2) and select(2). (Don't follow the example of
GNU Chess 4 and use the FIONREAD ioctl to poll for input. It is not very
portable; that is, it does not exist on all versions of Unix, and is broken
on some that do have it.) On Win32, you can use either the Unix-like _read(0,
...) or the native Win32 ReadFile() to read. Unfortunately, under Win32, the
function to use for polling is different depending on whether the input
device is a pipe, a console, or something else. (More Microsoft brain damage
here -- did they never hear of device independence?) For pipes, you can use
PeekNamedPipe to poll (even when the pipe is unnamed). For consoles, you can
use GetNumberOfConsoleInputEvents. For sockets only, you can use select().
It might be possible to use WaitForSingleObject more generally, but I have
not tried it. Some code to do these things can be found in Crafty's
utility.c, but I don't guarantee that it's all correct or optimal.

A second way to fix the problem might be to ask your I/O library not to
buffer on input. It should then be safe to poll the underlying file
descriptor as described above. With C, you can try calling setbuf(stdin,
NULL). However, I have never tried this. Also, there could be problems if
you use scanf(), at least with certain patterns, because scanf() sometimes
needs to read one extra character and "push it back" into the buffer; hence,
there is a one-character pushback buffer even if you asked for stdio to be
unbuffered. With C++, you can try cin.rdbuf()->setbuf(NULL, 0), but again, I
have never tried this.

A third way to fix the problem is to check whether there are characters in
the buffer whenever you poll. C I/O libraries generally do not provide any
portable way to do this. Under C++, you can use cin.rdbuf()->in_avail().
This method has been reported to work with EXchess. Remember that if there
are no characters in the buffer, you still have to poll the underlying file
descriptor too, using the method described above.

A fourth way to fix the problem is to use a separate thread to read from
stdin. This way works well if you are familiar with thread programming. This
thread can be blocked waiting for input to come in at all times, while the
main thread of your engine does its thinking. When input arrives, you have
the thread put the input into a buffer and set a flag in a global variable.
Your search routine then periodically tests the global variable to see if
there is input to process, and stops if there is. WinBoard and my Win32
ports of ICC timestamp and FICS timeseal use threads to handle multiple
input sources.


2007/3/29, JC Coez <[EMAIL PROTECTED]>:

I would like to use a chess engine , such as Arion
http://transversale.fr/Arion/Arion.htm . I am writing a GUI to play chess
with J (using grid) and would like to add as an extra, such an engine.
This
program is a windows application using stdin and stdout to read chess
moves
and write possible solutions. I tried other similar engines without
success.
I think I miss something in the use of pipes...or is it impossible to do
it
?

----- Original Message -----
From: "Björn Helgason" <[EMAIL PROTECTED]>
To: "Programming forum" <[email protected]>
Sent: Thursday, March 29, 2007 4:10 PM
Subject: Re: [Jprogramming] stdin/stdout


> Give us an example of what it is you are doing and/or trying to do
>
> 2007/3/29, JC Coez <[EMAIL PROTECTED]>:
>>
>> Thanks Björn,
>> I did try the shell, fork and spawn, but it does not work in my
case.  I
>> tried to use the verbs createPipe and CreateProcess but without
success.
>> Instead J is not responding...The createProcess is fine but when I want
>> to
>> Read or Write thru a Pipe, I have to close the process manually.
>> Any other track ?
>> Thanks for help
>> JC Coez
>>
>> >
>> > ----- Original Message -----
>> > From: "Björn Helgason" <[EMAIL PROTECTED]>
>> > To: "Programming forum" <[email protected]>
>> > Sent: Wednesday, March 21, 2007 11:37 AM
>> > Subject: Re: [Jprogramming] stdin/stdout
>> >
>> >
>> >> Try out
>> >>
>> >> require'task'
>> >>
>> >> shell
>> >> fork
>> >> spawn
>> >>
>> >> 2007/3/21, JC Coez <[EMAIL PROTECTED]>:
>> >
>> >
>> >
>>
>>
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>>
>
>
>
> --
> Björn Helgason, Verkfræðingur
> Fugl&Fiskur ehf, Þerneyjarsund 23, Box 127
> 801 Grímsnes ,t-póst: [EMAIL PROTECTED]
> Skype: gosiminn, gsm: +3546985532
> Landslags og skrúðgarðagerð, gröfuþjónusta
> http://groups.google.com/group/J-Programming
>
>
> Tæknikunnátta höndlar hið flókna, sköpunargáfa er meistari
einfaldleikans
>
> góður kennari getur stigið á tær án þess að glansinn fari af skónum
>          /|_      .-----------------------------------.
>         ,'  .\  /  | Með léttri lund verður        |
>     ,--'    _,'   | Dagurinn í dag                     |
>    /       /       | Enn betri en gærdagurinn  |
>   (   -.  |        `-----------------------------------'
>   |     ) |        (\_ _/)
>  (`-.  '--.)       (='.'=)
>   `. )----'        (")_(")
>



--------------------------------------------------------------------------------


> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
---------------------------------------------------------------------------------------
> Orange vous informe que cet  e-mail a ete controle par l'anti-virus
mail.
> Aucun virus connu a ce jour par nos services n'a ete detecte.
>
>


----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm




--
Björn Helgason, Verkfræðingur
Fugl&Fiskur ehf, Þerneyjarsund 23, Box 127
801 Grímsnes ,t-póst: [EMAIL PROTECTED]
Skype: gosiminn, gsm: +3546985532
Landslags og skrúðgarðagerð, gröfuþjónusta
http://groups.google.com/group/J-Programming


Tæknikunnátta höndlar hið flókna, sköpunargáfa er meistari einfaldleikans

góður kennari getur stigið á tær án þess að glansinn fari af skónum
         /|_      .-----------------------------------.
        ,'  .\  /  | Með léttri lund verður        |
    ,--'    _,'   | Dagurinn í dag                     |
   /       /       | Enn betri en gærdagurinn  |
  (   -.  |        `-----------------------------------'
  |     ) |        (\_ _/)
 (`-.  '--.)       (='.'=)
  `. )----'        (")_(")
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to