Information Security system design basics Nineth Week study summary-20135227 Huang

Source: Internet
Author: User
Tags erro signal handler

Tenth Chapter System-Level I/O

Input/output (I/O) is the process of copying data between main memory and external devices (I/O devices), such as disk drives, terminals, and networks. The input is copied from the I/O device to main memory. Conversely, it is reversed.

Reasons to learn Unix I/o:

Help to understand other system concepts.

Sometimes only Unix I/O is used. For example: Read file metadata (file size and creation time). In addition, it is risky to use a standard I/O library for network programming.

10.1Unix I/O

A UNIX file is a sequence of M bytes: b0,b1,b2....bm-1. All I/O devices are virtualized as files. All inputs and outputs are read and written as corresponding files. Mapping the device to a file, the Unix kernel leads to an application interface, UNIX I/O.

How the input and output is executed:

Open File:

The kernel returns a descriptor when the file is opened. The standard input (Stdin_fileno) descriptor is 0, the standard output (Stdout_fileno) descriptor is 1, and the standard error (Stderr_fileno) descriptor is 2.

Change the current file location:

File location k, which is the byte offset from the start of the file.

Read and Write files:

Reads are copied from the file to the memory. Write the opposite. The End-of-file (EOF) condition is triggered when K exceeds the file byte number M.

To close a file:

Releases the data structure that was created when the file was opened (the memory resource that freed the file), and restores the descriptor to the available descriptor pool.

10.2 opening and closing files

The return value of the Open function is a descriptor.

FileName file path string.

The flag parameter indicates how the process intends to access the file. The value of the flag parameter:

        

        

The mode parameter specifies the access permission bit for the new file. The value of the mode parameter:

      

10.3 read and write files

Read performing input (reading)

Write execution output (write)

Read function: Copies up to n bytes from the current file position of FD to the memory location BUF.

The return value 1 indicates an error, 0 is EOF, otherwise it represents the actual number of bytes transferred.

Write similarly.

Insufficient value: Read and write transmit fewer bytes than the application requires.

I met EOF when I read it.

Read line of text from terminal: If the open file is associated with a terminal, each read function will pass one line of text at a time, Insufficient value = text line size.

Read and write network sockets

10.4 with RIO package robust to read and write

The Rio package can handle the insufficient values automatically. Provides: unbuffered input and output functions with buffered input functions.

10.4.1RIO unbuffered input and output functions

The RIO_READN function copies up to n bytes from the FD to Usrbuf. The same is true of Rio_writen. RIO_READN only returns an insufficient value when EOF is encountered, Rio_writen will never return an insufficient value. For the same descriptor, unbuffered two functions can be cross-invoked

When errno==eintr, the expression function is interrupted by the return of an application signal handler, the read byte is 0, and the read function is reused.

10.4.2RIO the buffered input function

A line of text is a sequence of ASCII characters that ends with a line break. A newline character numeric value is a 0x0a.rio_readlineb function that copies a line of text from the internal read buffer and automatically calls read to refill the buffer when the buffer is empty. RIO_READN version with buffer: RIO_READNB.

The RIO_READINITB function links A read buffer of type rio_t at the FD and Rp.

Rio_readlineb reads a line of text from the RP (including newline characters) and saves it to Usrbuf, ending the line with a null character. Read up to maxlen-1 bytes, leaving a null character at the end.

RIO_READNB reads n bytes up to a maximum.

You can cross-call between the same descriptor and the buffered function.

For example, the Rio function copies a text file from the standard input one line at a time to the standard output.

Rio_t reads the buffer structure body. And the code that initializes it.

The rp->rio_cnt is a buffer of unread bytes. Error returns -1,eof return 0, normally read until >0. A 0 indicates that the buffer is empty, and when it is empty, it is called read to fill it. When the buffer is not empty, copies the minimum values in N and rp->rio_cnt to Usrbuf, and returns the minimum value.

The Rio_readlineb function is similar to Rio_read, except that each call returns a byte from the read buffer and checks whether the byte is a newline character.

Similar to RIO_READN.

10.5 Read File metadata

How to retrieve file metadata: Call the stat and FSTAT functions. Both functions are similar.

A member of the stat data structure. Focus on mastering St_mode,st_size.

St_size: Size of File bytes

St_mode: File access License bit and file type. (normal file: binary and text files.) Catalog Files: Additional file information. Sockets: Files that communicate with other processes over the network. )

    

Determine which of the three types of files it is. (Note that the socket is denoted by "other") only determines whether the owner of the file is readable.

10.6 Share Files

The open file is represented by three related data structures:

Descriptor tables: Each Open Descriptor table entry points to a list item in the File table.

File Table: The collection of open files is represented by a file table, and all processes share this table. Includes the file location, the reference count (the number of descriptor table entries that currently point to the item), and a pointer to the V-node table.

V-node table: Contains most of the information in the stat structure.

Open a file's kernel data structure

File sharing. Share the same disk file

The child parent process shares the file. The child process has a copy of the parent process description table, so they share a collection of open files. Note that the child parent process must close their descriptors before the kernel deletes the corresponding file table entries.

10.7 I/O redirect

Copy the OLDFD to NEWFD, overwriting the contents of the NEWFD. If the NEWFD is already open, dup2 will close it before overwriting.

Use the DUP2 function to redirect Descriptor 1 to descriptor 4.

10.8 Standard I/O

Standard I/O Library: A set of advanced input and output functions. Models an open file as a stream, and a stream is a pointer to the structure of the file type. Each ANSI C program starts with three open streams: stdin (Standard input), stdout (standard output), stderr (standard error).

A stream of type file is an abstraction of the document descriptor and stream buffer. To reduce system overhead.

10.9 synthesis: What should be used I/O function

Most of the time, you can use standard I/O.

Use the Rio function when using a network socket. You need to format the output, use the sprintf function to format a string, and then use Rio_writen to send it to the socket interface. Format the input, use Rio_readlineb to read a complete line of text, and then use scanf to extract the different fields from the line of text.

Summary:

The content of this chapter is relatively small, but it takes less time to spend, and it takes more time to read the code. Obviously, it can be seen from the reading that this chapter is more related to other chapters and has many foreshadowing. This chapter is primarily to help us understand the system-level input and output, Unix I/O, and tells us that although we have some advanced standard library functions to use, in some cases, such as viewing metadata, such as network sockets, you must use system-level I/O. The focus is on how to use the Rio package to solve the problem of returning to the low value continuously.

Questions:

In the book p603 on the Rio_read function, Erro!=eintr next to the comment is written by the application signal processing program return interrupt, but p601 rio_readn function, erro==eintr is interrupted, I think, meet this condition return- 1, the explanation here is error, should be the book p603 comment wrong.

reference: "in-depth understanding of computer systems"

Information Security system design basics Nineth Week study summary-20135227 Huang

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.