Linux Chapter 12th Notes

Source: Internet
Author: User
Tags volatile

Chapter One Linux kernel Introduction 1. Features of the Unix kernel
    • Concise: Provides only system calls and has a very clear design purpose
    • Abstract: Almost everything is treated as a file
    • Portability: Written in C , making it an amazing porting capability in front of a variety of hardware architectures
    • Process: Create quickly, perform a task at a time of quality and quantity; unique fork system call
    • Clear hierarchical structure: the concept of separation of strategy and mechanism, simple interprocess communication meta-language combines a single purpose program conveniently together

2. Introduction to the Linux kernel

    • linux unix unix system, design thought similar, for example it also realized unix api linux No direct use of unix unix Span style= "font-family: the song Body;" > design goals and ensure the consistency of the application programming interface.
    • linux
    • linux system is based on the kernel, c basic tools for libraries, Toolsets, and systems.

3. Introduction to operating system and kernel

    • The operating system is the part of the entire system that is responsible for the most basic functions and systems management. Includes kernels, device drivers, boot bootstrapper, command-line Shell , or other kinds of user interfaces, basic file management tools, and system tools.
    • The kernel is the core of the operating system. Typically, a kernel consists of an interrupt service program that is responsible for responding to interrupts, a scheduler that manages multiple processes to share processor time, a memory management program that manages the address space of the process, and a network of system services, such as communication between processes.
    • System state: Has a protected memory space and all permissions to access the hardware device. (The kernel space is the system state and the protected memory space.)
    • User state: When executing the normal user program, the system enters the user space by the user state execution. (Applications are executed in user space and can only see some of the system resources that they are allowed to use, and only use certain system features, no direct access to system hardware, and no access to other cores to other people's memory ranges)
    • Applications running in the system communicate with the kernel through system calls. (The application is stuck in the kernel through the system call interface, which is the basic behavior of the application to complete the work.)
    • Interrupt mechanism: An asynchronous interrupt signal interrupts the execution of the processor, which in turn interrupts the execution of the kernel. Interrupts usually correspond to an interrupt number, and the kernel finds the appropriate interrupt service program with the interrupt number and calls the program to respond and handle interrupts.
    • The activity of each processor at any given point in time must be one of the following:
      • Run in user space, execute user process
      • Runs in kernel space, is in the process context, executes on behalf of a particular process
      • Runs in kernel space, is in the interrupt context, is independent of any process, and handles a specific interrupt

4. Comparison between the Linux kernel and the traditional UNIX kernel
    • Single Core
      • The entire kernel runs on a large kernel address space.
      • Advantages: simple and efficient. All cores are in a large address space, so calls and call functions are similar between functions of the kernel, with little performance overhead.
      • Cons: A feature crash can cause the entire kernel to become unusable.
      • Micro-core
        • The kernel is divided into individual processes by function. Each process runs independently on its own address space.
        • Pros: safe. The various services of the kernel run independently, and one service hangs without affecting other services.
        • Cons: Calls between the kernel's various services involve inter-process communication, which is more complex and inefficient.
        • Linux Kernel Design
          • · Based on a single core
          • Features of the microkernel: modular design, preemptive kernel, kernel thread support, dynamic loading of kernel modules.
          • Avoid performance flaws in microkernel design: Let everything run in the kernel state, call functions directly, without message passing.
          • Linux Kernel features
          • - supports dynamic loading of kernel modules

- supports symmetric multi-processing (SMP)

-The kernel can preempt (preemptive), allowing the kernel to run tasks with the ability to prioritize execution

- no distinction between threads and processes

5. Linux kernel version

There are two types of Linux cores: stable (with industrial strength, can be widely applied and deployed), and in development.

Linux naming mechanism (can be used to differentiate between stable and in-development kernels):

If the version number is even, the kernel is stable, and if it is odd, the kernel is the development version.

The second chapter starts from the kernel, obtains the kernel source code 1. Git

Git is actually an open source, distributed version Control tool. Linux as an open source kernel, its source code can also be downloaded and managed with Git

- get a copy of the latest commit to the version tree

-$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git

- After downloading the code, update your branch to the latest branch

-$ git pull

2. Install the kernel source code

Compressed form bzip2:$ tar xvjf linux-x.y.z.tar.bz2

Compressed form zip:$ tar xvzf linux-x.y.z.tar.gz

About parameters:

-X unpack . Tar formatted files

-V Show more information

-j using the bzip2 program

-Z using the gzip program

-F using an archive file

3. Using Patches

Starting from the internal source tree, run $ patch-p1 <. /patch-x,y,z

Second, the kernel source code structure

Third, compile kernel 3.1 configuration kernel

Two select one and three of the configuration items select one:

Option two:Yes or no

Option three:Yes or no or module( module means that the configuration is selected, generated as a module. Drivers typically use three to select one of the configuration items)

Command-line tools for character pages:

Make Config

Graphical interface tools:

Make Menuconfig

Create a configuration for the architecture based on the default configuration:

Make Defconfig

To verify and update the configuration:

Make Oldconfig

3.2 Compiling the kernel

Compile kernel:

Make

Redirect to this file:

Make >: /detritus

REDIRECT useless output information to a black hole with no return value:

Make >/dev/null

3.3 Installing the new kernel

Install all compiled modules into the correct home directory /lib/modules

Make Modules_install

Four, the core development features 1. No libc library/Standard header file

Reason: (speed and size) ensure the core is efficient and concise.

The kernel source code file cannot contain an external header file.

Base header file: In the include in the top-level directory of the kernel source code

Architecture-Related header files: Under the Arch/<architecture>/include/asm directory of the kernel source tree

PRINTK () function: Copy the formatted string to the kernel log buffer, andthesyslog program can read the buffer to obtain the kernel information.

2. The GNU C must be used

What is GNU? GNU is an operating system, andthe C compiler provided by GNU is the GCC we used before .

(1) Inline function

static inline void Wolf (unsigned long tail_size);

-Static: keyword

-Inline: for qualifying keywords

inline function: The compile time is expanded where it is called.

Advantages: Reduce the cost of function calls, performance is good.

Cons: Frequent use of inline functions also makes the code longer, which consumes more memory at run time.

Defines an inline function feature: a function with a high time requirement and a shorter length of itself.

Inline functions are defined before they are used, and are generally defined in the header file.

For type safety and legibility, it is preferable to use inline functions instead of complex macros.

(2) inline assembly

unsigned int low, high;

ASM volatile ("RDTSC": "=a" (Low), "=d" (high));

/* Low and high are included in the lower -level and higher -bit time stamps, respectively */

-ASM: Embed assembly code

-Volatile: not optimized

Assembly language is used in places that are near the bottom or strict in execution time.

(3) Branch declaration

/* if error is 0 ( false ) in most cases */

if (unlikely (error)) {

/* ... */

}

/* if success is not 0 ( true ) in most cases */

if (likely (success)) {

/* ... */

}

For conditional selection statements, when a condition is often / seldom present, the compiler can optimize the conditional branching selection by an instruction built into GCC.

The kernel encapsulates this instruction as a macro.

3. No memory protection mechanism

The risk of the kernel's own unauthorized access to memory

The memory in the kernel is not paged: Each byte is used, and the physical memory decreases by one

4. Difficult to perform floating-point arithmetic

When using floating-point numbers, you need to manually save and restore floating-point registers and some other tedious operations.

Do not recommend using

5. Each process has only a small fixed-length stack

The size of the kernel stack is determined when the kernel is compiled, and the size of the kernel stack is different for unused architectures, but is fixed. (unlike user-space stacks that can grow dynamically)

6. Always be aware of synchronization and concurrency

Reason:

Linux is a preemptive multitasking operating system

The kernel supports symmetric multiprocessor systems (SMP)

Interrupt Asynchronous arrival

The kernel can preempt

Common solutions: Spin locks and semaphores

7. Consider the importance of portability

Features that need to be maintained: most C -language code is not architecture-independent.

Linux Chapter 12th Notes

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.