Linux driver programming (1)

Source: Internet
Author: User

Simple Linux driver code:

// Mem. c file # include <Linux/init. h> # include <Linux/module. h> module_license ("GPL"); static int mem_init (void) {printk (kern_alert "Hello, kernel \ n"); Return 0;} static void mem_exit (void) {printk (kern_alert "Goodbye, kernel \ n");} module_init (mem_init); module_exit (mem_exit );

Makefile:

Ifneq ($ (kernelrelease),) obj-M: = mem. oelsekerneldir? =/Lib/modules/$ (shell uname-R)/buildpwd :=$ (shell PWD) Default: $ (make)-C $ (kerneldir) m =$ (PWD) modulesendifclean: Rm-RF *. ko *. mod. C *. O *. symvers *. order

After compilation, use insmod and rmmod to load and uninstall the driver module.

For example: sudo insmod mem. Ko

Note that you must add sudo before the command; otherwise, an error is returned: insmod: Error inserting 'mem. Ko ':-1 operation not permitted.

To view the mounted modules, run the following command: lsmod.

To view the output from printk, enter the command dmesg in the terminal.

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.