Sharing and Discussion on the essence of Linux kernel cultivation (10)-module mechanism and "Hello world !"

Source: Internet
Author: User

Recommended blog posts:Linux Kernel "problem Gate"-learning issues and experience highlights

Recommended download:Methodology of essence edition of "Cultivation of Linux kernel"

 

There is a kind of emotion, called inner Niu's face, and a mechanism called module mechanism. Obviously, this module mechanism has brought convenience to those Linux enthusiasts, because it means that people can divide a huge Linux kernel into many small modules. For developers who write device drivers, since then, they can write device drivers without compiling them into the kernel or reboot machines. She is just a module, when you need her, you can hold her in your arms (insmod). When you no longer need her, you can kick her out (rmmod ).

As a result, the kernel is a module everywhere. Let's get to know the module from a great example. This is the legendary "Hello world! "We have seen this fantastic name countless times. Every time she appears, it means that we are getting started with a new computer language. (A programmer is very interested in calligraphy. After retirement, he decided to make achievements in this regard. So I spent a lot of money to purchase the finest four treasures of wenfang. One day, after dinner, suddenly Yaxing, a bit of ink to paper, and lit a good sandalwood, quite Wang's style, but also with a real momentum, God for a moment, splash ink, solemnly write a line of words: hello World)

The following code shows the simplest module in Linux. When you install this module, she will confess to you in her special language: "Hello, world !", Later, you uninstalled this module, and you abandoned her relentlessly. She was sad and desperate, but she didn't complain. She just said, "Goodbye, cruel world !" (Goodbye, cruel world !)

 

/************ Hello. c *********************/

1 # include <Linux/init. h>/* needed for the macros */
2 # include <Linux/module. h>/* needed for all modules */
3 module_license ("dual BSD/GPL ");
4 module_author ("fudan_abc ");
5
6 static int _ init hello_init (void)
7 {
8 printk (kern_alert "Hello, world! /N ");
9 return 0;
10}
11
12 static void _ exit hello_exit (void)
13 {
14 printk (kern_alert "Goodbye, cruel world/N ");
15}
16
17 module_init (hello_init );
18 module_exit (hello_exit );

You need to use module_init () and module_exit (). You can call them as functions, but they are actually some macros. You don't have to know the story behind them, you just need to know, in the world of Linux kernel 2.6, any module you write needs to be initialized or exited, or registered or deregistered later.

After you use module_init () to register a module, the function registered by module_init () will be executed when you use the insmod command to install it. When you use the rmmod command to uninstall a module, the function registered by module_exit () will be executed. Module_init () is called the driver initialization entry point ).

How can I demonstrate how to run the above Code? Yes, you need a makefile.

 

1 # To build modules outside of the kernel tree, we run "make"
2 # in the kernel source tree; The makefile these then provided des this
3 # makefile once again.
4 # This conditional selects whether we are being encoded from
5 # kernel makefile or not.
6 ifeq ($ (kernelrelease ),)
7
8 # assume the source tree is where the running kernel was built
9 # You shoshould set kerneldir in the environment if it's elsewhere
10 kerneldir? =/Lib/modules/$ (shell uname-R)/build
11 # the current directory is passed to sub-makes as argument
12 PWD: = $ (shell PWD)
13
14 modules:
15 $ (make)-C $ (kerneldir) M = $ (PWD) Modules
16
17 modules_install:
18 $ (make)-C $ (kerneldir) M = $ (PWD) modules_install
19
20 clean:
21 Rm-RF *. O *~ Core. depend. *. CMD *. Ko *. Mod. C. tmp_versions
22
23. Phony: modules modules_install clean
24
25 else
26 # called from kernel build system: just declare what our modules are
27 obj-M: = Hello. o
28 endif

You can find this example on lwn.net. You can put the above two files in one of your directories and then execute make, maybe you may not be able to succeed, linux kernel 2.6 requires you to execute make in the kernel source code directory before compiling the module. In other words, you must configure the kernel and run make, then you can make your own modules. The reason is not detailed. Just do what you want. After the make command is run in the top-level directory of the kernel, You can execute make in the directory where makefile is currently placed. After make, you should see a file named hello. Ko generated. Congratulations, this is the module you will test.

Run the command,

 

# Insmod hello. Ko

At the same time, in another window, run the command tail-F/var/log/messages to view the log file. You will see that Hello world is printed. Then execute the command,

 

# Rmmod hello. Ko

In another window, you will see Goodbye, cruel world! Printed.

Congratulations, you have been able to write the Linux kernel module. This is a wonderful feeling, isn't it? You can laugh at Qin Huang's Hanwu and lose the literary talent Tang Zong, Song Zuo, and laugh at a generation of tianyao to get a glimpse of the Big Arn. Yes, Sister Jiao told us that as long as I like it, there is nothing else.

In the kernel 2.6, every module starts with module_init and ends with module_exit. For most people, there is no need to know why, just remember. For most people, this is like why 1 + 1 equals 2, just like the shortest line between two points, you do not need to prove that, if you must prove the shortest straight line between the two points, you can throw a bone at point B to let a dog start from point A. You will find that the dog is in a straight line, yes, do you know what the dog knows?

 

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.