Linux Practice 2.2 Compilation module

Source: Internet
Author: User

1. Understanding Module Principles

Linux modules are a collection of functions and data types that can be compiled as standalone programs. The module mechanism is provided because Linux itself is a single core. Single core because all content is integrated and efficient, but scalability and maintainability is relatively poor, the module mechanism can compensate for this flaw.

Linux modules can be loaded into the kernel space by static or dynamic methods, and static loading refers to loading during kernel boot, and dynamic loading means loading at any time during kernel running.

When a module is loaded into the kernel, it becomes part of the kernel code. When the module is loaded into the system, the system modifies the symbol table in the kernel, adding the resources and symbols provided by the newly loaded module to the kernel symbol table for inter-module communication.

2 , writing module code

Module constructor: The initialization function that is called when the kernel module is loaded by executing a insmod or modprobe instruction. The function prototype must be Module_init (), the function pointer inside the parentheses

Module destructor: The function called when executing the rmmod instruction unload module. Function prototype is module_exit ()

Module License Statement: The function prototype is Module_license (), which tells the kernel which license the program uses, otherwise it will prompt the module to contaminate the kernel when it is loaded. The GPL is generally written.

Header file Module.h, this file must be included;

Header file Kernel.h, containing commonly used kernel functions;

The header file Init.h contains macros _init and _exit, which allow the kernel to free up memory.

Write a simple code to output a piece of text to the kernel.

3 , compile the module

Next write Makefile.

Box one to write the name of the code above

Box two to write their own/usr/src path under the name of the kernel

Explain the Make command:

Make-c $ (Linux_kernel_path) indicates jump to the kernel source directory to read the makefile there

m=$ (Current_path) indicates that returning to the current directory continues to perform the current makefile.

This is the effect after make

4 , loading modules

sudo insmod 1.ko

5 , test module

DMESG: Look at the kernel information

6 , uninstalling the module

sudo rmmod 1

When you look at the kernel information with DMESG, you will see the output written in Module_exit ().

Linux Practice 2.2 Compilation module

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.