Article directory
1. Problems raised
Ordinary files, directories, character devices, block devices, network devices, etc. are all treated as files in Linux. Although their types are different, a unified operation interface is provided in Linux.
Ordinary files and directory files are obviously very easy to understand, so in this article, I will use the most simple language to help you understand how toperipheralsTreat it as a document.
2. How to understand
There will be various files opened in a process, should the operating system manage them in a unified way? Obviously yes. So how to manage it? This has to mention the design philosophy of Linux——先描述,在组织
- First describe: From the perspective of philosophy, people understand the world through attributes. For example, when I mention "singing, dancing, rap, basketball" , everyone must know who I am talking about. Therefore, as long as the properties of the object are described, we can realize the description of an object, and then realize the management. Linux is written in C language. In C language, we can use structures to describe all properties of objects.
- In organization: Suppose we have used
file
a structure to describe a file, then using a certain data structure, we can organize all files. The Linux system uses the structure of the linked list to organize the relationship of each file
Let's think about such a question again: Can C language be used to simulate a class that implements C++? The answer is yes. You may question that member functions cannot exist in the C language structure, but don't forget that the structure can contain function pointers, so that the effect of member functions can be simulated.
Based on this understanding, it is not difficult for us to imagine that the "member function" for reading and writing files must be included in the struct file, and its pseudocode form is as follows (fp is like the this pointer in the class): Although the reading and writing methods of different
devices They must be different (reading and writing are not necessarily available, if not, an empty function is provided), but they must provide their own reading and writing methods to the file structure. Therefore, the file structure can 统一
look at all devices from a perspective, and we can manage all files in a unified manner by using the file structure
Summary:
There are obviously differences in the bottom layers of different peripherals, but we introduce a software layer (virtual file system) to shield the bottom layer differences and provide the same operation interface to achieve unified management.
3. Linux source code verification
struct file
The structure in the Linux kernel is as follows:
struct file {
union {
struct list_head fu_list;
struct rcu_head fu_rcuhead;
} f_u;
struct path f_path;
#define f_dentry f_path.dentry
#define f_vfsmnt f_path.mnt
const struct file_operations *f_op;
spinlock_t f_lock; /* f_ep_links, f_flags, no IRQ */
atomic_long_t f_count;
unsigned int f_flags;
fmode_t f_mode;
loff_t f_pos;
struct fown_struct f_owner;
const struct cred *f_cred;
struct file_ra_state f_ra;
// 略……
};
We observed that there are const struct file_operations *f_op
member variables, which represent various operation interfaces, including the read-write interface we mentioned earlier (partial content is as follows):
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
// 略 ……
};