System level I/O learning record

Source: Internet
Author: User
Tags types of functions

Important points of knowledge

Input/output (I/O)

I/O is the process of copying data between main memory and external devices such as disk drives, terminals, and networks.

    • The input operation is to copy data from the I/O device to main memory.
    • The output operation is copied from main memory to the I/O device.

Unix I/O

All I/O devices in Unix are modeled as files, which allows all input and output to be carried out in a uniform manner.

    • Open File : An application that requires the kernel to open the appropriate file, To announce that it wants to access an I/O device. The kernel returns a small, non-negative integer called the descriptor , which identifies the file in all subsequent operations on the file.

      unix Shell creates three open files at the beginning of each process: standard input (descriptor 0), standard output (descriptor 1), and standard error (descriptor 2)

    • change the current file location : For each open file, the kernel maintains a file location K, which initially is 0. This file location is the byte offset starting at the beginning of the file. The application is able to display the current file location by performing a seek operation.
    • Read- write files : One reading is to copy n>0 bytes from a file to memory, starting with the current file location K, and then adding to K+n. Given a file of size m bytes, the application detects this symbol when the read operation triggers a condition called end-of-file (EOF) when K>=m is executed. Note: There is no explicit "EOF symbol" at the end of the file.
    • Close File : When the app finishes accessing the file, it notifies the kernel to close the file. The kernel frees the data structure created when the file is opened and restores this descriptor to the available description descriptors. Whenever a process terminates for any reason, the kernel closes all open files and frees up their storage resources.

Opening and closing files

The process opens an existing file or creates a new file by calling the Open function.

    • FileName is a file descriptor and returns a descriptor number. The returned descriptor is always the smallest descriptor that is not currently open in the process.
    • The flags parameter indicates how the process intends to access the file

Flags parameter:

    • O_rdonly: Read only.
    • O_wronly: Write only.
    • O_RDWR: Readable and writable.
    • O_creat: If the file does not exist, create a truncated (empty) file for it.
    • O_trunc: truncates a file if it exists.
    • O_append: Sets the file location to the end of the file before each write operation.
    • The mode parameter specifies the access permission bit for the new file.

      Access permission bits:

The process closes an open file by using the close function.

Closing a closed descriptor will cause an error.

Read and write files

The program performs input and output by invoking the read and write functions.

Note: size_t: defined as unsigned int,ssize_t is defined as int.

Rio Package

The Rio package automatically handles insufficient values for read-write file values. It provides two different types of functions:

    • unbuffered input/output functions : These functions directly exist between the memory and the file transfer data, there is no application-level buffering. They are especially useful for reading and writing binary data to and from the network.
    • buffered input functions : These functions allow you to efficiently read lines of text and binary data from a file that is cached in an application-level buffer, similar to a buffer provided for standard I/O functions like printf

Meta data

Metadata is the information for the file. Applications can retrieve metadata by invoking the stat and FSTAT functions.

The kernel represents the data structure of the Open file

    • descriptor Tables: Each process has its own descriptor table, which is indexed by the file descriptor opened by the process. Each Open Descriptor table entry points to a representation in the file tables.
    • File Table : The collection of open files is represented by a file table, and all processes share the table. The table entries for each file table consist of the current file location, the reference count (that is, the number of descriptor items currently pointing to the table), and a pointer to the corresponding table entry in the V-node table. Closing a descriptor reduces the number of reference counts in the corresponding file table entry. The kernel does not delete this file table entry until his reference count is zero.
    • v-node table : As with the file table, all processes share this v-node table. Each table entry contains most of the information in the stat structure, including St_mode and st_size members.

I/O redirection

The Unix shell provides an I/O redirection operator that allows users to link disk files to standard input and output. I/O redirection is typically achieved by calling the DUP2 function.

Standard I/O

ANSI C defines a set of advanced input-output functions, called Standard I/O libraries, that provide programmers with a higher-level alternative to UNIX I/O.

The Library provides:

    • Functions to open and close files.
    • Functions for reading and writing sections.
    • Functions for reading and writing strings.
    • Complex formatted I/O functions.

The standard I/O library models an open I/O library to a stream with an opened file. For programmers, a stream is a pointer to a structure of type file. Each ANSI C program starts with three open streams stdin, stdout, and stderr, which correspond to standard input, standard output, and standard errors, respectively:

the stream of file is an abstraction of the document descriptor and stream buffer.

The purpose of the stream buffer (as in the Rio read buffer): Make the number of high-overhead UNIX I/O system calls as small as possible.

Encounter problems

Problem:

Use the CSAPP.H compilation errors defined on the textbook.

  

Workaround:

From the classmate that acquired the official website of the Csapp.h header file, can be compiled.

Resources

"in-depth understanding of computer Systems" Chapter 10th system Level I/O.

System level I/O learning record

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.