26. in-depth understanding of computer system notes, system level I/O

Source: Internet
Author: User

1. OneUNIXFile isMByte sequence (B 0B1B2... BM-1 ). AllIoDevices, such as networks, disks, and terminals, are modeled as files, and all input and output are read and written to the corresponding files.

2. All inputs and outputs are processed in a unified manner:

1) open the file. An application Program requires the kernel to open the corresponding file, Io device. The kernel returns a small non-negative integer called a descriptor, which identifies the file in all subsequent operations on the file. The kernel records all information about the opened file, and the application only needs to remember this descriptor.

each process created in Unix has three open files at the beginning: standard input (descriptor: 0 ), standard output (descriptor 1 ), standard Error (descriptor: 2 ). Header file unistd. h

2) change the current location. The kernel maintains a file locationKFor each open file0. The file location is the byte offset starting from the beginning of the file.

3) The data structure created when the kernel releases the file and restores the descriptor to the available descriptor pool.

3. Related functions

Open, close, read, write. For specific definitions, seeManManual, or related webpage, or book11.3.

1) In some cases, the original Read, writeThe number of bytes transmitted is less than the number of bytes required by the application (Short count) Is not necessarily an error (meaning, for exampleReadFunction parameters to read5Bytes. The result is read-only.3Bytes, that is, less5 ), The following situations may result in insufficient values:

( 1 ) EOF .

(2) Read text lines from the terminal.

(3) Read and write network socket.

to avoid the problem of insufficient values (robust program requirements, such as Web network applications such as servers. Author 11.4 Rio the package meets this situation.

4. functions are thread-safe: they can be called alternately on the same descriptor.

5. Metadata: file information, such as the creation time, modification time,Metadata. YesStat,FstatFunction.

6. There is no difference between file files and binary files in the kernel.

7. share files

The kernel uses three data structures to indicate open files.

1) Descriptor Table: its table items are indexed by the file descriptor opened by the process.

2) file table: the set of opened files is represented by a file table, which is shared by all processes.

3)V-nodeTable: like the file table, all processes share thisV-nodeTable. Each table item containsStatStructure, such as file size.

Multiple descriptors can also reference the same file through different file table items.

DisplayForkWhen a child process is created: the child process has a copy of the parent process descriptor. The Parent and Child processes share the same set of open file tables, so they share the same file location. Note: Before the kernel deletes the corresponding file table items, the Parent and Child processes must disable their descriptors.


UNIX> ls> foo.txt

ActuallyDup2Function (One case).Dup2The function migrates the object Descriptor Table.

9. StandardIo

Standard functions suchFopen, fclose, fread, fwrite, fgets, fputs, scanf, printf.

Standard Io the library modeled an opened file as a stream. For programmers, A stream is a point type file a pointer to the structure. Each ansi c the program starts with three open streams stdin, stdout, stderr corresponds to standard input, output, and error, respectively.

Type:FileThe stream is the abstraction of the file descriptor and the stream buffer.

10. Example Diagram

11. Standard Io stream, in a sense, is full because the program can be on the same stream. Execute input and output. But there are some limitations:

1) Limitation1:After the input function is followed by the output function. If noFflush, fseek, fsetposOrRewindAn input function cannot be followed by an output function.FflushClears the buffer related to the stream. The last three functions are used.UNIX Io lseekFunction to reset the location of the current file.

2) Limitation2:The output function is followed by the input function. The same is true.

These limitations pose a problem for network applications: LseekIt is invalid. UseRioPackage can solve this problem. UseSprintfFormat the output into a string by usingRio_writenOutput, FuRio_readlinebTo read a row, useScanf To extract different fields from the text.

Related Article

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.