Engineers Garage

  • Electronics Projects and Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Kernel Programming

By Ajish Alfred October 2, 2014

What is kernel?

Kernel is nothing but the core of any operating system. It is the kernel that makes an OS entirely different from others. A kernel is actually a large block of code which keeps the system up and running from the time of booting, till shutdown. Kernel is that part of an OS which directly communicates with the hardware of the machine in which it runs and also with external hardware devices.
 
The kernel can be viewed as resource managing code of an OS. It is responsible for managing and allocating resources like memory, processors etc. It also allows OS to communicate and control various external hardware devices like pendrive, memory card, keyboard etc.
 
User program Vs Kernel program:
User program or applications runs in a memory area called “user space”.  The kernel program runs in another memory area called “kernel space”. Kernel program always runs in a supper access mode. So from programming point of view, kernel programming means power to access anything and everything in a system.
 
Why kernel programming:
Through kernel programming we can access or control the basic subsystems of kernel like, scheduling, memory management, file system management, networking management, inter-process communication etc. We can develop separate kernel modules which can be inserted into the kernel while the system is running.
 
The most attractive part of the kernel programming is device driver coding, through which we can interface various external hardware devices like USB, disk drives, data card or any other latest peripheral devices in the market.

Importance of Kernel in an OS

Importance of kernel in an OS:
Let us talk about the Linux kernel only. The basic architecture of an OS can be viewed as a layered structure.
 
Image Showing Basic Architecture of an OS
 
Fig. 1: Image Showing Basic Architecture of an OS
 
From the above figure it is clear that the innermost layer is the system hardware, and the user applications communicate with the hardware through the kernel layer.
 
System call interface:
User space – This is the memory area in which user applications runs.
Kernel space – This is the memory area in which the kernel program runs.
 
Image showing System Call Interfaces
 
Fig. 2: Image showing System Call Interfaces
 
The system call interface forms a layer between the user space and kernel space. It is the only mechanism through which the user space applications can access the kernel resources.
 
Image showing System Call Interface between Kernel and User Space
 
Fig. 3: Image showing System Call Interface between Kernel and User Space
 
The fundamental architecture of the GNU/Linux operating system:
 

Image showing Architecture of Linux Operating System

Fig. 4: Image showing Architecture of Linux Operating System

Linux Kernel Subsystems

The linux kernel subsystems:
The linux kernel is conceptually divided into several subsystems like,
1)      Process management subsystem
2)      Memory management subsystem
3)      File management subsystem
4)      Inter process communication subsystem
5)      Network management subsystem
6)      Device driver subsystem
 
Image showing Linux Kernel Sub Systems
 
Fig. 5: Image showing Linux Kernel Sub Systems
 
1) Process Management:
Process management subsystem interacts directly with all other subsystems of the kernel. Its purpose is to schedule and control various process. It is responsible for multiprocessing  or multitasking. Each process is broken down to units called “threads” and run those threads from various processes at a very high speed, so that the user feels like all the applications are running simultaneously.
 
2) Memory Management:
This subsystem is responsible for managing the physical memory. It provides functions like, memory mapping, shared virtual memory, swapping etc. Virtual memory is provided for the user applications by swapping in and out the applications with the RAM and swap space in Disk drive, so that the running application feel that it is having lot more memory available in RAM than it actually dose.
 
3) File system Management:
Linux supports various kinds of file systems. The responsibility of the subsystem is to manage the files. All the kernel modules interact with this subsystem. It also interacts with device drivers forming a virtual file called device file. Both the kernel and the hardware can read into or read from the device file. Thus it forms an interface between kernel and actual hardware.
 
4) Inter-Process Communication:
The Inter-Process Communication (IPC) subsystem provides a means, and controls the communication between various processes. The IPC is achieved through different mechanisms like Socket, Pipe, and Shared Memory etc.
 
5) Network Interface:
The network interface subsystem provides network access to the linux machine. It supports various protocols like TCP, UDP, IPV4, IPV6 etc. This subsystem directly interacts with network device driver.
 
6)  Device driver:
A device driver is a specific code written for accessing and controlling a particular kind of device. It forms a device file through which the user applications can access the device just like accessing a file. So the application developers can do their coding without need to know the hardware details. We generally don’t consider the code for accessing the CPU and Physical memory by the kernel as device drivers even though they also form the hardware interface part of an OS.  The device driver code alone forms the largest part of the kernel. The device driver subsystem is again divided into character driver, block driver, network driver etc.

Important codes for the subsystems

Where can we find important piece of codes for these subsystems?
 
/usr/src/kernels/linuxX.Y.Z/

 

 

DEVICE DRIVER ———————————————————————————————————–/drivers

 

Character device driver —————————————————-/drivers/char
Block device driver ———————————————————-/driver/block
Network device driver ——————————————————/driver/net
Buffer cache——————————————————————-/fs/buffer.c
 

 

FILE SYSTEM MANAGEMENT —————————————————————————————–/fs

 

Various kernel modules —————————————————-/modules
 

 

INTER PROCESS COMMUNICATION ———————————————————— ——————–/ipc

 

Shared memory IPC ————————————————————————————- |
Signal IPC ————————————————————————————————— |
System V IPC ———————————————————————————————–|—/ipc
Pipe IPC —————————————————————————————————– |
IPC manager ———————————————————————————————– |
Message queue manager ——————————————————————————-|
 

 

MEMORY MANAGEMENT ————————————————————————————————/mm

 

Shared virtual memory ————————————————————/mm/shmem.c
Memory mapper ——————————————————————–/mm/mmap.c
Swapper ——————————————————————————-/mm/swap.c
Demand pager ———————————————————————–/mm
Memory manager ——————————————————————-/mm
 

 

NETWORK MANAGEMENT ———————————————————————————————-/net

 

Network protocol devices ———————————————————————–|_ _ /net
Network device manager ————————————————————————|
 

 

PROCESS MANAGEMENT ———————————————————————————————–/kernel

 

Scheduler ——————————————————————————–/kernel/sched.c
Task queue ——————————————————————————/kernel
Architectural specific scheduler —————————————————-/arch/i386/kernel/process.c
 

Kernel Modules

 
Kernel modules:
Modules can be considered as parts of kernel code. A module never runs by itself, but can be loaded or removed into the kernel while the system is running. Once a module is loaded into the kernel, it behaves like a part of the actual kernel itself. The kernel modules are mainly used to load device drivers. Suppose we have plugged in a USB device, now the kernel will search for the appropriate driver module and load it immediately.
 
A kernel module basically has two necessary functions, ‘init_module ()’ and ‘cleanup_module ()’ which are linked to the ‘init’ and ‘exit’ respectively. When a module get inserted, the ‘init_module ()’ is executed and when the module get removed the ‘cleanup_module ()’ is executed.  The following picture gives an overview of the module loading and unloading details
 
Image showing Kernel Modules in Linux
 
Fig. 6: Image showing Kernel Modules in Linux

Hello World Kernel Module

Hello Word Kernel module:
Let’s try out a simple kernel program. As always we start with “hello world” here also.
We are going to edit a kernel module, ‘make’ it and insert it.
At first create a folder and name it. Make sure that the parent folders or the folder itself does not have white spaces in their folder name.  Open any text editor, VI or VIM and edit the following code. Save the code as “hello_world.c”.
 
#include <linux/module.h>
#include <linux/kernel.h>
int init_module (void)
{
        printk ( “nHELLO WORLDn” );            
        return 0;
}
 
void cleanup_module (void)
{
        printk ( “nGOOD BYEn” );
}
 
The ‘printk ()’ is the kernel equivalent of ‘printf ()’ in user-space programming.
 
Now we need to ‘make’ the code using gcc compiler and for that we use the “make” command. We need a file called “Makefile” in order to make the .c file in the same folder.
 
Create a file having the following contents and save it as “Makefile” for our hello_world.c
 
obj-m += hello_world.o
all:
            make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
            make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
 
Now open the terminal in that folder and type “make” and then enter.
If the make was successful then a number of files get generated in the folder. Among those files, there will be a ‘hello_world.ko’ file. The ‘.ko’ represents Kernel Object.
We can manually insert the kernel module using the command “insmod”. Type the following in the terminal and press enter.
 
insmod hello_world.ko
 
Now we know that the ‘init_module’ might have been executed while we inserted the module. Let’s see if the message has been printed or not. Type the following command in the terminal and press enter.
 
dmesg –c
 
You can see a lot of message printed so far by other modules and at the very bottom we can find our message also.
Ok, now you have your first kernel program executed successfully. If you want to see your module listed along with other modules, type the following command in the terminal and press enter.
 
lsmod
 
If you’ve done with your module, it should be removed so as to free the resource and to avoid unexpected errors. Type the following command in the terminal and press enter.
 
rmmod hello_world.ko
 
Now try ‘dmesg –c’ ones more to find the message printed by the ‘cleanup_module ()’. Try ‘lsmod’ also to check whether the module is removed from the list of loaded modules. 

Filed Under: Tutorials
Tagged With: computer, kernel, operating system
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • BOM sent to Contract assemblers doesnt correspond to schem
  • Half-bridge LLC resonant converter using UCC25600
  • Lightbox circuit help
  • Transistor circuit in audio
  • Antiparallel Schottky Diodes VDI-Load Pull

RSS Electro-Tech-Online.com Discussions

  • Kawai KDP 80 Electronic Piano Dead
  • An Update On Tarrifs
  • Trying to use a L9110s motor driver chip
  • I want to make a CRT with some modifications But i have no Idea where to start
  • Funny Images Thread!

Featured – Designing of Audio Amplifiers part 9 series

  • Basics of Audio Amplifier – 1/9
  • Designing 250 Milli Watt Audio Power Amplifier – 2/9
  • Designing 1 Watt Audio Power Amplifier – 3/9
  • Designing a Bass Boost Amplifier – 4/9
  • Designing a 6 Watt Car Audio Amplifier – 5/9
  • Design a low power amplifier for headphones- 6/9

Recent Articles

  • Fischer connector system adds ratchet locking system designed for 300g shock resistance
  • Littelfuse introduces tactile switch with enhanced bracket peg design for mounting strength
  • Infineon releases GaN switch with monolithic bidirectional design
  • Sienna Semiconductor data converters feature sample rates from 20 to 250 Msps
  • Delta’s 5,500 W power supplies achieve 97.5% energy efficiency for AI servers

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronics Projects and Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe