Chapter 1 file I/O

Source: Internet
Author: User
Overview

This chapter mainly discusses two topics: 1. I/O without buffering; 2. multi-process file sharing.

In a Unix environment, I/O devices are abstracted into files, so these two contents are put together for explanation. Let's learn with questions, and the effect may be better.

1. What is a file descriptor?

From the operating system perspective, we know that I/O devices are shared devices. However, shared devices can cause competition between processes, that is, critical resources. For example, a printer is a shared device. process a cannot print the content that process B wants to print. How can we manage this critical resource? UNIX systems use the kernel for management. Example 1:


Figure 1 kernel and Process

For the kernel, it uses the file descriptor to reference an open file and returns the file descriptor to the corresponding process. Subsequent operations on this file will be performed on this file descriptor. In an image, the file is equivalent to the student himself, and the file descriptor is equivalent to the student ID. Obviously, the student ID is easier to operate and manage than the name.

2. What is buffering?

Because there is no standard I/O (I/O with buffer), here is an overview of the buffer.

When it comes to buffering, it usually refers to application-level buffering, rather than the buffer in the kernel, because either I/O without buffering or standard I/O, i/O buffers are available in the kernel. For example:


Figure 2 Buffering

Three simple I/O instances without buffering

Int readt (unsigned char * & T, const string name) {int FD = open (name. c_str (), o_rdwr); // open the file if (FD =-1) // determine whether the file is successfully opened {fprintf (stderr, "error Open Text "); return-1;} int n = lseek (FD, 0, seek_end); // obtain the object length if (n =-1) // check whether the file is successful {fprintf (stderr, "Error seek text"); Return-1;} t = new unsigned char [N]; // allocate a buffer space for the content of the stored file if (t = NULL) // whether the space allocation is successful {fprintf (stderr, "error molloc space"); Return-1;} lseek (FD, 0, seek_set); // locate the start of the file Position int cur = 0; int num = 0; while (1) {num = read (FD, T + cur, N-cur ); // read the object if (num =-1) {fprintf (stderr, "error read text 43"); Return-1 ;} else if (num = 0) break; cur + = num;} If (cur! = N) {fprintf (stderr, "error in read text, cur is % d, n is % d \ n", cur, n); Return-1 ;} close (FD); // close the opened file return N ;}
This is an application instance I wrote that reads data from the file name. Where
Num = read (FD, T + cur, N-cur); // read the file as much as possible
This line is a very good method.

4. How to Implement file sharing 4.1 kernel open file data structure

In this case, process a is reading file 1 and wants to read file 2. Image one point: if I want to read "apue" and "TCP/IP" for a while, I will keep the two books at hand. I want to see which one will not interfere with it, how can a computer implement it?


Figure 3 kernel data structure of the opened file

4.2 two processes share the same file.

In this case, 2: process a is reading file 1, process B is reading file 1, and the two processes do not interfere with each other. How can this problem be solved? Take an image as an example: Xiao Yu is watching the recruitment poster, and Xiao Wei is also watching the same recruitment poster. Obviously, they do not have to compete for a fight, just look at each other. So how does the operating system kernel handle this situation? It is also similar,


Figure 4 process shared files

After understanding the kernel data structure of file sharing, you can clearly understand how file redirection is implemented. The details of the specific function can be API learning, you will be familiar with it several times.

5. Modify the File status flag

4. We can see that a process accesses a file through a file table. An important item in a file table is the file status identifier, is it possible to set the value only when the file is opened? Obviously not, the fcntl function is used.

Summary

By studying this chapter, you can write correct I/O without buffering. Understand the core mechanism of file sharing. Of course, it also involves API parameters and so on. Many details are not mentioned, but we understand its entire design concept and these details can be well grasped after being practiced.

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.