Explain threads in the operating system's Perspective | Process _ Multithreading

Source: Internet
Author: User

1, what is the thread

What is a thread. What the thread has to do with the process. This is a very abstract question, but also a very broad topic, involving a lot of knowledge.


2, Task scheduling

Thread is set. To understand this concept, you must first understand some of the relevant concepts of the operating system. Most operating systems (such as Windows, Linux) task scheduling is the use of time-slice rotation of preemption scheduling, that is, a task after a short period of time to force a pause to perform the next task, each task rotation execution. A short period of time when a task is performed is called a time slice, when a task is executing in a state called a running state, and a task is forced to suspend the next task for a period of time, and the suspended task is in a ready state awaiting the arrival of the next time slice that belongs to it. So that each task can be executed, because the CPU's execution efficiency is very high, the time slice is very short, between each task fast switch, gives the person's feeling is many tasks at "simultaneously", this is what we say concurrent, the multitasking operation process schematic diagram as follows:


3. Process

We all know that the core of the computer is the CPU, it takes all the computing tasks, while the operating system is the manager of the computer, he is responsible for task scheduling, resource allocation and management, commanding the entire computer hardware; The application is a program that has a function that runs on the operating system.

Process is an independent function of the program on a data set of a dynamic execution process, is the operating system for resource allocation and scheduling of an independent unit, is the carrier of application operation. The process is an abstract concept that never has a unified standard definition. Process is generally composed of programs, data sets and process control blocks of three parts, the program used to describe the process to complete the function, is the control process execution of the instruction set, data collection is the program in the implementation of the required data and work area; The description and control information that contains the process is the only identification of the existence of the process.

The characteristics of a process:

(1) Dynamic: The process is a procedure of the implementation process, is temporary, has a life cycle, is dynamic, dynamic extinction;

(2) Concurrency: Any process can be executed concurrently with other processes;

(3) Independence: The process is a system of resource allocation and scheduling of an independent unit;

(4) Structure: The process is composed of the program, the data and the Process control block three parts;

4, Thread

There is no thread in the early operating system, the process is the smallest unit that can have resources and run independently, and is the smallest unit of program execution. Task scheduling is a time-slice rotation of the preemption scheduling, and the process is the smallest unit of task scheduling, each process has its own separate piece of memory, so that the memory address between each process isolation.

Later, with the development of the computer, the requirement of CPU is more and more high, the switching overhead between processes is more and more complicated. Then invented the thread, thread is a single sequence control process in the program execution, is the smallest unit of program execution flow, is the basic unit of processor dispatch and dispatch. A process can have one or more threads, sharing the memory space of the program between threads (that is, the process memory space in which it resides). A standard thread consists of a thread ID, the current instruction pointer (PC), a register, and a stack. Processes consist of memory space (code, data, process space, open files), and one or more threads

5. The difference between process and thread

We talked about processes and threads, but you may still feel confused and feel like they are similar, indeed, the process is inextricably linked to threads, let's take a look at the following:

(1) The thread is the smallest unit of program execution, and the process is the smallest unit of the operating system allocating resources;

(2) A process consists of one or more threads, which are different routes of execution of code in a process;

(3) Want to be independent between processes, but share the memory space of the program (including code snippets, datasets, heaps, etc.) and some process-level resources (such as opening files and signals) between threads under the same process, and the threads within a process are not visible in other processes;

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

Diagram of thread-process relationship:



In short, threads and processes are abstract concepts, threads are a smaller abstraction than processes, and both threads and processes can be used for concurrency.

There is no thread in the early operating system, the process is the smallest unit that can have resources and run independently, and is the smallest unit of program execution. It is equivalent to one thread in a process, and the process itself is a thread. So threads are sometimes referred to as lightweight processes (lightweight PROCESS,LWP).


Later, with the development of the computer, the efficiency of the context switch between multiple tasks is more and more high, and a smaller concept-----------------thread, generally a process will have multiple (also can be one) thread.


6. Multi-thread and multi-core

The above mentioned time slice rotation scheduling means that a task after a short period of time to force a pause to perform the next task, each task in turn, many operating system books say "only one task at a time is executing." Anyone might want to ask a dual-core processor. Aren't the two cores running at the same time?

In fact, the phrase "only one task at a time is executed" is inaccurate, at least it is not comprehensive. How does a thread execute in the case of multi-core processors? You need to understand kernel threads.

Multi-core processor is the integration of multiple computing cores on one processor to improve technical capability that is, there are a number of real parallel computing processing core, each processing core corresponding to a kernel thread. Kernel thread (Kernel thread,klt) is a thread that is directly supported by the operating system kernel, which completes thread switching by the kernel, which schedules the thread process through the action scheduler, and is responsible for mapping the task of the thread to the individual processors. A typical processing core corresponds to a kernel thread, such as a single core processor corresponding to a kernel thread, a dual-core processor corresponds to two kernel threads, and a quad-core processor corresponds to four kernel threads.

Today's computers are typically dual-core four-thread, quad-core, eight-thread, is using Hyper-threading technology to simulate a physical processing core into two logical processing core, corresponding to two kernel threads, so the number of CPUs seen in the operating system is twice times the actual number of physical CPUs, if your computer is a dual-core four thread, open the Task Manager/ Performance "can see 4 CPU monitors, quad core eight threads can see 8 CPU monitors.


Hyper-Threading technology is the use of special hardware instructions, a physical chip simulation into two logical processing core, so that a single processor can use thread-level parallel computing, and thus compatible with multi-threaded operating systems and software, reduce the CPU idle time, improve the efficiency of the CPU. This hyper-Threading technology is determined by the processor hardware and requires the support of the operating system to be displayed on the computer.

Programs typically do not use kernel threads directly, but rather use the advanced interfaces of the kernel thread--lightweight processes (Light Weight PROCESS,LWP), the lightweight process being the thread we're talking about in the usual sense (we're here as a user thread), Because each lightweight process is supported by a kernel thread, a lightweight process can be achieved only if kernel threads are supported first. The corresponding relationship between user thread and kernel thread has three models: one-to-one model, multi-pair model, Many-to-many model, in which three modes are described with 4 kernel threads and 3 user threads as examples.

One-to-one model:

For a one-to-one model, a user thread uniquely corresponds to a kernel thread (which in turn is not necessarily true, and a kernel thread does not necessarily have a corresponding user thread). Thus, if the CPU does not use Hyper-Threading technology (such as a quad-core four-thread computer), a user thread uniquely maps to a thread of a physical CPU, the concurrency value between threads is truly concurrent, and the one-to-one model gives the user thread the same advantages as the kernel thread, The execution of other threads is not affected by a thread blocking for some reason; here, a one-to-one model can also allow multithreaded routines to perform better on multiprocessor systems. But a one-to-one model also has two drawbacks:

(1) Many operating systems limit the number of kernel threads, so one-to-one models limit the number of user threads;

(2) Many operating system kernel thread scheduling, context switching overhead is relatively large, resulting in the user's thread execution efficiency down.


Multi-pair Model:

Multiple-pair models map multiple user threads to a kernel thread. The switch between threads is done by the code of the user state, so the relative one-to-one model, the more than one model of the thread switching speed much faster; therefore, the Many-to-many model has almost no limit on the number of user threads, but there are two disadvantages to the multiple pairs of models:

(1) If one of the user threads is blocked, all other threads will not be able to execute, so the kernel thread is blocked at this time;

(2) On multiprocessor systems, the increase in the number of processors does not significantly increase the threading performance of the Many-to-many model, so all user threads are mapped to a single processor.


More than 3 models:

Multi-Multiple models combine the advantages of one-to-one models and multiple pairs of models to map multiple user threads to multiple kernel threads, and the advantages of multiple models are:

(1) The blocking of a user thread does not cause all threads to block, so there are other kernel threads being scheduled to execute at this time;

(2) There is no limit to the number of users ' threads in many pairs of models;

(3) In multiprocessor operating systems, many pairs of multiple model threads can also achieve a certain performance improvement, but the increase is not as high as the height of a one-to-one model.


7. Thread Life cycle

When the number of threads is less than the number of processors, the concurrency of threads is real concurrency, and different threads run on different processors, but when the number of threads is greater than the number of processors, the concurrency of threads is hampered by the fact that there is not real concurrency at this time because at least one processor will run multiple threads.

Concurrency is a simulated state when multiple threads are running on a single processor. The operating system rotates each thread in turn using a time slice rotation. Today, almost all modern operating systems use preemptive scheduling, such as the familiar UNIX, Linux, Windows, and Mac OS, which are the most popular operating systems in the time slice rotation. We know that the thread is the smallest unit of program execution and the smallest unit of task execution, in which the process has five states in the early operating system, creating, on line, running, blocking, exiting. Earlier processes are equivalent to a single thread process now, and now there are five of multiple threads, and today's multi-threaded life cycle is similar to the lifecycle of earlier processes.


Processes are running in three states: ready, running, blocking, creating and exiting states describe process creation and exit procedures.

(1) Create: Processes are being created, not yet running, and the operating system's work to create processes involves assigning and building process Control block table entries, creating resource tables and allocating resources, loading programs, and building address spaces;

(2) Ready: The time slice has been used up, this thread is forced to suspend, waiting for the next belong to his time slice arrival;

(3) Run: This thread is executing, taking time slices;

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

(5) Exit: The process has ended, so is also called the end state, freeing the resources allocated by the operating system.


Thread declaration cycle:



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.