20145239 "Information Security system Design Fundamentals" 9th Week Study Summary

Source: Internet
Author: User

20145239 "Fundamentals of Information Security system Design" 9th Week study summary Textbook Learning Content Summary Unix I/O
    • / o

      1, the input is from the I/O device copy data to storage, the output is from main memory copy data to I/O device.

      2. All I/O devices are modeled as files, and all input and output are treated as read/write to the corresponding file.
    • Open File

      1. Procedure: The application requests the kernel to open the corresponding file, the kernel returns the file descriptor (a small non-negative integer).

      2, the kernel records all the information about this file, the application only need to remember this descriptor.

      3. The UNIX shell creates three open files at the beginning of each process:
      • Standard input (descriptor 0)
      • Standard output (Descriptor 1)
      • Standard error (Descriptor 2)
      Define constants Stdin_fileno, Stdout_fileno, Stderr_fileno, respectively, for header file <unistd.h>
    • Change the current file location

      1, for each open file, the kernel maintains a file location K, initially 0. This position is the byte offset starting at the beginning of the file.

      2. The application can manipulate the explicit settings of the file by seek to the current location of K.
    • Read and write files

      1, read operation: From the file copy n>0 bytes to memory, starting from the current file location K, and then add K to K+n.

      2, write operation: Copy n>0 bytes from memory to a file, start with the current file location K, and then update K.

      3, given a file size m bytes, K >= m when performing a read operation will trigger a condition called end-of-file (EOF), the application can detect this condition, but there is no explicit "EOF symbol" at the end of the file.
    • Close File

      1, Process: The application notifies the kernel to close the file, the kernel frees the data structure when the file is opened, restores the descriptor, releases the memory resource.

Opening and closing files
  • Open File

    1. Open () function: If successful, the return value is a new file descriptor (the returned descriptor is always the smallest descriptor that is not currently open in the process); If there is an error, the return value is-1

    #include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int open(char *filename,int fliags,mod_it mode);

    Parameters:

      • FileName: file name
      • Flags: Indicates how the process intends to access this file

          means additional tips for access: o_rdonly: Read only. o_wronly: Write only. O_RDWR: Readable and writable. One or more bitmask or: O_creat, which indicates that a truncated file is created if the file does not exist. o_trunc: If the file already exists, truncate it. O_append: Sets the file location to the end of the file before each write operation.     
      • Mode: Specifies the access permission bit for the new file

        Each process has a umask, Set by calling the Umask function. When the process uses the open function with a mode parameter to create a new file, the access permission bit of the file is set to Mode & ~umask.
  • Close file:

    1, close () function: returns 0 if successful, or 1 if unsuccessful.

     #include< Span class= "hljs-meta-string" ><unistd.h>int close (int FD); //FD: The descriptor of the file.           

    2 Closing an already closed descriptor program will cause an error.

    Read and write files
  • Read function

     # Include<unistd.h>ssize_t Read ( int fd,void *buf,size_t N); //ssziet is defined as int, signed; Sizet is defined as unsigned int, unsigned          

    1, return value: If successful, returns the number of bytes that were actually transferred, i.e., if EOF (given the M-byte size of the file, at the beginning of the K-byte position to read or write, the discovery of K >=M), returns 0, or 1 if an error occurs.

    2, meaning: The Read function copies a maximum of n bytes from the current file position of FD to the memory location BUF.
  • Write function

    #include<unistd.h>ssize_t write(int fd,const void *buf,size_t n);

    1, return value: If successful, returns the number of bytes written, or 1 if an error occurs.

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

  • By calling the Lseek function, the application is able to display the location of the current file.
  • Insufficient value

    1. In some cases, read and write transmit fewer bytes than the application requires (these do not indicate errors):

    • Encountered EOF while reading.
    • Reads a line of text from the terminal.
    • Read and write network sockets.

    2. In fact, in addition to EOF, you will not encounter an insufficient value when reading and writing disk files. If you want to create a robust network application such as a Web server, you must pass the read and write processing values repeatedly until all the required bytes are delivered.

  • Use the Rio package to read and write robustly
  • The Rio package will automatically handle the insufficient values.
  • Rio provides two different types of functions:
    • unbuffered input and output functions. These functions transmit data directly between the memory and the file, without application-level buffering, which is especially useful for reading and writing binary data to and from the network. The
    • buffered input function. These functions allow you to efficiently read lines of text and binary data from a file (functions copy a line of text from the internal buffer, and when the buffer becomes empty, it automatically calls read to refill the buffer), and the contents of those files are cached in the application-level buffer, similar to standard i/like printf The buffer provided by the O function. The buffered Rio input function is thread-safe and can be called interleaved on the same descriptor.
  • Non-buffered input and output functions for Rio

    #include "csapp.h"ssize_t rio_readn(int fd,void *usrbuf,size_t n);ssize_t rio_writen(int fd,void *usrbuf,size_t n);

    1. The RIO_READN function transmits a maximum of N and bytes from the current file position of the descriptor FD to the memory location USRBUF, which can only return an insufficient value when EOF is encountered. Return value: The number of bytes transferred if successful, EOF is 0 (one of the insufficient values), and error is-1.

    2. The Rio_writen function transmits n bytes from the position usrbuf to the descriptor FD. Return value: The number of bytes transferred if successful, an error of-1, and never returning an insufficient value.

    3, for the same descriptor, you can arbitrarily interleaved call RIO_READN and Rio_writen. By invoking the RIO_READN and Rio_writen functions, the application can transfer data directly between the memory and the file.
  • The buffered input function for Rio

    To achieve the calculation of the number of Chinese lines of text files, for example:

    #include "csapp.h"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_t n);

    1. The RIO_READINITB function links the descriptor FD to a read buffer of type rio_t at the address Rp.

    2. The Rio_readlineb function reads a line of text from a file RP, including a newline character, copies it to the memory location usrbuf, and ends the line of text with a null character. Reads up to maxlen-1 bytes and the last null character to the end.

    3. The RIO_READNB function reads a maximum of n characters from the file RP to the memory location USRBUF.

    4, return Value: Success returns the number of bytes transferred, EOF is 0, error is-1.

    5. A line of text is a sequence of ASCII characters ending with a newline character. In Unix systems, the newline character (' \ n ') is the same as the ASCII line break (LF) and the numeric value is 0x0a.
  • The core of the Rio reading program is the Rio_read function

    Read file metadata
  • Meta data

    1. The application can retrieve information about the file (metadata) by invoking the stat and FSTAT functions.

    #include <unistd.h>#include <sys/stat.h>int stat(cost char *filename,struc sta *buf);//stat函数以文件名作为输入int fstat(int fd,struct stat *buf);//fstat函数以文件描述符作为输入

    2. STAT Data structure

      • St_size member contains the size of the number of bytes in the file
      • St_mode members encode file access License bits and file types

    2. UNIX-supplied macro directives determine the type of file based on the St_mode member

    宏指令:S_ISREG()   普通文件    二进制或文本数据宏指令:S_ISDIR()   目录文件    包含其他文件的信息宏指令:S_ISSOCK()  网络套接字   通过网络和其他进程通信的文件
    Problems in code debugging and the resolution process
  • After adding the csapp.h and csapp.c files, the following problems are still being compiled:

After careful investigation, it was found that there was a problem in modifying the Csapp.h file:

It can be seen that the color of #endif has changed, it may be annotated. After modification:

After the modified code can run, the first run is because there is no two TXT file, so the return value is negative, add foo.txt and baz.txt two files after the return value is normal.

This week's code hosting

Code Hosting Links:

Https://git.oschina.net/929210354/Linux

Learning progress Bar

/Cumulative) new/cumulative)
lines of code (newBlog volume (Learning time (new/cumulative) Important growth
Goal 5000 rows 30 Articles 400 hours
First week 200/200 2/2 20/20
Second week 300/500 2/4 18/38
Third week 500/1000 3/7 22/60
Week Four 300/1300 2/9 30/90
Week Five 500/1000 3/12 22/120
Week Six 100/1300 2/15 30/150
Seventh Week 500/1000 3/18 22/180
Eighth Week 100/1500 2/20 30/210
Nineth Week 500/1600 2/22 32/242
 

20145239 "Information Security system Design Fundamentals" 9th Week 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.