Tenth chapter System level I/O
输入/输出(I/O)
: Refers 主存
to the 外部设备
process of copying data between and (such as disks, terminals, networks).
10.1 Unix I/O
One Unix 文件
is m
the sequence of a byte:
- All
I/O
devices are modeled 文件
.
- All inputs and outputs are treated as the corresponding files
读和写
.
设备
Gracefully mapped 文件
to allow the Unix
kernel to elicit a simple , low-level 应用接口
. CalledUnix I/O
10.2 Opening and closing files
进程
is open
to open an existing file or create a new file by calling a function.
#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>int open(char *filename,int flags,mode_t mode); //返回:若成功则为新文件描述符,若出错为-1
open
The function is filename
converted to one 文件描述符
, and a descriptor number is returned.
The returned 描述符
always is not currently open in the process 最小描述符
.
- The
-
flags
parameter indicates how the process intends to access the file:
- but with one or more
masks
. (Take binary thinking Thinking)
-
o_rdonly
: Read-only
-
o_wronly
: Write only
-
o_creat
: If the file does not exist, create a truncated (truncated)
(empty) file.
-
o_trunc
: truncate it
(length truncated to 0
, property unchanged)
-
O_append
: Before each write operation, set the file location to the end of the file
-
o_rdwr
: Readable writable
example code//read-only mode open a file FD = open ("F Oo.txt ", o_rdonly,0);//Open an existing file and add a data to the back side FD = open (" Foo.txt ", o_wronly|
o_append,0);
mode
parameter specifies 新文件
the access permission bit.
Each process has umask
权限掩码
Or权限屏蔽字
- All permissions that are set are subtracted from this
权限掩码
is the actual permission.
777-022=755
Or is 777&~022
.
- Through
umask()
function settings
mode
Not actual permissions
- The permission bit of the file is set to
mode & ~umask
, or it can be subtracted from.
Example
#define DEF_MODE S_IRUSR|S_IWUSER|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH//所有人都能读和写#define DEF_UMASK S_IWGRP|S_IWOTH //屏蔽了用户组的写和其他人的写umask(DEF_UMASK);fd=oepn("foo.txt",O_CREAT|O_TRUNC|O_WRONLY,DEF_MODE);//创建了一个新文件,文件的拥有者有读写权利,其他人只有读权限。(屏蔽了用户组的写和其他人的写)
close
function to close an open file
#include <unistd.h>int close(int fd); //返回: 若成功则为0,若出错则为-1
Closing a closed descriptor will cause an error.
10.3 Reading and writing files
Invoking read
and write
completing input and output
#include <unistd.h>ssize_t read(int fd,void *buf,size_t n);//read函数从描述符fd的当前文件位置拷贝最多n个字节到存储器buf 返回:若成功则为读的字节数,若EOF则为0,若出错为 -1.ssize_t write(int fd,const void *buf,size_t n)//write函数从存储器位置buf拷贝至多n个字节到描述符fd的当前文件位置 返回:若成功则为写的字节数,若出错则为-1
Shows a program that uses read
and write
invokes one byte at a time from 标准输入
copy to 标准输出
.
By invoking lseek
a function, the application is able to display the location of the current file
What's the difference between ssize_t and size_t?
- SIZE_T: is defined as
unsigned int
- ssize_t: is defined as
int
- In order to make an error, return-1.
- Interestingly, because of this-1, the maximum value of read was reduced by half.
In some cases, read
and the write
transmitted bytes are less than the application requires, there are the following reasons.
The value returned by such a condition is called 不足值
.
General disk files In addition EOF
to, generally do not encounter the problem of insufficient values.
10.4 read in a robust way with the Rio package
RIO
Package: Full-name Robust I/O
package, robust I/O
package. is automatically processed as described above 不足值
.
provides two different types of functions:
unbuffered input and OUTPUT functions
- Data is transferred directly between the memory and the file, with no application-level buffering .
- They are especially useful for binary reading and writing to the network and for reading binary data from the network .
Input function with buffering
- Allows you to efficiently read text lines and binary data from a file.
- The contents of these files are
缓存
within the application-level buffer.
The buffered RIO
input function is 线程安全
the same descriptor can be interleaved call
unbuffered input and output functions for 10.4.1 Rio
- With the ordinary
read
, the write
difference
网络套接字
does not produce 不足值
when read and write
- It is
rio_writen
impossible to return不足值
- Thread-safe.
wirte
read
A manual restart is allowed when the return of the application signal handler is interrupted.
Source
Buffered input function for 10.4.2 Rio
A 文本行
sequence of code characters that ends with a newline character ASCII
.
- In Unix systems, the newline character (
\n
) is equal to the ASCII line break LF
, and the numeric value is0x0a
RIO_READNB and Rio_readlineb introduced
Suppose we're going to write a program to calculate the number of Chinese lines in a text file ?
Scenario 1: The read
function transfers from file to user memory one byte at a time, checking each byte to find a newline character.
- Inefficient, one character per read file is required to fall into the kernel.
A better approach is to call a wrapper function rio_readlineb
.
rio_readn
The version of the band buffer used rio_readnb
.
- For files that contain text lines that also contain binary data (for example, the response that is mentioned in section 11.5.3
HTTP
).
- and
rio_readlineb
the same read buffer in which the raw bytes are transmitted.
RIO_READINITB and Rio_readnb,rio_readlineb instances
A 描述符
function is called once for each opening rio_readinitb
.
- It links the descriptor
fd
to rp
a rio_t
read buffer of the type at the address.
rio_readlineb(&rio,buf,MAXLINE)
Function
rio_readlineb
The function rio
reads a line of text (including the trailing newline character) from (the buffer), copies it to the memory location buf
, and \0
ends the line of text with a character.
rio_readlineb
The most read MAXLINE-1
characters, the rest is truncated, the end is always \0
.
rio_readnb(&rio,buf,n)
rio_readnb
The function rio
reads from n
a maximum of bytes tobuf
For the same descriptor, the rio_readlineb
call to and rio_readnb
can be arbitrarily crossed.
- But the buffered and unbuffered should not cross-reference.
The remainder gives an example of a large number RIO
of functions.
- Figure 10-5 shows a format and the
读缓冲区
code that initializes it rio_readinitb
.
rio_readinitb
The function creates an empty read buffer and associates an open file descriptor with it.
The function shown in Figure 10-6 rio_read
is the core of the Rio read program.
rio_read
is a Unix read
buffered version of the function.
- When the call
rio_read
requires reading n
a byte.
- At this point, if the buffer is empty, the call
read
fills up, but it may not be full.
- Reads the
缓冲区
internal unread min(n,rp->rio_cnt)
bytes.
For an application, rio_read
it Unix read
has the same semantics as the function.
10.5 Read File meta data
Applications can stat
fstate
retrieve information about a file (sometimes referred to as a file) through calls and functions 元数据(metadata)
#include<unistd.h>#include<sys/stat.h>int stat(const char *filename,struct stat *buf);int fstat(int fd,struct stat *buf);//填写stat数据结构中的各个成员 返回 : 成功0 ,出错-1
st_size
The member contains the file 字节数大小
.
st_mode
The member encodes the file 访问许可位
and 文件类型
- File type
- Common Type : what we generally say
文件
- catalog File : Contains information about other files
- sockets : A file that is used to communicate with other processes over the network.
- UNIX provides a
宏指令
basis for st_mode
determining the file type, which is part of the following.
S_ISREG()
#这是一个普通文件吗
S_ISDIR()
#这是一个目录文件吗
S_ISSOCK()
#这是一个网络套接字吗
- In the
sys/stat.h
definition
Figure 10-10 shows how to use 宏
and stat
function to read and interpret
10.6 Sharing files
Unless you know 内核
how to represent an open file, 文件共享
The concept is quite difficult to understand.
内核
There are three related data structures to represent open files:
There are three possible scenarios for opening a file:
The most common types
- is to open two different files, and the file disk location is not the same.
- is not shared .
Sharing Scenario 1
- Multiple
描述符
can also 文件表表项
refer to the same by referencing a different one 文件
.
- Same content,
文件位置
different (point to disk location is the same block)
- Example
- This can happen if you
filename
call two times with the same one open
.
- Each
描述符
has its own file location, so different 描述符
read operations can fetch data from different locations in the file.
Child Parent Process sharing situation
We can also understand how a parent-child process can share files.
10.7 I/O redirection
Unix
The shell provides I/O
redirection capabilities that allow users to link disk files to standard input and output.
I/O
How does redirection work?
Left and right.hoinkies
10.8 Standard I/O
ANSI C
Defines a set of advanced input and output functions called 标准I/O
libraries.
10.9 Synthesis: What I/O functions should I use
The figure summarizes the various packages we have discussed I/O
.
Unix I/O
。
RIO I/O
标准I/O
磁盘
and terminal equipment selection.
- In
网络输入输出
use, there are some problems.
Unix
The abstraction of a network is a 套接字
type of file called.
- As with any
Unix
file, it 套接字
is also used 文件描述符
to refer to, called 套接字描述符
.
- The application process
套接字描述符
communicates with a process running on another computer by reading and writing.
- Most c programmers, in their careers, use only
标准I/O
标准I/O流
In a sense 全双工
, because the program can execute input and output on the same one 流
.
However, the 流
limitations and limitations of the pair are 套接字
sometimes conflicting, and very few documents describe these phenomena:
(not understand)
- Limit one: The input function followed by the output function.
- If there are no intervening pairs,
fflush
fseek
fsetpos
or rewind
calls, an input function cannot be followed by the output function.
fflush
The function empties the buffer associated with the stream.
- The following three functions use
Unix I/O
the lseek
function to reset the current file location.
- Limit two: The output function followed by the input function.
- If there is no intervening pair
fseek
, fsetpos
or rewind
call, an output function cannot follow an input function unless the input function encounters an ' EOF '.
Do not understand, after reading the look again.
Therefore, we recommend that you 网络套接字
do not use it for 标准I/O
input and output. and to useRIO
10.10 Summary
[Csapp notes] [Tenth. System-Level I/O]