Multithreading learning (a) --- threads and processes

Reference article:

Multithreading can be three minutes into the door!
Detailed summary of the full multi-threading

1. The definition of processes and threads

1.1 Process

Very common in the windows, open the Task Manager, you can see the process of operating system you are running:
process
there are many definitions of online process:

Narrow definition: the process is an instance of a program running (an instance of a computer program that is being executed).
Broadly defined: a process is run activities must have a separate function on a data collection program. It is the basic unit operating system dynamically performed in traditional operating systems, the process is both a basic allocation unit, is the basic unit of execution.

A program execution process is the process that occurs when active and a program execution sequence data on the processor, the program process is a process having a function independent running on a data set, which is a system for resource allocation and scheduling a separate unit.

Process is an independent unit of the system for resource allocation and scheduling. Each process has its own memory space and system resources.

Is executing a program, that is, once the program is loaded into memory and ready to perform, it is a process. Process is a fundamental concept of resource allocation, but also is the basic unit is scheduled to run, is a unit of concurrent execution of the system.

A program is being executed, perform each process has an execution order, the execution order is a path, or a call control unit.

1.2 thread

Thread, computer science terms, sometimes referred to as lightweight processes (Light Weight Process, LWP), which is running the program scheduling units. A standard thread by the thread ID, the current instruction pointer (PC), register set and stack components. Threads are included in the process, it is a physical process, is the basic unit of CPU scheduling and dispatching, which is smaller than the process of the basic unit can operate independently. Basically thread does not own its own system resources, has only a little in the operation of essential resources (such as a program counter, a set of registers and stack), but it may have to share the process with other threads belonging to the same process of all resources .

In simple terms, the implementation of a single process for each task is a thread. A thread is the smallest unit of the operation is performed in the process.

2. The process is already possible resource allocation and scheduling, why thread

To make the program can execute concurrently, the system must perform the following sequence of operations:

(1) creation process, when a process is created in the system, they must be allocated necessary, all the resources except for its processor, such as memory space, I / O device, and to the corresponding the PCB;
(2) cancel process the system in the revocation process, they must share some of their resources to perform the recovery operation, and then undo the PCB;
(3) the process of switching to the process context switching, the need to retain CPU environment of the current process, setting a new check process the CPU environment, and therefore must spend a lot of processor time.

process
You can see the process to achieve the process of scheduling in a multi-processor environment, distribution, switching, you need to spend so much time and space overhead.

The main thread is introduced in order to improve the efficiency of the system, reduce the time and scheduling processor idle time switching, and to facilitate management of the system , causes the OS to better concurrency.

In simple terms: the process of multi-CPU processing resources consuming, and we are introducing the thread as a basic unit of scheduling and dispatching of (part of the process to replace the basic functions ------ schedule ), so that a process can perform multiple tasks .

In other words: in the same process can perform multiple tasks, each of these tasks can be seen as a thread.

So: there will be a process of one or more threads!

3. Comparison of processes and threads

Process is the basic unit of resource allocation. All the resources associated with the process, are recorded in the process control block PCB. To indicate that the process has the resources or are using them.
In addition, the process is preempted processor scheduling unit, it has a complete virtual address space.

The corresponding process, thread nothing to do with resource allocation, it belongs to a particular process, and the process of sharing resources with other threads in the process.
When the scheduling process occurs, different processes have different virtual address space, different threads share the same address space within the same process.

Thread consists of only the relevant stack (stack system or a user stack), and thread control registers Table TCB. Register can be used to store threads in a local variable, but can not store other relevant variables thread.

The occurrence of the process compared to when a thread switch is switched to the occurrence, issues involving the preservation and changes related to the resource pointer of address space, etc. When a process switch; when a thread switch, because the threads share resources and address space within the same no process will not involve resources save and change address information, thereby reducing the overhead time operating system. Moreover, scheduling and switching processes are completed by the operating system kernel, and the thread is either done by the operating system kernel, but also by the user program.

Summary:
The basic unit processes as resource allocation.
Thread scheduling resources as the basic unit, a program execution unit, the execution path (single thread: an execution path, multithreading: a plurality of execution paths). Program is the basic unit of CPU usage.

4. Thread

3 basic state of the thread: execute, ready, blocked
five basic thread operations: derivative, blocking, activation, scheduling, end
Thread State
property threads:

  1. Light entity
  2. The basic unit of scheduling and dispatching of independence
  3. Can be executed concurrently
  4. Resource sharing process

There are two basic types of threads:

  1. User-level threads: management process completed by the user program within the operating system kernel only manage the process.
  2. System-level threads (kernel-level threads): managed by the operating system kernel. The operating system kernel to the application to provide the appropriate system calls and application program interface API, so that the user program can create, execute and undo the threads.

Thread of the life cycle:

  1. New: a new thread from the object to the program state between the thread start (), both the new state;
  2. Ready: thread object calls the start () method after, is in a ready state, JVM thread scheduler in scheduling a wait;
  3. Run: thread in the ready state in acquiring CPU resources can be performed after the run (), this time it is running thread, the thread may become ready to run the state, obstruction and death three states.
  4. Waiting / blocking / Sleep: In a thread executes sleep (sleep), suspend (pending), etc. Methods lose share some resources to enter the blocked state, can re-enter the ready state at the end of sleep.
  5. Switches to a state other termination condition is terminated after the run () method is completed or the occurrence of: terminating.

5. MultithreadingImplementation process

Multi-threaded exist, not to improve program execution speed. In fact, in order to improve the utilization of the application, execution of the program are actually grab CPU resources, execution of the CPU. Multiple processes in this looting of resources, of which one process if the execution path more, there will be a higher chance to grab the execution of the CPU.

Why use multiple threads:
①, in order to better utilize resources cpu, if only one thread, the second task must wait until after the end of the first task, but if you use multiple threads to perform tasks in the main thread can perform other tasks without the need to wait;
can not share data between ②, processes, threads can;
③, the system needs to create a process to re-allocate system resources to the process, the cost of creating a thread is relatively small;
④, the Java language built-in multi-threaded function support, simplifying the java multithreaded programming.

6. Parallel and Concurrent

Parallelism refers to two or more events occur within the same time.
Parallel multiple events on different entities.

Concurrency refers to two or more events occur within the same time interval.
Concurrent multiple events on the same entity.

This shows that: the parallel is for the process, it is for concurrent threads.

Guess you like

Origin blog.csdn.net/a770794164/article/details/91979238