Linux driver _ The simplest example __linux

Source: Internet
Author: User

1.helloworld.c

#include <linux/module.h>//information related to module
 
#include <linux/kernel.h>
#include <linux/init.h >      //init-related functions
 
static int __init hellokernel_init (void)
{
        printk (kern_info "Hello kernel!\n");
        return 0;
}
 
static void __exit hellokernel_exit (void)
{
        PRINTK (kern_info "Exit kernel!\n");
}
 
 
Module_init (hellokernel_init);
Module_exit (hellokernel_exit);
 
 
Module_license ("GPL");
Module_author ("xxxx");
2.Makefile
<pre name= "code" class= "CPP" >obj-m: = HELLOWORLD.O
 
PWD       : = $ (Shell PWD) all
 
:
        make-c/lib/ modules/$ (Shell uname-r)/build m=$ (PWD) modules clean: RM-RF *.o
        core. *~ *.cmd. *.mod.c



3. Implementation and Results

Execute Make:

After the success of the compilation will generate a corresponding KO file, that is, we want to drive the

Driver-related actions:

To view the information of the KO module modinfo insert module insmod Helloworld.ko Uninstall module Rmmod HelloWorld also has a modprobe feature that later describes viewing driver printing information: Use DMESG to view the related printing information in the driver       。 Now there are examples of the following printed content:---------------------log start----------------------------[27520.195551] Exit kernel!   [27948.531569] Hello kernel! ---------------------Log End----------------------------

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.