First, the tenth chapter Knowledge Point ***unix I/O
Unix I/O: A simple low-level application interface that enables all input and output to be executed in a uniform manner.
- Open the file. An application declares that it wants to access an I/O device by requiring the kernel to open the appropriate file, and the kernel returns a descriptor (a small non-negative integer). Each process starts with three open files: a standard input (descriptor 0), a standard output (descriptor 1), and a standard error (descriptor 2).
- Changes the current file location. Each open file core maintains a file location of K, initially 0.
- Read and write files. The read operation is to copy n>0 bytes from the file to the memory, starting from the current file location k to increase the K to K+n.
- Close the file. Restores the descriptor to the available description pool.
Opening and closing files
--open: The process opens an existing file or creates 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。
The Open function converts the filename to a file descriptor and returns a descriptor number. The returned descriptor is the smallest descriptor that is not currently open in the process.
The flags parameter indicates how the process intends to access the file:
- O_rdonly: Read-only
- O_wronly: Write only
- O_RDWR: Readable and writable
Open a file that already exists in read-only mode:
fd = Open("foo.txt, O_RDONLY, 0");
Open a file that already exists and add some data later:
fd = Open("foo.txt, O_WRONLY|O_APPEND, 0");
--Close: The process closes an open file by calling the close function.
#include<unistd.h>int close(int fd);返回:若成功则为0,若出错则为-1。
Closing a closed descriptor will cause an error.
Read and write files
--Read and write functions: the application performs input and output by invoking the read and write functions, respectively.
#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。
- The read function copies up to n bytes from the current position of the descriptor to FD to the memory location BUF.
- The Write function copies the current file position of up to n bytes to the descriptor FD from the memory location BUF.
Note: "Csapp.h" is a written header file for "in-depth understanding of the computer system," which you need to download and move the header file to/usr/include when you run the code.
--the difference between Ssize_ T and size_t
- Input parameters of the Size_t:read function, defined as unsigned int
- The return value of the Ssize_t:read function, defined as int. You must return-1 because of an error.
Use the Rio package to read and write robustly
--Rio Package: Automatic processing of insufficient values, providing two different types of functions
- unbuffered input-Output function: Transmits data directly between the memory and the file.
- Buffered input function: Efficiently reads text lines and binary data from a file.
--Non-buffered input and output function for Rio
Applications can transfer data directly between memory and files by calling the Rio_ Readn and Rio_ writen functions.
#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。
The Rio_ Readn function transmits a maximum of n bytes from the current file position of the descriptor FD to the memory location USRBUF. The Rio_ writen function transmits n bytes from the memory location usrbuf to the descriptor FD.
--Rio's buffered input function
A line of text is a sequence of ASCII characters that ends with a newline character.
Call the wrapper function Rio_ readlineb, copy a line of text from an internal read buffer, and automatically call read to refill the buffer when the buffer becomes empty.
#include"csapp.h"void rio_ readlineb(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);返回:若成功则为读的字节数,若EOF则为0,若出错则为-1。
- The Rio_ Readlineb 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 an empty (0) character. The Rio_ Readlineb function reads a maximum of maxlen-1 bytes, leaving the remaining character to the trailing null character.
- The Rio_ readnb function reads a maximum of n bytes from the file RP to the memory location USRBUF. For the same descriptor, calls to Rio_ Readlineb and Rio_ READNB can be arbitrarily interleaved.
Read file metadata
-- 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(const char *filename, struct stat *buf);int fstat(int fd, struct stat *buf);返回:若成功则为0,若出错则为-1。
--STAT data structure member:st_ mode, st_ size ...
The St_ size member contains the number of bytes in the file, and the St_ mode member encodes the file access License bit and file type.
Share files
--the kernel represents an open file with three related data structures:
- Descriptor descriptor
- File table
- V-node table
Multiple descriptors can refer to the same file through different file table entries. Each descriptor has its own file location, so reading from different descriptors can fetch data from different locations in the file.
After calling fork, the child process has a copy of the parent process descriptor, sharing the same file location. Before the kernel deletes the corresponding file table entries, the parent-child process must close their descriptors.
I/O redirection
I/O redirection operator: allows users to connect disk files to standard input and output. I/O redirection can work with the DUP2 function.
#include<unistd.h>int dup2(int oldfd, int newfd);返回:若成功则为非负的描述符,若出错则为-1。
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.
Standard I/O
The standard I/O library 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 (scanf and printf).
Error handling
--Error handling wrapper function
Definition: Given a basic system-level function Foo, define a wrapper function foo with the same parameters, but with the initial capitalization. The wrapper function calls the basic function and checks for errors. If the wrapper function finds an error, prints a message and terminates the process. Otherwise it returns to the caller.
--Error handling in UNIX systems
Three different styles:
- UNIX-style error handling: When an error is returned-1, Success returns useful results.
- POSIX-style error handling: Only the return value is used to indicate success (0) or failure (not 0)
- DNS-style error handling: Returns a null pointer on failure, and sets the global variable H_errno
--Error handling wrapper function
UNIX style:
pid_t Wait(int *status){ pid_t pid; if(pid = wait(status)<0) unix_error("wait error"); return pid;}
POSIX style:
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 hostname *p; if((p = gethostbyname(name)) == NULL) dns_error("Gethostbyname error"); return p;}
Ii. Definition of Classroom Summary * * * process
-What is a process: an instance of an executing program in which each program runs in the context of a process.
--the key abstraction that the process provides to the application
- A separate logical control flow
- A private address space
Concurrent
-What is concurrency: The general phenomenon of concurrent execution of multiple streams is called concurrency (P1, P2 alternately running over time)
-Parallel Flow: A true subset of concurrent streams, with two streams running concurrently on different processor cores or computers, running in parallel and executing in parallel.
Process Control
--Get Process ID
#include<sys/types.h> #include<unistd.h> pit_t getpid(void);//返回调用进程的PID pit_t getppid(void);//返回它的父进程的PID
--Create and terminate processes
Process Tri-State:
- Run
- Stop (the execution of the process is suspended and will not be scheduled)
- Terminating (reason: ① receives a default behavior is to terminate the process signal ② return ③ from the main program to call the Exit Function)
The parent process creates a new run subroutine by calling the fork function
#include<sys/types.h> #include<unistd.h> pid_t fork(void); 子进程返回0,父进程返回子进程的PID(大于0),出错返回-1。
Note: When the parent process calls fork, the child process can read and write to any file that is open in the parent process, and the biggest difference between the parent and new child processes is that they have different PID.
Fork function
Features: Called only once, returned two times. Once in the calling parent process, the PID of the child process is returned, and once in the new child process, 0 is returned.
- Concurrent execution: The parent and child processes are independent processes running concurrently, and the kernel can alternately execute instructions in their logical control flow in any way.
- The same but separate address space.
- Share files
Iii. references
"In-depth understanding of computer systems"
"Embedded Linux application Development Standard Tutorial"
Iv. Experience
The task of this week is to review the I/O knowledge learned last week and summarize the process knowledge of the lectures.
I summed up the specific usage of the fork function and understood the process of running.
This knowledge let me keep in-depth understanding of the computer, know it, know why.
5233 Yeung Kwong--Tenth Week study summary