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.

定義

流是一種可重複使用的代碼,它提供了處理數據的複雜性,同時提供了對數據執行的有用操作。

那麼總的來說,“流”我個人理解為是為了管理數據而存在的一種機制,比如讀,寫,查找等等相關的功能,比如我知道數據的內存在哪,那麼就要寫一個相關的操作來讀,寫等等一系列的操作,那麼我們把這些操作封裝,數據通過流的時候,我們可以任意的操作數據,構成一種機制就是“流”,所以文件只是為了管理數據,而“流”是為了操作數據!!!

以上總結只是個人理解!!!


分享到:


相關文章: