Linux_day05_03_Basic knowledge of process management

Basic knowledge of process management

  1. What is a process? What is a thread? What is a coroutine?

    • process
      • A program being executed is the smallest unit of operating system resource allocation and has an independent address space
      • Dynamic: The essence of the process is the one-time execution process of the program in the multi-program system. The process is dynamically generated and died dynamically
      • Concurrency: Any process can execute concurrently with other processes
      • Independence: A process is a basic unit that can run independently, as well as an independent unit for system resource allocation and scheduling
      • Asynchrony: Due to the mutual restriction between processes, the process has intermittent execution, that is, the processes advance at an independent and unpredictable speed.
      • Structural features: the process is composed of three parts: program, data and process control block
      • Multiple different processes can contain the same program: a program in different data sets constitutes different processes and can get different results; but the program cannot be changed during execution
    • Thread
      • Lightweight Process (LWP) is the smallest unit of operating system scheduling (CPU scheduling) and an entity of the process
      • The main thread is the first thread generated in the creation process, which is the thread corresponding to the main function
      • The thread itself does not own system resources, only resources that are essential in operation, but it can share all resources owned by the process with other threads belonging to the same process
      • One thread can create and cancel another thread, and multiple threads in the same process can execute concurrently
      • Threads also have three basic states: ready, blocked and running
    • Coroutine
      • More lightweight than threads, it is a user-mode lightweight thread, and the scheduling of the coroutine is completely controlled by the user
      • A coroutine is executed by a thread, and two sub-processes cooperate with each other to complete a task
      • The coroutine can be interrupted inside the subroutine, and then it will execute other subroutines, and then return to continue execution when appropriate
  2. What are the advantages and disadvantages of processes, threads, and coroutines

    • process

      • Each process is independent of each other and does not affect the stability of the main program
      • Performance can be improved by increasing CPU
      • Performance can be improved by reducing the impact of thread locking/unlocking
      • No resources are shared between processes, which is conducive to resource protection
      • The logic control is complicated and needs to interact with the main program
      • The process has an independent memory space, creating a process is expensive
      • Multi-process scheduling overhead is relatively large
    • Thread

      • The threads of the same process share resources, which improves the efficiency of the program
      • Create thread is less expensive, and run consumes less resources
      • Thread cannot run independently, it must exist in the process
      • It is troublesome to synchronize and lock/unlock between threads
      • Each thread shares the address space with the main program, limited to 2GB address space
      • After reaching a certain number of threads, even if the CPU is increased, the performance cannot be improved
      • The total performance that threads can improve is limited, and when there are more threads, the scheduling of the threads themselves also needs to consume more CPU
    • Coroutine

      • No thread context switching overhead
      • Convenient to switch control flow and simplify programming model
      • High concurrency, high scalability, low cost
      • The coroutine requires users to write their own scheduling logic
      • Unable to use multi-core resources: the essence of coroutine is single-threaded, it cannot use the multi-core of a single CPU at the same time, and the coroutine needs to cooperate with the process to run on the multi-core CPU
      • Process blocking operation will block the entire program
  3. The difference and connection between process and thread

    • Process is the smallest unit of resource allocation, thread is the smallest unit of cpu scheduling
    • A thread can only belong to one process, and a process can have multiple threads, but at least one thread
    • Resources are allocated to the process, all threads of the same process share all the resources of the process;
    • Concurrency: Not only can processes be executed concurrently, but also between multiple threads of the same process
    • System overhead: Multi-process programs are more robust than multi-threaded programs, but when switching between processes, it consumes more resources and is less efficient
  4. Compared with multithreading, what are the advantages of coroutine?

    • Extremely high execution efficiency: because subprogram switching is not thread switching, but controlled by the program itself, there is no thread switching overhead. Compared with multithreading, the more threads, the more obvious the performance advantage of coroutine
    • There is no need for a multi-threaded lock mechanism: because there is only one thread, there is no conflict of writing variables at the same time, and the shared resources are controlled in the coroutine without locking, only the state is judged, so the execution efficiency is much higher than that of multi-threads.
  5. Process type

    • Interactive process: A process started by a shell. The interactive process can run either in the foreground or in the background.
    • Batch process: This kind of process has no connection with the terminal and is a sequence of processes.
    • Monitoring process (also known as daemon process): A process started when the Linux system starts and runs in the background.
  6. Process communication mechanism

    • Pipe (pipe):
      Pipe is a half-duplex communication method. Data can only flow in one direction, and it can only be used between processes that are related. The kinship of the process usually refers to the parent-child process relationship.
    • Named pipe (namedpipe): The
      named pipe is also a half-duplex communication method, but it allows communication between unrelated processes.
    • Semaphore (semophore): A
      semaphore is a counter that can be used to control access to shared resources by multiple processes. It is often used as a locking mechanism to prevent other processes from accessing the shared resource when a process is accessing it. Therefore, it is mainly used as a means of synchronization between processes and between different threads in the same process.
    • Message queue (messagequeue): The
      message queue is a linked list of messages, stored in the kernel and identified by the message queue identifier. The message queue overcomes the shortcomings of less signal transmission information, the pipeline can only carry unformatted byte streams, and the limited buffer size.
    • Signal (sinal):
      Signal is a more complex communication method used to notify the receiving process that an event has occurred.
    • Shared memory (shared memory):
      Shared memory is to map a section of memory that can be accessed by other processes. This shared memory is created by one process but can be accessed by multiple processes. Shared memory is the fastest IPC method. It is specifically designed for the low efficiency of other inter-process communication methods. It is often used in conjunction with other communication mechanisms, such as signal two, to achieve synchronization and communication between processes.
    • Socket (socket):
      Socket is also an inter-process communication mechanism. Different from other communication mechanisms, it can be used for communication between different devices and processes.

Guess you like

Origin blog.csdn.net/qq_44924544/article/details/108912513