linux_Thread Concept-Kernel Thread Implementation Principle-Thread Shared Resources-Thread Advantages and Disadvantages

  Today I started to share threads. Threads are an important knowledge point in programming. You need to study hard and focus on learning. Next, let's take a look at what threads are:

The catalog of articles published by this blogger on CSDN: My CSDN catalog, as a guide to the types of articles published by bloggers on CSDN

1. What is a thread

LWP: light weight process lightweight process, the essence is still a process (in the Linux environment)
   process: independent address space, with PCB
   thread: also has PCB, but no independent address space (shared)
   difference: whether to share the address space .
   Under Linux:
    thread: the smallest execution unit
    process: the smallest resource allocation unit, which can be regarded as a process with only one thread.

2. The principle of Linux kernel thread implementation

1、轻量级进程(light-weight process),也有PCB,创建线程使用的底层函数和进程一样,都是clone
2、从内核里看进程和线程是一样的,都有各自不同的PCB,但是PCB中指向内存资源的三级页表是相同的
3、进程可以蜕变成线程
4、线程可看做寄存器和栈的集合
5、在linux下,线程最是小的执行单位;进程是最小的分配资源单位

  View LWP number: ps –Lf pidView the lwp number of the specified thread. (LWP: thread number)
  For a process, the same address (the same virtual address) is used repeatedly in different processes without conflicting. The reason is that although they have the same virtual address, the page directory, page table, and physical page are different. The same virtual address is mapped to different physical page memory units, and finally accesses different physical pages.
  Threads are different, two threads have their own independent PCB, but share the same page directory, that is, share the same page table and physical page. So the two PCBs share one address space.
   In fact, whether it is fork to create a process or pthread_create to create a thread , the underlying implementation calls the same kernel function clone .
   If the address space of the other party is copied, a "process" will be produced; if the address space of the other party is shared, a "thread" will be produced.
   Therefore: The Linux kernel does not distinguish between processes and threads. The distinction is made only at the user level. Therefore, all thread operation functions pthread_ * are library functions, not system calls.

3. Threads share resources

1、文件描述符表
2、每种信号的处理方式
3、当前工作目录
4、用户ID和组ID
5、内存地址空间 (.text/.data/.bss/heap/共享库)

4. Thread non-shared resources

1、线程id
2、处理器现场和栈指针(内核栈)
3、独立的栈空间(用户空间栈)
4、errno变量
5、信号屏蔽字
6、调度优先级

5. Thread advantages and disadvantages

Advantages:
  1. Improve program concurrency
  2. Small overhead
  3. Convenient data communication and data sharing
Disadvantages:
  1. Library functions, unstable
  2. Difficult to debug and write, gdb does not support
  3. Poor signal support
  The advantages are relatively prominent, The shortcomings are not flawed. According to the implementation method under Linux, the difference between process and thread is not very big.

  The above is the sharing of this time, I hope it will be helpful to everyone, welcome to follow the blogger to learn more new knowledge together!

Guess you like

Origin blog.csdn.net/qq_44177918/article/details/130434935