久综合色-久综合网-玖草影视-玖草资源在线-亚洲黄色片子-亚洲黄色片在线观看

Hello! 歡迎來到小浪云!


【Linux】Linux文件I/O


文件I/O

直接使用系統調用的缺點:

影響系統性能

系統調用比普通函數調用開銷大,因為系統調用要進行用戶空間和內核空間的切換。

系統調用一次所能讀寫的數據量大小,受硬件的限制。

解決方案:使用帶緩沖功能的標準I/O庫,以減少系統調用的次數。 例如: fwrite、fread、fopen、fclose、fseek、fflush


文件系統接口

【Linux】Linux文件I/O

文件系統緩存

【Linux】Linux文件I/O

標準文件訪問方式

【Linux】Linux文件I/O

直接IO方式

【Linux】Linux文件I/O

示例:

代碼語言:JavaScript代碼運行次數:0運行復制

#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要考慮到硬件特性//磁盤最基本的單位是扇區,一個扇區512字節#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和標準方式進行對比<p>**示例:**測試20s內對同一文件的讀取次數0</p>代碼語言:javascript<i class="icon-code"></i>代碼運行次數:<!-- -->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>運行<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>復制<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>標準方式</p> <p>(高速頁緩存,多次讀取速度快。)</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>為了保證磁盤系統與緩沖區內容一致,Linux系統提供了sync,fsync,fdatasync三個函數。</p> <p>函數描述:向打開的文件寫數據,成功返回寫入的字節數,出錯則返回-1。</p>代碼語言:javascript<i class="icon-code"></i>代碼運行次數:<!-- -->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>運行<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>復制<pre class="prism-token token line-numbers javascript">#include<unistd.h>int fsync(int fd);int fdatasync(int fd);void sync(void);</unistd.h>

說明:

sync——將所有修改過的塊緩沖區排入寫隊列,然后就返回,它并不等待實際寫磁盤操作結束。fsync——將fd對應文件的塊緩沖區立即寫入磁盤,并等待實際寫磁盤操作結束返回。fdatasync——類似fsync,但只影響文件的數據部分。而除數據外,fsync還會同步更新文件屬性。


Linux文件IO流程圖

【Linux】Linux文件I/O

相關閱讀

主站蜘蛛池模板: 国产精品高清在线观看93 | 久久91精品国产91久久户 | 久久国产精品无码网站 | 一级成人毛片免费观看欧美 | 久久午夜鲁丝片午夜精品 | 二区久久国产乱子伦免费精品 | 国产成人精品福利网站人 | 日本高清视频www夜色资源 | 成人ab片 | 欧美成人一级视频 | 国产网站免费视频 | 午夜三级a三级三点在线观看 | 精品国产品欧美日产在线 | 欧美亚洲另类久久综合 | 精品久久久久久久久久久久久久久 | 国产九区| 亚洲免费天堂 | 免费狼人久久香蕉网 | 久久免费公开视频 | 久久一日本道色综合久久m 久久伊人成人网 | 成人国产亚洲 | 欧美日韩在线观看免费 | 亚洲国产高清在线精品一区 | 欧美www| 国产午夜精品理论片免费观看 | fc2在线播放| 特级做a爰片毛片免费看一区 | 一级特黄aaa大片免费看 | 97国产在线视频公开免费 | 蜜桃日本一道无卡不码高清 | 亚洲午夜免费 | 中文字幕 亚洲 一区二区三区 | 女人野外小树林一级毛片 | 一级欧美日韩 | 久久国产精品成人免费 | 久久99中文字幕 | 日韩亚洲国产综合久久久 | 手机看片1024欧美日韩你懂的 | 97视频免费上传播放 | 免费黄色网址在线播放 | 在线视频三区 |