美文网首页
APUE读书笔记

APUE读书笔记

作者: josephok | 来源:发表于2014-10-10 12:25 被阅读321次

最近在看大神W.Richard StevensAdvanced Programming in the UNIX® Environment,简称APUE

以下是我写的读书笔记。

  • File I/O

UNIX下一切皆文件,内核通过文件描述符来访问文件。
每个进程默认打开3个file descriptor: STDIN_FILENO, STDOUT_FILENO, STDERR_DILENO
每个进程都有一个entry in process table entry,每个entry可以看作是一个(python中的)字典,字典的key是文件描述符,value是一个Tuple,Tuple中的元素为(file descriptor flags, A pointer to a file table entry

内核为所有打开的文件维护一个file table, 其中的每个entry包含:
(a) The file status flags for the file, such as read, write, append, sync, and
nonblocking;
(b) The current file offset
(c) A pointer to the v-node table entry for the file

Each open file (or device) has a v-node structure that contains information about the type of file and pointers to functions that operate on the file.

每个打开的文件(或设备)都有一个v-node的结构,其包含了文件的元信息。(Linux has no v-node.

用图表示:

file_table.png
  • 习题 3.6:
If you open a file for read–write with the append flag, can you still read from anywhere in the file using lseek? Can you use lseek to replace existing data in the file? Write a program to verify this.

答:可以。

举个例子:有如下两个程序。
3.6_1.c, 编译为w

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int fd, i;
    char buf[] = "123456789abcdefghijklmnopqrstuvwxyz";
    fd = open("a.txt", O_RDWR| O_CREAT | O_APPEND);
    if (fd == -1) {
        perror("open");
        exit(1);
    }
    for (i=0; i<strlen(buf); i++) {
        write(fd, buf+i, 1);
        sleep(3);
    }
    return 0;
}

3.6_2.c, 编译为r

#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

#define BUFSIZE 32

int main(int argc, char const *argv[])
{
    int fd;
    fd = open("a.txt", O_WRONLY);
    char buf[BUFSIZE] = "0";
    if (fd == -1) {
        perror("open");
        exit(1);
    }

    lseek(fd, 0, SEEK_SET);
    
    write(fd, buf, 1);
    printf("The written char is %c\n", buf[0]);

    read(fd, buf, 1);
    printf("The read char is %c\n", buf[0]);
    return 0;
}

我们先运行w,(等几秒)然后再运行r。这里会生成a.txt文件,我们可以看看文件的内容:

0234567

程序3.6_2.clseek到文件开头处 并且替换了文件开头的1
这说明不同进程间是可以共享文件的,如果有多个进程同时操作文件,可能会造成文件内容的损坏。

相关文章

  • 信号函数编写研究

    APUE读书笔记 ToDoList [ ] sleep初级实现 2017年12月6日 10:41:30 [ ] ...

  • APUE 第一章 UNIX基础知识

    最近重新学习APUE,特开文章做学习笔记apue.h被我封装在all.h中,apue配置可以参见我的另一篇文章[C...

  • APUE读书笔记

    最近在看大神W.Richard Stevens的Advanced Programming in the UNIX®...

  • linux中信号的术语

    generated delivered pending blocking action 参考:APUE

  • UNIX环境高级编程“apue.h”头文件

    UNIX环境高级编程“apue.h”头文件 "apue.h"头文件 异常输出 “error.h” 将这两个文件包含...

  • APUE读书笔记-后序

    这个后序,其实精确地算起来,是从2011年11月份就开始写的了,每发现一些关于它的片断,都记录下来,最后整理而成。...

  • APUE读书笔记-前言

    本文主要介绍在UNIX环境下编程所需要了解的内容,可做为UNIX/Linux环境下编程的学习资料,或者需要时查阅的...

  • 《UNIX 环境高级编程》实验

    http://foolishflyfox.xyz/blog/tags/APUE/ 详细地记录了 《UNIX 环境高...

  • APUE读书笔记-20应用(2)

    2、应用2——与网络打印机通信 这里对应原书中的第21章,讲述开发了一个可以和网络打印机通信的程序,以及期间需要考...

  • APUE读书笔记-20应用(1)

    1、应用1——用于操作数据库的库 这里对应原书中的第20章,讲述了一个用来操作数据库的库的开发,以及期间需要考虑的...

网友评论

      本文标题:APUE读书笔记

      本文链接:https://www.haomeiwen.com/subject/hicptttx.html