Textbook one or two chapters

Source: Internet
Author: User
Tags comparison table using git

Chapter One introduction to the Linux kernel

UNIX History

A file system prototype developed by Bell Labs evolved into UNIX, and the UNIX operating system was rewritten with the C language, allowing UNIX to be widely ported. That's after all the innovation, the variant, the UNIX becomes a powerful and stable operating system.

UNIX Features

    • Concise: Provides only hundreds of system calls and has a very clear design purpose
    • Abstract: Everything is treated as a file, and the operation of data and devices is done through a set of identical system invocation interfaces.
    • Extensive portability: Unix kernel and System Tools software is written in C, making it an amazing porting capability in front of a variety of hardware architectures
    • Fast process creation: Unique fork () system call
    • Simple and stable inter-process communication meta-language: one-time implementation of quality and quantity to complete a task;
    • Clear hierarchical structure: the concept of separation of strategy and mechanism, simple interprocess communication meta-language combines single-purpose programs easily to achieve increasingly complex tasks
About Linux

Since the source code of the Minix system cannot be modified and published, Linus developed its own operating system--linux,linux Unix-like systems, but did not use UNIX source code directly, but did not abandon the Unix design goal and ensured the consistency of the application programming interface. Linux is non-commercial, free and public.

Linux System Basics:

内核C库工具集系统的基本工具
Introduction to operating systems and kernels

Operating system: The parts of the system that are responsible for the most basic functions and systems management. Includes kernel, device driver, boot to program, command line shell or other kind of user interface, basic file management system tools.

The user interface is the external representation of the operating system, and the kernel is the inner core

The composition of the kernel:

1.中断服务程序(响应中断)2.调度程序(管理多个进程分享处理器的时间)3.内存管理程序(管理进程地址空间)4.系统服务程序(网络、进程间通信)

Kernel space: System State and protected memory space

System call: Application communicates with kernel

应用程序通常调用库函数,再由库函数通过系统调用界面让内核完成任务。应用程序通过系统调用在内核空间运行,内核称为运行于进程上下文中。交互关系(应用程序通过系统调用界面陷入内核)是应用程序完成工作的基本方式。

The kernel is responsible for managing the system's hardware devices:

硬件设备相遇系统通信,首先要发出一个异步的中断信号来打断处理器的执行。中断——中断号——中断服务程序注意:中断服务程序运行在与所有进程都无关的,专门的中断上下文中运行。

The activity of each processor at any given point in time must be summed up as:

运行于用户空间,执行用户进程运行于内核空间,处于进程上下文,代表某个特定的进程执行运行于内核空间,处于中断上下文,与任何进程无关,处理某个特定的中断
Comparison between the Linux kernel and the traditional UNIX kernel
    • The Unix kernel is an indivisible static executable library that must run in a separate address space in the form of a huge, separate executable block, usually requiring a hardware system to provide a page mechanism (MMU) to manage memory;
    • Linux kernel: Modular design, preemptive kernel. Supports kernel threads and dynamically loading kernel modules

    • Single Core:

      The biggest feature is that the kernel can call functions directly, and all processes are in the kernel state. It is completed as a single large process in its entirety and also runs in a separate address space.

      Simple & High Performance

    • Micro-Kernel:

      Functions are divided into multiple independent processes, each of which is called a server. There are two types of privileged mode and user mode, which run independently in the respective address space. IPC communication mechanisms are used among processes.

Linux kernel version

Two kinds: stable and in development.

Naming mechanism:

点号隔开:主版本号.从版本号.修订版本号.稳定版本号

Note: From the version number is even-stable version, odd--development version

Chapter two from the kernel to get the kernel source code 1, using Git

Git: A control system that manages the kernel source code, is distributed.

获取最新提交到Linus版本树的一个副本$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git下载代码后,更新自己的分支到Linus的最新分支$ git pull

Can be kept consistent with the kernel's official code tree at all times.

2. Install kernel source code

Two forms of kernel compression: GNU Zip & bzip2

Extract:

bzip2:$ tar xvjf linux-x.y.z.tar.bz2zip:$ tar xvzf linux-x.y.z.tar.gz

Note: The kernel source code is generally installed in the/usr/src/linux directory, this source tree is not used for development

3. Using Patches

Incremental patches can be used as a bridge for version transfer.

Just make an incremental patch to the old version and let it facelift.

从内部源码树开始,运行$ patch -p1 < ../patch-x,y,z
Kernel source Tree

Example:

COPYIN:内核许可证CREDITS:开发者列表MAINTAINTERS:维护者列表(维护内核子系统和驱动程序)Makefile:是基本内核的Makefile。
Compiling kernel 1, configuring the kernel

The various options that can be configured are prefixed with CONFIG.
Configuration items have

二选一:yes or no三选一:yes or no or module(选定该配置项但编译的时候以模块形式生成)

Simplify kernel configuration:

逐一遍历:make config(耗时长)基于图形工具:make menuconfig            make gconfig基于默认配置:make defconfig 验证和更新配置:make oldconfig配置选项CONFIG_IKCONFIG_PROC把完整的压缩过的内核配置文件存放在/proc/config.gz中,再次编译时可以方便地克隆当前的配置。如果当前启用了此选项,可以从/proc下复制出配置文件:zcat /proc/config.gz > .config

Compile kernel:

make:默认的Makefile自动化编译。
2, reduce the compilation of garbage information

To redirect the output:

$ make > ../detritus#将错误报告和警告信息重定向到文件中$ make > /dev/null#将无用的输出信息重定向到/dev/null中(永无返回值的黑洞)
3. Multiple compilation jobs derived

The Make program splits the compilation process into multiple parallel jobs. Each of these jobs runs independently and concurrently, helping to speed up the compilation process on multiprocessor systems and improving processor utilization.

By default, make only derives one job.

#以多个作业编译内核$ make -jn  - j:指定同时执行多任务- n:要衍生出的作业数
4. Install the new kernel

Kernel installation Follow the instructions for booting the boot tool.

The module installation is automatic and is independent of the architecture:

以root身份运行:$ make modules_install#把所有已编译的模块安装到正确的主目录/lib/modules下

At compile time, a System.map file is created in the root directory, which is a symbol comparison table that corresponds the kernel symbol to their starting address.

Kernel development Features 1, the kernel development can not access the C library and can not access the standard C header file
内核源代码文件不能包含外部头文件。基本头文件:内核源代码顶级目录下的include中体系结构相关头文件:内核源代码树的arch/<architecture>/include/asm目录下

PRINTK () Function: Copy the formatted string to the kernel log buffer, and the Syslog program can read the buffer to obtain the kernel information.

Notable differences between PRINTK () and printf ():

printk()函数允许通过指定一个标志来设置优先级,syslog根据这个标志决定在什么地方显示这个系统信息。注意:标志和信息之间没有逗号。
2, the kernel programming must use the GNU C
GNU:一种操作系统gcc:GNU编译器的集合

1. Inline function: The function is expanded at the point where it is called. You can eliminate the overhead of function calls and returns, but the code gets longer.

定义时,需要使用static作为关键字,用inline限定它:static inline void wolf (unsigned long tail_size)内联函数必须在使用之前就定义好,一般在头文件中定义。内核中优先使用内联函数而不是宏。

2. Inline assembly: the ASM () Directive is usually used to embed assembly code, which is not optimized with volatile.

The Linux kernel is mixed with C language and assembly language, and assembly language is generally used in places near the bottom of the architecture or where the execution time is strictly required.

3. Branch declaration: Used to optimize the conditional selection statement. (a condition often appears or rarely occurs)

The kernel encapsulates this directive as a macro, such as:

unlikely() - 很少出现,通常为假likely() - 经常出现,通常为真
3. Kernel programming lacks memory protection mechanism like user space

User program for illegal memory access, the kernel will block, but the kernel itself illegally access the memory, no one can take care of the kernel.

Memory errors in the kernel can cause oops (very common errors)

Another: Memory in the kernel is not paged, each byte is used, and the physical memory is reduced by one.

4. It is difficult to perform floating-point arithmetic when kernel programming

The process of user space is a floating-point operation, the kernel completes the conversion of integer-to-floating-point operation mode, but the kernel itself cannot be trapped.

5, the kernel to each process only a small fixed-length stack

The stack of user space is large and can grow dynamically.

The kernel stack has an exact size that changes with the architecture.

6. Because the kernel supports asynchronous interrupts, preemption, and SMP, you must always be aware of synchronization and concurrency

The kernel is prone to race conditions.

Especially:

Linux是抢占多任务操作系统Linux内核支持对称多处理器系统(SMP)Linux内核支持异步中断Linux内核可以抢占

Common ways to solve competition: spin locks and semaphores.

Textbook one or two chapters

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.