Information Security system Design Fundamentals Nineth Chapter Study Summary

Source: Internet
Author: User

Tenth. System level I/O learning process 1 Unix I/O

Input and output (I/O) is the process of copying data between main memory and external devices. A UNIX file is a sequence of M bytes, all the I/O devices are modeled as files, and all inputs and outputs are executed as read and write to the corresponding file.

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

Each process created by the Unix shell starts with three open files that can be used to

    • 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. Set the file location to K by performing a seek operation.

    • Read and write files. Read operation: From the file copy n>0 bytes to the memory, starting from the current file location K, increased to k+n. A condition called end-of-file (EOF) is triggered when the length of the k>= file is large. Write operations: Copy n>0 bytes from memory to a file, start with the current file location K, and then update K.

    • Close the file. The kernel frees the data structure created when the file is opened.

2 Opening and closing files

Open a file that already exists or create a new file:

int open(char *filename,int flags,mode_t mode) (若成功则返回新文件描述符,若出错为-1)
    • The Open function converts the filename to a file descriptor and returns a descriptor number.
    • The flags parameter indicates how the process accesses the file.
    • The mode parameter specifies the access permission bit for the new file.

To close an open file:

int close(int fd)(若成功则为0,若出错则为-1)

Closing a closed descriptor will cause an error

3 Reading and writing files
    • Read file: The read function copies up to n bytes from the current file position of the descriptor to FD to the memory location BUF. The return value represents the number of bytes actually transferred, and the error returns -1,eof 0.

    • Write file: The Write function copies the current file position of up to n bytes to the descriptor FD from the memory location BUF.

Insufficient value
    • EOF encountered while reading: the number of bytes remaining at the end of the file is not sufficient to read the file's byte slice size.

    • Read line of text from terminal: If the open file is associated with a terminal, each read function will be routed one at a time to ask the bank. The value returned is equal to the size of the text line.

    • Read and write network sockets: If the open file corresponds to a network socket, then the internal buffering constraints and the longer network latency cause the read and write to return insufficient values.

4 Non-buffered input and output functions for robust reading and writing to Rio using the Rio packet

By calling the Rio _ Readn and Rio _ writen functions, the application can transfer the number of bytes directly between the memory and the file
According to

ssize_t rio_readn(int fd,void *usrbuf,size_t n);ssize_t rio_writen(int fd,void *usrbuf,size_t n);(若成功则返回传送成功的字节数,若EOF则为0(只对rio_readn而言),若出错 则为-1)
The buffered input function for Rio

The Rio _ Readlineb and Rio _ READNB functions copy a line of text from an internal read buffer and automatically calls read to refill the buffer when the buffer becomes empty.

void rio_readinitb(rio_t *rp,int fd);(无返回)ssize_t rio_readlineb(rio_t *rp,void *usrbuf,size_t maxlen);ssize_t rio_readnb(rio_t *rp,void *usrbuf,size_n);(若成功则返回传送成功的字节数,若EOF则为0,若出错 则为-1)

Opening each descriptor invokes the Rio _ READINITB function, which links the descriptor FD to a read buffer of type Rio T at the address Rp.

Calls with buffered functions should not be used in cross-Readn with unbuffered Rio _.

5 Read file meta data
  • The application can retrieve information about the file (metadata) by invoking the stat and FSTAT functions. The stat function takes a filename as input, and the Fstat function takes a file descriptor as input.

    #include <unistd.h>#include <sys/stat.h>int stat(const char *filename,struct stat *buf);int fstat(int fd,struct stat *buf);
  • 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.

    • Ordinary files include some type of binary or textual data.

    • The catalog file contains information about other files.

    • A socket is a file that is used to communicate with other processes over a network.

6 Sharing files

The kernel uses three related data structures to represent its open files.

    • Descriptor tables: Table items are indexed by the file descriptor opened by the process, each open descriptor table points to a table entry in the file table, and each process has its own descriptor tables.

    • File Table: The collection of open files is represented by a file table, and all processes share this table. Includes: Current file location, reference count, and a pointer to the corresponding table entry in the V-node table.

    • V-node table: Each table entry contains most of the information in the stat structure, including St_mode and st_size members, all processes shared.

7 I/O redirection

The I/O redirection operator, which allows the user to link disk files to standard input and output.

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

#include <unistd.h>int dup2(int oldfd,int newfd);
8 Standard I/O

The standard I/O library models an open file as a stream, which is a pointer to the structure of the file type.

#include <stdio.h>extern FILE *stdin;  /*标准输入,文件描述符为0*/extern FILE *stdout; /*标准输出,文件描述符为1*/extern FILE *stderr; /*标准错误,文件描述符为2*/

A stream of type file is an abstraction of the files descriptor and stream buffers to make the number of expensive UNIX I/O system calls as small as possible.

Problems encountered

Flags parameter:

What is the difference between "truncation" and "deletion"? What is the point of setting such a flag bit when writing a file?

O_ Cerat: Creates a truncated (empty) file for a file if it does not exist.

O_trunc: If the file already exists, truncate it.

Sometimes we need to intercept some data at the end of the file to shorten the file. The file is emptied to 0, which is a special case. You can do this by using the O_TRUNC flag when you open the file. The file was not deleted.

#include <unistd.h>  int truncate(const char* pathname,off_t length) int ftruncate(int filedes,off_t length); 若成功则返回0,出错则返回-1


参考资料:课本+博客园

Information Security system Design Fundamentals Nineth Chapter Study Summary

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.