当前位置:网站首页>1. Threads and processes

1. Threads and processes

2022-07-18 19:55:00 123_ YQH

Threads

1. Thread and process overview

Similar to the process , Threads are a mechanism that allows applications to perform multiple tasks concurrently . A process can contain multiple threads . All threads in the same program will execute the same program independently , And Share the same global memory area , This includes initializing data segments 、 Uninitialized data segment (BSS paragraph ), And heap memory segments .

② The process is CPU The smallest unit of allocation of resources , Threads are the smallest unit of scheduling execution in the operating system .

③ Threads are lightweight processes , stay LInux In this environment, the essence of thread is still process .

④ View the... Of the specified process LWP( Threads ) Number : ps -Lf pid

Such as : stay unbuntu Open Firefox in .

perform ps aux The process of checking Firefox browser is 42569

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-LuWT4B4W-1657785561256)(C:\Users\ThinkStation K\AppData\Roaming\Typora\typora-user-images\1657783228477.png)]

Input :ps -Lf 42569 Get the thread opened in the Firefox browser process .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-nHb3EJJp-1657785561257)(C:\Users\ThinkStation K\AppData\Roaming\Typora\typora-user-images\1657783342343.png)]

2. Thread is different from process

There are several differences :

  • Information between processes is difficult to share . Because except for the read-only code segment , The parent-child process does not share memory , Therefore must Adopt some process communication methods , Exchange information between processes .
  • call fork() The cost of creating a process is relatively high , Even with Share while reading , Copy on write technology , You still need to copy a variety of process attributes such as memory page tables and file descriptors , It means fork() The time cost of calling is still expensive .
  • Threads can be easily 、 Share information quickly . Just copy the data to the share ( Global or heap ) Variable .
  • Creating threads is faster than creating processes 10 Times or more . Inter thread time Sharing virtual space , No write time assignment is needed to copy memory , There is no need to copy the page table .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-GQI2JL6s-1657785561257)(C:\Users\ThinkStation K\AppData\Roaming\Typora\typora-user-images\1657784838746.png)]

原网站

版权声明
本文为[123_ YQH]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/199/202207161639581405.html