stdout vs stderr

When a process is created in Linux, three file handles are already open for use:

1. stdin (Standard input) with file descriptor value '0'
2. stdout (Standard output) with file descriptor value '1'
3. stderr (Standard Error) with file descriptor value '2'



read(0, buf, sizeof(buf)) will read from the standard input which is the keyboard and store it in 'buf'. Note: We need to null terminate the buffer manually.

write(1, "hello", sizeof("hello")) will write to the standard output which is the console
write(2, "hello", sizeof("hello")) will write to the standard error which is also console

So, why we have two (stdout and stderr), as they are performing the same operation. No there are some differences:

stdout vs stderr:

stdout is fully buffered and stderr is not buffered. This means when we write to stdout, it goes to a buffer and when we command it to display on the console, it displays it. For example, whenever we write system call encounters '\n' character it flushes the buffer on the console, the other way is to call 'fflush(stdout)'. Whereas with stderr, it will be directly flushed to console, there is no buffer in between.

stdout should be used for regular messages and stderr should be used for error(diagnostic) messages.

When you redirect the output of command using redirection operator, it only redirects stdout messages

command > logfile

It is similar to

command 1> logfile

If you want to redirect stderr messages to logfile. Note: There should not be any space before redirection operator

command 2> logfile

If you want to redirect both stdout and stderr to different files

command 1> outfile 2> errfile

To redirect both stdout and stderr to a single file

command > outfile 2>&1

Comments

Popular posts from this blog

bb.utils.contains yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig

PR, PN and PV Variable in Yocto