Linux Kernel
Linux Kernel
#linux-
kernel
Table of Contents
About 1
Remarks 2
Versions 2
Examples 2
Installation or Setup 2
Introduction 4
Examples 4
Examples 6
Examples 7
Introduction 9
Examples 9
Find the "likely" maintainers for the FTDI USB serial converter 9
Examples 10
Examples 12
It is an unofficial and free linux-kernel ebook created for educational purposes. All the content is
extracted from Stack Overflow Documentation, which is written by many hardworking individuals at
Stack Overflow. It is neither affiliated with Stack Overflow nor official linux-kernel.
The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to info@zzzprojects.com
https://riptutorial.com/ 1
Chapter 1: Getting started with linux-kernel
Remarks
This section provides an overview of what linux-kernel is, and why a developer might want to use
it.
It should also mention any large subjects within linux-kernel, and link out to the related topics.
Since the Documentation for linux-kernel is new, you may need to create initial versions of those
related topics.
Versions
4.4 2016-01-10
4.1 2015-06-21
3.18 2014-12-07
3.16 2014-08-03
3.12 2013-11-03
3.10 2013-06-30
3.4 2012-05-20
3.2 2012-01-04
Examples
Installation or Setup
https://riptutorial.com/ 2
wget http://www.kernel.org/pub/linux/kernel/v4.7/linux-4.7.tar.gz
tar zxvf linux-4.7.tar.gz
cd linux-4.7
make menuconfig will select the features required for the kernel. Old kernel configurations can be
copied by using old .config file and executing make oldconfig. Also we can use make xconfig as a
graphical version of the configuration tool.
Alternatively if you want to reconfigure the old kernel and re compile it, execute the below
commands:
make mrproper
make menuconfig
make dep
make clean
make bzImage
make modules
make modules_install
image = /boot/vmlinuz-4.7
label = "Linux 4.7"
https://riptutorial.com/ 3
Chapter 2: Creation and usage of Kernel
Threads
Introduction
This topic discusses how to create and use kernel threads.
Examples
Creation of kernel threads
kern_thread.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/kthread.h>
#include <linux/sched.h>
https://riptutorial.com/ 4
ts1 = kthread_run(kthread_func, NULL, "thread-1");
if (IS_ERR(ts1)) {
printk(KERN_INFO "ERROR: Cannot create thread ts1\n");
err = PTR_ERR(ts1);
ts1 = NULL;
return err;
}
module_init(init_func);
module_exit(exit_func);
MODULE_AUTHOR(AUTHOR);
MODULE_DESCRIPTION(MODULE_AUTHOR);
MODULE_LICENSE("GPL");
Makefile
obj-m += kern_thread.o
all:
make -C /lib/module/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/module/$(shell uname -r)/build M=$(PWD) clean
Starting 2 threads
I am thread: thread-1[PID = 6786]
I am thread: insmod[PID = 6785]
I am thread: thread-2[PID = 6788]
https://riptutorial.com/ 5
Chapter 3: Event Tracing
Examples
Tracing I2C Events
If not, try:
cd /sys/kernel/debug/tracing/
The trace events user-space API documentation can be found in the file
Documentation/trace/events.txt of the kernel source.
https://riptutorial.com/ 6
Chapter 4: Fork System call
Examples
fork() system call
fork() is a system call. fork is used to create a child process from the running process, which is a
replica of the parent process(Process which executed fork() ). Child process is derived from the
parent process. Both the parent and child have different address space, each is independent of
the changes made to the variables.
The child process has its own PID(process identification). PPID(Parent Process ID) of child
process is same as PID of parent process.
Format:
On successful creation of child process the pid of the child process is returned to the parent
process and 0 is returned in the child process. On Failure return -1 with no process created.
Usage example:
#include <stdio.h>
#include <unistd.h>
void child_process();
void parent_process();
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
child_process();
else
parent_process();
return 0;
}
void child_process()
{
printf("Child process with PID : %d and PPID : %d ", getpid(),getppid());
}
void parent_process()
https://riptutorial.com/ 7
{
printf("Parent process with PID : %d", getpid());
}
The sequence of the printf statements from the child and parent depend on the scheduling
mechanism which purely depends on the system.
https://riptutorial.com/ 8
Chapter 5: How to find the right person for
help.
Introduction
This should mirror some of the official Linux kernel docs, and post links to the latest versions of
said documents in tovalds/linux on GitHub.com. The idea is to encourage individuals to make use
of the MAINTAINERS files, linux-kernel mailing list, git log, and scripts/get-maintainer, so that they
are familiar with the commonly-used ways of identifying a key point of contact.
Examples
Find the "likely" maintainers for the FTDI USB serial converter
First, determine the source file for this particular driver. Found it at drivers/usb/serial/ftdi_sio.c.
./scripts/get_maintainer.pl drivers/usb/serial/ftdi_sio.c
Now we know who to ping for help with this particular driver, and which e-mail addresses should
be CC'ed when submitting a patch against this driver.
Read How to find the right person for help. online: https://riptutorial.com/linux-
kernel/topic/10056/how-to-find-the-right-person-for-help-
https://riptutorial.com/ 9
Chapter 6: Linux Hello World Device driver
Examples
An empty kernel module
#include <linux/init.h>
#include <linux/module.h>
/**
* This function is called when the module is first loaded.
*/
static int __init hello_kernel_init(void)
{
printk("Hello, World!\n");
return 0;
}
/**
* This function is called when is called if and when the module is unloaded.
*/
static void __exit hello_kernel_exit(void)
{
printk("Goodbye, cruel world...\n");
}
/* The names of the init/exit functions are arbitrary, and they are bound using the following
macro definitions */
module_init(hello_kernel_init);
module_exit(hello_kernel_exit);
By itself, the kernel module does nothing; it has no meaningful way to communicate with the
userspace. Using the entry point it is possible to create a new character-device, for example,
which is then used to communicate with the userspace.
In order to compile the driver, it is necessary to have the Linux Kernel source tree.
Assuming the sources are at /lib/modules/<kernel-version>, the following Makefile will compile the
file driver.c into the driver.ko Kernel Object
obj-m := driver.o
KDIR := /lib/modules/$(shell uname -r)/build/
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
https://riptutorial.com/ 10
Notice how this Makefile calls make in the build directory of the Kernel.
When the compilation step finishes successfully, the src directory of the driver will look somewhat
like this:
In order to "run" the module, it is necessary to insert into the running kernel:
$ insmod driver.ko
$ dmesg | tail -n 1
[133790.762185] Hello, World!
$ rmmod driver.ko
$ dmesg | tail -n 1
[133790.762185] Goodbye, cruel world...
https://riptutorial.com/ 11
Chapter 7: Linux: Named Pipes(FIFO)
Examples
What is Named Pipe (FIFO)
A named pipe is really just a special kind of file (a FIFO file) on the local hard drive.
Unlike a regular file, a FIFO file does not contain any user information. Instead, it allows
two or more processes to communicate with each other by reading/writing to/from this file.
A named pipe works much like a regular pipe, but does have some noticeable differences.
The easiest way to create a FIFO file is to use the mkfifo command. This command is part of
the standard Linux utilities and can simply be typed at the command prompt of your shell. You
may also use the mknod command to accomplish the same thing.
https://riptutorial.com/ 12
Credits
S.
Chapters Contributors
No
Getting started with Community, EsmaeelE, Marek Skiba, Matt, Tejus Prasad, vinay
1
linux-kernel hunachyal
Linux: Named
7 chait
Pipes(FIFO)
https://riptutorial.com/ 13