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

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


当前回答

我认为人们说stderr应该只用于错误消息是一种误导。

它还应该用于为运行命令的用户而不是数据的任何潜在下游消费者提供的信息性消息(例如,如果您运行一个连接多个命令的shell管道,您不希望像“获取42424的item 30”这样的信息性消息出现在stdout上,因为它们会混淆消费者,但您可能仍然希望用户看到它们。

看看下面的历史逻辑:

"All programs placed diagnostics on the standard output. This had always caused trouble when the output was redirected into a file, but became intolerable when the output was sent to an unsuspecting process. Nevertheless, unwilling to violate the simplicity of the standard-input-standard-output model, people tolerated this state of affairs through v6. Shortly thereafter Dennis Ritchie cut the Gordian knot by introducing the standard error file. That was not quite enough. With pipelines diagnostics could come from any of several programs running simultaneously. Diagnostics needed to identify themselves."

其他回答

标准输入——这是进程读取的文件句柄,以便从您获取信息。

标准输出—您的进程将常规输出写入此文件句柄。

标准错误-进程将诊断输出写入此文件句柄。

这是我能做到的最愚蠢的了:-)

当然,这主要是惯例。如果您愿意,没有什么可以阻止您将诊断信息写入标准输出。您甚至可以完全关闭三个文件句柄,并打开您自己的文件进行I/O。

当您的进程启动时,它应该已经打开了这些句柄,并且可以读取和/或写入它们。

默认情况下,它们可能连接到您的终端设备(例如,/dev/tty),但是shell将允许您在进程启动之前在这些句柄和特定的文件和/或设备(甚至是到其他进程的管道)之间建立连接(可能的一些操作相当聪明)。

一个例子是:

my_prog <inputfile 2>errorfile | grep XYZ

将:

为my_prog创建一个进程。 打开inputfile作为标准输入(文件句柄0)。 打开errorfile作为标准错误(文件句柄2)。 为grep创建另一个进程。 将my_prog的标准输出附加到grep的标准输入。


回复你的评论:

当我在/dev文件夹中打开这些文件时,为什么我从来没有看到进程运行的输出?

因为它们不是普通的文件。虽然UNIX将所有内容都作为文件系统中的某个文件,但在最低级别上并不是这样。/dev层次结构中的大多数文件不是字符设备就是块设备,实际上是一个设备驱动程序。它们没有尺寸,但有主设备号和副设备号。

当您打开它们时,您连接到的是设备驱动程序,而不是物理文件,而且设备驱动程序足够智能,知道应该分别处理不同的进程。

Linux /proc文件系统也是如此。这些不是真正的文件,只是严格控制的通往内核信息的网关。

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

有关这些文件的权威信息,请查看手册页,在您的终端上运行该命令。

$ man stdout 

但简单来说,每个文件都是用于:

输出流的Stdout

流输入的Stdin

打印错误或日志消息的标准错误。

每个unix程序都有这些流中的每一个。

恐怕你的理解完全落后了。:)

从程序的角度考虑“标准输入”、“标准输出”和“标准错误”,而不是从内核的角度考虑。

当一个程序需要打印输出时,它通常会打印到“标准输出”。程序通常使用printf将输出输出打印到标准输出,printf仅打印到标准输出。

当程序需要打印错误信息时(不一定是异常,那些是编程语言的结构,在更高的级别上强加),它通常打印为“标准错误”。它通常使用fprintf来实现,fprintf接受打印时使用的文件流。文件流可以是任何打开用于写入的文件:标准输出、标准错误或任何其他已使用fopen或fdopen打开的文件。

当文件需要读取输入时,使用"standard in",使用fread或fgets或getchar。

这些文件中的任何一个都可以很容易地从shell重定向,就像这样:

cat /etc/passwd > /tmp/out     # redirect cat's standard out to /tmp/foo
cat /nonexistant 2> /tmp/err   # redirect cat's standard error to /tmp/error
cat < /etc/passwd              # redirect cat's standard input to /etc/passwd

或者,整个玉米卷饼:

cat < /etc/passwd > /tmp/out 2> /tmp/err

这里有两个重要的警告:首先,“标准输入”、“标准输出”和“标准错误”只是一种约定。它们是一个非常强大的约定,但它只是一个协议,能够运行这样的程序是非常好的:grep echo /etc/services | awk '{print $2;}' | sort,并将每个程序的标准输出连接到管道中下一个程序的标准输入。

其次,我给出了用于处理文件流(file *对象)的标准ISO C函数——在内核级别,它是所有的文件描述符(文件表的int引用)和更低级的操作,如读和写,它们不像ISO C函数那样进行愉快的缓冲。我想保持简单,使用更简单的函数,但我认为你仍然应该知道替代方案。:)

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是为您预定义的,以方便编程。这只是 一种约定,操作系统不强制执行。