What is a thread
Linux thread is a special kind of process, with its own task_struct, the kernel does not have special scheduling algorithm and data structure to characterize the thread, but only as a common process, but only with other processes to share the process space. That is, if the program is running in a multithreaded environment, the program must be written to check the project: whether it uses a system function that is not reentrant, such as a string split function:: Strtok (), whether a global variable or static variable has been lock, and whether the third Third-party supports multithreading.
Note: To get the thread ID from the core, you should use Current->pid. In the nuclear heart PID is the process number for the process, and for the thread is the thread number. The PID of the different threads of the same process is different, but the different threads of the same process have uniform tgid, so such system calls like Getpid (), Kill (), are returned with tgid values.
Scheduling of Threads
If the system has only one CPU, it is impossible to actually do more than one thread at a time, it can only divide the CPU running time into a number of time slices, and then allocate the time slice to each thread execution, in a time slice of the thread code run, the other thread is in a suspended state, this way called concurrency ( Concurrent).
Because I/O operation speed is far slower than CPU processing speed, key mouse input, hard disk read and write, network transmission, such as speed is much slower than memory read and write speed, and memory speed is much slower than the CPU cache speed, so effective use of I/O operation Gap CPU, become the early single nuclear concurrency mechanism In concurrent environments, CPU to "suspend-> execution-> suspend" asynchronous way to run each thread in a very small time slice, not only allow each thread to get reasonable CPU resources, to avoid waiting for slow processing of devices, but also to the user with each thread is running the illusion. In this environment, multithreaded programs really improve the system's response performance and program friendliness.
Kernel thread
Kernel thread is a standard process that runs independently in kernel space, which differs from ordinary processes in that the kernel thread has no independent address space, and its code snippets and data are in kernel space, but can be scheduled and preempted.
A kernel thread can create a new kernel thread. In general, the kernel thread will always execute the function it created at creation time, which is usually composed of a loop (kernel thread 0). When needed, the kernel thread is awakened and executed, and it automatically sleeps after the task is completed.
Note: The kernel can be imagined as a collection with many kernel threads running at the same time in kernel space.
Kernel thread 0
Kernel thread 0 is a kernel thread created from scratch by the Start_kernel () function in the system initialization phase, which includes the process descriptor, the kernel stack, MM, FS, files, signals, and so on. Kernel thread 0 The final initialization is to create the init kernel thread, then run Cpu_idle and thread 0 as the idle thread.
Process 1
When the dispatcher chooses to the init thread, the init thread begins executing the init () function. The init () function finally calls the EXECVE () system call to mount the executable program init. Since then, the init kernel thread has become an ordinary process, the init process, with a process number of 1, which is the parent process for all other user processes. The init process never terminates because it creates and monitors the activity of all processes outside the operating system.