From: Eric Wong Date: 2013-02-26T15:39:24+09:00 Subject: Re: STDIN inheritance during exec and difference between getc and sysread Dmitry Bolshakov wrote: > 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? 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.