当前位置:网站首页>Interview process and thread
Interview process and thread
2022-06-09 05:18:00 【wolf_ break】
List of articles
0. Reference article
- Deep understanding of threads and processes
- UNP — <UNIX Network Programming Volume 1: Socket networking API>
1. Definition
1.1 Process definition
Process is the smallest unit of operating system resource allocation , Threads are the smallest unit of task scheduling and execution .
process , It is the basic unit of allocating and managing resources during the execution of concurrent programs , Represents a logical control flow , Have a separate virtual memory address space . The following is the virtual space allocation model of the process .
For further discussion on the virtual space of processes, you can browse linux Step by step exploration of process address space One article .
1.2 Thread definition
Threads are the basic unit of independent scheduling and dispatching , It can also execute concurrently , Sharing process resources , Is a lightweight entity .
The multithreaded process address space is as follows :
2. expenses
Process passing fork Create , Threads pass through clone Create .
2.1 Process switching overhead
fork It's expensive , When a child process is created, the memory image of the parent process is copied to the child process ,fork Not just copying the page table structure ( The virtual address corresponds to the physical address ), The file descriptor table of the parent process is also copied (linux Everything below is a document ), Signal control table , Process information , Register resources, etc . It is a deep replication .
Learn more about : Despite the write - time replication technology (fork When it was first created, it adopted shared technology , Only pointer to the physical resources of the parent process , When a child process needs these resources to write , Only then can a piece of physical resources be copied for the child process to use ).
The operation of a process depends on these resources , When switching process context , These resources need to be saved . This is also the defect of multi process switching .
2.2 Thread cost
We know , A thread is one that shares the resources of a process , When a thread makes a context switch , Just save some simple information about the thread running , So thread switching is lightweight .
say concretely , All threads in the same process share not only global variables, but also :
- Most of the data
- File descriptor
- Signal processing functions and signal processing
- Current working directory
- user id And groups id
- Process instruction
Threads have their own - Threads id
- Register set ( Program counter and stack pointer )
- Stack
- errno
- Signal mask
- priority
3. communication mode
3.1 Interprocess communication
Different processes have different virtual address spaces , Therefore, the communication between processes cannot simply share memory directly . Common process methods are
- The Conduit : Half duplex communication , It can only flow in one direction , Suitable for communication between parent and child processes
- Famous pipeline : Similar pipe , Allow unrelated processes to communicate .
- Shared memory : Shared memory is mapping a piece of memory that can be accessed by other processes , This shared memory is created by a process , But multiple processes can access . Shared memory is the fastest IPC The way , The semaphore is used to realize the synchronization and communication between processes
- Message queue : Link table of messages , Stored in the kernel . A message queue consists of an identifier ( That's the queue ID) To mark
- Semaphore : It's a counter , Used to realize mutual exclusion and synchronization between processes , Do not store messages .
- Socket
Specific explanations and demo For examples, please refer to the article Interprocess communication (IPC)
3.2 Inter thread communication
A thread is the memory space of a shared process , So consider mutual exclusion and synchronization . Common measures are :
- lock ( Mutually exclusive ): The mutex 、 spinlocks 、 Read-write lock . Lock mutex before accessing shared resources , Release the mutex after the access is completed , Used to prevent simultaneous access to a shared variable .
- Condition variables, ( Sync ): It is applicable to a thread that sends a signal to other threads through condition variables after completing a certain action , Other threads do some more actions . Go to sleep while waiting for certain conditions .
- Semaphore ( Sync ): Using thread semaphores can efficiently complete thread based resource counting
Brief introduction and function use reference unp Chapter 26 or [linux Basics ——linux Summary of inter thread communication and synchronization mechanism (https://blog.csdn.net/a987073381/article/details/52029070).
边栏推荐
- [series of troubles caused by inaccurate positioning] Part 2: what's wrong with the weak satellite signal
- Remove duplicates from sort array -leetcode
- Transaction code qc51 of SAP QM preliminary level creates quality certificate for purchase order
- Intranet penetration hash delivery attack
- 内网渗透 - 哈希传递攻击
- STM32 FreeRTOS task Basics
- P1779 Xiaohu's springboard
- validate-npm-package-name
- wps ppt背景图片如何换颜色
- Faster RCNN
猜你喜欢
随机推荐
Load research of Marathon LB
Apache devlake code base guide
Typescript learning [5] type
How WPS ppt pictures come out one by one
Apache Devlake 代码库导览
^26浏览器的内核
Openstack Learning Series 12: installing CEPH and docking openstack
myql报错 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
Sword finger: duplicate number in array (JS)
Simple process and problem handling of cmdbuilding
function
Built in objects for typescript
Make in-depth research and summary, go to a higher level, and make new year's resolutions
Product weekly report issue 28 | CSDN editor upgrade, adding the function of inserting existing videos
June 2022 Tsinghua Management Tsinghua University Ning Xiangdong
application. Properties mysql. cj. jdbc. Driver red
Smart curly bracket escape
pytest_allure优先级、fixture-scope参数介绍
ETF operation practice record: March 2, 2022
P1743 Audiophobia




![Typescript learning [7] advanced types: Union type and cross type](/img/c7/91d104f948cb5c7f5d33854ca374d1.png)



