Unix高级编程“流”的意思

常常看到“流”这个关键字,经过百度没找到答案,最后翻墙到用google,算是找到一种算是比较详细的解释吧。额。。。英文大致翻译一下,原文如下:

The Metaphor

As mentioned a stream is a metaphor, an abstraction of something more complex. To get your imagination working I offer some other metaphors:

you want to fill an empty pool with water. one way to accomplish this is to attach a hose to a spigot, placing the end of the hose in the pool and turning on the water.

the hose is the stream

大致意思如下:

隐喻

流是一种隐喻,一个比较复杂的抽象的概念,为了理解它是怎么工作的,我们提供如下的比喻:

如果你想用一个水管充满空的池子,水管的一端链接到房子的套管,另一端放到池子里。

房子就是流

similarly, if you wanted to refill your car with gas, you would go to a gas pump, insert the nozzle into your gas tank and open the valve by squeezing the locking lever.

the hose, nozzle and associated mechanisms to allow the gas to flow into your tank is the stream

if you need to get to work you would start driving from your home to the office using the freeway.

the freeway is the stream

同样的,如果你想用气体来充满一辆车,你会去加油泵,将喷嘴插入油箱,然后挤压锁定杆打开阀门。

房子,喷嘴以及一切能让气体流入你容器的机制就叫“流”。

当你要去工作时,你将开车通过高速路从家到达工作地点。

高速公路就是“流”。

Hopefully you notice in these examples that the stream metaphors only exist to allow something to travel through it (or on it in the case of the freeway) and do not themselves always poses the thing they are transferring. An important distinction. A hose is still a hose if no water is flowing through it, but we have to connect it to a spigot for it do its job correctly. A car is not the only 'kind' of vehicle that can traverse a freeway.

Thus a stream can exist that has no data travelling through it as long as it is connected to a file

希望你在这些例子中注意到流隐喻只存在允许某些东西穿过它(或者在高速公路的情况下在它上面)而且它们本身并不构成他们正在传递的东西。一个重要的区别。 如果没有水流过软管,软管仍然是软管,但是我们必须将它连接到套管才能正常工作。 汽车不是唯一可以穿越高速公路的“种类”汽车。

因此,只要连接到文件,就可以存在没有数据通过它的流。

Removing the Abstraction

Next, we need to answer a few questions. I'm going to use files to describe streams so... What is a file? And how do we read a file? I will attempt to answer this while maintaining a certain level of abstraction to avoid unneeded complexity and will use the concept of a file relative to a linux operating system because of its simplicity and accessibility.

删除抽象

接下来,我们需要回答几个问题。 我将使用文件来描述流,所以...什么是文件? 我们如何读取文件? 我将尝试回答这个问题,同时保持一定程度的抽象以避免不必要的复杂性,并且由于其简单性和可访问性,将使用相对于Linux操作系统的文件概念。

What is a file?

A file is an abstraction

Or, as simply as I can explain, a file is one part data structure describing the file and one part data which is the actual content.

The data structure part (called an inode in UNIX/linux systems) identities important pieces of information about the content, but does not include the content itself (or a name of the file for that matter). One of the pieces of information it keeps is a memory address to where the content starts. So with a file name (or a hard link in linux), a file descriptor (a numeric file name that the operating system cares about) and a starting location in memory we have something we can call a file.

(the key takeaway is a 'file' is defined by the operating system since it is the OS that ultimately has to deal with it. and yes, files are much more complex).

So far so good. But how do we get the content of the file, say a love letter to your beau, so we can print it?

文件是一种抽象

或者,正如我可以解释的那样,文件是描述文件的一部分数据结构和一部分数据,它是实际内容。

数据结构部分(在UNIX / linux系统中称为inode)标识有关内容的重要信息,但不包括内容本身(或该文件的名称)。 它保留的信息之一是内容开始的内存地址。 因此,使用文件名(或Linux中的硬链接),文件描述符(操作系统关心的数字文件名)和内存中的起始位置,我们可以调用文件。

(关键的一点是'文件'是由操作系统定义的,因为它是最终必须处理它的操作系统。是的,文件要复杂得多)。

到现在为止还挺好。 但是我们如何得到文件的内容,给你的男友写一封情书,以便我们打印出来?

Reading a file

If we start from the result and move backwards, when we open a file on our computer its entire contents is splashed on our screen for us to read. But how? Very methodically is the answer. The content of the file itself is another data structure. Suppose an array of characters. We can also think of this as a string.

So how do we 'read' this string? By finding its location in memory and iterating through our array of characters, one character at a time until reaching an end of file character. In other words a program.

A stream is 'created' when its program is called and it has a memory location to attach to or connect to. Much like our water hose example, the hose is ineffective if it is not connected to a spigot. In the case of the stream, it must be connected to a file for it to exist.

Streams can be further refined, e.g, a stream to receive input or a stream to send a files contents to standard output. UNIX/linux connects and keeps open 3 filestreams for us right off the bat, stdin (standard input), stdout (standard output) and stderr (standard error). Streams can be built as data structures themselves or objects which allows us to perform more complex operations of the data streaming through them, like opening the stream, closing the stream or error checking the file a stream is connected to. C++'s cin is an example of a stream object.

Surely, if you so choose, you can write your own stream.

读一个文件

如果我们从结果开始向后移动,当我们在计算机上打开文件时,它的全部内容都会在我们的屏幕上显示,以供我们阅读。但是为什么会这样?答案非常有条理。文件本身的内容是另一种数据结构。假设一个字符数组。我们也可以将其视为一个字符串。

那么我们如何'读'这个字符串呢?通过在内存中查找其位置并遍历我们的字符数组,一次一个字符,直到达到文件结束字符。换句话说,一个程序。

调用其程序时会“创建”一个流,并且它具有要连接或连接的内存位置。与我们的水管示例非常相似,如果软管没有连接到套管,则软管无效。对于流,它必须连接到文件才能存在。

可以进一步细化流,例如,用于接收输入的流或用于将文件内容发送到标准输出的流。 UNIX / linux连接并保持打开3个文件流,直接用于我们,stdin(标准输入),stdout(标准输出)和stderr(标准错误)。流可以构建为数据结构本身或对象,这允许我们通过它们执行数据流的更复杂操作,例如打开流,关闭流或错误检查流连接到的文件。 C ++的cin是流对象的一个​​例子。

当然,如果您这样选择,您可以编写自己的流。

Definition

A stream is a reusable piece of code that abstracts the complexity of dealing with data while providing useful operations to perform on data.

定义

流是一种可重复使用的代码,它提供了处理数据的复杂性,同时提供了对数据执行的有用操作。

那么总的来说,“流”我个人理解为是为了管理数据而存在的一种机制,比如读,写,查找等等相关的功能,比如我知道数据的内存在哪,那么就要写一个相关的操作来读,写等等一系列的操作,那么我们把这些操作封装,数据通过流的时候,我们可以任意的操作数据,构成一种机制就是“流”,所以文件只是为了管理数据,而“流”是为了操作数据!!!

以上总结只是个人理解!!!


分享到:


相關文章: