Tenth chapter: System-level i/o10.1 Unix I/O
A UNIX file is a sequence of M bytes, all I/O devices are modeled as files, and all inputs and outputs are executed as read and write to the corresponding file.
This simple and elegant way is called unixi/o.
① Open File Descriptor: 0, standard input
1, standard output
2, standard error
② change the current file position to 0, change to K by the seek operation
③ read-write files are copied from the file to the memory called read, from the memory copy to the file is called write; End-of-file (EOF). 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.
④ closed file: The kernel frees the data structure created when the file is opened.
10.2 Opening and closing files
Open: Call the Open function to execute
The Open function converts the filename to a descriptor and returns a descriptor number;
The flags parameter can also be one or more bit masks;
The mode parameter specifies the Access throne for the new file.
Close: Closes an open file with a call to the close function.
For example: Open:int open (char *filename,int flags,mode_t mode) (returns new file descriptor if successful, 1 if error)
OFF:int close (int fd) (0 if successful ,1 if error)
10.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.
10.4 Robust read and write with 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
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 _.
10.5 Read File meta data
The application is able to retrieve information about the file, the metadata of the file, by invoking the stat and FSTAT functions.
#include <unistd.h>
#include <sys/stat.h>
int stat(const char *filename,struct stat *buf);
< Span class= "hljs-function" > int fstat (int fd,struct stat *buf);
< Span class= "hljs-function" > return: 0 if successful,-1 if an error occurs.
< Span class= "Hljs-title" >stat function takes a file name as input and fills in a stat data structure for each member.
< Span class= "hljs-function" >10.6 shared file
To represent an open file with three related data structures:
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: All processes are shared, and the table entries consist of the current file location, reference count, and a pointer to the corresponding table entry in the V-node table. When the reference count is 0 o'clock, the kernel deletes the file table entry.
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.
10.7 redirects
I/O redirection: Use the DUP2 function:
#include <unistd.h>
int dup2(int OIDFD,int newfd);
Returns: a non-negative descriptor if successful, or1 if an error occurs
10.8 Standard I/O
Standard I/O library (LIBC): Advanced input-Output functions
#include <stdio.h>
< Span class= "Hljs-title" > extern FILE *stdin;/* standard input, file descriptor 0*/
< Span class= "Hljs-title" > extern FILE *stdout;/* Standard output, file descriptor 1*/
< Span class= "Hljs-title" > extern file *STDERR;/* standard error, file descriptor 2*/
The standard I/O library models an open file as a stream, and a stream is a struct pointer to the file type.
References from Shang students
Information Security system Design Foundation Nineth Week study Summary