UbuntuLinux module programming

Source: Internet
Author: User
First, you need to build the source code tree of the system kernel, because the module is a kernel programming, and the C language of the user space becomes different. It depends on the existence of the kernel source code tree. A mechanism of the kernel module allows the Kernel File (zImage or bzImage) to not contain a certain component, but when the component needs to be used, dynamic addition to the running kernel Linux provides a mechanism called & ldquo; kernel module & rdquo; to achieve the above effect. Features & bull; modules are not compiled

First, you need to build the source code tree of the system kernel, because the module is a kernel programming, and the C language of the user space becomes different. It depends on the existence of the kernel source code tree.

Kernel module

A mechanism allows the Kernel File (zImage or bzImage) to not contain a certain component, but when the component needs to be used, dynamic addition to the running kernel Linux provides a mechanism called "kernel module" to achieve the above results.

Features
• The module itself is not compiled into the Kernel File (zImage or bzImage)
• Dynamic installation or uninstallation during kernel running as needed.

Step 1: Create source code: hello. c

Root @ Ubuntu:/home/Mydocument/Modules # vi hello. c

# Include
# Include
MODULE_LICENSE ("Dual BSD/GPL ");
MODULE_AUTHOR ("fudan_abc ");

Static int _ init hello_init (void)
{
Printk (KERN_ALERT "Hello, world! \ N ");
Return 0;
}

Static void _ exit hello_exit (void)
{
Printk (KERN_ALERT "Goodbye, cruel workd! \ N ");
}

Module_init (hello_init );
Module_exit (hello_exit );

Step 2: Create Makefile

Root @ ubuntu:/home/Mydocument/Modules # vi Makeile


Obj-m + = hello. o
All:
Make-C/usr/src/linux-headers-$ (shell uname-r) M = $ (shell pwd) modules

Clean:
Make-C/usr/src/linux-headers-$ (shell uname-r) M = $ (shell pwd) clean

Step 3: Compile

Root @ ubuntu:/home/Mydocument/Modules # make

Root @ ubuntu:/home/Mydocument/Modules # ls

Hello. c hello. mod. o Module. symvers hello. ko


Hello. o Makefile hello. mod. c modules. order

Step 4: Install the module

Root @ ubuntu:/home/Mydocument/Modules # insmod hello. ko
Root @ ubuntu:/home/Mydocument/Modules # lsmod
Module Size Used
Hello 12393 0
Rfcomm 38408 0
Bnetp 17923 2
Bluetooth 148839 10 rfcomm, bnep
Parport_pc 32114 0

 

Step 5: view the output and uninstall the module

Root @ ubuntu:/home/Mydocument/Modules # dmesg

[1, 29665.689269] Hello, world!

Root @ ubuntu:/home/Mydocument/Modules # rmmod hello
Root @ ubuntu:/home/Mydocument/Modules # lsmod

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.