九色91_成人精品一区二区三区中文字幕_国产精品久久久久一区二区三区_欧美精品久久_国产精品99久久久久久久vr_www.国产视频

Hello! 歡迎來到小浪云!


【Linux】Linux文件I/O


avatar
小浪云 2025-04-19 21

文件I/O

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

影響系統性能

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

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

解決方案:使用帶緩沖功能的標準I/O庫,以減少系統調用的次數。 例如: fwrite、fread、fopenfclose、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

相關閱讀

主站蜘蛛池模板: 国产免费一区 | 精品伊人 | 国产中文原创 | a在线视频| 日日干天天操 | 欧美一级在线视频 | 国产精品成人国产乱一区 | 精品久久久久久亚洲精品 | 黑人巨大精品欧美黑白配亚洲 | 久久99精品国产麻豆婷婷 | 成人网在线观看 | 精精精精xxxx免费视频 | 国产一区二区三区 | 91精品入口蜜桃 | 中文字幕一区二区三区不卡在线 | 精品国产乱码久久久久久图片 | av大全在线| 国产91一区 | 日韩av在线免费 | 久久一本 | 国产精品一区二区久久久久 | 99久久婷婷国产综合精品电影 | 欧美精品欧美精品系列 | 午夜久久久久久久久久一区二区 | 精品免费国产视频 | 国产99免费视频 | 91精品国产91久久综合桃花 | 日韩中文字幕一区二区三区 | 99av成人精品国语自产拍 | 久久久久国产一区二区三区 | 人人看人人干 | 日本中文在线视频 | 久久中文字幕一区 | 亚洲天堂一区二区 | 亚州中文 | 在线观看成人 | av毛片免费 | 久久1区| 精品一区在线看 | 欧美成人a | 亚洲综合色|