Core development and summary of Linux kernel Design Foundation (10)

Source: Internet
Author: User

(1) Linux hierarchy:



(2) The Linux kernel consists of:

It consists of 5 subsystems, such as process scheduling (SCHED), memory Management (MM), virtual file system (VFS), network Interface (NET), and interprocess communication (IPC).


(3) Differences with UNIX:

    • Linux supports dynamic loading of kernel modules
    • Supports symmetric multi-processing (SMP) mechanisms
    • The Linux kernel can preempt
    • The Linux kernel does not differentiate between threads and other general processes
    • Linux provides object-oriented device models with device classes, hot plug events, and device file systems for user space (SYSFS)

(4) Features of kernel development:
    • Kernel programming can neither access C libraries nor access standard C header files
    • The kernel programming must use the GNU C
    • Kernel programming lacks a memory protection mechanism like user space
    • Difficult to perform floating-point arithmetic during kernel programming
    • The kernel gives each process only a small fixed-length stack
    • Because the kernel supports asynchronous interrupts, preemption, and SMP, you must always be aware of synchronization and concurrency
    • To consider the importance of portability

(5) the preparation and operation of the module:
Hello World, an old friend of the programmer.
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h>/* * hello_init initialization function, When the module is loaded, it is called if the load successfully returns 0, * otherwise a non 0 value */static int hello_init (void) {PRINTK (Kern_alert "I bear a Charmed life.\n") is returned, and return 0; /* * Hello_exit Exit Function, called when the module is unloaded */static void hello_exit (void) {PRINTK (Kern_alert "Out, out, brief candle!\n");} Module_init (Hello_init); Module_exit (Hello_exit); Module_license ("GPL"); Module_author ("Qiushan"); Module_description ("A Hello, World Module");

This is the simplest kernel module, hello_init () is the module's entry point, which is registered to the system via Module_init () and is called when it is loaded. In addition, all module initialization functions must conform to the following form:
int my_init (void);

The following is an example of makefile:
Obj-m: = hello.omake-c/kernel/source/location subdirs= $PWD Modules

and then execute
sudo make Modules_installsudo insmod hello.ko//load sudo rmmod hello//Uninstall



(6) The migration should be aware of the alignment:
If the memory address of a variable is exactly the integer multiple of its length, it is naturally aligned. For example, for a 32-bit type of data, if its address in memory is exactly divisible by 4 (that is, the lowest two bits are 0), then it is naturally aligned. For RISC, loading unaligned data causes the processor to sink in.
Alignment principle:
    • For a standard data type, the address is aligned as long as it is an integer multiple of its length.
    • For arrays, all the elements that follow are naturally aligned as long as they are aligned to the underlying data type.
    • For a union, it is possible to align as long as it contains the largest data type of the length.
    • For a struct, as long as each element in the struct is correctly aligned.
for structs, an example is presented here:
struct Animal_struct {char dog;                   /* 1 byte */unsigned long cat;    /* 4 bytes */unsigned short pig;  /* 2 bytes */char Fox;                  /* 1 byte */};



This is not the correct alignment of each element. The compiler will actually make the following changes:
struct Animal_struct {char dog;                   /* 1 byte */u8 __pad0[3];            /* 3 bytes */unsigned long cat;     /* 4 bytes */unsigned short pig;   /* 2 bytes */char Fox;                    /* 1 byte */u8 __pad1;               /* 1 byte */};

The first padding __pad0 is to ensure that cat can be aligned in 4 byte, so that the other small objects are automatically aligned, the last fill __pad1 to fill the size of the struct itself, is the structure of the length can be divisible by 4, so that the structure is composed of an array, Each array item is naturally aligned.
struct Animal_struct {unsigned long cat;     /* 4 bytes */unsigned short pig;   /* 2 bytes */char dog;                    /* 1 byte */char Fox;                     /* 1 byte */};








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.