Information Security system Design Foundation Nineth Week study Summary
"Learning Time: 6 hours"
"Learning content: CHAPTER 10--system Level I/O"
I. Textbook content 1. Each UNIX file is a sequence of M-bytes, and all I/O devices such as networks, disks, and terminals are modeled as files, and input and output are read and write operations on these files. Operation of input and output in 2.unix system:
- Open 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 small non-negative integer called the descriptor. UNIX systems have three open files when creating each process: standard input, standard output, standard error.
- Changes the current file location. For each open file, the kernel maintains a file location K (the byte offset from the start of the file).
- Read and write files. The read operation is to copy n>0 bytes from the file to the memory, starting with the current file location K, and then adding the K to the k+n.
- Close the file. The application notifies the kernel to close the file, and in response, the kernel frees the data structure created when the file is opened and restores the descriptor to the available description descriptors.
The abstraction of I/O into a file is the manipulation of everything inside the system into a file (a sequence of bytes), which is a great way to clean up all kinds of actions. The above description of the file, in fact, is the input and output type of "file Operations", respectively, the corresponding is the I/O, read and write operations. 】
3. Open and Close files
- Open file:
FD = Open ("filename", flag bit--means access and additional hints, mode parameter); Return 1
- mode parameter specifies the access permission bit for the new file. As part of the context, each process has a umask, and 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.
- FD is the file descriptor (number) returned, always returning the smallest descriptor that is not currently open in the process.
- flag parameter can be one or more masks or, for example: O_creat, indicating that if the file does not exist, Just create a truncated file for it.
To close a file:
int close (int fd);//returns 0 if successful, 1 if unsuccessful
"Here's an open file with a detailed description of the return value FD (e.g. what is" the smallest descriptor currently not open in the process "can refer to exercise 10.1)"
4. Read and Write files
The read function copies up to n bytes from the current file location of the descriptor to FD to the memory location BUF.
"What is EOF?" is given a file of M-byte size, and when reading or writing from the position of K-byte, it is found K>=m "
"What's the difference between Ssziet,sizet?" The former is defined as int, signed; the latter is defined as unsigned int, unsigned "
5.RIO Pack
unbuffered input and output functions. Data is transferred directly between the memory and the file (allowing the interrupted bytes to be called and restarting them when necessary).
SsizeT Riowriten (int fd,const void *usrbuf,size_t n);
SsizeT Riowriten (int fd,const void *usrbuf,size_t n);
The Rio__writen function returns 0 when it encounters EOF;
RIO__READN returns an insufficient value (that is, the number of bytes of that portion of less than N) when EOF is encountered.
- The input function with buffering. Allows users to efficiently read lines of text and binary data from a file (previously read one byte at a time, after applying the function, you can read one line of functions at a time).
Principle: The function copies a line of text from the internal buffer, and when the buffer becomes empty, it automatically calls read to refill the buffer.
6. Detailed input function with buffering
- RioREADINITB (RioT *rp,int FD);
This function is called once per open descriptor, which links the descriptor FD to a buffer of type rio_t at the address Rp.
- RioREADNB (Riot *rp,void *usrbuf,size_t N);
Reads a maximum of n bytes from the file RP to the memory location USRBUF. Calls to the same descriptor, RioREADNB and RioReadlineb can be interleaved.
- SsizeT Readlineb (Riot *rp,void *usrbuf,size_t maxlen);
Reads a line of text (including the end of the newline character) from the file RP, copies it to the memory location usrbuf, and ends the line of text with an empty character.
7.RIO Reading Program Core: Rio-read function
static ssize_t rio_read(rio_t *rp,char *usrbuf,size_t n){ int cnt; while(rp->rio_cnt<=0)//如果缓冲区为空,先调用函数填满缓冲区再读数据 { rp->rio_cnt=read(rp->rio_fd,rp->rio_buf,sizeof(rp->rio_buf));//调用read函数填满缓冲区 if(rp->rio_cnt<0)//排除文件读不出数据的情况 { if(error != EINTR) { return -1; } } else if(rp->rio_cnt=0) return 0; else rp->rio_bufptr = rp->rio_buf;//更新现在读到的位置 } cnt=n; if(rp->rio_cnt<n) cnt=rp->rio_cnt;//以上三步,将n与rp->rio_cnt中较小的值赋给cnt memcpy(usrbuf,rp->rio_bufptr,cnt);把读缓冲区的内容拷贝到用户缓冲区 rp->rio_bufptr+=cnt; rp->rio_cnt-=cnt; return cnt;}
8. read file meta data
9. Three data structures to open a file
- Descriptor table. Each process has its own descriptor table, and its tables are indexed by the file descriptor opened by the process.
- V-node table. All processes are shared. Each table entry contains most of the information in the stat structure.
- File table. Represents a collection of open files, all of which are shared by the process. Table entries are: file location, reference count, pointer to the corresponding table entry in the V-node table.
10. How to share files
- There is no shared file, and each descriptor corresponds to a different file.
- Multiple descriptors can also 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)
- Parent-child processes can share files. The child process will have a copy of the parent Process Descriptor table entry, and the parent-child process opens the same set of file tables, sharing the same file location. The parent-child process must close the corresponding descriptor table entry before the kernel deletes the corresponding file table entry.
Ii. exercise Screening 1. What is the output of the following program?
#include "csapp.h"int main(){ int fd1,fd2; fd1=Open("foo.txt",O_RDONLY,0); Close(fd1); fd2=Open("baz.txt",O_RDONLY,0); printf("fd2=%d\n",fd2); exit(0);}
"At the beginning of the UNIX process life cycle, there were three open descriptors: standard input, standard output, and standard error, respectively, 0,1,2. So the value of FD2 should be 3 "
2. What is the function of the following code?
#include "csapp.h"int main(void){ char c; while(Read(STDIN_FILENO,&C,1) !=0) Write(STDOUT_FILENO,&c,1); exit(0);}
"One byte at a time, passed from standard input to standard output"
3. Assume that the disk file Foobar.txt consists of 6 ASCII character "Foobar". So, what is the output of the following programs?
#include "csapp.h"int main(){ int fd1,fd2; char c; fd1=Open("foobar.txt",O_RDONLY,0); fd2=Open("foobar.txt",O_RDONLY,0); Read(fd1,&c,1); Read(fd2,&c,1); printf("c=%c\n",c); exit(0);}
The descriptors fd1 and fd2 each have their own open file table entries, so they have their respective file locations (that is, they do not affect each other and do not cause the FD2 open file position to be pushed backwards because the FD1 executes first). FD2 Open the first letter of the file read out or F. 】
4. As before, the disk file Foobar.txt consists of 6 ASCII character "Foobar". So, what is the output of the following programs?
#include "csapp.h"int main(){ int fd; char c; fd=Open("foobar.txt",O_RDONLY,0); if(Fork()==0) { Read(fd,&c,1); exit(0); } Wait(NULL); Read(fd,&c,1); printf("c=%c\n",c); exit(0);}
The parent-child process shares the same file table entries, so read "F" and "O" in turn. The output is O. 】
Experience
The chapters of this study are less, but the code part worth digging into is also very important. I felt that reading the code was a very interesting thing--it dawned on the line, like steady. The code in this chapter is designed to decompose the "basic operations" (so many sequential steps behind the original simple input and output), which is interesting to read.
In addition, when using the virtual machine, I found that the virtual machine platform prompted "hard disk location changes" caused the Linux system is not available, after Baidu's data, the use of virtual media management to repair the error.
Information Security system Design Foundation Nineth Week study Summary