What is a thread

What is a thread? What is the relationship between threads and processes? This is a very abstract question, but also a very broad topic, involving a lot of knowledge. I can’t be sure that I can say what it says, and I can’t be sure that EVERYTHING I say is correct. Even so, I hope to make it as simple and clear as possible, because this is a complicated and confusing field of knowledge that has puzzled me for a long time, and I hope to uncover its mysterious veil one by one through my understanding.

Task scheduling

What is a thread? To understand this concept, it is necessary to understand some operating system concepts.

Most operating systems (such as Windows and Linux) employ the preemptive scheduling mode of time slice rotation. That is, after a task is executed for a short period of time, it is forced to suspend to execute the next task. Each task is executed in turn. A short period of time for task execution is called time slice, and the state during task execution is called running state. After a period of time, the task is forced to suspend to execute the next task, and the suspended task is in the ready state waiting for the arrival of the next time slice.

So that each task can be performed, because the CPU execution efficiency is very high, time is very short, to quickly switch between the various tasks, in the sense that gives a person is more tasks “to” at the same time, this is what we call the concurrent (don’t think how advanced concurrency, its implementation is very complex, but the concept is very simple, is a word: Multiple tasks simultaneously). The multi-task operation process is shown as follows:

Figure 1: Task scheduling in an operating system

process

We all know that the core of a computer is the CPU, which undertakes all the computing tasks; The operating system is the manager of the computer, it is responsible for task scheduling, resource allocation and management, command the entire computer hardware; The application side is the program that has a certain function, and the program is running on the operating system.

Process is a dynamic execution process of a program with certain independent functions in a data set. It is an independent unit of the operating system for resource allocation and scheduling. It is the carrier of application program operation.

Process is an abstract concept and there has never been a unified standard definition. Process is generally composed of three parts: program, data set and process control block.

Program is used to describe the process to complete the function, is the control process execution instruction set; The data set is the data and workspace required by the program during execution. Program Control Block (PCB), which contains the description and Control information of a process, is the unique symbol of the existence of a process.

Characteristics of a process:

Dynamic: process is an execution process of the program, which is temporary and has a life span, dynamic generation and dynamic extinction.

Concurrency: Any process can execute concurrently with other processes.

Independence: a process is an independent unit of the system for resource allocation and scheduling.

Structure: process consists of program, data and process control block.

thread

In early operating systems, there was no concept of a thread. A process was the smallest unit that could own resources and run independently, and the smallest unit of program execution.

Task scheduling adopts the preemptive scheduling method of time slice rotation, and process is the smallest unit of task scheduling. Each process has its own independent piece of memory, so that the memory address of each process is isolated from each other.

Later, with the development of the computer, the CPU requirements are higher and higher, the switching overhead between processes is larger, has been unable to meet the requirements of more and more complex programs. Thus, thread was invented. Thread is a single sequence control flow in program execution, the smallest unit of program execution flow, and the basic unit of processor scheduling and dispatching.

A process can have one or more threads that share the memory space of the program (that is, the memory space of the process in which it is running). A standard thread consists of a thread ID, the current instruction pointer (PC), registers, and a stack. A process consists of memory space (code, data, process space, open files) and one or more threads.

The difference between processes and threads

I talked about processes and threads, but you might be confused by how similar they are. Processes and threads have a lot to do with each other. Let’s take a look at them:

1. Thread is the smallest unit of program execution, and process is the smallest unit of resources allocated by the operating system;

2. A process is composed of one or more threads, threads are different lines of code execution in a process;

3. The processes are independent from each other, but each thread under the same process shares the program’s memory space (including code segments, data sets, heap, etc.) and some process-level resources (such as open files and signals). Threads in one process are invisible to other processes.

4. Scheduling and switching: Thread context switching is much faster than process context switching.

Diagram of thread and process relationship:

Figure 2: Resource sharing relationship between process and thread

Figure 3: Single-thread vs. multi-thread relationship

In short, threads and processes are both abstractions. Threads are a smaller abstraction than processes, and both can be used to achieve concurrency.

In early operating systems, there was no concept of a thread. A process was the smallest unit that could own resources and run independently, and the smallest unit of program execution. It is equivalent to only one thread in a process, the process itself is the thread.

So threads are sometimes called Lightweight processes (LWP).

Figure 4: Early operating systems had processes, not threads

Later, as computers became more efficient at switching contexts between multiple tasks, a smaller concept was abstracted out — threads, which typically have multiple (but one) threads per process.

Figure 5: The emergence of threads makes it possible for a process to have multiple threads

Multithreading versus multicore

The time-slice rotation schedule mentioned above says that a task is executed for a short period of time and then forced to pause for the next task, each task is executed in turn. Many operating system books say that “only one task is executing at a time.” One might ask what about dual-core processors? Don’t the two cores run simultaneously?

The phrase “only one task at a time” is inaccurate, or at least incomplete. How does a thread execute in the case of a multi-core processor? This requires an understanding of kernel threads.

Multi-core (core) processors are the integration of multiple computing cores on a single processor to improve computing power, that is, there are multiple truly parallel computing processing cores, each corresponding to a kernel thread.

Kernel threads (KLT) are threads directly supported by the operating system Kernel. This Thread is switched by the Kernel, which schedules the threads through the operation scheduler and maps the tasks of the threads to each processor. Generally, one processing core corresponds to one kernel thread. For example, a single-core processor corresponds to one kernel thread, a dual-core processor to two kernel threads, and a quad-core processor to four kernel threads.

Now the computer is generally dual-core four threads, four cores eight threads, is the use of hyperthreading technology to simulate a physical processing core into two logical processing cores, corresponding to two kernel threads, so the number of CPU seen in the operating system is twice the number of actual physical CPU, such as your computer is dual-core four threads, Open Task Manager Performance to see a 4-CPU monitor, and quad core 8-thread to see an 8-CPU monitor.

 

Figure 6: dual-core four-thread view results under Windows8

Hyperthreading technology is to use special hardware instructions to simulate a physical chip into two logic processing cores, so that a single processor can use thread-level parallel computing, and then compatible with multi-threaded operating system and software, reduce the IDLE time of CPU, improve the efficiency of CPU operation.

This hyper-threading technique (such as dual-core quad threading) is dictated by the processor hardware and requires the support of the operating system to manifest itself in the computer.

Instead of using kernel threads directly, programs use an advanced interface of kernel threads called light-weight processes (LWP). Lightweight processes are what we normally call threads (we call them user threads here). Since each lightweight Process is supported by a kernel thread, So you can’t have lightweight processes until kernel threads are supported first.

There are three models for the corresponding relationship between user threads and kernel threads: one-to-one model, many-to-one model and many-to-many model. Here, four kernel threads and three user threads are taken as examples to illustrate the three models.

One-to-one model

For the one-to-one model, a user thread uniquely corresponds to a kernel thread (the reverse is not necessarily true; a kernel thread does not necessarily have a corresponding user thread). Thus, if the CPU does not employ hyperthreading technology (such as a four-core, four-threaded computer), a user thread uniquely maps to a physical CPU thread, and the concurrency between threads is true concurrency. The one-to-one model gives user threads the same advantages as kernel threads. If one thread is blocked for some reason, the execution of other threads is not affected. Here, too, the one-to-one model allows multithreaded programs to perform better on multiprocessor systems.

However, the one-to-one model also has two disadvantages: 1. Many operating systems limit the number of kernel threads, so the one-to-one model limits the number of user threads; 2. In many operating system kernel thread scheduling, the overhead of context switch is high, resulting in the execution efficiency of user thread is reduced.

 

Figure 7: One-to-one model

Many-to-one model

The many-to-one model maps multiple user threads to a kernel thread, and the switching between threads is carried out by user-mode code, so the switching speed of many-to-one model is much faster than that of one-to-one model. In addition, the many-to-one model has an almost unlimited number of user threads. However, the many-to-one model also has two disadvantages: 1. If one of the user threads is blocked, all the other threads cannot execute because the kernel thread is blocked. 2. On a multiprocessor system, an increase in the number of processors does not significantly increase the threading performance of the many-to-one model, because all user threads are mapped to a single processor.

 

Figure 8: Many-to-one model

Many-to-many model

The many-to-many model combines the advantages of the one-to-one and many-to-one models to map multiple user threads to multiple kernel threads. The advantages of the many-to-many model are as follows: 1. The blocking of one user thread does not cause all threads to block, because other kernel threads are scheduled to execute. 2. The many-to-many model does not limit the number of user threads; 3. In a multi-processor operating system, many-to-many threads can also get a certain performance improvement, but the improvement is not as high as that of the one-to-one model.

In today’s popular operating systems, most use the many-to-many model.

 

Figure 9: Many-to-many model

View processes and threads

An application can be multithreaded or multiprocessed. how do I check? On Windows we can view the number of processes and threads in an application simply by opening the task Manager. Press Ctrl+Alt+Del or right-click the toolbar to open the Task Manager.

Check the number of processes and threads:

Figure 10: View the number of threads and processes

Under the Processes TAB, we can see the number of threads an application contains. If an application has multiple processes, we can see that each process, as in the image above, has multiple processes in Google’s Chrome browser. Also, if you open multiple instances of an application, there will be multiple processes. As shown in the figure above, when I open two CMD Windows, there will be two CMD processes. If you can’t see the thread count column, you can click on the “View Selected Columns” menu to add a listening column.

View the CPU and memory usage:

In the performance TAB, we can view CPU and memory usage, and the number of logical processing cores based on the number of monitors recorded for CPU usage, such as my dual-core, four-threaded computer, which has four monitors.

Figure 11: View CPU and memory usage

The life cycle of a thread

When the number of threads is smaller than the number of processors, the concurrency of threads is true concurrency, with different threads running on different processors. However, when the number of threads is greater than the number of processors, thread concurrency is somewhat hindered, which is not true concurrency because at least one processor is running multiple threads.

Concurrency is an simulated state when a single processor is running multiple threads. The operating system takes turns executing each thread in a time-slice rotation. Today, almost all modern operating systems, such as the familiar Unix, Linux, Windows, and Mac OS X popular operating systems, use the preemptive scheduling method of time slice rotation.

We know that a thread is the smallest unit of program execution, and the smallest unit of task execution. In the early process-only operating systems, processes had five states: created, ready, running, blocked (waiting), and quit. The earlier process was equivalent to today’s single-thread process, so today’s multithreading has five states, and the life cycle of multithreading is similar to that of the earlier process.

Figure 12: Life cycle of an early process

A process can be in three states: ready, running, and blocked. The created and exit states describe the creation and exit of a process.

Created: The process is being created and cannot run yet. When the operating system creates a process, the work to be carried out includes allocating and establishing process control block table entries, establishing resource tables and allocating resources, loading programs and establishing address space;

Ready: The thread is forced to pause when the slice is used up, waiting for the next slice to arrive.

Running: This thread is executing, occupying the time slice;

Blocking: Also called a wait state, waiting for an event (such as an IO or another thread) to complete;

Exit: The process is terminated. Resources allocated by the operating system are released.

Figure 13: Thread life cycle

Create: A new thread is created and waits for it to be called to execute.

Ready: The thread is forced to pause when the slice is used up, waiting for the next slice to arrive.

Running: This thread is executing, occupying the time slice;

Blocking: Also called a wait state, waiting for an event (such as an IO or another thread) to complete;

Exit: when a thread completes a task or other termination condition occurs, the thread terminates and enters the exit state, which releases the resources allocated by the thread.

Author: luoweifu

Blog.csdn.net/luoweifu/ar…

Phase to recommend

Handwriting a “dozen airplane” small game attached [source code]

A SpringBoot2 + MybatisPlus mall management system, really sweet ~~

Why does The Ali Developer manual prohibit SELECT * altogether?

Springboot+MybatisPlus efficient implementation of add, delete, change, check, write is too good!!

Seven open source Spring Boot back-end separation projects, must be favorites!

More than ten recommended IDEA plug-ins, greatly improving the development efficiency

Java ID card number identification system

10 Useful Linux Commands you may not have used

Interviewer: Can you explain the principle of method overloading and method rewriting?

Worried about a project? Java + Vue test system

Stuck for a project? Use Java to develop an interesting expression generator

If you think the article is good, you can give it a thumbs up at the end of the article