The Tenth Chapter Study notes summary

Source: Internet
Author: User
Tags types of functions

10.1 Unix I/O

A UNIX file is a sequence of M bytes, all the I/O devices are modeled as files, and all inputs and outputs are executed as read and write operations for the corresponding files.

    • Open File

An application that requires the kernel to open the appropriate file to declare that he wants to access an I/O device, the kernel returns a small non-negative integer called a descriptor, he identifies the file in all subsequent operations on the file, and the kernel records all the information about the open file, and the application simply remembers the descriptor.

Each process created by the Unix shell starts with three open files: standard input (descriptor 0), standard output (1), standard error (2).

    • Change the current file location

For each open file, the kernel maintains a file location K, initially 0, when the file location starts at the beginning of the byte offset, the application is able to explicitly set the file's current location to K by performing a seek operation.

    • Read and write files

One read is to copy n>0 bytes from a file to memory, starting with the file location K, and then adding the K to K+n. Given a file of size M bytes, this condition is detected when a read operation is performed when the k≥m is executed. A similar write operation is to save the memory copy n>0 bytes to a file, starting with the current file location K and then updating K.

    • Close File

After the application has completed access to the file, he notifies the kernel to close the file, and in response, the kernel frees the file to open the data structure that was created and restores the descriptor to the available descriptor pool, regardless of the cause for which the kernel shuts down all open files and frees up their storage resources.

10.2 Opening and closing files

The flags parameter indicates how the process accesses the file

    • O_rdonly: Read-only

    • O_wronly: Write only

    • O_RDWR: Readable and writable

The flags parameter can also be one or more bit masks or:

    • O_creat: If the file does not exist, create one of his truncated empty files

    • O_trunc: truncates a file if it already exists

    • O_append: Sets the file location to the end of the file before each write operation

10.3 Reading and writing files
    • Read function

Copies up to n bytes from the current file position of the descriptor to FD to the memory location BUF. A return value of 1 represents an error, and a return value of 0 means EOF. Otherwise, the return value represents the actual number of bytes transferred.

    • Write function

Copies the current file position of up to n bytes to the descriptor FD from the memory location BUF. The return value is either 1 or the number of bytes written.

    • Lseek function

Displays the modification of the current file location.

    • An insufficient value occurred
    • a. Read Span class= "Hljs-constant" >eof. At this time read return eof signal.
    • b. Reads a line of text from the terminal. If the open file is associated with a terminal, then each read function transmits a line of text, returning an insufficient value equal to the size of the text line.
    • c. Read and write network sockets. There may be blocking behavior.
10.4 Robust read and write with Rio packet
    • Two different types of functions are available:
无缓冲的输入输出函数 直接在存储器和文件之间传送数据。带缓冲的输入函数
    • Rio_readn function

1. Transfer up to n bytes from the current file position of the descriptor FD to the memory location USRBUF.

2. Encountering EOF can only return an insufficient value.

    • Rio_writen function

1. Transfer n bytes from the location usrbuf to the descriptor FD.

2. An insufficient value will never be returned.

    • For the same descriptor, RIO_READN and Rio_writen can be called arbitrarily interleaved.

    • How do I implement the number of lines in a text-based writing file?

1.read函数,一次一个字节从文件传送到用户存储器,检查每个字节来查找换行符。(效率低)2.rio_realineb包装函数,从一个内部读缓冲区拷贝一个文本行,当缓冲区变空时,会自动调用read重新填满缓冲区。
    • Rio_realineb

1. Read a line of text from the RP of the file, copy it to the memory location usrbuf, and end the line of text with a null character.

2. Read up to maxlen-1 bytes, leaving the remaining one character to the end of the empty string.

10.5 Read File meta data

Metadata: The application is able to retrieve information about the file, such as creation time, modification time, metadata, by invoking the stat and FSTAT functions.

    1.  #include <unistd.h> #include <sys/stat.h> int stat(const char *filename, struct stat *buf); int fstat(int fd, struct stat *buf);                      返回: 若成功则为0,若出错则为-1.

      The stat function takes a file name as input, and the ftast as input with a filename descriptor.

    1. A function is thread-safe: it can be called alternately on the same descriptor.

10.6 Sharing files
    1. There is no difference between a file file and a binary file for the kernel.

    2. Share files

The kernel uses three related data structures to represent open files.

    • 1)描述符表:它的表项由进程打开的文件描述符来索引的。每个打开的描述符表项指向文件表中的一个选项。2)文件表:打开文件的集合是一张文件表来表示的,所有的进程共享这张表。包括当前的文件位置、引用计数以及一个指向v-node表中对应表项的指针。3)v-node表:同文件表一样,所有的进程共享这张v-node表。每个表项包含stat结构中的大部分信息,包括st_mode和st_size成员。

10.7 I/O redirection

Unix>ls>foo.txt

Enables the shell to load and execute the LS program, directing standard output to a disk file

Problems encountered and solutions:

UNIX provides a small number of system-level functions, which allow the use of programs to open, close, read and write files, extract the metadata of the file, and perform I/O redirection, Unix read and write operations will appear insufficient values, such a situation, the book said the use of the Rio package, the Rio packet through repeated read and write operations, It will automatically handle the insufficient value. But it's not automatically processed on my computer system.

Reference: "In-depth understanding of csapp,h and csapp.c in Computers" http://www.cnblogs.com/LZYY/p/3423594.html

Http://www.cnblogs.com/lhc-java/p/4941254.html

The Tenth Chapter Study notes summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.