Linux Progamming

TLPI - The Linux Programming Int

2019-01-30  本文已影响0人  帆子_8c3a

Book
Code

Chapter 4. File I/O: The Universal I/O Model

Changing the File Offset: lseek()

off_t lseek(int fd, off_t offset, int whence);

Chapter 5. File I/O: Further Details

Relationship Between File Descriptors and Open Files

file offset记录在open file table中


image.png

Duplicating File Descriptors

Chapter 13. File I/O Buffering

Kernel Buffering of File I/O: The Buffer Cache

Controlling Kernel Buffering of File I/O

int fsync(int fd); //针对某个文件
void sync(void);//针对整个系统

打开文件用O_SYNC参数,每次write后自动flush

fd = open(pathname, O_WRONLY | O_SYNC);

Summary of I/O Buffering

fflush()保证调用了write()系统调用,fsync(),sync()保证写到了磁盘上。


image.png

Bypassing the Buffer Cache: Direct I/O

O_DIRECT参数打开文件,不走kernel cache,影响性能。

Chapter 21. Signals: Signal Handlers

Interruption and Restarting of System Calls

  1. We establish a handler for some signal.
  2. We make a blocking system call, for example, a read() from a terminal device, which blocks until input is supplied.
  3. While the system call is blocked, the signal for which we established a handler is delivered, and its signal handler is invoked.
    默认情况下,read()会返回EINTR。在设置flag为SA_RESTART时,system call会自动重启。

Chapter 23. Timers and Sleeping

Setting Timeouts on Blocking Operations

参见书中例子

Chapter 24. Process Creation

Overview

image.png

File Sharing Between Parent and Child

When a fork() is performed, the child receives duplicates of all of the parent’s file descriptors. These duplicates are made in the manner of dup().
下面的例子证明了,父子进程共享文件,子进程修改描述符的offset,父进程也同样结果。


image.png

Memory Semantics of fork()

为了避免拷贝内存:

  1. 代码段设置为只读,父子进程的代码段指向同一段物理内存。
  2. 对于 data, heap, and stack,Linux内核采用copy-on-write.

The vfork() System Call

和fork的区别:

  1. 父子进程共享内存
  2. 父进程不需要调用wait(),就可以等待子进程退出。

Chapter 27. Program Execution

Executing a New Program: execve()

exec家族函数是调用execve()实现的
/proc/PID/exe是对运行文件的符号链接。

Interpreter Scripts

#! interpreter-path [optional-arg]

File Descriptors and exec()

$ ls /tmp > dir.txt

以上的shell脚本做了几件事

  1. A fork() is performed to create a child process that is also running a copy of the shell
  2. The child shell opens dir.txt for output using file descriptor 1
    1. The child shell closes descriptor 1 (STDOUT_FILENO) and then opens the file dir.txt.
    2. The shell opens dir.txt, obtaining a new file descriptor.
  3. The child shell execs the ls program.

The close-on-exec flag (FD_CLOEXEC)

在执行exec函数前,关掉FD。此功能可以用fcntl()查看,并且修改。

Chapter 55. File Locking

Overview

File Locking with flock()

image.png

Semantics of Lock Inheritance and Release

释放锁可以通过:

  1. LOCK_UN操作
  2. 所有关联的FD都关闭
    锁的信息存在Open file table中,而不是存在FD中。flock操作的是内核中的file object,而不是FD。下图通过dup句柄,然后对新句柄操作,就可以释放锁(因为这两个FD都指向同一个file object)。
    image.png
    下图说明两个不同open()会产生两个不同file。第二次lock会被锁住。
    image.png
    fork()后得到的子进程的FD,其实dup父进程得到的,所以子进程可以解锁lock.
    image.png

flock()的限制

  1. Only whole files can be locked.
  2. We can place only advisory locks with flock().
  3. Many NFS implementations don’t recognize locks granted by flock().

Record Locking with fcntl()

image.png
image.png

lock split


image.png

Lock Limits and Performance

image.png

Semantics of Lock Inheritance and Release

下图的例子,说明close(fd2)就可以关闭lock,即使lock发生在fd1上。


image.png

Mandatory Locking

使用Mandatory Locking的先决条件

# mount -o mand /dev/sda10 /testfs

The /proc/locks File

image.png

其中第6列的含义是major dev:minor dev:inode number


image.png

确定ID 3:7 是 /dev/sda7


image.png
image.png
箭头代表被block的lock

Running Just One Instance of a Program

The /var/run directory is the usual location for such lock files. For example, syslogd creates the file /var/run/syslogd.pid.

Chapter 63. Alternative I/O Models

上一篇下一篇

猜你喜欢

热点阅读