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

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


当前回答

使用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中有许多错误

其他回答

stderr将不做IO缓存缓冲,所以如果我们的应用程序需要打印关键消息信息(一些错误,异常)到控制台或文件使用它作为使用标准输出打印一般日志信息,因为它使用IO缓存缓冲有一个机会,在写入我们的消息到文件应用程序可能会关闭,留下复杂的调试

下面是一篇关于stdin, stdout和stderr的长篇文章:

Linux中的stdin、stdout和stderr是什么?

总结:

Streams Are Handled Like Files Streams in Linux—like almost everything else—are treated as though they were files. You can read text from a file, and you can write text into a file. Both of these actions involve a stream of data. So the concept of handling a stream of data as a file isn’t that much of a stretch. Each file associated with a process is allocated a unique number to identify it. This is known as the file descriptor. Whenever an action is required to be performed on a file, the file descriptor is used to identify the file. These values are always used for stdin, stdout, and stderr: 0: stdin 1: stdout 2: stderr

具有讽刺意味的是,我在堆栈溢出和上面的文章中发现了这个问题,因为我正在搜索关于异常/非标准流的信息。所以我的探索还在继续。

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

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

第一个例子根本没有使用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

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

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

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

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

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

当然,这主要是惯例。如果您愿意,没有什么可以阻止您将诊断信息写入标准输出。您甚至可以完全关闭三个文件句柄,并打开您自己的文件进行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文件系统也是如此。这些不是真正的文件,只是严格控制的通往内核信息的网关。