Process-related file systems

Source: Internet
Author: User
Tags temporary file storage

The process accesses files through file descriptors (Files descriptor,fd), and each process can use up to Nr_open file descriptors at the same time, which is defined as 1024 in include/linux/limits.h. Each process uses an open file table Files_struct to describe the file descriptor usage of the process. Each file has a file pointer.

There are file system-related data members in the task_struct of the process:

struct task_struct {... /* * *       struct fs_struct *FS;  /*struct files_struct *files, ...};       

The structure fs_struct gives information about the file system associated with the process, such as the current working directory of the process itself, its root directory, etc., which is defined as:

---------------------------------------------------------------------
include/linux/fs_struct.hstruct fs_struct {    int users;    /*/ *intstruct path root, pwd;};         
---------------------------------------------------------------------

The root and PWD two members of the path structure describe the information of the two directories most commonly used by the process, that is, the roots and current directories, and the path structure is defined as follows:

---------------------------------------------------------------------
include/linux/path.hstruct path {    struct vfsmount *mnt;    struct dentry *dentry;};      
---------------------------------------------------------------------

MNT: Describes the file system objects that the directory is installed on

Dentry: directory entry describing the directory

There is also a table that represents the files field that the process opens, that is, the files_struct type of the TASK_STRUCT structure. It gives the usage of all process descriptors, and the file structure pointer array member gives the information of the descriptor, which is defined as follows:

---------------------------------------------------------------------
include/linux/Fdtable.hstructfiles_struct {/** Read mostly part*/atomic_t count;/*Number of processes sharing the table*//*File Descriptor Descriptor*/struct Fdtable *FdtstructFdtable Fdtab;/** Written part to a separate cache line in SMP*//*Read/write spin lock for fields in table*/ spinlock_t File_lock ____CACHELINE_ALIGNED_IN_SMP; int next_fd; /* Maximum file description assigned multibyte 1 *//* The collection of file descriptors that need to be closed when exec () is executed */ Span style= "COLOR: #0000ff" >struct Embedded_fd_set close_on_exec_init; /* Initial collection of file descriptors */ struct Embedded_fd_set open_fds_init; /* Initialize an array of pointer to a file object */ struct file * Fd_array[nr_open_default];};     
---------------------------------------------------------------------

In the unlocked model of the new administrative file descriptor, the lock mechanism is based on RCU. The File description table contains multiple members--FD sets (Open_fds and close_on_exec, the file pointer array, the file description descriptor, and the size of the file pointer array). To make the update appear to be atomic for an unlocked reader, all the elements of the file descriptor are placed in a separate structure--struct fdtable.

That is, the fdtable structure is the file descriptor of the process, which is defined as follows:

---------------------------------------------------------------------
include/linux/fdtable.hstruct fdtable {    int Max_fds;    /* */  fd_set *close_on_exec; Fd_set *structstruct fdtable *next;};   
---------------------------------------------------------------------

The FD field points to an array of file object pointers. The length of the array is stored in the Max_fds. Typically, the FD field points to the Fd_array field of Files_struct, which contains 32 file object pointers. If a process opens more than 32 files, the kernel allocates a new, larger array of file pointers and places its address in FD, and the kernel updates the value of the Max_fds field.

For each file that has elements in the FD array, the index of the array is the file descriptor. The UNIX process takes a file descriptor as the primary file identifier. Two file descriptors can point to the same open file.

A process cannot use more than Nr_open file descriptors. The Open_fds field initially contains the address of the Open_fds_init field, Open_fds_init a bitmap that represents the currently open file descriptor. The Max_fds field holds the number of bits in the bitmap.

The FD_SET structure is a set of file descriptors that put together multiple file descriptors in the same situation. In the include/linux/types.h there is a definition:

typedef __kernel_fd_set Fd_set;

The __KERNEL_FD_SET structure is defined in the Include/linux/posix_types.h:

typedef struct {

unsigned long fds_bits [__fdset_longs];

} __kernel_fd_set;

Some of the macros related to __fdset_longs:

#define __NFDBITS  (8 * sizeof (unsigned long))#undef __fd_setsize#define __fd_setsize  1024x768#undef _ _fdset_longs#define __fdset_longs (__fd_setsize/__nfdbits)   

The EMBEDDED_FD_SET structure is a small set of file descriptors that put together file descriptors in the same case and can only hold unsigned long, which is sufficient for many processes.

/**/struct embedded_fd_set {   long fds_bits[1];};     

The root filesystem is a file system, but, compared to the normal file system, it is unique in that it is the first file system that the kernel starts with, and the kernel code image file is stored in the root filesystem. The system boot launcher will load some basic initialization scripts and services into memory after the root file system is mounted.

We first understand some of the root file system information from the Linux operating system installed on the host. For example, the author works on the Linux desktop system can get the following results:

# mount

/dev/hda2 on/type ext3 (rw)

None On/proc type proc (rw)

/DEV/HDA1 on/boot type ext3 (rw)

None on/dev/pts type devpts (rw,gid=5,mode=620)

None On/dev/shm type TMPFS (rw)

# DF

Filesystem 1k-blocks used Available use% mounted on

/dev/hda2 16216016 5667* 9724600 37%/

/DEV/HDA1 101089 9321 8*9 10%/boot

None 63028 0 63028 0%/dev/shm

From the mount command above we can see that in Desktop Linux, the root filesystem "/" is mount to the/DEV/HDA2 device, the file system type is ext3, the property is RW to read and write. From the DF command, you can get more information about the usage space of the root file system.

The root filesystem has always been an important part of all UNIX-like operating systems, and it can be considered as an important feature of the embedded Linux system that distinguishes it from some other traditional embedded operating systems, which brings many powerful and flexible functions to Linux, and also brings some complexity. We need to have a clear understanding of the basic structure of the root filesystem, and carefully select the system libraries, kernel modules and applications required, and configure various initialization script files, and select the appropriate file system type and place it in the appropriate location of the actual storage device.

Basic directory structure of the root file system

The root file system of Linux is organized in a tree structure, containing the various files and programs required for kernel and system management, in general, the top-level directory under the root directory "/" has some relatively fixed names and uses.

The following is a list of the more common directory structures in a Linux root file system:

/bin directory where binary executable commands are stored

/dev directory where device files are stored

/etc directory to store system administration and configuration files

/home User main directory, such as the user's home directory is/home/user, can be represented by ~user

/lib directory to store dynamic link shared libraries

/sbin the directory where the hypervisor is stored by the system administrator

/tmp Common temporary file storage point

/root system Administrator's home directory

The/MNT system provides this directory to allow users to temporarily mount other file systems.

/proc virtual file system, you can access this directory directly to obtain system information.

/var overflow area for some large files

/usr The largest directory, the applications and files to be used almost all in this directory.

Most of these directories should be familiar to readers who often use Linux systems. However, there are several directories that are easy to confuse for beginners, such as/bin,/sbin,/usr/bin and/usr/sbin. Here is a brief description of their differences:/bin directory is generally stored for the user and the system is a necessary binary files, and the/sbin directory is only for the system management of the binary files, the directory of files will not be used by ordinary users. Conversely, those that are not necessary for user binaries are stored under/usr/bin, and those that are not very necessary for system administration tools are placed under/usr/sbin. In addition, for some local libraries are very similar, for those who need to start the system and run the necessary commands to be stored in the/lib directory, and for other not necessary inventory in the/usr/lib directory can be.

For embedded Linux system root file system, generally may not be as complex as listed above, such as embedded system is usually not for multi-user, so/home this directory in general embedded Linux may be rarely used, and/ The boot directory depends on whether the bootloader you are using will be able to regain the kernel image from your root filesystem before the kernel boots. Generally speaking, only/bin,/dev,/etc,/lib,/proc,/var,/usr these needs, while others are optional.

Simply put, the root filesystem includes the virtual root file system and the real root file system. In the initial phase of kernel startup, we first create the virtual root filesystem, then call Do_mount to load the real filesystem and switch the root filesystem to the real filesystem, which is the real file system.

A What is a root file system

On traditional Windows machines, the directory structure may include C: or D: disks, which they generally refer to as the root directory of a particular logical disk. From the filesystem level, each partition contains a root directory, which is the presence of multiple roots in the system.

However, in a Linux system, the directory structure is significantly different from that of Windows. There is only one root directory in the system, the path is "/", and the other partition is just a folder mounted in the root directory, such as "/proc" and "system", where the "/" is the root directory in Linux.

The corresponding root directory also exists a root filesystem concept, we can mount a partition as a root file system, such as the 6410 public version of the Mtdblk2 mount as the root file system. The program can be specified by U-boot to kernel specified parameters or compile options, such as the current Development Board by the following compilation options to develop the root file system:

Config_cmdline= "console=ttys0,115200 mem=108m rdinit=/linuxrc Root=/dev/mtdblock2"

Simply put, the root file system is a directory structure that includes some of the directory structures and important files that Linux must have when it starts.

There are two types of root file system, one is virtual root file system and the other is the real root file system. In general, you will first do some work in the virtual root filesystem, and then switch to the real root file system.

Broadly speaking, the virtual root filesystem consists of three types, namely Initramfs, CPIO-INITRD, and IMAGE-INITRD.

Process-related file systems

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.