当前位置:网站首页>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 .

原网站

版权声明
本文为[Sunspot ball]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060535594601.html