2

Does Tcl do any internal input buffering that's out of the script writers control? Will the following code possibly waste entropy (read more than 1 byte), and if so, how can I prevent it?

set stream [open "/dev/srandom"]
chan configure $stream -translation binary
set randomByte [chan read $stream 1]
potrzebie
  • 1,768
  • 1
  • 12
  • 25

1 Answers1

4

Yes, tcl defaults to buffering and will waste enthropy (as much as a single read call will decide to hand over).

I thought that you can prevent it with

chan configure $stream -buffering none

But no, -buffering has no effect on input queue (it's not a single buffer internally).

However,

chan configure $stream -buffersize 0

does the trick, as I've seen from an experiment with stdin under strace. It makes any input go in reads (syscall) of size 1 (an argument to TCL read doesn't matter), so it would be extremely slow for normal use.

Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69
  • That's what I've always thought too, until I realized that [chan configure -buffering](http://www.tcl.tk/man/tcl8.6/TclCmd/chan.htm#M9) only mentions **output** buffering. See http://stackoverflow.com/a/8750872/1246115. – potrzebie Jan 25 '13 at 18:58
  • @potrzebie thanks. Fortunately there is `-buffersize` which is useful for the situation in question. – Anton Kovalenko Jan 25 '13 at 19:15