3.文件io

3.1 文件内核数据结构

3.2 复制文件描述符的内核数据结构

3.3 对指定的描述符打印文件标志

#include "apue.h"
#include <fcntl.h> int
main(int argc, char *argv[])
{
int val; if (argc != 2)
err_quit("usage: a.out <descriptor#>"); if ((val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0)
err_sys("fcntl error for fd %d", atoi(argv[1])); switch (val & O_ACCMODE) {
case O_RDONLY:
printf("read only");
break; case O_WRONLY:
printf("write only");
break; case O_RDWR:
printf("read write");
break; default:
err_dump("unknown access mode");
} if (val & O_APPEND)
printf(", append");
if (val & O_NONBLOCK)
printf(", nonblocking");
if (val & O_SYNC)
printf(", synchronous writes"); #if !defined(_POSIX_C_SOURCE) && defined(O_FSYNC) && (O_FSYNC != O_SYNC)
if (val & O_FSYNC)
printf(", synchronous writes");
#endif putchar('\n');
exit(0);
}

结果

3.4 开启文件状态标志

#include "apue.h"
#include <fcntl.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");
}

如果将中间的一条语句修改如下(关闭文件标志):

val &= ~flags;  //turn flags off

3.5 将标志输入复制到标准输出

#include "apue.h"

#define	BUFFSIZE	4096

int
main(void)
{
int n;
char buf[BUFFSIZE]; //set_fl(STDOUT_FILENO,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);
}

程序运行时,设置O_SYNC会增加系统时间和时钟时间。

4.文件和目录

4.1 access函数的使用

#include "apue.h"
#include <fcntl.h> int
main(int argc, char *argv[])
{
if (argc != 2)
err_quit("usage: a.out <pathname>");
if (access(argv[1], R_OK) < 0)
err_ret("access error for %s", argv[1]);
else
printf("read access OK\n");
if (open(argv[1], O_RDONLY) < 0)
err_ret("open error for %s", argv[1]);
else
printf("open for reading OK\n");
exit(0);
}

8.进程控制

8.1 fork函数实例

#include "apue.h"

int		globvar = 6;		/* external variable in initialized data */
char buf[] = "a write to stdout\n"; int
main(void)
{
int var; /* automatic variable on the stack */
pid_t pid; var = 88;
if (write(STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf)-1)
err_sys("write error");
printf("before fork\n"); /* we don't flush stdout */ if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) { /* child */
globvar++; /* modify variables */
var++;
} else {
sleep(2); /* parent */
} printf("pid = %ld, glob = %d, var = %d\n", (long)getpid(), globvar,
var);
exit(0);
}

运行结果

分析

  • 为啥是sizeof(buf)-1:

  • 为什么出现有两次before fork出现:

8.2 fork之后父子进程对文件的共享

8.3 vfork函数实例

#include "apue.h"

int		globvar = 6;		/* external variable in initialized data */

int
main(void)
{
int var; /* automatic variable on the stack */
pid_t pid; var = 88;
printf("before vfork\n"); /* we don't flush stdio */
if ((pid = vfork()) < 0) {
err_sys("vfork error");
} else if (pid == 0) { /* child */
globvar++; /* modify parent's variables */
var++;
_exit(0); /* child terminates */
} /* parent continues here */
printf("pid = %ld, glob = %d, var = %d\n", (long)getpid(), globvar,
var);
exit(0);
}

运行结果

linux中文件内核数据结构的更多相关文章

  1. linux中文件IO

    一. linux常用文件IO接口 1.1. 文件描述符 1.1.1. 文件描述符的本质是一个数字,这个数字本质上是进程表中文件描述符表的一个表项,进程通过文件描述符作为index去索引查表得到文件表指 ...

  2. [转]Linux中文件权限目录权限的意义及权限对文件目录的意义

    转自:http://www.jb51.net/article/77458.htm linux中目录与文件权限的意义 一.文件权限的意义 r:可以读这个文件的具体内容: w:可以编辑这个文件的内容,包括 ...

  3. LSOF查看linux中文件打开情况

    如何查看linux中文件打开情况 前言 我们都知道,在linux下,“一切皆文件”,因此有时候查看文件的打开情况,就显得格外重要,而这里有一个命令能够在这件事上很好的帮助我们-它就是lsof. lin ...

  4. linux中文件压缩介绍

    原文内容来自于LZ(楼主)的印象笔记,如出现排版异常或图片丢失等问题,可查看当前链接:https://app.yinxiang.com/shard/s17/nl/19391737/1c62bb7f-f ...

  5. linux中文件颜色,蓝色,白色等各自代表的含义

    linux中文件颜色,蓝色,白色等各自代表的含义 绿色文件---------- 可执行文件,可执行的程序 红色文件-----------压缩文件或者包文件 蓝色文件----------目录 白色文件- ...

  6. [转载] linux中文件描述符fd和文件指针flip的理解

    转载自http://www.cnblogs.com/Jezze/archive/2011/12/23/2299861.html 简单归纳:fd只是一个整数,在open时产生.起到一个索引的作用,进程通 ...

  7. linux中文件的三种time(atime,mtime,ctime)

    linux下文件有3个时间的,分别是atime,mtime,ctime.有些博友对这3个时间还是比较迷茫和困惑的,我整理了下,写下来希望对博友们有所帮助. 1 这三个time的含义 简名 全名 中文名 ...

  8. linux中文件的时间戳

    touch命令 touch用来创建新文件,或者修改时间戳. linux中的文件有三个时间点,利用stat命令查看: 1.atime:最后访问时间:文件最后一次被存取或执行的时间. 2.mtime:最后 ...

  9. Linux中文件压缩与解压

    压缩与解压 compress 文件名 1 -v //详细信息 2 3 -d //等于 uncompress 默认只识别 .Z 如果使用别的后缀,会导致不识别,解压缩失败.也可以使用 -d -c 压缩包 ...

随机推荐

  1. 淘宝的sign参数js逆向

    前言:现在网站都有很强的反爬机制,都是非常常见的是用js前端加密参数,所以不得不去分析和逆向js混淆后的代码 一. 打开天猫或淘宝,shift+ctrl+F12全局搜索sign参数. 这里发现很多地方 ...

  2. interpration

    On interpreting the effects of repetition interpreting 释意1. If you interpret something in a particul ...

  3. kali2020安装中文界面

    1.安装中文字体:apt-get install xfonts-intl-chinese ttf-wqy-microhei 2.设置系统语言:dpkg-reconfigure locales 3.选择 ...

  4. 13、java——常用类

    ​  枚举类型   描述一种事物的所有情况|所有可能|所有实例 (1)通过enum关键字定义枚举类型 (2)枚举的成员,字段都作为当前枚举类型的实例存在,默认被public static final修 ...

  5. HDFS学习总结之API交互

    第一种.shell交互 官方文档:http://archive.cloudera.com/cdh5/cdh/5/hadoop-2.6.0-cdh5.7.0/hadoop-project-dist/ha ...

  6. static能修饰什么

    简洁易懂讲清原理,讲不清你来打我~ 修饰普通变量,修改变量的存储区域和生命周期,使变量存储在静态区,在main函数运行前就分配空间,有初始值就初始值,没有初始值就系统默认值初始化 修饰普通函数,修改函 ...

  7. linux安装tomcat后启动报错Cannot find ./catalina.sh的解决方法

    linux安装tomcat后启动报错: Cannot find ./catalina.shThe file is absent or does not have execute permissionT ...

  8. 基于Gin+Gorm框架搭建MVC模式的Go语言后端系统

    文/朱季谦 环境准备:安装Gin与Gorm 本文搭建准备环境:Gin+Gorm+MySql. Gin是Go语言的一套WEB框架,在学习一种陌生语言的陌生框架,最好的方式,就是用我们熟悉的思维去学.作为 ...

  9. 第十四篇 -- CPU学习二——此部分重点在AMD CPU

    一.CPU的一些英文简写或解释 Definitions: ACPI--Advanced Configuration and Power Interface APP--Adjusted Peak Per ...

  10. Python -- 长字符串

    如果需要写一个非常非常长的字符串,它需要跨多行,那么,可以使用三个引号代替普通引号. print '''This is a very long string. It continues here. A ...