Multithreading Theory of Concurrent Programming

1. What is a thread

  In traditional operating systems, each process has an address space, and by default there is a thread of control

  As the name implies, a thread is a process of working in an assembly line (the work of the assembly line requires power, and the power supply is equivalent to the CPU), and an assembly line must belong to a workshop. The work process of a workshop is a process, and the workshop is responsible for integrating resources. It is a Resource unit, and there is at least one assembly line in a workshop.

  Therefore, a process is only used to group resources together (a process is just a resource unit, or a collection of resources), and a thread is the execution unit on the CPU.

  The concept of multithreading (that is, multiple threads of control) is that there are multiple threads in a process, and multiple threads share the address space of the process, which is equivalent to multiple pipelines in a workshop, all sharing the resources of a workshop. For example, Beijing Subway and Shanghai Subway are different processes, while Line 13 in the Beijing Subway is a thread, and all the lines of the Beijing Subway share all the resources of the Beijing Subway, for example, all passengers can be pulled by all lines.

Second, the thread creation overhead is small

1. The overhead of creating a process is much greater than that of a thread?

  If our software is a factory, the factory has multiple pipelines, and the pipeline work requires power, and there is only one power supply, that is, the cpu (single-core cpu). A workshop is a process, and a workshop has at least one assembly line (a process has at least one thread); to create a process is to create a workshop (apply for a space, and build at least one assembly line in the space), and to build a thread is just a workshop. A pipeline is built inside, no need to apply for space, so the cost of creation is small.

2. Is there a competitive relationship between processes and a cooperative relationship between threads?

  The workshop is directly related to competition/power grabbing, competition (different processes are directly competitive, programs written by different programmers are run, Xunlei preempts the network speed of other processes, and 360 kills other processes as viruses)
  A ​​workshop The relationship between different pipelined cooperative work (the threads of the same process are cooperative relationships, and are started within the program written by the same program, and the threads in Thunder are cooperative relationships, and they will not do their own work)

The difference between thread and process

  1. Every time a process is started, there is at least one thread in the process.

  2. The process itself is just a resource unit and cannot be executed. The thread opened in the process is the real running unit.

  3. Multiple threads can be started in a process, and resources can be shared among threads in the same process.

  4. The overhead of starting a thread is much less than that of starting a process.

  5. Threads can control threads under the same process to a considerable extent, and processes can only control their child processes.

  6. Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of other threads of the process; modifications to the parent process will not affect the child process.

Fourth, why use multithreading

  Multithreading refers to opening multiple threads in a process. Simply put: if multiple tasks share an address space, multiple threads must be opened in a process. The detailed talk is divided into 4 points:

  1. Multiple threads in the same process share the address resources in the process

      2. Threads are more lightweight than processes, and threads are easier to create and revocable than processes . In many operating systems, creating a thread is 10-100 times faster than creating a process. When there are a large number of threads that need to be dynamically and quickly modified, This feature is very useful

      3. If multiple threads are cpu-intensive, there is no performance gain, but if there is a lot of computation and a lot of I/O processing, having multiple threads allows these activities to run on top of each other, thus speeding up The speed of program execution.

      4. In a multi-cpu system, in order to maximize the use of multi-core, multiple threads can be opened, which is much less expensive than opening a process. (this one does not apply to python)

Five, multi-threaded application example

  To start a word processing software process, the process must do more than one thing, such as monitoring keyboard input, processing text, and automatically saving the text to the hard disk at regular intervals. These three tasks operate on the same piece of data, so multiple processes cannot be used. . Only three threads can be started concurrently in one process. If it is a single thread, it can only be that when the keyboard is input, it cannot process text and auto-save, and it cannot input and process text during auto-save.

 

6. Comparison of user-level and kernel-level threads

 

 One: The following is the difference between user-level threads and kernel-level threads:

  1. Kernel-backed threads are OS kernel-aware, while user-level threads are OS-kernel-insensitive.
  2. The creation, cancellation and scheduling of user-level threads do not require the support of the OS kernel and are processed at the language (such as Java) level; while the creation, cancellation and scheduling of threads supported by the kernel require the support of the OS kernel, and are related to the process's Create, undo, and schedule are largely the same.
  3. When a user-level thread executes a system call instruction, the process to which it belongs will be interrupted, while when the kernel supports a thread to execute a system call instruction, only the thread will be interrupted.
  4. In a system with only user-level threads, the CPU scheduling is still based on a process. Multiple threads in a running process are controlled by the user program to run alternately; in a system with kernel-supported threads, CPU scheduling is based on The thread is the unit, and the thread scheduler of the OS is responsible for thread scheduling.
  5. The program entity of a user-level thread is a program running in user mode, while the program entity of a kernel-supported thread is a program that can run in any state.

    Two: the advantages and disadvantages of kernel threads

  advantage:

  1. When there are multiple processors, multiple threads of a process can execute simultaneously.

  shortcoming:

  1. Scheduled by the kernel.

Three: the advantages and disadvantages     of user processes

  advantage:

  1. The scheduling of threads does not require the direct participation of the kernel, and the control is simple.
  2. Can be implemented in operating systems that do not support threads.
  3. Thread management costs such as creating and destroying threads, thread switching costs, etc. are much less expensive than kernel threads.
  4. Allows each process to customize its own scheduling algorithm, and thread management is more flexible.
  5. Threads can utilize more tablespace and stack space than kernel-level threads.
  6. Only one thread can be running in the same process at the same time. If one thread is blocked using a system call, the entire process will be suspended. Also, page invalidation creates the same problem.

  shortcoming:

  1. Resource scheduling is performed according to the process. Under multiple processors, threads in the same process can only be time-shared under the same processor.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325800051&siteId=291194637
Recommended