我对这三个文件的用途感到相当困惑。如果我的理解是正确的,stdin是程序写入它的请求以在进程中运行任务的文件,stdout是内核写入它的输出和它访问的请求进程的信息的文件,stderr是所有异常都输入的文件。在打开这些文件来检查这些是否真的发生了,我发现似乎没有什么建议!

我想知道的是这些文件的确切目的是什么,绝对愚蠢的答案与很少的技术术语!


当前回答

A file with associated buffering is called a stream and is declared to be a pointer to a defined type FILE. The fopen() function creates certain descriptive data for a stream and returns a pointer to designate the stream in all further transactions. Normally there are three open streams with constant pointers declared in the header and associated with the standard open files. At program startup three streams are predefined and need not be opened explicitly: standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). When opened the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device

https://www.mkssoftware.com/docs/man5/stdio.5.asp

其他回答

It would be more correct to say that stdin, stdout, and stderr are "I/O streams" rather than files. As you've noticed, these entities do not live in the filesystem. But the Unix philosophy, as far as I/O is concerned, is "everything is a file". In practice, that really means that you can use the same library functions and interfaces (printf, scanf, read, write, select, etc.) without worrying about whether the I/O stream is connected to a keyboard, a disk file, a socket, a pipe, or some other I/O abstraction.

大多数程序都需要读取输入,写入输出,并记录错误,所以stdin, stdout, 和stderr是为您预定义的,以方便编程。这只是 一种约定,操作系统不强制执行。

stdin

通过控制台读取输入(例如键盘输入)。 在C语言中使用scanf

scanf(<formatstring>,<pointer to storage> ...);

stdout

产生输出到控制台。 在C中与printf一起使用

printf(<string>, <values to print> ...);

stderr

向控制台输出“错误”。 在C中与fprintf一起使用

fprintf(stderr, <string>, <values to print> ...);

重定向

stdin的源可以被重定向。例如,它不是来自键盘输入,而是来自文件(echo < file.txt)或另一个程序(ps | grep <userid>)。

stdout、stderr的目的地也可以重定向。例如,stdout可以重定向到一个文件:ls。> ls-output.txt,在这种情况下输出被写入文件ls-output.txt。Stderr可以用2>重定向。

作为上述答案的补充,以下是关于重定向的总结:

编辑:这张图并不完全正确。

第一个例子根本没有使用stdin,它将“hello”作为参数传递给echo命令。

图表还显示,2>&1与&>具有相同的效果

ls Documents ABC > dirlist 2>&1
#does not give the same output as 
ls Documents ABC > dirlist &>

这是因为&>需要重定向到一个文件,而2>&1只是将stderr发送到stdout

使用ps -aux显示当前进程,所有这些进程都列在/proc/ as /proc/(pid)/中,通过调用cat /proc/(pid)/fd/0,它会打印在该进程的标准输出中找到的任何东西。因此,或许,

/proc/(pid)/fd/0 -标准输出文件 /proc/(pid)/fd/1 -标准输入文件 /proc/(pid)/fd/2 -标准错误文件

例如

但只有在/bin/bash中才能很好地工作,其他进程通常在0中没有任何内容,但在2中有许多错误

A file with associated buffering is called a stream and is declared to be a pointer to a defined type FILE. The fopen() function creates certain descriptive data for a stream and returns a pointer to designate the stream in all further transactions. Normally there are three open streams with constant pointers declared in the header and associated with the standard open files. At program startup three streams are predefined and need not be opened explicitly: standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). When opened the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device

https://www.mkssoftware.com/docs/man5/stdio.5.asp