20145216 Shi Yao "Information Security system Design Fundamentals" Nineth Week Study Summary

Source: Internet
Author: User
Tags function prototype

20145216 Shi Yao "Information Security system Design Fundamentals" Nineth Week Study summary Teaching Contents summary Tenth chapter system level I/O

Input/output (I/O) is the process of copying data between main memory and external devices.

The first section of Unix I/O

This section covers one of the basic abstractions of the operating system-the file. In other words, all I/O devices are modeled as files, and all input and output are treated as read/write to the corresponding file. The relevant execution actions are as follows:

1. Open the file:

application requests to Kernel → require kernel to open corresponding file → kernel return file descriptor

File descriptor: A small non-negative integer used to identify the file in all subsequent operations on the file. There are three already specified as follows:

    • Standard input--0 (Stdin_fileno)
    • Standard output--1 (Stdout_fileno)
    • Standard error--2 (Stderr_fileno)

Constant representation in parentheses, with header file required for use <unistd.h>

That is, at the beginning of the Unix life cycle, 0, 1, 2 are occupied, after the open can only start from 3-Exercise 10.1

在UNIX下还有stdin,stdout,stderr表示同样的含义。二者的主要区别为:1.数据类型不同,前者为int类型,后者为FILE*;2.STDIN_FILENO主要用在read(),write()等中,后者主要用在fread(),fwrite()以f开头。

2. Change the current file location

Typically, read, write operations start at the current file offset (that is, the file location), and the offset increases the number of bytes read and write, which can be understood as the cursor location.

When you open a file, the file is initially offset to 0.

By the seek operation, the current location of the settings file that can be displayed is K.

3. Read and Write files

(1) Read

The read operation is to copy n>0 bytes from the file to the memory and change the current location of the file. (If the current position is K, change to K+n)

Sources of ※eof:

There is a long-standing misunderstanding: There is no explicit EOF signal at the end of the file, which is a condition called end-of-file when the value of the file's current position exceeds the file size and can be detected by the application, which is known as the EOF signal.

(2) Write

The write operation is to copy n>0 bytes from the memory to a file and then update the current file location.

4. Close the file

App notification kernel close file → kernel release file data structure → restore descriptor → release memory resource.

Section two opening and closing files

1.open function

(1) Function definition:

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

(2) Parameter analysis:

    • return value: Type int, returns a descriptor number, always the smallest descriptor that is not currently open in the process. If an error occurs, the return value is-1
    • FileName: File name
    • Flags: Indicates how the process intends to access this file, the values that can be taken are shown below:

      • O_rdonly: Read-only
      • O_wronly: Write only
      • O_RDWR: Readable and writable
      • O_creat: File does not exist, create new file
      • O_trunc: If the file exists, truncate it
      • O_append: Set file position to end before write operation

      (These values can be used or joined together)

    • Mode: Specifies the access permission bit for the new file

2.close function

(1) Function definition:

#include <unistd.h>int close(int fd);

(2) Parameter analysis:

    • Return value: Successfully returned 0, error returned-1 (closing a closed descriptor will cause an error)
    • FD: The descriptor of the file
Section three read and write files

1. Reading read

(1) Function prototype:

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

(2) Parameter analysis:

    • Return value: Success returns the number of bytes read, EOF returns 0, and an error returns-1. The return value is a signed number
    • FD: File descriptor
    • BUF: Memory Location
    • N: Copy up to n bytes from the current file location to the memory location BUF

2. Writing Write

(1) Function prototype:

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

(2) Parameter analysis:

    • Return value: Successful returns the number of bytes written, and an error returns-1. The return value is a signed number
    • FD: File descriptor
    • BUF: Memory Location
    • N: Copy up to n bytes from memory location BUF to the current file location

(Note that read and write normally return a value that is the actual number of bytes transferred)

3. You can explicitly modify the location of the current file by using the Lseek function

4. Insufficient value

The insufficient value refers to the fact that in some cases, read and write transmit fewer bytes than the application requires, for the following reasons:

    • I met EOF when I read it.
    • Read the line from the terminal
    • Read and write sockets
The four is a robust read and write of the Rio package

Rio,robust I/O, problems with insufficient values are present.

1.RIO unbuffered input and output function.

The function of these functions is to transfer data directly between the memory and the file, which is often applied between the network and the binary data.

the Rio Readn function and the Riowriten definition:

#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);

Parameter resolution:

    • FD: File descriptor
    • USRBUF: Memory Location
    • N: Number of bytes transferred
    • return value:
      • RIO_READN Success Returns the number of bytes transferred, EOF is 0 (an insufficient value), error is-1
      • Rio_writen Success Returns the number of bytes transferred, error 1, no insufficient value

2.RIO Buffered Input function

Disadvantage: The efficiency is not very high, each read the file of a byte is required to fall into the kernel

The fifth section reads the file meta data

Metadata is file information, and the functions that need to be used are stat and fstat. Defined as follows:

#include <unistd.h>#include <sys/stat.h>int stat(const char *filename, struct stat *buf);int fstat(int fd,struct stat *buf);

Parameter resolution:

    • Return value: Success is 0, error is-1
    • Input: Stat needs to enter a file name, and Fstat needs to enter a filename descriptor.

There are two, Stmode and Stsize to note.

    • St_size: The size of the number of bytes in the containing file
    • St_mode: Package encoded file access License bit and file type. The license bit mentioned in the first section, the Unix file type is as follows, and has the corresponding macro directive, meaning "is xx", these macros are defined in Sys/stat.h:

      • Plain file binary or text file (not bad for kernel) S_isreg ()
      • Directory files information about other files S_isdir ()
      • A file that the socket communicates with other processes over the network S_issock ()
section Sixth sharing files

The kernel represents an open file with three related data structures:

    • Descriptor descriptor
    • File Table: The collection of open files is represented by a file table.
    • V-node table

In Exercise 10.2, because FD1 and fd2 have separate file descriptors, they each have their own descriptor table, File table, and V-code tables, so their reads are independent and the final value is F.

In exercise 10.3, fork is a subroutine, and the parent program shares the same descriptor table, File table, and V-code tables, pointing to the same file, so after the subroutine executes, the parent program is performed on its basis, and the read takes down a character, which is O.

Section seventh I/O redirection

I/O redirection operator: >

ls > foo.txt

The meaning of this code is to make the shell load and execute the LS program, and redirect the standard output to the disk file foo.txt.

I/O redirection function: dup2

The function is defined as:

#include <unistd.h>int dup2(int oldfd, int newfd);

Return value: Successful return descriptor, error return-1

This function does this by copying the Descriptor table entry OLDFD, overriding the Description table entry NEWFD, and closing it before copying if the latter is open.

In Exercise 10.5, the FD1 and fd2 descriptors are 3 and 4, respectively, and so are two different descriptors, pointing to two different files, but because after reading the FD2 one byte, Fd1 is redirected to Fd2, so at this point the read FD1 is equivalent to reading FD2, that is, the result is O.

Eighth section standard I/O

1. Standard I/O library:

ANSI c defines a set of advanced input and output functions, called Standard I/O libraries, that contain:

    • fopen, Fclose, opening and closing files
    • Fread, fwrite, read and write sections
    • Fgets, fputs, read and write strings
    • scanf, printf, complex formatted I/O functions

2. Stream-A stream of type file is an abstraction of the document descriptor and stream buffer

The standard I/O library models an open file as a stream.

Each ANSI C program starts with three open streams: stdin, stdout, stderr, corresponding to standard input, standard output, and standard error (see section I), as defined below:

#include <stdio.h>extern FILE *stdin;extern FILE *stdout;extern FILE *stderr;
Appendix A error handling

1. Error handling Style

(1) Unix style

Returns 1 after encountering an error, and sets the global variable errno to the error code indicating the cause of the error;

If it completes successfully, it returns useful results.

(2) POSIX style

A return of 0 indicates success, and a return of non 0 indicates failure;

The useful results are in the function arguments that pass in.

(3) DNS style

There are two functions, gethostbyname and GETHOSTBYADDR, which return a null pointer on failure, and set the global variable H_errno.

2. Error handling wrapper function

    • UNIX style

Returns void when successful, returns an error when the wrapper function prints a message, and then exits.

void Kill(pid_t pid, int signum) {  int rc;  if ((rc = kill(pid, signum)) < 0)  unix_error("Kill error");}
    • POSIX style

Returns void on success and does not contain useful results in the error return code.

void Pthread_detach(pthread_t tid) {  int rc;  if ((rc = pthread_detach(tid)) != 0)  posix_error(rc, "Pthread_detach error");}
    • DNS Style

      struct hostent *Gethostbyname(const char *name) {  struct hostent *p;  if ((p = gethostbyname(name)) == NULL)  dns_error("Gethostbyname error");  return p;}
Analysis of textbook exercises

10.1: At the beginning of the Unix life cycle, 0, 1, 2 are occupied, the subsequent open can only start from 3, so the call to open returns descriptor 3.

10.2: Since FD1 and FD2 have separate file descriptors, they each have their own descriptor table, File table, and V-code tables, so their reads are independent and the final value is F.

10.3:fork is a subroutine, and the parent program shares the same descriptor table, file tables, V-code tables, pointing to the same file, so after the subroutine executes, the parent program is performed on its basis, and the read takes down a character, which is O.

10.5: In the initial case FD1 and FD2 descriptors are 3 and 4, so is two different descriptor, pointing to two different files, but because after reading the FD2 a byte, fd1 redirected to Fd2, so at this time read FD1 is equivalent to reading FD2, that is, the result is O.

Problems in code debugging and the resolution process

Problem: Unable to compile code on textbook, display error, unresolved

Code Hosting

Links: Https://git.oschina.net/sjy519/linux-program-C/tree/master

Other (sentiment, thinking, etc., optional)

This week to learn the system-level I/O knowledge points, although the contents of the textbook is not many, but it is not easy to learn, I feel that the text on the book is more abstract, so it is difficult to understand. In the process of code practice, there is a problem, show no csapp.h this file or directory, their own internet Baidu still did not solve the problem, see the students to publish related group topics, but also did not get the solution to the problem, I hope to be able to solve this problem, and then the code in the textbook verification again.

Learning progress Bar /Cumulative) new/cumulative)
lines of code (newBlog volume (Learning time (new/cumulative) Important growth
Goal 3000 rows 30 Articles 300 hours
First week 0/0 1/2 25/40 Learn Linux basics and Core commands
Second week 0/0 0/2 0/40
Third week 300/300 3/5 40/80

Learn the vim, GCC, gdb instructions;

Learn the information representation and processing

Week Five 200/500 1/6 45/125

Learn the machine-level representation of a program

Week Six 150/650 1/7 40/165

Learned the processor architecture

Seventh Week 100/750 1/8 40/205

Learning the Memory hierarchy

Eighth Week 46/796 2/10 40/245

reviewed the previous knowledge points

Nineth Week 124/920 1/11 40/285

Learn about system-level I/O content

20145216 Shi Yao "Information Security system Design Fundamentals" Nineth 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.