首页 > 运维 > linux运维 > 正文

【Linux】Linux文件I/O

爱谁谁
发布: 2025-04-19 20:41:59
原创
574人浏览过

文件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/types.h>#include <sys/stat.h>#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)) < 0) {fprintf(stderr,"write to %d failed,reason:%s.\n",fd,strerror(errno));return -3;}return wlen;}int main(int argc, char** argv) {//const char* TEXT = "This is a test.\n";char* buf = NULL;//buf = (char*)malloc(BUF_LEN); //地址要是512的倍数——内存边界对齐posix_memalign((void**)&buf,512,BUF_LEN);strcpy(buf,"This is test.\n");const char* filename = "./io_test.txt";int fd = 0;int i = 0;fd = open(filename,O_RDWR | O_TRUNC | O_CREAT | O_DIRECT);if (fd < 0) {fprintf(stderr, "fopen %s failed,reason:%s.\n exit\n",filename,strerror(errno));return -1;}for (i = 0; i < TOTAL; i++) {if (writeToFile(fd, buf, BUF_LEN) < 0) {fprintf(stderr,"write to %s failed,reason: %s.\n exit\n",filename,strerror(errno));//return -2;}printf("%d\n",i+1);}printf("finished.\n");//printf("Start to sleep 30 second....\n");if (buf)free(buf);close(fd);return 0;}
登录后复制

直接IO和标准方式进行对比

**示例:**测试20s内对同一文件的读取次数0

代码语言:javascript代码运行次数:0运行复制
#define _GNU_SOURCE#include <stdio.h>#include <string.h>#include <stdlib.h>#include <errno.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#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**)&buf,512,BUF_SIZE);if (ret)fprintf(stderr,"posix_memalign failed.reason:%s\n",strerror(errno));start = time(NULL);do {read_total++;//fd = open(filename, O_RDWR | O_DIRECT);fd = open(filename,O_RDWR);if (fd < 0) { fprintf(stderr, "fopen %s failed,reason:%s.\n exit\n", filename, strerror(errno));return -1;}do {if ((rlen = read(fd, buf, BUF_SIZE)) < 0) {fprintf(stderr, "read to %s failed,reason: %s.\n exit\n", filename, strerror(errno));}} while (rlen>0);close(fd);cur = time(NULL);} while ((cur-start) < 20);printf("total time:%d\n",read_total);return 0;}
登录后复制

直接IO

【Linux】Linux文件I/O

标准方式

(高速页缓存,多次读取速度快。)

【Linux】Linux文件I/O

O_SYNC
【Linux】Linux文件I/O
缓存同步

为了保证磁盘系统与缓冲区内容一致,Linux系统提供了sync,fsync,fdatasync三个函数。

函数描述:向打开的文件写数据,成功返回写入的字节数,出错则返回-1。

代码语言:javascript代码运行次数:0运行复制
#include<unistd.h>int fsync(int fd);int fdatasync(int fd);void sync(void);
登录后复制

说明:

sync——将所有修改过的块缓冲区排入写队列,然后就返回,它并不等待实际写磁盘操作结束。fsync——将fd对应文件的块缓冲区立即写入磁盘,并等待实际写磁盘操作结束返回。fdatasync——类似fsync,但只影响文件的数据部分。而除数据外,fsync还会同步更新文件属性。

Linux文件IO流程图
【Linux】Linux文件I/O

以上就是【Linux】Linux文件I/O的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号