Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Hello World Kernel Module | Writing Your First Character Driver
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C Device Drivers Basics

bookHello World Kernel Module

Writing your first kernel module is a fundamental step in understanding how device drivers interact with the Linux kernel. A typical starting point is the classic Hello World kernel module, which demonstrates how to insert code into the kernel safely, perform simple logging, and remove the module cleanly. This minimal example will help you become familiar with module structure, kernel logging, and the basic workflow of compiling and loading a module.

hello_world.c

hello_world.c

copy
123456789101112131415161718192021
#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> static int __init hello_init(void) { printk(KERN_INFO "Hello, kernel world!\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_INFO "Goodbye, kernel world!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple Hello World kernel module");

The key to communicating from within kernel modules is the printk function, which is similar to printf in user space but designed for kernel logging. The kernel module above defines two special functions: one marked with __init, which runs when the module is loaded, and one with __exit, which runs when the module is removed. The module_init and module_exit macros register these functions with the kernel. The printk function is used here with the KERN_INFO log level to print informational messages to the kernel log, which you can view using dmesg after loading or unloading the module. The module metadata macros at the end provide information about the module to the kernel.

hello_warn.c

hello_warn.c

copy
12345678910111213141516171819
#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> static int __init hello_init(void) { printk(KERN_WARNING "This is a warning from the kernel module!\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_WARNING "Kernel module exit with a warning!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL");
question mark

Which function is commonly used to print messages from kernel modules?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain more about the `printk` function and its log levels?

How do the `__init` and `__exit` function attributes work in kernel modules?

What is the purpose of the module metadata macros?

bookHello World Kernel Module

Swipe to show menu

Writing your first kernel module is a fundamental step in understanding how device drivers interact with the Linux kernel. A typical starting point is the classic Hello World kernel module, which demonstrates how to insert code into the kernel safely, perform simple logging, and remove the module cleanly. This minimal example will help you become familiar with module structure, kernel logging, and the basic workflow of compiling and loading a module.

hello_world.c

hello_world.c

copy
123456789101112131415161718192021
#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> static int __init hello_init(void) { printk(KERN_INFO "Hello, kernel world!\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_INFO "Goodbye, kernel world!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple Hello World kernel module");

The key to communicating from within kernel modules is the printk function, which is similar to printf in user space but designed for kernel logging. The kernel module above defines two special functions: one marked with __init, which runs when the module is loaded, and one with __exit, which runs when the module is removed. The module_init and module_exit macros register these functions with the kernel. The printk function is used here with the KERN_INFO log level to print informational messages to the kernel log, which you can view using dmesg after loading or unloading the module. The module metadata macros at the end provide information about the module to the kernel.

hello_warn.c

hello_warn.c

copy
12345678910111213141516171819
#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> static int __init hello_init(void) { printk(KERN_WARNING "This is a warning from the kernel module!\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_WARNING "Kernel module exit with a warning!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL");
question mark

Which function is commonly used to print messages from kernel modules?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
some-alt