What is thread
A Linux thread is a special type of process with its own task_struct. The kernel does not have a special scheduling algorithm or data structure to characterize the thread. Instead, it is just a common process, only process space is shared with other processes. That is to say, if the program runs in a multi-threaded environment, you must check the project when writing the program:
- Whether a non-reentrant system function is used, such as the string segmentation function: strtok ();
- Whether global or static variables have been locked;
- Whether the Third-Party Library supports multithreading.
Note: To obtain the thread ID from the core, use current-> PID. In the core, PID indicates the process number and thread number. The PID of different threads of the same process is different, but different threads of the same process have a uniform tgid. Therefore, for example, getpid (), kill () and so on.
Thread Scheduling
If the system has only one CPU, it is impossible to actually execute more than one thread at the same time. It can only divide the CPU running time into several time slices, and then allocate the time slice to each thread for execution, when the thread code of a time slice is running, other threads are suspended. This method is called concurrent ).
Because the I/O operation speed is far slower than the CPU processing speed, keymouse input, hard disk read/write, network transmission, and other speeds are far slower than the memory read/write speed, and the memory speed is far slower than the CPU cache speed, therefore, the effective use of the CPU in the I/O operation gap has become the reason for the early single-core concurrency mechanism. In a concurrent environment, the CPU runs each thread separately in a small time slice in an asynchronous mode of "suspend-> execute-> suspend", so that each thread can obtain reasonable CPU resources, this prevents devices that are waiting for processing speed to be slow and gives users the illusion that every thread is running. In this environment, multi-threaded programs actually improve the system response performance and program friendliness.
Kernel thread
A kernel thread is a standard process that runs independently in the kernel space. The difference between a kernel thread and a common process is that the kernel thread does not have an independent address space, and its code segments and data are all in the kernel space, but it can be scheduled and preemptible.
A new kernel thread can be created. Generally, the kernel thread executes the function obtained during creation forever. This function is usually composed of a loop (kernel Thread 0 ). When necessary, the kernel thread is awakened and executed. After the task is completed, it will automatically sleep.
Note: we can think of the kernel as a collection of many kernel threads running simultaneously in the kernel space.
Kernel Thread 0
Kernel Thread 0 is a kernel thread created by the start_kernel () function from none in the system initialization phase, the created content includes process descriptor, kernel state stack, mm, FS, files, and signals. The final initialization of kernel Thread 0 is to create the init kernel thread, and then run cpu_idle. Thread 0 becomes the idle thread.
Process 1
When the scheduler selects the init thread, the init thread starts to execute the init () function. The init () function finally calls execve () System Call to mount the executable program init. Since then, the init kernel thread has become a common process, that is, the INIT process. The process number is 1, which is the parent process of all other user processes. The INIT process never stops because it creates and monitors the activity of all processes outside the operating system.