20145317 "Information Security system Design Fundamentals" 9th Week Study Summary 1

Source: Internet
Author: User
Tags types of functions

20145317 "Information Security system Design Fundamentals" 9th Week Study Summary 1

Summary of learning contents of textbook 10.1 Unix I/O
    1. A UNIX file is a sequence of M bytes (b0b1b2 ... BM-1). All IO 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. 2. All inputs and outputs are treated as a unified approach:

      1) Open the file. An application declares that it wants to access an IO 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. The kernel records all the information about this open file, and the application simply remembers this descriptor.

      Each process created by UNIX starts with three open files: the standard input (descriptor 0), the standard output (descriptor 1), and the standard error (descriptor 2). The relevant constants are defined in the header file unistd.h.

      2) Change the current position. The kernel maintains a file location of K, starting with 0 for each open file. This file location is the byte offset starting at the beginning of the file.

      3) Read and write files. The read operation is to copy n>0 bytes from a file to memory, starting with the current file location K, and then adding K to K+n. The write operation is to copy n>0 bytes from the memory to the file, starting with the current file location K, and then updating the K.

      4) Close the file. The kernel frees the data structure created when the file is opened and restores the descriptor to the available descriptor pool.

10.2 Opening and closing files
    1. The process is to open an existing file or create a new file by calling the Open function

       #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(char *filename, int flags, mode_t mode);               返回:若成功则为新文件描述符,若出错则为-1。
    2. The flag parameter indicates how the process intends to access the file:

       O_RDONLY:只读 O_WRONLY:只写 O_RDWR:可读可写。

    3. The mode parameter indicates the access permission bit for the new file. Set by calling the Umask function.

    4. The close function closes the file.

      #include <unistd.h>int close(int fd);                返回:若成功则0,若出错则为-1。

      Closing a closed descriptor will cause an error.

10.3 Reading and writing files

#include <unistd.h>

ssize_t read(int fd, void *buf, size_t n);

                     返回:若成功则为读的字节数,若EOF则为0,若出错则为-1。

ssize_t write(int fd, const void *buf, size_t n);

                     返回:若成功则为写的字节数,若出错则为-1。

In some cases, the original Read,write transmits fewer bytes than the application requires, and these values (short count) are not necessarily errors (for example, the read function parameter reads 5 bytes, the result is read only 3 bytes, which is less than 5), and the following conditions may occur with insufficient values:

1)读时遇到EOF。2)从终端读文本行。3)读和写网络套接字。

To avoid the problem of insufficient values (the requirements of a robust program, such as a network application such as a Web server), you need to call read and write to process the insufficient values repeatedly.

10.4 Stout to read and write with Rio packet

The Rio package will automatically handle the insufficient values. Two different types of functions are available:

    • unbuffered input and OUTPUT functions

    • Input function with buffering

1. unbuffered input and OUTPUT functions

By invoking the RioReadn and Riowriten functions, the application can transfer data directly between the memory and the file.

 #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);                          返回:若成功则为传送的字节数,若EOF则为0(只对rio_readn而言),若出错则为-1。

2. Input function with buffering

1) Text line: A sequence of acsii code characters that ends with a newline character. The line break (\ n) acsii code newline (LF) numeric value in Unix is 0x0a.

2) Packing function (RIO_READLINEB):

Copies a file line from the internal read buffer, and automatically calls read to fill the buffer when the buffer is empty.

3) RIO_READNB:

The raw bytes are transferred from the same read buffer as the Rio_readlineb.

 #include “csapp.h” void rio_readinitb(rio_t *rp, int fd)                          返回:无 ssize_t rio_readlineb (rio_t *rp,void *usrbuf, size_t n); ssize_t rio_readn (rio_t *rp, void *usrbuf, size_t n);                          返回:若成功则为读的字节数,若EOF则为0,若出错则为-1。

10.5 Read File meta data
    1. Metadata: The application is able to retrieve information about the file, such as creation time, modification time, metadata, by invoking the stat and FSTAT functions.

       #include <unistd.h> #include <sys/stat.h> int stat(const char *filename, struct stat *buf); int fstat(int fd, struct stat *buf);                      返回: 若成功则为0,若出错则为-1.

      The stat function takes a file name as input, and the ftast as input with a filename descriptor.

Learning progress Bar
  Code lines (new/cumulative) Blog volume (new/cumulative) Learning Time (new/cumulative) important growth
Goal 5000 rows 30 Articles 400 hours
Seventh Week 1300/1750 11/11 140/140
Eighth Week 1700/2000 13/13 160/160
Nineth Week 2000/2400 14/15 180/180
Tenth Week 2500/2800 15/17 0/200
Resources
    • "In-depth understanding of computer system V2" Learning Guide

20145317 "Information Security system Design Fundamentals" 9th Week study Summary 1

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.