Learn Linux Kernel skills

Source: Internet
Author: User

Linux is becoming more and more popular in today's society. Do you know about Linux systems? This article introduces you to Linux Kernel. Linux Kernel has a good feature that supports expansion during running. This means that if the system starts running, we can still add features to Linux kernel. The code that can be added to the kernel during this operation is called the Module ).

Linux Kernel supports several module types, including device drivers. Each module consists of the target code and is not a complete executable program. During system running, we can use insmod to connect the module to the running kernel. You can also use lsmod to list loaded modules, rmmod or modprobe-r to remove them.

In Linux, devices are divided into three basic types: character devices, Block devices, and network interfaces.

Character devices are devices that can be accessed like byte streams. Generally, they can only be accessed sequentially. Its operations are similar to file operations.

Block devices can accommodate the file system and can be randomly accessed through the file system. Its operations are similar to file operations.

Network interfaces are responsible for transmitting and receiving data packets. Generally, they cannot be reflected on file system nodes. Communication with the kernel is different from the previous two devices, but through the socket method. A special data structure (sk_buff) is defined between the system and the driver for data transmission. The system supports the caching of sent and received data, provides traffic control mechanisms, and supports multiple protocols.

When writing a module, you should note that the module is only connected to the kernel, so it can only call the functions exported by the kernel, rather than other functions undefined by this module.

For module development in Linux kernel2.6.X, you must prepare the "kernel tree" in advance to obtain the source code of the same kernel as the system and compile the target file.

A simple hello world Driver example:

 
 
  1. hello_world.c:     
  2. #include <linux/init.h>    
  3. #include <linux/module.h>    
  4. MODULE_LICENSE("Dual BSD/GPL");     
  5. static int hello_init(void)     
  6. {     
  7. printk(KERN_ALERT "Hello, world\n");     
  8. return 0;     
  9. }     
  10. static void hello_exit(void)     
  11. {     
  12. printk(KERN_ALERT "Goodbye, cruel world\n");     
  13. }     
  14. module_init(hello_init);     
  15. module_exit(hello_exit);   
 
 
  1. Makefile:  
  2. obj-m := hello.o  
  3. KERNELDIR ?= /lib/modules/$(shell uname -r)/build  
  4. PWD := $(shell pwd)  
  5. default:  
  6. $(MAKE) -C $(KERNELDIR) M=$(PWD) modules 

In the source file, module_init and module_exit specify the initialization function and the cleanup function executed when the module is loaded. In addition, you can use module_param to specify the parameters that can be set during module loading. Obj-m in Makefile specifies the *. o target file used to construct the *. ko target file when you use make modules.

Linux Kernel helps you learn about Linux.

  1. How to install Linux
  2. Introduction to embedded Linux drivers
  3. 20 years of experience: Linux embedded
  4. 10 of the best free Linux ERP software
  5. Fully interpreting embedded Linux

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.