Linux kernel thread (daemons)

Source: Internet
Author: User

Kernel threads are processes that are initiated directly by the kernel itself. Kernel threads are actually delegating kernel functions to stand-alone processes, executing "parallel" with other processes in the system (in fact, parallel to the kernel itself), which are often referred to as kernel "daemons". They are primarily used to perform the following tasks:

L Periodically synchronizes modified memory pages with page source block devices.

L write to swap if memory pages are seldom used.

L Manage Delay action

L Implement the transaction log for the file system.

There are two main types of kernel threads:

1. The thread waits until it is started, until the kernel requests the thread to perform a specific operation.

2. Threads start and run at periodic intervals, detecting the use of specific resources and taking action when the usage exceeds or falls below the preset limit.

Kernel threads are generated by the kernel itself, which is characterized by:

1. They are executed in the CPU's pipe state, not the user state.

2. They can access only the kernel portion of the virtual address space (all addresses above task_size), but cannot access the user space.

The Task_struct process descriptor contains two fields related to the process address space mm, active_mm, for normal user processes, mm points to the user space portion of the virtual address space, and for kernel threads, MM is null.

ACTIVE_MM is primarily used for optimization, because kernel threads are not associated with any particular user-level process, and the kernel does not need to be switched to the user layer portion of the virtual address space, preserving the old settings. Because the kernel thread may be any user layer process in the execution, so the content of the user space is essentially random, kernel threads must not modify its contents, the MM is set to NULL, and if the switch out of the user process, the kernel will be the original process of the mm stored in the new kernel thread active_mm. If the kernel thread runs after a process that is the same as before, the kernel does not need to modify the user-space Address table, and the information in the TLB is still valid, only the processes executed after the kernel thread are not the same as the previous user-level process, and the corresponding TLB data is purged.

Kernel threads can be implemented in two ways:

1. Passing a function to Kernel_thread, which is then responsible for helping the kernel call Daemonize has been converted to a daemon, includes the following actions:

L This function frees all the resources of its parent process, otherwise the resources will remain locked until the thread ends.

L block the reception of the signal.

L use Init as the parent process of the daemon.

2. The more common method of creating a kernel is the auxiliary function kthread_create, which creates a new kernel thread. The initial thread is stopped and needs to be started with wake_up_process. Or using Kthread_run, unlike Kthread_create, it wakes up immediately after it creates a new thread.

View kernel thread information running in the system via the PS Fax command.

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.