编程知识 cdmana.com

Linux thread concept

stay LInux in , Threads are lightweight processes

Relationship between thread and process

  1. Lightweight process (light-weight process), Also have PCB, The underlying functions used to create threads are the same as processes clone
  2. From the kernel, processes and threads are the same , They're all different PCB, however PCB The three level page tables that point to memory resources in are the same
  3. Processes can degenerate into threads
  4. Threads are registers and stacks
  5. stay Linux Next , Thread is the smallest execution (CPU Dispatch ) Company , Process is the smallest unit of resource allocation

clone:

   When creating a process, use fork, Create a thread with creat, They all call the underlying functions clone, Can be cloned PCB,0~3G User space , The operating system does not provide a system call interface for users to create threads directly , So the big guys packaged a set of thread library to realize thread control , Therefore, the thread we create is also called user ecothread
   In kernel vfork Create a child process to share the same virtual address space , Fear of call stack confusion , Therefore, the parent process does not start running until the child process has finished or the program has been replaced

Multithreading pcb Use the same virtual address space : The implementation runs at the same time without the call stack confusion ---- Allocate a separate block of space in the virtual address space for each thread

Sharing resources between threads

  1. . Document descriptor table
  2. How each signal is processed , No shared signal mask word , Each thread has its own independent signal mask word
  3. Current working directory
  4. user ID And groups ID
  5. Memory address space

Non shared resources between threads

  1. Threads id
  2. Processor field and stack pointer ( Kernel stack )
  3. Independent stack space ( User space stack )
  4. errno Variable
  5. Signal mask word
  6. Scheduling priority

The advantages and disadvantages of threads

advantage :
   Improve the concurrency of programs
   Low overhead , No need to reallocate memory
   It is convenient to communicate and share data
   The execution granularity of thread is more fine
shortcoming :
   Thread instability ( Library function implementation )
   Thread debugging is more difficult (gdb Support is not good )
   Thread cannot be used UNIX Classic events , For example, signals
   Lack of access control between threads — system call (exit),
   The exception is for the entire process , Low robustness

版权声明
本文为[Ran1366]所创,转载请带上原文链接,感谢

Scroll to Top