Kthreadd-linux, Process 2nd.

Source: Internet
Author: User


Reference:



1. Common Linux processes and kernel threads



2. management and scheduling of the Kthreadd--linux process for Linux under process 2nd (vii)


Kthreadd: 

This kernel thread has only one function, which is to manage the dispatch of other kernel threads. It is created when the kernel is initialized and loops through a function called Kthreadd, which is the function of running the Kthread maintained in the Kthread_create_list global list. You can call Kthread_create to create a kthread that will be added to the Kthread_create_list list while kthread_create weak up kthreadd_task. Kthreadd in execution Kthread will call the old interface--kernel_thread run a kernel thread called "Kthread" to run the Kthread created, the Kthread will be executed from Kthread_create_ The list list is deleted, and Kthreadd will constantly call scheduler to yield the CPU. This thread cannot be closed.



At the end of the Linux-initiated C-phase start_kernel (), Rest_init () will open two processes: Kernel_init,kthreadd, then the main thread becomes the idle thread, init/main.c.



3 Special processes under Linux: The Idle Process (pid=0), the init process (pid=1), and Kthreadd (pid=2).



* The idle process is created automatically by the system, running in the kernel state
The pid=0 of the idle process, its predecessor is the first process created by the system, and the only process that has not been generated by fork or kernel_thread. After the system is loaded, it evolves into a process scheduling and switching.



* The init process was created by idle via Kernel_thread, and after the kernel space is initialized, the INIT program is loaded and the end user space runs
Created by the 0 process to complete the initialization of the system. Is the ancestor process of all other user processes in the system.
All processes in Linux are created and run by the Init process. Start the Linux kernel, start the init process in user space, and start other system processes. After the system boot completes, Init becomes the daemon monitoring system other processes.



* Kthreadd process created by idle through Kernel_thread and always runs in kernel space, responsible for all kernel thread scheduling and management
Its task is to manage and dispatch other kernel thread kernel_thread, which will loop through a kthreadd function, which is to run kthread_create_list the Kthread maintained in the global list, when we call Kernel_ Kernel threads created by thread are added to this list, so all kernel threads are directly or indirectly kthreadd to the parent process.


 
static __initdata DECLARE_COMPLETION(kthreadd_done);

static noinline void __init_refok rest_init(void)
{
    int pid;

    rcu_scheduler_starting();
    /*  
     * We need to spawn init first so that it obtains pid 1, however
     * the init task will end up wanting to create kthreads, which, if
     * we schedule it before we create kthreadd, will OOPS.
     */
    kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);
    numa_default_policy();
    pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
    rcu_read_lock();
    kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
    rcu_read_unlock();
    complete(&kthreadd_done);

    /*
     * The boot idle thread must execute schedule()
     * at least once to get things moving:
     */
    init_idle_bootup_task(current);
    preempt_enable_no_resched();
    schedule();

    /* Call into cpu_idle with preempt disabled */
    preempt_disable();
    cpu_idle();
}





Kthreadd-linux, Process 2nd.


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.