Operating System-Thread (1), operatingsystem

Source: Internet
Author: User

Operating System-Thread (1), operatingsystem

Starting the Thread journey, as a programmer, dealing with more threads, various multi-threaded programs, and parallel programming are based on threads. Main content of this article:

I. What and Why Thread1.1 What

A thread is the execution unit inside a process. A process is used as an instance of a running program to put resources together. A thread is a process,A thread exists as an entity executed on the CPU. The CPU runs a thread.The previous article said that a CPU can only run one process at a time. This is a general statement. The correct statement should be that a CPU can only run one process at a time.

Each process has its own independent address space, but all threads in a process share the address space of the process (including data of course ).

1.2 Why

First, we will introduce an example. A Word process has three threads (actually there are 30 threads ). One of them is responsible for receiving input from the keyboard, the other is responsible for automatically saving, and the other is responsible for adjusting the format (for example, the Word format should be adjusted immediately after a large segment of text is deleted ).

The three threads belong to the same Word process and share all resources, that is, the same Word document. Because the CPU actually runs a thread, these three threads can be parallel. for users, the three operations for Word operations seem to be performed simultaneously. From the perspective of system users, operations on the same resource (process) have also become parallel (in a single-core state, it is actually pseudo-parallel, hypothetical ).

Because multiple threads exist, even though the system has been automatically adjusting the format and saving it during Word processing, the user's input never feels delay.

The preceding example cannot be implemented by a process. If we replace the preceding three threads with three processes, and each process has only one thread to perform the corresponding tasks, what will happen?

The above Word example can also be used in Excel. For example, some table values in Excel are input by users, and some table values are automatically calculated by input. Here there are two threads, one is used to accept and process user input, and the other is used for automatic calculation.

The thread is more lightweight than the process, and the overhead of creation and destruction is much smaller than the process. Creating a thread in many systems is 10-times faster than creating a process.

Ii. Comparison of threads and processes

First, create a graph. T1 and T2 are the threads of Process1; T3 and T4 are the threads of Process2.

A process is abstracted at a higher level, representing a running program. It is a visible application from the user's perspective, such as a Word, an Excel, and a cursor waiting for input. Processes share various system resources at the system level, such as disks, files, and memory. Parallel Processing Between processes is the most perceptible parallelism for users. Users will say that my computer runs two words at the same time, and an Excel file.

The final execution will still be implemented to the thread. The thread is a process and is a real execution unit. The threads inside a process share the address space and resources of the current process, of course, each thread will still have personalized data (which will be specially written for introduction ). The parallel processing of two processes that the user perceives (for example, two Word documents run simultaneously) is actually the Parallel Processing Between the threads inside the two Word documents.

 

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.