Cainiao journey-learning thread (0), travel thread

Source: Internet
Author: User

Cainiao journey-learning thread (0), travel thread

In today's software programming, it is inevitable that multithreading or other methods will be used for asynchronous purposes. So what is thread and how to use it? These are all things that need to be learned and explored. However, before learning the thread, there is still some knowledge that needs to be mastered. Although it is all in books, it still plays a role in thread learning.

 

Process objective

There are many operating systems (OS) in today's computers. Most operating systems are real-time operating systems that can respond to user operations in real time. They often share common basic features:Concurrency and sharingAndVirtualThe process is highly correlated with concurrency and sharing.

The operating system can run many programs at the same time, and many programs may communicate and use some I \ O devices, such as printers, these are the conveniences provided by the operating system, but in factThe CPU can only process one program at a time.However, the operating system implements program concurrency, because at the micro level, multiple programs use the CPU and Other Resources alternately. However, in a multi-program environment, closed procedures will be lost, and there will be intermittent and non-reproducible,In order to better implement the concurrency features, the concept of process is introduced.. Each process uses a time slice rotation scheduling algorithm to schedule and allocate the running time slice. The CPU or other resources can be used in the time slice. Because the time slice is very short, you cannot feel the switching between processes.

Note: On a multi-processing machine, each processor can process different programs, which can be called True concurrency.

Definition

From different perspectives, processes can have different definitions. Typical definitions include:

1. A process is a program execution process.

2. A process is an activity that occurs when a program and its data are executed sequentially on the processing machine.

3. A process is a process in which programs with independent functions run on a dataset. It is an independent unit for the operating system to allocate and schedule resources.

Composition

PCB: Process Control Block. In order to enable the program (including data) involved in concurrent execution to run independently and ensure better concurrency and no conflict, you must configure a special data structure for the process, the operating system uses PCB to control and manage processes.Unique Identifier.

Program segment: The program segment that can be scheduled by the Process scheduler to run on the CPU. Note that the program is fixed and can be shared by multiple processes, that is, multiple processes can run the same program.

Data Segment: The data segment of a process. It can be the raw data processed by the program corresponding to the process, or the intermediate or final result generated during program execution.

Consists of three parts: PCB, program segment, and data segment.Process imageThe process is created to create the PCB in the process image, and the process is abolished to cancel the process PCB. After introducing the process image concept, you can define a process in a traditional operating system as a running process of the process image and an independent unit for system resource allocation and scheduling.Processes are dynamic, while process images are static.

Status

A process has five statuses: creation, readiness, running, blocking, and termination. A process switches over to the ready, blocked, and terminated state most of the time.
             

Process switching: The process switching means that the processor (CPU) is transferred from one process to another, and the running environment of the process changes substantially:

1. Save the CPU context, including program counters and other registers;

2. Update PCB information;

3. Move the PCB of the process into the corresponding queue, such as ready or waiting for the queue;

4. select another process to run and update its PCB;

5. Update the data structure of memory management;

6. Restore the CPU context information.

 

Thread

We briefly review the basic knowledge of the process from the above. Of course, there are still many knowledge points in the process that have not been introduced. Because of my limited ability, I will not introduce it more. Next we will review the basic thread knowledge:

Purpose

The process is introduced to solve the problem of implementing concurrent execution of multiple programs in the operating system. The appearance of the process makes the concurrent execution of each program well closed, and will always consume some resources during process switching, there is a certain amount of time-space consumption. However, as the software complexity increases, the process consumes more resources and requires more time-space overhead during process switching! ThereforeReduce the time-space overhead of the Program During Concurrent executionTo improve the concurrency performance of the operating system, the thread concept is introduced.

Definition

A lightweight process is a basic CPU execution unit consisting of the thread ID, program counter, register set, and stack. A thread is an entity in a process, yessystemBasic Unit of independent scheduling and assignment, Thread itselfDoes not possess system resourcesOnly have some resources that are essential for running, but can share all the resources of the process with the threads in the same process.

After the thread is introduced, the connotation of the process changes. The process is only used as the allocation unit of system resources other than the CPU, and the thread is used as the CPU allocation unit, in-process thread switching does not cause process switching, reducing the time-space overhead.

Comparison with processes

1. Scheduling: in traditional operating systems, the basic units with resources and independent scheduling are processes. After the thread is introduced, the thread becomes the basic unit for independent calls, A process is the basic unit with resources. Thread switching within a process does not lead to process switching, but thread switching between different processes will lead to process switching.

2. Owning resources: a process is the basic unit for owning resources, while a thread does not own system resources (except some essential resources), but the thread can use the resources of its processes.

3. System overhead: because a thread does not have system resources, when a thread in a process is scheduled to run on the CPU, only a small amount of register content needs to be saved and set, without the need to save and update processes in the CPU running environment, the time-space overhead is relatively less.

4. Concurrency: in the operating system of the introduced thread, because the thread consumes less time and space during creation, cancellation, or scheduling switching, in addition, thread switching between processes does not cause process switching. Therefore, in the overall concurrency performance, the operating system throughput of the introduced threads is higher.

Summary

Process: The computer's CPU only runs one program at a time, and the CPU is high-speed. If the CPU waits for other resources of the program to be ready, the CPU performance will be wasted. If the program is executed concurrently, when other resources of the current program are ready, you can first run other programs to minimize the waste of CPU performance. At this time, the concept of process is introduced, and the CPU runs the process in the form of time slice scheduling, the emergence of processes makes concurrent execution of multiple programs well closed and controllable, and the operating system has controllable and stable concurrency performance.

Thread: process as the basic unit of system resources and scheduling itself is no problem, but as the program becomes increasingly complex, the process has more resources, when a process is scheduled to switch to the CPU, the CPU needs to save the running environment of the current process and set the running environment of the process to be executed. The time-space overhead will increase; to reduce the scheduling overhead, a lighter process-thread is introduced. At this time, the process is no longer used as the basic unit of scheduling, and the thread becomes the basic unit of scheduling, the overhead of thread switching is much lower than that of process switching. At this time, the concurrent performance of the operating system can be better applied.

There are not many summarized knowledge points, such as the deep-layer running mechanism, data structure, and scheduling algorithm, which are not involved due to my own knowledge limitations. Further study is required here.

 

Related Article

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.