Information Security system design basics Eighth Week study summary-Lu Songhon

Source: Internet
Author: User

10th Chapter System Level I/O

I/O: the process of copying data between main memory and external devices.

10.1Unix I/O

A UNIX file is a sequence of M bytes:

B0,b1,b2 ... Bk... B (m-1)

1. All inputs and outputs can be executed in a uniform and consistent manner:

(1) Open file

A Descriptor: The kernel returns a small non-negative integer that identifies the file in all subsequent operations on the file.

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

(2) Change the current file settings.

(3) Read and write files

End-of-file (EOF)

(4) Close the file.

10.2 Opening and closing files

1.int Open (char *filename, int flags, mode_t mode)

The 2.open function converts the filename to a file descriptor. The flags parameter indicates how the process intends to access the file. O_rdonly (Read only), O_wronly (write only), O_RDWR (readable writable), O_creat, O_trunc, O_append. The mode parameter specifies the access permission bit for the new file.

10.3 Reading and writing files

1. Read function: Return value 1 indicates an error; return value 0 means EOF
2. Write function: The number of bytes written successfully returned, error returned-1.
3. Insufficient value: encountered EOF when reading, read text line from terminal, read and write socket.

10.4 Robust read and write with Rio packet

The Rio package will automatically handle the insufficient values.
1.RIO unbuffered Input-output function: Transmits data directly between the memory and the file.
2.RIO Buffered Input function:
3. Call wrapper function (RIO_READLINEB), which copies a line of text from an internal buffer, and automatically calls the read system call to refill the buffer when the buffer becomes empty. In a version with buffers, the RIO_READINITB function is called once per open descriptor, which links the descriptor FD to a read buffer of type rio_t at the address Rp. The RIO_READINITB function reads a line of text (including the trailing newline character) from the file RP, copies it to the memory location usrbuf, and ends the line of text with a null character.

The core of the 4.RIO reading program is the Rio_read function, which can be seen as a buffer version of the UNIX read function. When calling Rio_read requires reading n bytes, there are rp->rio_cnt unread bytes in the read buffer. If the buffer is empty, the read system function is called to fill the buffer. This read call is not an error if it receives an insufficient value, except that the read buffer is partially populated. Once the buffer is non-empty, Rio_read copies N and rp->rio_cnt from the read buffer to the user buffer, and returns the number of copy bytes.

10.5 Read file meta data

INT stat (const char *filename, struct stat *buf)

int fstat (int fd, struct stat *buf)

Stat with file name as input, fstat as input

10.6 Sharing Files

1. Descriptor Table File Table V-node tables

10.7 I/O redirection

1.unix>ls>foo.txt

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

Reference: "In-depth understanding of computer systems"

Information Security system design basics Eighth Week study summary-Lu Songhon

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.