当前位置:网站首页>Embedded interview questions (I: process and thread)
Embedded interview questions (I: process and thread)
2022-07-06 05:40:00 【Sunspot ball】
One 、 Processes and threads
1、 What is a process 、 Threads , What's the difference? ?
Process is resource (CPU、 Memory, etc. ) Assigned base unit , Thread is CPU Basic units for scheduling and distribution ( The smallest unit of program execution ). At the same time , If CPU It's a single core , Only one process is executing , The so-called concurrent execution , It is also executed sequentially , Just because the switching speed is too fast , You think these processes are executing synchronously . Multicore CPU You can have multiple processes executing at the same point in time .
2、 Multi process 、 The advantages and disadvantages of multithreading
explain : A process is controlled by a process control block 、 Data segment 、 Code segment composition , The process itself cannot run programs , But like a container , First create a main thread , Allocate certain system resources to the main thread , At this time, you can start to implement various functions in the main thread . When we need to implement more complex functions , You can create multiple child threads in the main thread , Multiple threads in the same process , Use the system resources owned by this process to cooperate to complete some functions .
Advantages and disadvantages :1) The death of one process does not affect other processes , A thread crash is likely to affect the whole process in which it is located .2) The system cost of creating multiple processes is greater than that of creating multiple threads .3) Multi process communication because it needs to cross process boundaries , It is not suitable for the transmission of large amounts of data , It is suitable for the transmission of small data or dense data . Multithreading does not need to cross process boundaries , It is suitable for the transmission of a large amount of data between threads . And multithreading can share shared memory and variables in the same process .
3、 When to use the process , When to use threads
1) Create and destroy more frequently used threads , Because the creation process costs a lot .2) Requires a lot of data transfer using threads , Because multithreading switching is fast , There is no need to cross process boundaries .3) Secure and stable selection process ; Select threads quickly and frequently ;
4、 Multi process 、 Multithreading synchronization ( Communications ) Methods
Interprocess communication :
(1) Famous pipeline / Nameless pipe (2) The signal (3) Shared memory (4) Message queue (5) Semaphore (6)socket
Thread communication ( lock ):
(1) Semaphore (2) Read-write lock (3) Condition variables, (4) The mutex (5) spinlocks
5、 State transition diagram of process thread
(1) Ready state : The process has been granted except CPU All necessary resources outside , Just wait CPU State of . A system will arrange multiple processes in the ready state into a ready queue .
(2) Execution status : The process has been CPU, Being implemented . In a single processor system , There is only one process in the execution state ; In a multiprocessor system , There are multiple processes in execution state .
(3) Blocked state : The executing process cannot continue for some reason , And then abandon the processor and put it in a suspended state , That is, the process execution is blocked .( This state is also called waiting state or blocking state )
Typical events that usually cause a process to block are : request I/O, Apply for buffer space, etc .
commonly , Queue up blocked processes , Some systems also integrate these blocks into multiple queues according to different blocking causes .
(1) be ready → perform
A process in a ready state , When the process scheduler assigns a processor to it , The process changes from ready state to execution state .
(2) perform → be ready
A process in an execution state is in its execution , The processor had to be given up because one of the time slots allocated to it had run out , So the process changes from execution state to ready state .
(3) perform → Blocking
When an executing process cannot continue because it is waiting for an event to occur , From execution state to blocking state .
(4) Blocking → be ready
Blocked process , If the event it is waiting for has happened , So the process changes from blocked state to ready state .
6、 The parent process 、 Subprocesses
Parent process call fork() in the future , Clone a child process , A code snippet in which the child process and the parent process have the same content 、 Data segments and user stacks . Whether the parent process or the child process executes first is not necessarily , see CPU. So we usually set the parent process to wait for the child process to finish executing .
7、 Explain what context switching is ?
You can have many angles , There is a process context , There is an interrupt context .
Process context : When a process is executing ,CPU The values in all registers of 、 The state of the process and the contents of the stack , When the kernel needs to switch to another process , It needs to save all the states of the current process , Save the process context of the current process , So that when the process is executed again , Able to restore the state when switching , Carry on .
Interrupt context : Due to trigger signal , Lead to CPU Interrupt the current process , Turn to another program . Then all resources of the current process should be saved , Such as stack and pointer . After saving, execute the interrupt handler instead , After the fast read is executed, return to , After returning, restore the resources of the previous process , Carry on .
边栏推荐
- 注释、接续、转义等符号
- 【经验】win11上安装visio
- 59. Spiral matrix
- 【华为机试真题详解】统计射击比赛成绩
- Pix2pix: image to image conversion using conditional countermeasure networks
- Redis message queue
- [experience] when ultralso makes a startup disk, there is an error: the disk / image capacity is too small
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- 【torch】|torch. nn. utils. clip_ grad_ norm_
- Codeforces Round #804 (Div. 2) Editorial(A-B)
猜你喜欢

Analysis of grammar elements in turtle Library
![[JVM] [Chapter 17] [garbage collector]](/img/f4/e6ff0e3edccf23399ec12b7913749a.jpg)
[JVM] [Chapter 17] [garbage collector]
![[experience] install Visio on win11](/img/f5/42bd597340d0aed9bfd13620bb0885.png)
[experience] install Visio on win11

大型网站如何选择比较好的云主机服务商?

What impact will frequent job hopping have on your career?

(column 22) typical column questions of C language: delete the specified letters in the string.

RustDesk 搭建一个自己的远程桌面中继服务器

B站刘二大人-反向传播

Vulhub vulnerability recurrence 68_ ThinkPHP
![[mask requirements of OSPF and Isis in multi access network]](/img/7d/1ba80bb906caa9be4bef165ac26d2c.png)
[mask requirements of OSPF and Isis in multi access network]
随机推荐
AUTOSAR从入门到精通番外篇(十)-嵌入式S19文件解析
Codeforces Round #804 (Div. 2) Editorial(A-B)
Improve jpopup to realize dynamic control disable
PDK工藝庫安裝-CSMC
B站刘二大人-线性回归 Pytorch
[string] palindrome string of codeup
巨杉数据库再次亮相金交会,共建数字经济新时代
SQLite add index
算法-- 爬楼梯(Kotlin)
[force buckle]43 String multiplication
Vulhub vulnerability recurrence 67_ Supervisor
Safe mode on Windows
Quantitative description of ANC noise reduction
26file filter anonymous inner class and lambda optimization
28io stream, byte output stream writes multiple bytes
网站进行服务器迁移前应做好哪些准备?
[Jiudu OJ 08] simple search x
Sequoiadb Lake warehouse integrated distributed database, June 2022 issue
Vulhub vulnerability recurrence 71_ Unomi
移植InfoNES到STM32