APUE中fcntl.h的使用及O_SYNC在Mac与Ubuntu下的测试
2015-12-19 17:37
537 查看
此部分测试涉及到APUE V3中,第三章的图3-12到图3-14。
通过fcntl.h提供的功能,修改fd的文件属性,本处增加O_SYNC功能,并测试其效果。
本文涉及代码:
2 使用O_SYNC功能的代码
set_fl.h头文件:
set_fl.c代码:
sync.c代码,即前面mycat.c中,取消set_fl函数 的注释。
makefile.sync文件:
Ubuntu14.04效果如下:
mac10.11效果:
可见,每一次buf数据都直接O_SYNC到磁盘,会影响写磁盘的效果,在mac上几乎近10倍的差异。
而在Ubuntu上,却没有特别明显的差异,如书上所述。通过fcntl对O_SYNC的控制是失效的。
#over
本文源代码链接。
通过fcntl.h提供的功能,修改fd的文件属性,本处增加O_SYNC功能,并测试其效果。
本文涉及代码:
tree ch3 ch3 ├── makefile.sync ├── mycat.c ├── set_fl.c ├── set_fl.h ├── sync.c └── test
1 不使用O_SYNC功能
mycat.c 代码:#include "../apue.h" #define BUFFSIZE 4096 int main(void) { int n; char buf[BUFFSIZE]; // set_fl(STDOUT_FILENO, O_SYNC); for O_SYNC while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) if (write(STDOUT_FILENO, buf, n) != n) err_sys("write error"); if (n < 0) err_sys("read error"); exit(0); }
2 使用O_SYNC功能的代码
set_fl.h头文件:
#ifndef SET_FL #define SET_FL void set_fl(int fd, int flags); /* flags are file status flags to turn on */ void clr_fl(int fd, int flags); /* flags are file status flags to turn off */ #endif
set_fl.c代码:
#include <fcntl.h> #include "set_fl.h" void set_fl(int fd, int flags) /* flags are file status flags to turn on */ { int val; if ((val = fcntl(fd, F_GETFL, 0)) < 0) err_sys("fcntl F_GETFL error"); val |= flags; /* turn on flags */ if (fcntl(fd, F_SETFL, val) < 0) err_sys("fcntl F_SETFL error"); }
sync.c代码,即前面mycat.c中,取消set_fl函数 的注释。
makefile.sync文件:
sync: sync.o set_fl.o gcc -o sync sync.o set_fl.o set_fl.o: set_fl.c gcc -c set_fl.c
3 测试对比
准备:分别在mac及ubuntu环境下生成一个1GB的文件,并编译文件。dd if=/dev/zero of=./test bs=512 count=2048000 2048000+0 records in 2048000+0 records out 1048576000 bytes (1.0 GB) copied, 11.1418 s, 94.1 MB/s make -f makefile.sync gcc mycat.c
Ubuntu14.04效果如下:
time ./a.out < test >./dup.buf real 0m9.965s user 0m0.014s sys 0m1.453s time ./sync < test >./dup.sync real 0m10.355s user 0m0.025s sys 0m1.350s
mac10.11效果:
time ./a.out < test >/dev/null ./a.out < test > /dev/null 0.10s user 1.17s system 60% cpu 2.079 total time ./sync < test >/dev/null ./sync < test > /dev/null 0.10s user 1.20s system 62% cpu 2.070 total time ./sync < test >./dup.sync ./sync < test > ./dup.sync 0.27s user 23.79s system 45% cpu 53.369 total time ./a.out < test >./dup.buf ./a.out < test > ./dup.buf 0.11s user 3.06s system 53% cpu 5.955 total
可见,每一次buf数据都直接O_SYNC到磁盘,会影响写磁盘的效果,在mac上几乎近10倍的差异。
而在Ubuntu上,却没有特别明显的差异,如书上所述。通过fcntl对O_SYNC的控制是失效的。
#over
本文源代码链接。
相关文章推荐
- (一)cdh5.3.6下hue3.7.0的安装部署
- UILabel 行间距
- 《每天3分钟学开发》ios开发之UI基础:UI 第三天!
- 借助mosquitto“实时”远程监控服务器数据库运行状态
- 《每天3分钟学开发》ios开发之UI基础:UI 第二天!
- 《每天3分钟学开发》ios开发之UI基础:UI 第一天!
- Android-Bluetooth
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
- Hibernate之Query.uniqueResult()结果为数值的注意事项
- 代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧
- ios控件-UILabel
- AutoLayout处理UITableView动态高度
- Arduino - IIC SCANNER
- 【iOS学习笔记 15-12-19】自定义cell侧滑按钮(UIButton)
- UIWindow和UIView的简单使用
- 黑马程序员_Java基础-GUI
- iOS学习之 iOS9新特性UIStackView
- dequeueReusableCellWithIdentifier: forIndexPath: 获取cell
- 分享一个使用NSIS制作安装包的UI插件
- 13、提升Android UI流畅度的建议