Beginner's Journey-learning Thread (Basic)

Source: Internet
Author: User

A Gentleman of pleasure.

Beginner's Journey-learning Thread (Basic)

In today's software programming, the inevitable use of multithreading or other ways to achieve the purpose of asynchrony, then, what is a thread, how to use it? These are the things that need to be learned and explored. However, before learning the thread, there is still some knowledge to grasp, although it is the book of things, but still on the thread of learning has a certain role.

Process Purpose

Now the computer has a lot of operating systems (OS), most operating systems are real-time operating systems, can respond to the user's actions in real time, they often have common basic characteristics: concurrency, sharing and virtual , process generated in the concurrency, sharing a great deal of contact.

The operating system can "simultaneously" run a lot of programs, and many programs have the possibility of communication, common use of certain i\o devices, such as printers, these are the operating system to provide us with many conveniences, but in fact the CPU at the same time can only process a program , However, the operating system realizes the characteristics of the program concurrency, because it is the micro-program that uses CPU and other resources alternately, but in the multi-channel program environment, the program will be closed, intermittent and non-reproducible, in order to better realize the concurrency characteristics, the concept of process is introduced. . Each process through a time slice rotation scheduling algorithm for scheduling, allocation of running time slices, in the time slice can use CPU or other resources, because the time slice is very short, the user does not feel the transition between processes.

Note: On multiprocessor machines, each processor can handle different programs, which can be said to be true concurrency.

Defined

From different angles, processes can have different definitions, and the typical definitions are:

1, the process is a process of execution of the program.

2, the process is a program and its data on the processing machine in the sequential execution of activities occurred.

3, the process is a separate function of the program running on a data set, is the operating system for resource allocation and scheduling of an independent unit.

Composition

PCB: Process Control block, in order to participate in concurrent execution of the program (including data) can run independently, better concurrency and no conflict, the process must be configured with a dedicated data structure, the operating system using the PCB to control and manage processes, the PCB is the unique identification of the process.

Program segment: Program section is can be dispatched by the process scheduler to the CPU execution of the program code snippet, note that the program is fixed, can be shared by multiple processes, that is, multiple processes can run the same program.

Data segment: The data segment of a process, which can be the raw data processed by the process's corresponding program, or it can be the intermediate or final result produced by the execution of the program.

The process image is made up of the PCB, the program section and the data section, and the process is created to create the PCB in the process image, and the process revocation is the PCB of the process. After introducing the concept of process image, the process of traditional operating system can be defined as: process is the process image running process, is a system for resource allocation and scheduling of a separate unit. The process is dynamic, and the process image is static.

Status

Processes exist in 5 states, namely, create, ready, run, block, and terminate, while most of the process is toggled in readiness, blocking, and terminating in three states.
             

Process switching: Process switching is where the processing machine (CPU) goes from running one process to another, and the process's operating environment has changed substantially:

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

2, update the PCB information;

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

4, select another process to run, update its PCB;

5, update the memory management data structure;

6. Restore CPU context information.

Thread

From the above we briefly review the basic knowledge of the process, of course, the process has a lot of knowledge points did not introduce, because I have limited capacity, not more introduction. Here's a recap of the basics of threading:

Objective

The introduction of the process is to solve the operating system implementation of multiple programs concurrent execution, the emergence of processes so that each program in the concurrent execution has a good seal, in the process of switching the time will always consume some resources, there is a certain amount of time and space consumption, but as the software complexity increases, the process takes up more resources, More time and space overhead is required for process switching! So in order to reduce the time and space overhead of the program in concurrent execution , and improve the concurrency performance of the operating system, the thread concept is introduced.

Defined

The most direct understanding of threads is that lightweight process, which is a basic CPU execution unit, consists of thread ID, program counter, register collection and stack, thread is an entity in the process, is the basic unit that is dispatched and dispatched independently by the system, the thread does not own system resources , you have only a few resources that are essential in the run, but you can share all of the resources of the process with threads under the same process.

After the introduction of the thread, the connotation of the process has changed, the process only as the allocation unit of the system resources other than the CPU, the thread as the allocation unit of the CPU, process inline switch does not cause process switching, reduce the space-time overhead.

Comparison with process

1, scheduling: In the traditional operating system, the basic unit with resources and independent dispatch is a process, after the introduction of the thread, the thread becomes the basic unit of the independent call, the process is the basic unit of the resource; the same process inline switch does not cause the process to switch, However, thread switching between different processes can cause the process to switch.

2. Have resources: a process is the basic unit of resources, and threads do not own system resources (except for some essential resources), but threads can use the resources of their processes.

3. System overhead: Because threads do not own system resources, the same process thread is dispatched to the CPU when it is scheduled to execute, just save and set a small amount of register content, without saving, updating the process in the CPU running environment, the time-space overhead has become relatively less.

4, Concurrency: In the introduction of the thread of the operating system, because the thread is created, revoked or in the scheduling switch when the space-time consumption is lower, and the process of thread switching will not cause the process of switching, so on the overall concurrency performance, the introduction of the thread's operating system throughput is higher.

Summarize

Process: The computer's CPU at the same time only one program runs, and the CPU is high speed, if the CPU waits for the other resources of the program is ready, it will cause the CPU performance waste, if the concurrent execution program, waiting for the other resources of the current program is ready, first go to run another program, Can minimize the CPU performance of waste, the introduction of the concept of the process, and the CPU is a time-slice scheduling process, the emergence of the process so that many to program concurrency has a good closed and controllable, so that the operating system has controllable, stable concurrency performance.

Thread: The process as the basic unit of system resources and scheduling itself is not a problem, but as the program becomes more complex, the process has more resources, when the process is dispatched to the CPU to switch, the CPU needs to save the current process running environment and set the running environment to execute the process, In order to reduce the overhead of the scheduling, the introduction of a lighter process-thread, the process is no longer as the basic unit of the dispatch, the thread becomes the basic unit of the dispatch, the thread is switching more than the process of switching overhead much less, In this case, the concurrency performance of the operating system can be more well applied.

Summary of the knowledge points are not many, deep layer of the operating mechanism, data structure and scheduling algorithm, because I have no knowledge limitations, there is also need to follow-up learning.

Beginner's Journey-learning Thread (Basic)

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.