Forum: Ruby STDIN inheritance during exec and difference between getc and sysread

Posted by Dmitry Bolshakov (bdimych)
on 2013-02-26 05:21
Hi all!

I've accidentally found that "getc" and "sysread" behave differently
after "exec"
getc does not get remaining data from the STDIN but sysread does it

bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $ echo -n ab | ruby -e "p STDIN.getc; exec 'ruby',
'-e', 'p STDIN.getc'"
"a"
nil
bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $ echo -n ab | ruby -e "p STDIN.sysread(1); exec
'ruby', '-e', 'p STDIN.sysread(1)'"
"a"
"b"
bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $
bdimych@mint-vbox ~ $

why so? where is "b" in the first case?
Posted by Eric Wong (Guest)
on 2013-02-26 07:40
(Received via mailing list)
Dmitry Bolshakov <lists@ruby-forum.com> wrote:
> "a"
> why so? where is "b" in the first case?
getc uses Ruby I/O buffering in user space, so the process read
more bytes than it needed the first time getc was called.

Buffered I/O leads to surprises like this behavior across fork/exec.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.