文件描述符常量(unistd.h):

  • STDIN_FILENO
  • STDOUT_FILENO
  • STDERR_FILENO

通常这些常量分别对应于数字0,1,2

文件操作需要头文件 fcntl.h ,一些常量需要头文件unistd.h

  • open int open(const char *pathname, int oflag, ... )

    • 其中oflag可以是以下值的集合

      • O_RDONLY、O_WRONLY、O_RDWR 读写属性
      • O_APPEND、O_CREAT、O_EXCL、O_TRUNC、O_NOCTTY、O_NONBLOCK
  • creat 等效于 open (pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
  • closeint close(int filedes);
  • lseek off_t lseek(int filedes, off_t offset, int whence); 
    • 与偏移 offset 相对的位置whence:

      • SEEK_SET 文件起始
      • SEEK_CUR 当前位置
      • SEEK_END 文件末尾
  • write ssize_t write(int filedes, const void *buf, size_t nbytes);

生成有洞的文件

使用lseek跳到文件原大小之外的范围然后写入数据,那么中间的部分数据不会被真正存储,不过读取是都当做0看待,但ls看到的文件大小还是将中间这些算进去的,这样可以非常快速的生成较大的空白文件。

 #include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <string.h> char file_header[] = "this is the file header msg.";
char file_end[] = "now we reach the file end."; int main() {
printf("size of off_t: %ld\n", sizeof(off_t));
printf("max file open number: %ld\n", sysconf(_SC_OPEN_MAX));
printf("max name length: %d\n", _POSIX_NAME_MAX);
printf("max path length: %d\n", _POSIX_PATH_MAX); int fd = open("tmpfile", O_RDWR | O_CREAT | O_TRUNC); if (fd < ) {
perror("file operation open");
return ;
} int head_len = strlen(file_header);
int end_len = strlen(file_end); if (write(fd, file_header, head_len) != head_len) {
perror("write file header msg failed.");
} if (lseek(fd, , SEEK_SET) == -) {
perror("lseek failed.");
} if (write(fd, file_end, end_len) != end_len) {
perror("write file end msg failed.");
} if (close(fd) < ) {
perror("file operation close");
}
return ;
}

缓存影响I/O效率

由于有预读取的过程存在使用缓存可以提高复制速度,但是当缓存超过其预读取大小时缓存作用就降低了

 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h> char buffer[ * ]; int main(int argc, char* argv[]) {
if (argc < ) {
fprintf(stderr, "usage:\n\t%s <buffer_size>\n", argv[]);
return ;
} int buffer_size = ;
sscanf(argv[], "%d", &buffer_size); if (buffer_size > sizeof(buffer)) {
fprintf(stderr, "buffer size too big. max: %ld\n", sizeof(buffer));
return ;
} int bytes_read = ; while ((bytes_read = read(STDIN_FILENO, buffer, buffer_size)) > ) {
if (write(STDOUT_FILENO, buffer, bytes_read) != bytes_read) {
fprintf(stderr, "write error.\n");
}
} if (bytes_read < ) {
fprintf(stderr, "read error.\n");
} return ;
}

测试了使用不同缓存的情况下IO效率(比较随意):

Linux中的fd设备

Unix Programming :文件IO的更多相关文章

  1. (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  2. UNIX高级环境编程(14)文件IO - O_DIRECT和O_SYNC详解 < 海棠花溪 >

    春天来了,除了工作学习,大家也要注意锻炼身体,多出去运动运动.  上周末在元大都遗址公园海棠花溪拍的海棠花.   进入正题. O_DIRECT和O_SYNC是系统调用open的flag参数.通过指定o ...

  3. Unix环境高级编程:文件 IO 原子性 与 状态 共享

    参考 UnixUnix环境高级编程 第三章 文件IO 偏移共享 单进程单文件描述符 在只有一个进程时,打开一个文件,对该文件描述符进行写入操作后,后续的写入操作会在原来偏移的基础上进行,这样就可以实现 ...

  4. Unix系统编程()深入探究文件IO概述

    open调用将引入原子atomicity操作的概念. 将某一系统调用所要完成的各个动作作为不可中断的操作,一次性加以执行. 原子操作是许多系统调用得以正确执行的必要条件. 还介绍一个系统调用fcntl ...

  5. Unix环境编程之文件IO

    1.文件IO 2.文件与目录 3.进程 4.多线程编程 5.信号 6.进程间通信 学习linux编程,首先要学会使用shell,这里一些基础命令就不介绍了.这里唯一要提的一个shell命令就是man. ...

  6. linux系统编程--文件IO

    系统调用 什么是系统调用: 由操作系统实现并提供给外部应用程序的编程接口.(Application Programming Interface,API).是应用程序同系统之间数据交互的桥梁. C标准函 ...

  7. 转:Linux 文件IO理解

    源地址http://blog.csdn.net/lonelyrains/article/details/6604851 linux文件IO操作有两套大类的操作方式:不带缓存的文件IO操作,带缓存的文件 ...

  8. 文件IO函数和标准IO库的区别

    摘自 http://blog.chinaunix.net/uid-26565142-id-3051729.html 1,文件IO函数,在Unix中,有如下5个:open,read,write,lsee ...

  9. 文件IO

    在unix世界中视一切为文件,无论最基本的文本文件还是网络设备或是u盘,在内核看来它们的本质都是一样的.大多数文件IO操作只需要用到5个函数:open . read . write . lseek 以 ...

  10. Golang文件IO 一

    Golang文件IO 一 文件IO编程最基本.最常用的就属读写文件操作了.ioutil包实现了一些IO实用功能,其中就包括非常简捷.好用的文件读取功能. ioutil包有7个函数1个变量: var D ...

随机推荐

  1. 微信小程序 Unexpected end of JSON input/Unexpected token o in JSON at position 1

    原因JSON.parse无法识别某些url中的特殊字符,所以报错 mistakes.js中 nextBtn:function(){ var nextData = this.data.dataNextI ...

  2. Codeforces450 B. Jzzhu and Sequences (找规律)

    题目链接:https://vjudge.net/problem/CodeForces-450B Jzzhu has invented a kind of sequences, they meet th ...

  3. ZZNU 1719(最长上升子序列+最长下降子序列)

    先吐血一发,噗! 再吐血一次,啊啊啊啊! 好吧,做了那么多次最长上升子序列,看这题看了半天才发现还有最长下降子序列,呵呵哒! AC代码: #include<stdio.h>//老恶心#in ...

  4. Jmeter监控系统等资源,ServerAgent端口的本次启动端口修改

    默认情况下在下载的ServerAgent下,如果服务是windows系统,则直接启动"startAgent.bat"即可,如果是Linux系统,则直接启动"./start ...

  5. iOS 代码混淆的简单使用

    1.工具下载  http://stevenygard.com/projects/class-dump/  选择dmg安装包 2.打开终端输入:open/usr/local/bin 3. 4.修改权限在 ...

  6. POJ 1118

    #include<iostream> #include<set> #include<stdio.h> #include<math.h> #include ...

  7. J07-Java IO流总结七 《 InputStreamReader和OutputStreamWriter 》

    前面在介绍FileReader和FileWriter的时候有说到,FileReader的读取字符功能,以及FileWriter的写出字符的功能,都不是它们自己实现的,而是,它们分别继承了InputSt ...

  8. 快捷键&小技巧

    shift+鼠标滚轮:实现左右移动 alt+鼠标左键双击:打开属性 chrome中在F12下的Element中,可以先选中某一项,可以直接按住F2进行编辑 chrome中element的右下方我们可以 ...

  9. ubuntu生成core转储文件

    1.ulimit -c 判断是否开启转储 为0 则没有开启 2.ulimit -c unlimited 设置转储core大小没有限制 3.设置转储文件位置 echo "/var/core/% ...

  10. (01)JVM-内存三大核心区域以及分析

    package org.burning.sport.jvm; /** *  从JVM调用的角度分析Java程序对内存空间的使用, * 当JVM进程启动的时候,会从类加载器路径中找到包含main方法的入 ...