色偷偷91综合久久噜噜-色偷偷成人-色偷偷尼玛图亚洲综合-色偷偷人人澡久久天天-国内精品视频一区-国内精品视频一区二区三区

Hello! 歡迎來(lái)到小浪云!


【Linux】Linux文件I/O


文件I/O

直接使用系統(tǒng)調(diào)用的缺點(diǎn):

影響系統(tǒng)性能

系統(tǒng)調(diào)用比普通函數(shù)調(diào)用開銷大,因?yàn)橄到y(tǒng)調(diào)用要進(jìn)行用戶空間和內(nèi)核空間的切換。

系統(tǒng)調(diào)用一次所能讀寫的數(shù)據(jù)量大小,受硬件的限制。

解決方案:使用帶緩沖功能的標(biāo)準(zhǔn)I/O庫(kù),以減少系統(tǒng)調(diào)用的次數(shù)。 例如: fwrite、fread、fopenfclose、fseek、fflush


文件系統(tǒng)接口

【Linux】Linux文件I/O

文件系統(tǒng)緩存

【Linux】Linux文件I/O

標(biāo)準(zhǔn)文件訪問(wèn)方式

【Linux】Linux文件I/O

直接IO方式

【Linux】Linux文件I/O

示例:

代碼語(yǔ)言:JavaScript代碼運(yùn)行次數(shù):0運(yùn)行復(fù)制

#define _GNU_SOURCE#include <stdio.h>#include <string.h>#include <stdlib.h>#include <errno.h>#include <unistd.h>#include <sys>#include <sys>#include <fcntl.h>#define TOTAL 10//直接IO要考慮到硬件特性//磁盤最基本的單位是扇區(qū),一個(gè)扇區(qū)512字節(jié)#define BUF_LEN 512int writeToFile(int fd,const char* buf,int len) {int wlen = 0;if ((wlen = write(fd, buf, len)) <figure class=""><hr></figure>直接IO和標(biāo)準(zhǔn)方式進(jìn)行對(duì)比<p>**示例:**測(cè)試20s內(nèi)對(duì)同一文件的讀取次數(shù)0</p>代碼語(yǔ)言:javascript<i class="icon-code"></i>代碼運(yùn)行次數(shù):<!-- -->0<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewbox="0 0 16 16" fill="none"><path d="M6.66666 10.9999L10.6667 7.99992L6.66666 4.99992V10.9999ZM7.99999 1.33325C4.31999 1.33325 1.33333 4.31992 1.33333 7.99992C1.33333 11.6799 4.31999 14.6666 7.99999 14.6666C11.68 14.6666 14.6667 11.6799 14.6667 7.99992C14.6667 4.31992 11.68 1.33325 7.99999 1.33325ZM7.99999 13.3333C5.05999 13.3333 2.66666 10.9399 2.66666 7.99992C2.66666 5.05992 5.05999 2.66659 7.99999 2.66659C10.94 2.66659 13.3333 5.05992 13.3333 7.99992C13.3333 10.9399 10.94 13.3333 7.99999 13.3333Z" fill="currentcolor"></path></svg>運(yùn)行<svg width="16" height="16" viewbox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M4.5 15.5V3.5H14.5V15.5H4.5ZM12.5 5.5H6.5V13.5H12.5V5.5ZM9.5 2.5H3.5V12.5H1.5V0.5H11.5V2.5H9.5Z" fill="currentcolor"></path></svg>復(fù)制<pre class="prism-token token line-numbers javascript">#define _GNU_SOURCE#include <stdio.h>#include <string.h>#include <stdlib.h>#include <errno.h>#include <unistd.h>#include <sys>#include <sys>#include <fcntl.h>#define BUF_SIZE 512int main(int argc, char** argv) {char* buf = NULL;const char* filename = "./open_compare.txt";int fd = -1;time_t start;time_t cur;int rlen = 0;int ret = 0;static int read_total = 0;ret = posix_memalign((void**)&amp;buf,512,BUF_SIZE);if (ret)fprintf(stderr,"posix_memalign failed.reason:%sn",strerror(errno));start = time(NULL);do {read_total++;//fd = open(filename, O_RDWR | O_DIRECT);fd = open(filename,O_RDWR);if (fd 0);close(fd);cur = time(NULL);} while ((cur-start) <p>直接IO</p> <figure class=""><img src="https://img.php.cn/upload/article/001/503/042/174506652360359.jpg" alt="【Linux】Linux文件I/O"></figure><p>標(biāo)準(zhǔn)方式</p> <p>(高速頁(yè)緩存,多次讀取速度快。)</p> <figure class=""><img src="https://img.php.cn/upload/article/001/503/042/174506652369371.jpg" alt="【Linux】Linux文件I/O"></figure><figure class=""><hr></figure>O_SYNC<figure class=""><img src="https://img.php.cn/upload/article/001/503/042/174506652331485.jpg" alt="【Linux】Linux文件I/O"></figure>緩存同步<p>為了保證磁盤系統(tǒng)與緩沖區(qū)內(nèi)容一致,Linux系統(tǒng)提供了sync,fsync,fdatasync三個(gè)函數(shù)。</p> <p>函數(shù)描述:向打開的文件寫數(shù)據(jù),成功返回寫入的字節(jié)數(shù),出錯(cuò)則返回-1。</p>代碼語(yǔ)言:javascript<i class="icon-code"></i>代碼運(yùn)行次數(shù):<!-- -->0<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewbox="0 0 16 16" fill="none"><path d="M6.66666 10.9999L10.6667 7.99992L6.66666 4.99992V10.9999ZM7.99999 1.33325C4.31999 1.33325 1.33333 4.31992 1.33333 7.99992C1.33333 11.6799 4.31999 14.6666 7.99999 14.6666C11.68 14.6666 14.6667 11.6799 14.6667 7.99992C14.6667 4.31992 11.68 1.33325 7.99999 1.33325ZM7.99999 13.3333C5.05999 13.3333 2.66666 10.9399 2.66666 7.99992C2.66666 5.05992 5.05999 2.66659 7.99999 2.66659C10.94 2.66659 13.3333 5.05992 13.3333 7.99992C13.3333 10.9399 10.94 13.3333 7.99999 13.3333Z" fill="currentcolor"></path></svg>運(yùn)行<svg width="16" height="16" viewbox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M4.5 15.5V3.5H14.5V15.5H4.5ZM12.5 5.5H6.5V13.5H12.5V5.5ZM9.5 2.5H3.5V12.5H1.5V0.5H11.5V2.5H9.5Z" fill="currentcolor"></path></svg>復(fù)制<pre class="prism-token token line-numbers javascript">#include<unistd.h>int fsync(int fd);int fdatasync(int fd);void sync(void);</unistd.h>

說(shuō)明:

sync——將所有修改過(guò)的塊緩沖區(qū)排入寫隊(duì)列,然后就返回,它并不等待實(shí)際寫磁盤操作結(jié)束。fsync——將fd對(duì)應(yīng)文件的塊緩沖區(qū)立即寫入磁盤,并等待實(shí)際寫磁盤操作結(jié)束返回。fdatasync——類似fsync,但只影響文件的數(shù)據(jù)部分。而除數(shù)據(jù)外,fsync還會(huì)同步更新文件屬性。


Linux文件IO流程圖

【Linux】Linux文件I/O

相關(guān)閱讀

主站蜘蛛池模板: 亚洲精品老司机综合影院 | 久久久精品久久久久三级 | 久久综合给合久久狠狠狠色97 | 一级床片45分钟的视频 | 精品久久人人做人人爽综合 | 一级黄色片免费的 | 国产在线色视频 | 人人干人人玩 | 一区二区三区在线免费观看视频 | 成人αv在线视频高清 | 成人全黄三级视频在线观看 | 国产网站大全 | 国产色综合天天综合网 | 国产欧美精品区一区二区三区 | 狠狠色综合色综合网站嗯 | 伊人激情久久综合中文字幕 | 久久久综合结合狠狠狠97色 | 2021久久精品国产99国产精品 | 九色欧美| 国产精品美女久久久久久 | 久久久国产99久久国产首页 | 国产人人草 | 久久亚洲精品tv | 久久本道久久综合伊人 | 国内一级片 | 亚洲男人天堂 | 国产精品一级二级三级 | 一本色道久久88加勒比—综合 | 国产区精品福利在线社区 | 天天看片日日夜夜 | 中文有码中文字幕免费视频 | 97se亚洲国产综合自在线观看 | 亚洲天堂男人的天堂 | 日韩高清在线免费看 | 激情五月激情综合网 | 国产无卡一级毛片aaa | 欧美一区综合 | 欧美色视频在线观看 | 四虎影院永久免费观看 | 欧美特黄高清免费观看的 | 狠狠色噜噜狠狠狠狠网站视频 |