Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is stdin, stdout, stderr buffered or unbuffered in Lua?

Tags:

stdout

stdin

lua

I can't really find much information out there that mentions this. Are these standard io descriptors setup to be buffered or unbuffered by default in Lua? Can I switch from one mode to the other if desired?

For instance, in python it provides something like sys.stdin.detach for making it unbuffered.

like image 540
greatwolf Avatar asked Aug 30 '13 21:08

greatwolf


People also ask

Is stdin line buffered?

Unbuffered stream invokes a system call on every read or write . Whereas line buffered stream invokes a system call on every line(whenever the buffer encounters a newline). Also, stdin and stdout are both standard I/O stream and are typically line buffered.

Is stderr buffered?

Notes. The stream stderr is unbuffered. The stream stdout is line-buffered when it points to a terminal. Partial lines will not appear until fflush(3) or exit(3) is called, or a newline is printed.

What type of buffering is used by default for stdout in Unix?

By default writes to stdout pass through a 4096 byte buffer, unless stdout happens to be a terminal/tty in which case it is line buffered.

How do I make stdout unbuffered?

You can use the setvbuf function: setvbuf(stdout, NULL, _IONBF, 0);


1 Answers

See setvbuf. It is an interface to the underlying C setvbuf function.

For example you can use it like this:

io.stdout:setvbuf 'no'    -- switch off buffering for stdout

AFAIK Lua relies on the underlying C runtime to hook into standard streams, therefore I think the usual guarantees for C standard streams apply.

like image 92
Lorenzo Donati -- Codidact.com Avatar answered Oct 06 '22 13:10

Lorenzo Donati -- Codidact.com