Kernel notes-kernel compilation and process management

Source: Internet
Author: User

Kernel and operating system

Because of the flaws in the design of some commercial operating systems and the increasing complexity of the system, the concept of "operating system" has become ambiguous for many people. Before we further discuss the topic of the Linux kernel, we first distinguish between the concepts of "kernel" and "operating system".

    • operating System : the part that completes the most basic functions and systems management throughout the system, including kernels, device drivers, file management tools, system Administration tools, shell command lines, or other user interfaces (GNOME/KDE, etc.)
    • kernel : Is the core of the operating system, complete process management, CPU scheduling, memory management, interrupt processing and other functions

Generally we write applications, running on the operating system, complete text editing, music playback, web browsing and other specific features.

Kernel compilation

Kernel source code is generally placed in the/USR/SRC directory, we can also get the required kernel version of the source package. The first step in compiling the kernel is to configure the kernel features, such as whether the configuration supports symmetric multiprocessor (SMP), by setting the value of the CONFIG_SMP.

Usually we use the "Make menuconfig" command to configure it, which provides a friendly configuration interface:

 

After the configuration is saved, the. config profile is generated under the source directory, and the file is opened and you can see its contents set for various options:

    1. Config_x86_64=y
    2. Config_64bit=y
    3. Config_x86=y
    4. Config_semaphore_sleepers=y
    5. Config_mmu=y
    6. ......

We can also use the current kernel configuration to quickly generate a. config file using the following command:

    1. zcat/proc/config.gz >. config

The source code is then compiled according to the. config configuration:

    1. Make-j4

The above uses the-J option to specify the number of concurrent compilation tasks, which reduces compilation time in multi-core environments.

Generate kernel compression image after compilation:

    1. Make Bzimage

The resulting kernel compressed image file is located in the Arch/x86/boot directory:

    1. linux-2.6.32.59 # ll Arch/x86/boot/bzimage
    2. -rw-r--r--1 root root 2814112 07-02 22:27 arch/x86/boot/bzimage

Then install the kernel module:

    1. Make Modules_install

The new module will be placed in the/lib/modules directory:

    1. /lib/modules # LL
    2. Total 8
    3. Drwxr-xr-x 4 root root 4096 03-08 23:53 2.6.32.12-0.7-default
    4. Drwxr-xr-x 3 root root 4096 07-02 23:31 2.6.32.59-0.7-default

Finally execute the make install kernel, and the System.map, Vmlinuz, and INITRD files will be generated in the/boot directory:

    1. linux-2.6.32.59 # Make Install
    2. sh/home/lx/kernel/linux-2.6.32.59/arch/x86/boot/install.sh 2.6.32.59-0.7-default arch/x86/boot/bzImage \
    3. System.map "/Boot"
    4. Kernel Image:/boot/vmlinuz-2.6.32.59-0.7-default
    5. INITRD Image:/boot/initrd-2.6.32.59-0.7-default
    6. ......

After the installation is complete, the corresponding startup item of the new kernel is added to the/boot/grub/menu.lst file, and we can modify the file to specify that the system is started using the newly translated kernel.

Processes and Threads

Under Linux, the biggest difference between a process and a thread is that the process has a separate memory address space, and the thread shares the memory address space with other threads. In addition, the process and the implementation of the thread are basically the same, there are task_struct structure, are assigned PID.

Kernel threads do not have separate address spaces, they do specific work and accept the dispatch of the kernel, unlike normal user processes, which do not receive signals sent by the KILL command:

    1. F S UID PID PPID C PRI NI ADDR SZ wchan stime TTY time CMD
    2. 1 S Root 2 1 0-40--0 migrat Jul01? 00:00:00 [migration/0]
    3. 1 S Root 3 1 0 94 19-0 ksofti Jul01? 00:00:00 [ksoftirqd/0]
    4. 5 S Root 1 0 70-5-0 worker Jul01? 00:00:00 [events/0]
    5. ......

Task_struct

The TASK_STRUCT structure contains information such as the virtual memory used by the process, the open file, the process state, the process PID, and the memory that is allocated by slab and defined in the file. The first field of the THREAD_INFO structure is a pointer to the Task_struct type, and when the process is created, Thread_info is stored at the top of the process kernel stack:

The current global variable points to the TASK_STRUCT structure of the currently running process, because the position of the thread_info is fixed, so that we can easily calculate the value of values by the following assembly instructions:

    1. MOVL $-8192,%eax
    2. Andl $esp,%eax

Process status

The process can be in several states:

    1. RUNNING
    2. Interruptable
    3. Uninterruptable
    4. STOP
    5. ZOMBIE

These process states are defined as macros in the Sched.h file.

    • The running state indicates that the process is executable or is executing or running in a queue that consumes or waits for CPU resources.
    • After the process calls Exit function exit, the process aborts and enters the zombie state. In the zombie state, the process does not consume the CPU, but because its task_struct structure is not released, it consumes a bit of memory until the parent process calls the wait function to accept the child process's last wish, and if the parent process exits before the child process, the INIT process accepts the child process's wish. If a process is in the zombie state for a long time, then wait is not called in the parent process, encoding the problem for the program.
    • The Uninterruptable status indicates that the process is non-interruptible, the process in this state is in the kernel state, and no signal is received.

The function that sets the state of the process is the Set_task_state function, which is defined in the file.

Inter-process relationships

The inter-process relationship is the same as the directory structure, which is a tree structure with the directory structure as root, and the process relationship with Init as the root. We can use Pstree to view inter-process relationships:

    1. Linux-14:~ # echo $$
    2. 10939
    3. linux-14:~ # PSTREE-G-P 10939
    4. Bash (10939) ─┬─pstree (12806)
    5. └─sh (12796) ───sleep (12801)

A two-way closed-loop list is provided in the kernel code, and since the Init process, the list is connected to the TASK_STRUCT structure of all processes and can be traversed by the For_each_process macro for all processes of the system:

    1. #define FOR_EACH_PROCESS (P) \
    2. for (p = &init_task; (p = next_task (p))! = &init_task; )

Process creation

Linux kernel divides the process creation into two steps: fork and exec. Fork generates the PID of the child process, copies the contents of the parent Process execution context, open file descriptor, and so on to the child process; Exec loads the child process's own execution context into the memory address space. With the following fork example, how many 1 will the output of the program be executed?

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. int main ()
  4. {
  5. int i;
  6. for (i=0; i <; i++)
  7. {
  8. Fork ();
  9. }
  10. printf ("%d\n", 1);
  11. return 0;
  12. }

Fork Copy the contents of the parent process to the child process, the cost is large, if called after the fork call exec, the child process to load their own execution file, the copy of the action is superfluous. Copy-on-write (Copy-on-write,cow) solves the problem of unnecessary overhead of copying and triggers the copy action when the child process writes the parent process address space. It is one of the reasons why Linux kernel is efficient not to do extra things, but to finish the work when it has to.

The Do_fork function in the kernel completes the work of the fork call, Do_fork calls Copy_process. The Copy_process function mainly accomplishes the following tasks:

    1. Call the Dup_task_struct function to request the TASK_STRUCT, thread_info structure of the new process
    2. According to the CLONE_FLAGS flag, call Copy_files, Copy_fs, copy_mm and other functions to complete the file, file system, memory and other information copies
    3. Call Alloc_pid to request a new process PID

Fork returns two times, implemented in the Do_fork function.

Process Abort

The final process calls the Exit function to abort, and the exit system call eventually calls the kernel's do_exit function, which is defined in Do_exit to do the following:

    1. Call Exit_signals Set process flags flag to pf_exiting
    2. Call exit_mm, Exit_files, EXIT_FS and other functions to release the structure of the process memory, file, file system, etc.
    3. Set the Exit_code of a process
    4. Call exit_notify, send a signal to the parent process, child process of the current process, tell the current process to abort, and set the current process exit status Exit_state to Exit_dead or Exit_zombie
    5. Call schedule, switch to another process, from the Do_exit function does not return to the function that called it

Reference:chapter 1 to Chapter 3, Linux kernel development.3rd.edition

Kernel notes-kernel compilation and process management

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.