• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            關(guān)于流和緩沖區(qū)的理解

            0. 序曲

            寫(xiě)這篇短文的起因是,前兩天想去天大的acm在線(xiàn)系統(tǒng)找?guī)椎李}做做。為什么呢?因?yàn)楸救颂齑螽厴I(yè),這個(gè)天大呢可是中國(guó)最早的大學(xué),原名北洋大學(xué)堂,這可絕對(duì)是貨真價(jià)實(shí)的第一所大學(xué)。給大家推薦推薦啊,學(xué)風(fēng)那是相當(dāng)?shù)暮谩?/p>

            扯多了,還是回到本來(lái)的話(huà)題上。上了acm系統(tǒng)之后,就先看了1001。那道題的意思是輸入一些正整數(shù)(以EOF結(jié)束),把對(duì)應(yīng)的字符輸出。這個(gè)簡(jiǎn)單,程序很快就出來(lái)了:


             

            #include <stdio.h>
            int main()
            {
            int c;
            while(scanf("%d"&c) != EOF)
            {
            putchar(c);
            }
            return 0;
            }

             


            程序運(yùn)行,輸入103 102 105 107<enter>

            輸出gfik。

            當(dāng)時(shí)運(yùn)行完之后馬上想,為什么不是輸入一個(gè)數(shù)字馬上輸出一個(gè)字符呢,因?yàn)榭闯绦虼_實(shí)是這樣的邏輯,只要不是EOF,就會(huì)輸出。又一想,對(duì)了,是緩沖的問(wèn)題。想起來(lái)APUE里邊說(shuō)得stdin應(yīng)該是行緩沖的,另外,可以用setbuf,setvbuf設(shè)定流的緩沖。于是想將stdin設(shè)成無(wú)緩沖的。于是程序變成這樣:


            #include <stdio.h>
            int main()
            {
            int c;
            setbuf(stdin, NULL);
            while(scanf("%d"&c) != EOF)
            {
            putchar(c);
            }
            return 0;
            }


            可是編譯運(yùn)行,還是老樣子,沒(méi)有變化。想了想,沒(méi)想出是啥原因,于是開(kāi)始google和APUE。終于算是明白了些,整理在這兒。


            聲明:

            本文很大部分內(nèi)容來(lái)自APUE--UNIX環(huán)境高級(jí)編程。

            1. 緩沖類(lèi)型。

            標(biāo)準(zhǔn)庫(kù)提供緩沖是為了減少對(duì)read和write的調(diào)用。提供的緩沖有三種類(lèi)型(整理自APUE):

            • 全緩沖。

            在這種情況下,實(shí)際的I/O操作只有在緩沖區(qū)被填滿(mǎn)了之后才會(huì)進(jìn)行。對(duì)駐留在磁盤(pán)上的文件的操作一般是有標(biāo)準(zhǔn)I/O庫(kù)提供全緩沖。緩沖區(qū)一般是在第一次對(duì)流進(jìn)行I/O操作時(shí),由標(biāo)準(zhǔn)I/O函數(shù)調(diào)用malloc函數(shù)分配得到的。

            術(shù)語(yǔ)flush描述了標(biāo)準(zhǔn)I/O緩沖的寫(xiě)操作。緩沖區(qū)可以由標(biāo)準(zhǔn)I/O函數(shù)自動(dòng)flush(例如緩沖區(qū)滿(mǎn)的時(shí)候);或者我們對(duì)流調(diào)用fflush函數(shù)。

            • 行緩沖

            在這種情況下,只有在輸入/輸出中遇到換行符的時(shí)候,才會(huì)執(zhí)行實(shí)際的I/O操作。這允許我們一次寫(xiě)一個(gè)字符,但是只有在寫(xiě)完一行之后才做I/O操作。一般的,涉及到終端的流--例如標(biāo)注輸入(stdin)和標(biāo)準(zhǔn)輸出(stdout)--是行緩沖的。

            • 無(wú)緩沖

            標(biāo)準(zhǔn)I/O庫(kù)不緩存字符。需要注意的是,標(biāo)準(zhǔn)庫(kù)不緩存并不意味著操作系統(tǒng)或者設(shè)備驅(qū)動(dòng)不緩存。

            ISO C要求:

            • 當(dāng)且僅當(dāng)不涉及交互設(shè)備時(shí),標(biāo)準(zhǔn)輸入和標(biāo)準(zhǔn)輸出是全緩存的。
            • 標(biāo)準(zhǔn)錯(cuò)誤絕對(duì)不是全緩存的。

            但是,這并沒(méi)有告訴我們當(dāng)標(biāo)準(zhǔn)輸入/輸出在涉及交互設(shè)備時(shí),它們是無(wú)緩存的還是行緩存的;也沒(méi)有告訴我們標(biāo)準(zhǔn)錯(cuò)誤應(yīng)該是行緩存的還是無(wú)緩存的。不過(guò),大多數(shù)實(shí)現(xiàn)默認(rèn)的緩存類(lèi)型是這樣的:

            • 標(biāo)準(zhǔn)錯(cuò)誤總是無(wú)緩存的。
            • 對(duì)于所有的其他流來(lái)說(shuō),如果它們涉及到交互設(shè)備,那么就是行緩存的;否則是全緩存的。

            2. 改變默認(rèn)緩存類(lèi)型

            可以通過(guò)下面的函數(shù)改變緩存類(lèi)型(摘自APUE):

            void setbuf(FILE *restrict fp, char *restrict buf);
            int setvbuf(FILE *restrict fp, char *restrict buf, int mode, size_t size);

            這些函數(shù)必須在流打開(kāi)之后、但是未對(duì)流做任何操作之前被調(diào)用(因?yàn)槊總€(gè)函數(shù)都需要一個(gè)有效的文件指針作為第一個(gè)參數(shù))。

            利用setbuf,可以打開(kāi)或者關(guān)閉緩存。為了打開(kāi)緩存,buf參數(shù)必須一個(gè)大小為BUFSIZ的緩存,BUFSIZ是定義在stdio。h中的常量。&amp;lt;&amp;lt;ISO/IEC 9899&amp;gt;&amp;gt;要求:BUFSIZ至少為256。如果要關(guān)閉緩存,可以將buf設(shè)成NULL。

            利用setvbuf,我們可以設(shè)定緩存類(lèi)型。這是通過(guò)mode參數(shù)指定的。

            關(guān)于這兩個(gè)函數(shù),可以看下表(摘自APUE):


            Function

            mode

            buf

            Buffer and length

            Type of buffering

            setbuf

            non-null

            user buf of length BUFSIZ

            fully buffered or line buffered

            NULL

            (no buffer)

            unbuffered

            setvbuf

            _IOLBF

            non-null

            user buf of length size

            fully buffered

            NULL

            system buffer of appropriate length

            _IOFBF

            non-null

            user buf of length size

            line buffered

            NULL

            system buffer of appropriate length

            _IONBF

            (ignored)

            (no buffer)

            unbuffered

            需要注意的是:如果在函數(shù)內(nèi)為流分配了自動(dòng)變量作為緩存,那么在退出之前需要將流關(guān)閉。因此最好讓系統(tǒng)自己分配緩存,這些緩存在流關(guān)閉的時(shí)候會(huì)自動(dòng)被釋放。


            3.如果清理輸入緩存

            關(guān)于這點(diǎn)可以參看comp.lang.c FAQ的Question12.26b:

            Q: If fflush won't work, what can I use to flush input?

            A: It depends on what you're trying to do. If you're trying to get rid of an unread newline or other unexpected input after calling scanf (see questions 12.18a-12.19), you really need to rewrite or replace the call to scanf (see question 12.20). Alternatively, you can consume the rest of a partially-read line with a simple code fragment like

            while((c = getchar()) != '\n' &amp;&amp; c != EOF)
            /* discard */ ;

            (You may also be able to use the curses flushinp function.)

            There is no standard way to discard unread characters from a stdio input stream. Some vendors do implement fflush so that fflush(stdin) discards unread characters, although portable programs cannot depend on this. (Some versions of the stdio library implement fpurge or fabort calls which do the same thing, but these aren't standard, either.) Note, too, that flushing stdio input buffers is not necessarily sufficient: unread characters can also accumulate in other, OS-level input buffers. If you're trying to actively discard input (perhaps in anticipation of issuing an unexpected prompt to confirm a destructive action, for which an accidentally-typed ``y'' could be disastrous), you'll have to use a system-specific technique to detect the presence of typed-ahead input; see questions 19.1 and 19.2. Keep in mind that users can become frustrated if you discard input that happened to be typed too quickly.

            References: ISO Sec. 7.9.5.2
            H&amp;S Sec. 15.2

            4. 幾點(diǎn)需要注意的地方

            • 對(duì)輸入流進(jìn)行fflush操作是無(wú)定義的。
            • 無(wú)緩存并不意味著一個(gè)個(gè)的那樣處理輸入,而是說(shuō)當(dāng)操作系統(tǒng)返回它們時(shí),對(duì)于標(biāo)準(zhǔn)庫(kù)函數(shù)來(lái)說(shuō)它們是立即可用的。因?yàn)檫€可能有操作系統(tǒng)級(jí)甚至是硬件級(jí)的緩存,這些并不是setbuf可以控制的。
            • 另外可以參考這里(我就是最先從這里開(kāi)始看的)。還有這里。我從后面那個(gè)鏈接摘錄一些重要的下來(lái):

            setbuf() has to do with the delivery of bytes between the
            C library FILE* management layer and the OS I/O layer.

            Calls to fread(), fgets(), fgetc(), and getchar() work within
            whatever FILE* buffered data is available, and when that data
            is exhausted, the calls request that the FILE* buffer be refilled
            by the system I/O layer.

            When full buffering is turned on, that refill operation results in the
            FILE* layer requesting that the operating system hand it a full
            buffer's worth of data; when buffering is turned off, that
            refill operation results in the FILE* layer requesting that the
            operating system return a single character.

            ...setting an input stream to be unbuffered
            does NOT tell the operating system to tell the device driver
            to go into any kind of "raw" single-character mode. There are
            system-specific calls such as ioctl() and tcsetterm() that
            control what the device driver will do.





            posted on 2008-04-07 12:48 季陽(yáng) 閱讀(6415) 評(píng)論(4)  編輯 收藏 引用

            評(píng)論

            # re: 關(guān)于流和緩沖區(qū)的理解 2008-04-07 15:03 亨德列克

            受教了  回復(fù)  更多評(píng)論   

            # re: 關(guān)于流和緩沖區(qū)的理解 2008-04-07 15:51 yyxxh

            上次看譚浩強(qiáng)的C程序設(shè)計(jì)例子也提了這樣一個(gè)問(wèn)題,再次看到有人提出這樣的問(wèn)題,溫故而知新啊  回復(fù)  更多評(píng)論   

            # re: 關(guān)于流和緩沖區(qū)的理解 2011-10-11 15:12 靜靜

            int main()
            {
            int c;
            setbuf(stdin, NULL);
            while(scanf("%d", &c) != EOF)
            {
            putchar(c);
            }
            return 0;
            }
            為啥輸出還是一樣呢?setbuf(stdin,NULL)沒(méi)發(fā)揮作用嗎?樓主解釋一下。  回復(fù)  更多評(píng)論   

            # re: 關(guān)于流和緩沖區(qū)的理解 2011-12-26 19:48 金艷

            對(duì)啊,為什么第二個(gè)例子不起作用還沒(méi)說(shuō)啊  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(2)

            隨筆檔案(12)

            搜索

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久综合给合久久狠狠狠97色| 精品免费久久久久国产一区| 久久99久国产麻精品66| 一本久道久久综合狠狠爱| 久久这里只有精品18| 精品无码人妻久久久久久| 国产日韩久久久精品影院首页| 精品无码久久久久久国产| 亚洲第一极品精品无码久久| 国产L精品国产亚洲区久久| 欧洲人妻丰满av无码久久不卡| 青青青国产成人久久111网站| 国产精品久久久久久久久久影院| 9久久9久久精品| 精品久久久久香蕉网| 久久久无码精品亚洲日韩京东传媒| 精品久久久久久中文字幕人妻最新| 亚洲另类欧美综合久久图片区| 91精品日韩人妻无码久久不卡| 久久精品国产亚洲AV不卡| 久久精品夜色噜噜亚洲A∨| 久久青青草原国产精品免费| 久久精品人成免费| 亚洲精品乱码久久久久久| 精品久久久中文字幕人妻| 亚洲欧美国产日韩综合久久| 久久久艹| 日日狠狠久久偷偷色综合0| 久久99精品久久久久久野外 | 狠狠久久综合| 99久久超碰中文字幕伊人| 无码超乳爆乳中文字幕久久| 久久无码AV中文出轨人妻| 国产99久久久国产精品~~牛 | 成人精品一区二区久久久| 国产精品久久影院| 午夜不卡888久久| 久久国产视屏| 久久亚洲精品国产亚洲老地址| 伊人久久大香线蕉综合热线| 麻豆精品久久久久久久99蜜桃|