Nineth Week Tenth chapter study

Source: Internet
Author: User
Tags signal handler types of functions truncated

Learning tasks

(i) The concept of chapter tenth

1. Input \ output (i\o) is the process of copying data between main memory and external devices. (input)i\o→ main memory (output) main memory →i\o.

2. Understanding UNIX I/O will help you understand other system concepts, and sometimes you have no choice but to use UNIX I/O.

10.1

all i/o devices such as networks, Disks and terminals are modeled as files, and all inputs and outputs are executed as read and write to the corresponding file. This is a   way to gracefully map a device to a file, allowing unix un ix i/o

  • The
  • opens the file. An application declares that it wants to access a I/O equipment. The kernel returns a small non-negative integer, called a descriptor, that identifies the file in all subsequent operations on this file  . The kernel logs all information about this open file. The application simply remembers this descriptor. Each process created by the shell starts with three open files: Standard   input (descriptor 0 1 ) and standard error (Descriptor
  • Changes the current file location. For each open file, the kernel maintains a file location K, which is initially 0. This file location is the byte offset starting at the beginning of the file. The application is able to explicitly set the file's current location to Kby performing a seek operation.
  • Read and write files. A read operation is a copy from a filen>0bytes to memory, from the current file locationkStart,then thekincreased tok+N. Given a size ofmbytes of files, whenk>=mwhen you perform a read operation, it triggers―a calledEnd-of-file(EOF), the application can detect this condition. At the end of the file there is no clear"EOF"symbols.
  • Close the file. When the app finishes accessing the file, it notifies the kernel to close the file. In response, the kernel frees the data structure created when the file is opened and restores the descriptor to the available descriptor pool. Whenever a process terminates for any reason, the kernel closes all open files and frees their storage resources.

10.2

The process is to open an existing file or create a new file by calling the Open function:

Int Open (char *filename,int flags,mode_t mode);

The Open function converts the filename to a file descriptor and returns a descriptive number. The return descriptor is always the smallest descriptor that is not currently open in the process.

The Flag parameter indicates how the process intends to access the file

    1. O_rdonly: Read only.
    2. O_wronly: write only.
    3. O_RDWR: Readable and writable .

It can also be one or more bits of the mask or, providing additional instructions for writing

1.o_creat: Creates one of his truncated (empty) files if the file does not exist.

2.o_trunc: If the file already exists, truncate it.

3.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. Symbolic names such as. As part of the context, each process has a umask that is set by calling the umask function. when a process is called by an open function with a mode parameter to create a new file, the access permission bit of the file is set to Mode&umask .

10.3

An application performs input and output by invoking the system function read and write functions separately.

The read function copies up to n characters from the current file position of the descriptor to FD to the storage location BUF. The return value 1 indicates the error one, and 0 means EOF.

size_t is a usigned int, and ssize_t is an int.

In some cases, read and write transmit fewer bytes than the application requires. Possible causes of this situation are:

    • Encountered EOF while reading. Assuming that the file starts at the current file location with only 20 bytes, and the application requires that we read with a 50-byte slice, this read returns a value of 20, and after that, read returns 0.
      Reads a line of text from the terminal. If the open file is associated with a terminal, each read function transmits one line of text at a time, and the value returned is equal to the size of the text line.
      Read and write sockets. If the open file corresponds to a network socket, the internal buffering constraints and the longer network latency cause read and write to return insufficient values.

10.4

1.Rio provides two different types of functions:

unbuffered input and OUTPUT functions
Input function with buffering

The 2.RIO_READN function transmits a maximum of n bytes from the current file position of the descriptor FD to the memory location USRBUF. A similar Rio_writen function transmits n bytes from the position usrbuf to the descriptor FD. The RIO_READN function can only return an insufficient value when it encounters EOF. The Rio_writen function will never return an insufficient value.

Note: If the RIO_READN and Rio_writen functions are interrupted by a return from the application signal handler, then each function will manually restart read or write.

3. A line of text is a sequence of ASCII characters that ends with a newline character. In Unix systems, the newline character is ' \ n ', the same as the ASCII newline character LF, and the value is 0x0a. Suppose we're going to write a program that calculates the number of Chinese lines in a text file.

4. One way is to use the Read function to transfer from file to user memory one byte at a time, checking each byte to find a newline character. The problem with this approach is that efficiency is not high, and each fetch of a byte in a file requires a kernel.
A better approach is to call a 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 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.
    • For applications, Rio_read and system invoke read have the same semantics. Returns 1 On Error, 0 on EOF, or an insufficient value if the requested byte exceeds the number of unread bytes in the read buffer. The RIO_READLINEB function calls the Rio_read function multiple times. Each call returns a byte from the read buffer, and then checks to see if the byte is the end of the line break.
    • The Rio_readlineb function reads a maximum of (maxlen-1) bytes, leaving the remaining byte at the end of the null character. Lines of text that exceed maxlen-1 bytes are truncated and end with a null character.

10.5

The application is able to retrieve information about the file by invoking the stat and FSTAT functions.

STAT function structure

The St_size member contains the size of the number of bytes in the file. The St_mode member encodes the file access License bit and file type. UNIX recognizes a large number of different file types. Ordinary files include some type of binary or textual data. For the kernel, there is no difference between a text file and a binary file.

The catalog file contains information about other files. A socket is a file that is used to communicate with other processes over a network. The UNIX-provided macro directive determines the type of file based on the St_mode member. A subset of these macros is as follows:

10.6

The kernel uses three related data structures to represent open files

Descriptor descriptor
File table
V-node table

10.7

The Unix shell provides an I/O redirection operator that allows users to link disk files to standard input and output.

How I/O redirection works: One is to use the DUP2 function.

The DUP2 function copies the descriptor table entry OLDFD to the Descriptor table entry NEWFD, overwriting the previous contents of the Descriptor table entry NEWFD. If the NEWFD is already open, Dup2 will close NEWFD before copying the OLDFD.

10.8

ANSI c defines a set of advanced input and output functions as standard I/O libraries, providing programmers with a higher-level alternative to UNIX I/O. This library (LIBC) provides functions for opening and closing files (fopen and fclose), functions for reading and writing sections (Fread and fwrite), functions for reading and writing strings (Fgets and fputs), and complex formatted I/O functions (printf and scanf).

The standard I/O library models an open file as a stream. 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:

10.9

Various I/O packages

The standard I/O stream is, in a sense, full-duplex, because the program can perform input and output on the same stream.

It is recommended that you do not use standard I/O functions for input and output on network sockets. Instead, use the robust Rio function.

(b) Exercises


1. Complete after-school exercises (with reference to the answer in the book) Focus: 10.1, 10.2, 10.3, 10.4, 10.5

10.1 Because the front did not learn, do not know what the following is the situation

So is the back.

10.4 redirect the standard input (descriptor 0) to descriptor 5, we will call Dup2 (5,0) or equivalent dup2 (5,stdin_fileno).

2. Important Commands:
Man-k Key1 | grep key2| grep 2: Retrieving system calls based on keywords
GREP-NR xxx/usr/include: Find macro definition, type definition

Nineth Week Tenth chapter study

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.