Linux Kernel structure Detailed __linux

Source: Internet
Author: User

Http://www.sudu.cn/info/html/edu/20070101/291108.html

The Linux kernel consists of five subsystems: process scheduling, memory management, virtual file system, network interface, and interprocess communication.
1.
Process scheduling (SCHED): Controls the process's access to the CPU. When you need to select the next process to run, the scheduler chooses the process that is most worthwhile to run. A running process is actually only waiting for CPU resources
Process, if a process is waiting for another resource, the process is not running. Linux uses a simpler process scheduling algorithm based on priority to select a new process.
2.
Memory Management (MM) allows multiple processes to secure shared primary memory areas. Linux memory management supports virtual memory, that is, the program running on the computer, its code, data, the total stack can exceed the actual
The size of the memory, the operating system simply keeps the program block currently in memory, and the rest of the program blocks remain on disk. When necessary, the operating system is responsible for exchanging the program blocks between disk and memory. Memory Management from logical
is divided into hardware-independent parts and hardware-related parts. The hardware-independent part provides the mapping of the process and the swapping of logical memory; the hardware-related section provides a virtual interface for the memory management hardware.
3.
The virtual file system (VIRTUALFILESYSTEM,VFS) hides the specifics of various hardware, provides a unified interface for all devices, and the VFS provides up to dozens of different kinds of text
Parts System. Virtual file systems can be divided into logical file systems and device drivers. A logical file system refers to a file system supported by Linux, such as Ext2,fat, which is a device driver for each hard
A device driver module written by a piece controller.
4. Network Interfaces (net) provide access to a variety of network standards and support for various network hardware. Network interfaces can be divided into network protocols and network drivers. The network protocol is partly responsible for implementing every possible network transport protocol. Network device drivers are responsible for communicating with hardware devices, and each possible hardware device has a corresponding device driver.
5. Inter-process communication (IPC) supports the various communication mechanisms between processes.

Process scheduling at a central location, all other subsystems depend on it because each subsystem needs to suspend or restore the process. Typically, when a process waits for a hardware operation to complete, it is suspended;
The process is resumed execution when it is actually completed. For example, when a process sends a message over the network, the network interface needs to suspend the sending process until the hardware successfully completes sending the message, when the message is successfully sent
After that, the network interface returns a code to the process that represents the success or failure of the operation. Other subsystems rely on process scheduling for similar reasons.
the dependencies between each subsystem are as follows:
The relationship between process scheduling and memory management: These two subsystems are interdependent. In a multiple-program environment, a program must create a process to run, and the first thing to create a process is to load the program and data into memory.
The relationship between interprocess communication and memory management: The interprocess communication subsystem relies on memory management to support shared memory communication mechanisms that allow two of processes to access common memory areas in addition to their own private space.
The relationship between virtual file system and network interface: Virtual file system uses network interface to support network File System (NFS), also use memory management to support RAMDisk device.
Relationship between memory management and virtual file systems: Memory management leverages virtual file system support swaps, Exchange processes (SWAPD) are scheduled by scheduler periodically, which is the only reason memory management relies on process scheduling. When a process accesses a memory map that is swapped out, memory management makes a request to the file system and suspends the currently running process.
In addition to these dependencies, all subsystems in the kernel are dependent on some common resources. These resources include processes that are used by all subsystems. Examples include the process of allocating and freeing memory space, the process of printing a warning or error message, the debugging routines of the system, and so on.
System Data Structure
In the Linux kernel implementation, some data structures are used more frequently, they are:
Task_struct.
Linux
The kernel utilizes a data structure (TASK_STRUCT) to represent a process, and a data structure pointer representing the process forms a task array (in Linux, tasks and processes are the same
, this array of pointers is sometimes referred to as a pointer vector. The size of this array is nr_tasks (default 512), indicating the maximum number of processes that can run concurrently on the Linux system. When building a new
, Linux assigns a TASK_STRUCT structure to the new process, and then saves the pointer to the task array. The scheduler has been maintaining a current pointer that he points to
The process that is currently running.
Mm_struct
The virtual memory of each process is represented by a mm_struct structure that actually contains information about the current execution image and contains a set of pointers to the VM_AREA_STRUCT structure that describes a region of virtual memory.
Inode

files, directories, and so on in the virtual file system (VFS) are represented by the corresponding index node (inode). The content in each VFS index node is provided by the file system-specific routines. The VFS index node is only
exists in kernel memory and is actually stored in the index node cache of the VFS. If two processes are opened with the same process, you can share the Inade data structure, which is shared through two of processes
According to the block point to the same inode completed.
the specific structure of Linux
The so-called concrete structure refers to the system implementation structure.
The specific structure of Linux is similar to the abstract structure, this correspondence is because the abstract structure from the concrete structure, our division is not strictly according to the source code directory structure, and the subsystem does not exactly match the grouping, but it is very close to the source code directory structure.
Although the abstract structure of the preceding discussion shows that there are few dependencies between subsystems, there is a high degree of dependency between the 5 subsystems of the specific structure. We can see that many of the dependencies in the concrete structure do not appear in the abstract structure.
Linux kernel source code

Currently, newer and more stable versions of the kernel are 2.0.x and 2.2.x, because the versions are slightly different, so if you want a new driver that supports both 2.0.x and 2.2.x,
Need to base on the kernel version of the conditional compilation, to do this, you will support the macro Linux_version_code, if the kernel version of the A.B.C to express, the value of this macro is
216a+28b+c. To use the specified kernel version of the value, we can use the Kernel_version macro, we can also define it ourselves.
Changes to the kernel are published in patch file mode. The patch utility is used to make a series of modifications to the kernel source files. For example: You have 2.2.Array of source code, but want to move to 2.2.10. You can get 2.2.10 patch files and apply patch to modify 2.2.Array source files. For example:
$ cd/usr/src/linux
$ Patch Plthe structure of the Linux kernel source code
The Linux kernel source code is located in the/usr/src/linux directory.
The/include subdirectory contains most of the included files needed to build kernel code, and this module rebuilds the kernel with other modules.
The/init subdirectory contains the kernel initialization code, which is the starting point for the kernel to work.
The/arch subdirectory contains all the hardware structure-specific kernel code. such as: I386,alpha
The/drivers subdirectory contains all the device drivers in the kernel, such as block devices and SCSI devices.
The/FS subdirectory contains the code for all file systems. such as: Ext2,vfat and so on.
The/net subdirectory contains the kernel's networking code.
The/MM subdirectory contains all the memory management code.
The/IPC subdirectory contains interprocess communication code.
The/kernel subdirectory contains the main kernel code.
where to start reading the source code.
On the internet, someone has made the source Navigator, which provides good conditions for reading the source code, and the site is Lxr.linux.no/source.
Here's a clue to reading the source code:
Startup and initialization of the system:

On intel-based systems, the kernel starts when loadlin.exe or Lilo loads the kernel into memory and passes control over to the kernel. For this part, please see,
Arch/i386/kernel/head. S,head. s to set the specific structure, and then jumps to the main () routine of INIT/MAIN.C.
Memory Management:

The code for memory management is primarily/mm, but the code for a particular structure is arch/*/mm. Page fault interrupt processing code in/MM/MEMORY.C
, while the memory-mapped and page-cache code is/MM/FILEMAP.C. Buffer cache is in/MM/BUFFER.C
, while the swap cache is in mm/swap_state.c and mm/swapfile.c.
Kernel:

In the kernel, the specific structure of the code in the Arch/*/kernel, scheduler in the Kernel/sched.c,fork code in KERNEL/FORK.C, kernel routines
The handler is in the INCLUDE/LINUX/INTERRUPT.H,TASK_STRUCT data structure in the inlucde/linux/sched.h.
Pci:
The PCI pseudo driver is DRIVERS/PCI/PCI.C, which is defined in inclulde/linux/pci.h. Each structure has some specific PCI BIOS code, Intel is in arch/alpha/kernel/bios32.c.
Inter-process communication:

All SYSTEMVIPC object permissions are contained in the IPC_PERM data structure, which can be found in include/linux/ipc.h. SYSTEMV message is
Implemented in IPC/MSG.C. There are implementations in IPC/SHM.C within the share. The semaphore is in the IPC/SEM.C and the pipe is implemented in the/ipc/pipe.c.
Interrupt Processing:
The kernel's interrupt handling code is unique to almost all microprocessors. The interrupt handling code is in ARCH/I386/KERNEL/IRQ.C and is defined in Include/asm-i386/irq.h.

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.