[Operating System] 2.2 Process Management (Thread)

What is a thread

Before the introduction of the process: each program in the system can only be executed serially.
After the introduction of the process: the process is one execution of the program. But some functions obviously cannot be realized by one program in order.
Insert picture description here
Obviously, QQ cannot do video, chat and file transfer at the same time,
so the process may need to do many things "simultaneously", while traditional processes can only be executed serially. A series of procedures.
Therefore, threads are introduced toIncrease concurrency

  • traditionalprocessIt is the smallest unit of program execution flow
  • After introducing the thread,ThreadBecameProgram execution flowofSmallest unit
    Insert picture description here
  • Thread can be understood as " lightweight process "
  • ⭐Thread: Is a basicCPU execution unit, AlsoThe smallest unit of program execution flow
  • After the introduction of threads, not only can the process be concurrent , but also each thread in the process can be executed concurrently , which further improves the concurrency of the system
  • After the thread is introduced, **process**Only as other than CPUSystem resource allocation unit(Such as printers, memory address space, etc. are all allocated to the process)
    Insert picture description here

Changes after the introduction of threads

  • Changes brought about:
    1. Resource allocation and scheduling
      1. In the traditional process mechanism, the process is the basic unit of resource allocation and scheduling
      2. After the thread is introduced, the process is the basic unit of resource allocation, and the thread is the basic unit of scheduling
    2. Concurrency
      1. In the traditional process mechanism, only concurrency between processes
      2. After the thread is introduced, the threads of the process can also be concurrent
    3. System overhead
      1. The traditional concurrency between processes requires switching the operating environment of the process, and the system overhead is very high.
      2. Concurrency between threads, if it is a thread switching within the same process, there is no need to switch the process environment, and the system overhead is small
      3. After the introduction of threads, the system overhead caused by concurrency is reduced

Thread attributes

  1. ⭐Threads are processorsSchedulingThe unit
  2. Multi-CPUIn the computer, each thread occupiesDifferent CPU
  3. Each thread has oneThread ID, thread control block TCB
  4. Thread also hasReady, blocking, runningThree basic states
  5. Threads have almost no system resources
  6. Different threads of the same processbetweenShared process resources
  7. Due to the shared memory address space ,Same processofInter-thread communicationEven without system intervention
  8. Thread switching of the same process will not cause process switching
  9. Thread switching of different processes will cause process switching
  10. Switch the threads of the same process, the system overhead is very small
  11. Switching processes requires a lot of system overhead

⭐How to implement threads

  • The realization of the thread:
    1. User-level threads: Some applications are implemented through the thread library.
      All thread management work isResponsible by the application(Including thread switching) In
      user-level threads, thread switching is inUser modeIt can be done without the intervention of the operating system.
      In the eyes of the user, there are multiple threads, but in the eyes of the operating system kernel, they are not aware of the existence of
      threads.Threads that can be seen from the user's perspective
      Insert picture description here
    2. Kernel-level thread: The management of kernel-level threadsBy the operating system kernelThe completion of
      thread scheduling, switching and other tasks are all the responsibility of the kernel.
      Therefore, the switching of kernel-level threads must be done inCore stateNext to complete
      it, kernel-level threads can be understood as "Threads that can be seen from the perspective of the operating system kernel
      Insert picture description here
    3. The combination of the two: In a system that supports both user-level threads and kernel-level threads, map n user-level threads to m kernel-level threads (n>=m)
      ⭐⭐⭐The operating system only "sees" kernel-level threads, so onlyKernel-level threads are the unit of processor scheduling

      For example: in the model below, the process has two kernel-level threads and three user-level threads .From the user's perspective, this process has three threads, But even if the process is running in a4 nuclear CPUOn the computer, and also up toCan only be assigned two cores,becauseFrom the perspective of the operating system, the process has only two threads,and so,Can only be executed in parallel by two user-level threads at mostInstead of three
      Insert picture description here

⭐Multi-threaded model

In a system that supports both user-level threads and kernel-level threads, the problem of mapping several user-level threads to several kernel-level threads leads to the problem of "multi-threading model":

  1. Many-to-one model: Multiple user-level threads are mapped to a kernel-level thread, and each user-level thread corresponds to the same kernel-level thread

    1. Advantages: user-level thread switching can be completed in user space,No need to switch to core mode, The system overhead of thread management is small, and the efficiency is high
    2. Disadvantages: When a user thread is blocked, the entire process will be blocked.Concurrency is not high. And because there is only one kernel-level thread, multiple threads cannot run in parallel on a multi-core CPU
      Insert picture description here
  2. One-to-one model: A user-level thread is mapped to a kernel-level thread, and each user process has the same number of kernel-level threads as user-level threads

    1. Advantages: When a user thread is blocked, other threads can continue to execute,High concurrency. Multi-threads can be executed in parallel on multi-core CPUs
    2. Disadvantages: A user process will occupy multiple kernel-level threads,Thread switchingIt is completed by the operating system kernel, so it needs to switch to the kernel mode, so the overhead of thread management is large
      Insert picture description here
  3. Many-to-many model: Map n user-level threads to m kernel-level threads (n>=m), and each user process corresponds to m kernel-level threads.
    advantage: Overcoming the shortcomings of the one-to-one model that the concurrency is not high, and overcoming the problem of too much overhead caused by too many kernel-level threads occupied by one user process
    Insert picture description here

Guess you like

Origin blog.csdn.net/Qmilumilu/article/details/112545809
Recommended