Unix System Programming Summary (1) ------ file I/O

Source: Internet
Author: User

After reading the unix/linux Programming Practice tutorial, I realized a few small projects and felt quite uncomfortable. I didn't talk about the details and principles of many system calls in the book. I found a lot of difficulties when trying to read the Linux Kernel Analysis book. So I picked up apue and continued to lay the foundation. Reading and reading, addicted to apue. This is an out-of-the-box question. I. My Understanding of file I/O the two most basic functions of file I/O are read and write, which are also called unbuffered I/O in the book. When I first saw this "unbuffered", I was wondering, isn't the operating system caching all input and output (delayed write)? Why is there unbuffered? But then I understand that the ubuffered here refers to the fact that there is no cache mechanism for read and write itself, such as read (fd, temp, 100 ), it is very simple to return after reading 100 bytes or encountering an EOF file. Fread and fwrite in the C library function call read and write by using the cache technology, which can be called buffered I/O. II. A file descriptor is an integer and can be understood as a pointer to a file (Note: an integer can also be understood as a pointer, not only void * can be called a pointer ). Note that a process closes all opened file descriptors at the end, except 0, 1, and 2. 0, 1, 2, indicating the standard input stream, the standard output stream, and the standard error stream respectively. These three file descriptors are opened and managed by the kernel, and do not need to be opened or closed. 3. The file sharing mechanism is more important to understand the file table. File table includes file status flags, the current file offset, a pointer to the v-node table entry for the file. for example, after calling open once for the temp file, fd1 points to temp and a file table is created. After calling open again for temp, fd2 points to temp and generates a file table (dup and dup2 can be used for implementation, but they are different, see below ). In this case, we exclude other factors. Our system has a temp file, two file descriptors: fd1, fd2, and two file tables (representing the same file ). Note that if an exclusive lock is applied to the write operation, O_WRONLY cannot be used to open two file descriptors at the same time. However, you can use O_RDONLY to open two file descriptors at the same time. fd1 and fd2 can be read at the same time. Correspondingly, the current file offset of the file table pointed by fd1 can naturally be different from that pointed by fd2, this makes it easy to understand the role of multiple file tables. Iv. Principle and analysis of system call I like to use functions to summarize what I have learned. 1. lseek (fd, 5, SEEK_SET). modify current offset. A new offset is returned. 2. open (file, O_RDWR). Do not go into details. For details, see man. First, write a question: O_SYNC. I cannot use the code to feel this option. If you can help me, I am very grateful. It is worth noting that the O_APPEND is very powerful. It is the same as the "add to end" of> redirection. Note that when open (file, O_RDWR | O_APPEND), write and read get different benefits. Each opened file has a current offset, which can be modified using lseek. First paste the code: 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/163ZQ137-0.gif "Alt =" Copy code "style =" border: none! Important; box-sizing: border-box; "/>

Int main () {int fd; char buf [100] = "abcde"; // Note: there is only one aaaaabbbbbccccc row in temp. if (fd = open ("temp", O_RDWR | O_APPEND) <0) err_sys ("error open! "); If (lseek (fd, 5, SEEK_SET) =-1) err_sys (" error lseek! "); If (read (fd, buf, 5) <0) err_sys (" error read! "); Buf [5] = '\ 0'; printf (" % s \ n ", buf); // if (write (fd, buf, 5 )! = 5) // err_sys ("write error"); return 0 ;}
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/163ZQ137-0.gif "alt =" Copy code "style =" border: none! Important; box-sizing: border-box; "/> when you use lseek to modify the current offset to 5, read it again. The result is as follows :?
bbbbb
This indicates that the read can be free from the influence of O_APPEND and can read content anywhere in the file. After modifying the above code like this: 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/163ZQ137-0.gif "alt =" Copy code "style =" border: none! Important; box-sizing: border-box; "/>
Int main () {int fd; char buf [100] = "abcde"; // Note: there is only one aaaaabbbbbccccc row in temp. if (fd = open ("temp", O_RDWR | O_APPEND) <0) err_sys ("error open! "); // If (lseek (fd, 5, SEEK_SET) =-1) // err_sys (" error lseek! "); // If (read (fd, buf, 5) <0) // err_sys (" error read! "); // Buf [5] = '\ 0'; // printf (" % s \ n ", buf); if (write (fd, buf, 5 )! = 5) err_sys ("write error"); return 0 ;}
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/163ZQ137-0.gif "alt =" Copy code "style =" border: none! Important; box-sizing: border-box; "/> the result of the temp file is as follows :?
aaaaabbbbbcccccabcde
Ah, is lseek blocked by O_APPEND? Haha, It's not blocked. The principle is as follows. Before each write operation, the system sets current offset to the end of the file because of O_APPEND. This shows the above result. 3. fcntl (fd, F_GETFL, 0). Modify file attributes. 4. dup (fd1). dup2 (fd1, fd2). Both return the copied file descriptor. Different from the two open file descriptors, The fd2 obtained by dup2 (fd1, fd2) shares a file table with fd1, and the former has a file table. Naturally, when read and write are used to process fd1 and fd2, both of them work. Dup and dup2 are all atomic operations, while
close(fd2);fcntl(fd1,F_DUPFD,fd2);
Dup2 can be implemented, but it will destroy its atomicity. 5. Summarize the basics and principles, which all need to be mastered. I was a little radical before and tried to read the Linux kernel as soon as possible. Now it seems that there is still a long way to go. 6. some of my notes (some from apue) 1. off_t is a long int 2. read, write the two system calls will change the current offset 3. read, write starts at the file's current offset. 4. size_t is defined to facilitate migration between systems. It is an unsigned int. ssize_t is signed size_t 5.the lseek fnction modifies only the current file offset in the file table entry. no I/O takes place. 6.Any operation that requires more that one function call cannot be atomic, as there is always the pos Sibility that the kernel can temporarily suspend the process between the two function CILS. 7. creat (filename, FILE_MODE ). if the filename file already exists, all content of filename will be deleted 8. delayed write: When we write data to a file, the data is normally copied by the kernel into one of its buffers and queued for writing to disk at some later time. 9. cat does not directly accept standard input. For example, the results of more file1 | cat file2 will only output file2, and more file1 | cat-file2 will output file1 and file2. Note:-it can be replaced by/dev/fd/0, indicating the link standard input. 10. when the read and write systems are called, the user does not cache the input and output, but the kernel caches all the input and output. It can be said that the user time of this process is not cached, but the system time cache 11. O_SYNC will always work. fsync and fdatasync are one-time operations. 12. after each open system call, a new file table entry is generated. 13. note the '\ 0' issue of functions such as write, read, and printf. 14.2> & 1 redirects 2 to 1, that is, to redirect stderr to stdout, that is, to point stderr to stdout. 2> & 1: redirects file descriptor 2 to the file pointed to by file descriptor 1 so that the two point to the same file. Reference: apue (Advanced Programming in unix environment). If you think my article is helpful to you, please like it. Thank you very much!

This article is from the Neil blog, please be sure to keep this source http://neilhappy.blog.51cto.com/5504414/1078667

Related Article

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.