Linux Kernel (6)-module mechanism with "Hello world!"

Source: Internet
Author: User

There is a kind of moving, called Nei surface, there is a mechanism called module mechanism. Obviously, this kind of modular mechanism brings convenience to those Linux enthusiasts, because the modular mechanism means that people can divide the huge Linux kernel into many small modules. For developers writing device drivers, since then they can write device drivers do not need to compile her into the kernel, not reboot machine, she is just a module, when you need her, you can carry her into the arms (insmod), when you no longer need her, You can kick her off (rmmod).

So, suddenly as the night breeze, the kernel is a module everywhere. Let's get to know the module from a great example. This is the legendary "Hello world!", this fantastic name we have seen countless times, every time she appears in front of us, it means that we are beginning to touch a new computer language. (A programmer is very interested in calligraphy and has decided to make a contribution in this regard after retirement.) So he spent a fortune to buy the finest four treasures. One day, after the sudden birth yaxing, a grind the ink paper, and point on a good sandalwood, quite Wang Xizhi demeanor, and with Yan Zhenqing momentum, moments of calm, splash brush, solemnly write the next line: Hello World)

Take a look at the code below, which is one of the simplest modules under Linux. When you install this module, she will say to you in her own language: "hello,world! "And then you uninstalled this module, and you ruthlessly abandoned her, she was sad, she was desperate, but she didn't complain, she just lightly 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 MO Dule_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}
 
   static void __exit hello_exit (void)
    {
   14          PRINTK (Kern_alert "Goodbye, cruel world/n");
    15}
  
    module_init (hello_init);
    Module_exit (hello_exit);  

You need to use Module_init () and Module_exit (), you can call them functions, but in fact they are some macros, you can not know the story behind them, just know, in the world of Linux Kernel 2.6, Any module that you write needs to use them to initialize or exit, or to register and later log off.

When you register with Module_init () for a module, the function that Module_init () registers will be executed when you use the Insmod command to install. And when you use the Rmmod command to unload a module, the Module_exit () registered function will be executed. Module_init () is called the driver's initialization entry (driver initialization entry point).

How do you demonstrate the operation of 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 includes this
3 # Makefile once again.
4 # This conditional selects whether we is being included from the
5 # Kernel Makefile or not.
6 ifeq ($ (kernelrelease),)
7
8 # Assume the source tree is where the running kernel was built
9 # should set Kerneldir in the environment if it ' s elsewhere
Ten kerneldir? =/lib/modules/$ (Shell uname-r)/build
The current directory is passed to Sub-makes as argument
PWD: = $ (Shell PWD)
13
Modules:
$ (make)-C $ (Kerneldir) m=$ (PWD) modules
16
Modules_install:
$ (make)-C $ (Kerneldir) m=$ (PWD) Modules_install
19
Clean:
RM-RF *.o *~ core depend. *.cmd *.ko *.mod.c. tmp_versions
22
23. Phony:modules Modules_install Clean
24
-Else
Called from kernel build system:just declare what we modules are
Obj-m: = hello.o
endif

This example can be found on the lwn.net, you can put the above two files in one of your directories, and then do make, perhaps you may not be successful, because Linux Kernel 2.6 requires you to compile the module before you must first execute make in the kernel source code directory, in other words, You have to configure the kernel, perform the make before you make your own module. The reason is not to elaborate, you follow the request to do so. After the kernel top-level directory is made, you can execute make in the directory where you are currently placing the makefile. After make you should see a file called Hello.ko generated, congratulations, this is the module you will be testing.

Execute the command,

#insmod Hello.ko

At the same time in another window, use the command tail-f/var/log/messages to view the log file, you will see Hello World is printed out. And then execute the command,

#rmmod Hello.ko

At this point, in another window you will see Goodbye,cruel world! was printed out.

Here, I should congratulate you, because you have been able to write the Linux kernel module. It's a wonderful feeling, isn't it? You can laugh at the Qin Emperor Han Wulüe lose literary talent Tang Zong Song zu less coquettish, also can ridicule a generation of Genghis Khan only know Bow shoot big eagle. Yes, Gillian Sister told us, as long as I like, there is nothing to do.

We will see in the future that each module in the 2.6 kernel starts with module_init and ends with Module_exit. For most people there is no need to know why, remember it, for most people, it is like 21, it is similar to the two point is the shortest line, do not need to prove that if you must prove the shortest line between two points, you can throw a bone at B point, let a dog from a point to start, You will find that the dog is walking in a straight line, yes, the dog knows, can we not know?

Linux Kernel (6)-module mechanism with "Hello world!"

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.