Linux kernel Programming-0: HelloWorld from the kernel

Source: Internet
Author: User

Linux kernel programming has always been a skill I'd like to master. If you ask me why, I can not say.
Maybe one day my id also appears in the message list of the kernel development group? or the kernel release file on the credits file?
Maybe it is. In fact more, may be for the bottom of the worship, as well as the spirit of the core quest.
Think of the complexity of the operating system, the idea of the interface between software systems, the inner feel exquisite at the same time, is deeply infatuated.
So from the beginning of this article, I want to really go into the Linux kernel, let the Code guide me, to the wonderful world to explore.

In this article, come together to say hello world to the kernel.
This time the programming environment:
CentOS 6.8

Linux CentOS 2.6.32-573.8.1.el6.x86_64

If the kernel is not installed, you may need to install the kernel source package
Kernel-devel-2.6.32-642.4.2.el6.x86_64

Yum Install kernel-devel-2.6.32-642.4.2.el6.x86_64

Once installed, this version of the kernel can be found in/usr/src/linux.

Then say not much first, look at the code first.

//20160904//kernel_hello_world.c#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>static int __init lkp_init(void){    printk("Hello,World! --from the kernel space...\n");    return 0;}static void __exit lkp_cleanup(void){    printk("Goodbye,World! --leaving kernel space...");}module_init(lkp_init);module_exit(lkp_cleanup);

The above code is KERNEL_HELLO_WORLD.C content.
As a kernel module, at compile time, the makefile file writes:

#File:Makefileobj-m += kernel_hello_world.o

This command can then be used to compile:

make -C /usr/src/linux SUBDIRS=$PWD modules

After compiling, the following file in the directory might look like this:

kernel_hello_world.ko.unsigned  kernel_hello_world.o  Module.symverskernel_hello_world.c   kernel_hello_world.mod.c        Makefilekernel_hello_world.ko  kernel_hello_world.mod.o        modules.order

There are so many files generated, where Kernel_hello_world.ko is the compiled kernel module file, there are many such modules in the Linux kernel, they may act as different roles, may be drivers, or it may be a variety of devices.
This module prints a line of characters in the/var/log/message file, which is hello,world! --from The kernel space ...
You can use the Insmod Kernel_hello_world.ko to load this module into the kernel.
Use Lsmod to see if it is already loaded.
Use Rmmod Kernel_hello_world.ko to uninstall this module.
Could you tail/var/log/message to see if it was executed successfully?

Hello,kernel.

Linux kernel Programming-0: HelloWorld from the kernel

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.