Interview recitation version - operating system

Interview Stereotype - Operating System

1. Process and thread

1. Process thread difference

  • Fundamental difference : Process is the basic unit of operating system resource allocation, while thread is the basic unit of task scheduling and execution
  • Environment : Multiple processes (programs) can run simultaneously in the operating system; while multiple threads execute simultaneously in the same process (program) (through CPU scheduling, only one thread executes in each time slice)
  • In terms of overhead : each process has an independent code and data space (program context), switching between programs will have a large overhead; threads can be regarded as lightweight processes, and threads of the same type share code and data spaces. Each thread has its own independent running stack and program counter (PC), and the overhead of switching between threads is small.
  • Memory allocation : the system will allocate different memory space for each process when it is running; for threads, except for CPU, the system will not allocate memory for threads (the resources used by threads come from the resources of the process to which they belong), Only resources can be shared between thread groups.
  • Inclusion relationship : A process contains at least one thread.

2. What is the difference between a coroutine and a thread?

  • Both threads and processes are synchronous mechanisms, while coroutines are asynchronous mechanisms.
  • Threads are preemptive, while coroutines are non-preemptive. The user needs to release the right to switch to other coroutines, so only one coroutine has the right to run at the same time, which is equivalent to the ability of a single thread.
  • A thread can have multiple coroutines, and a process can also have multiple coroutines.
  • Coroutines are not managed by the operating system kernel, but completely controlled by the program. A thread is a divided CPU resource, a coroutine is an organized code flow, and a thread is a resource of a coroutine. But coroutines do not directly use threads, and coroutines directly use any thread or thread pool associated with the executor.
  • Coroutines retain the state of the last call.

3. The difference between multi-process and multi-thread

The most intuitive process is a pid. The official statement is: a process is an execution activity of a program on a computer.
The call to create a child process under linux is fork();
insert image description here

4. The core parameters of the thread pool;

1.1 Why use a thread pool?

  1. Thread reuse. The reuse of threads is the focus of the thread pool design. If you need to open 1000 threads to execute the program, the system will create 1000 threads. If you use the thread pool to execute 1000 tasks, you don’t need to open 1000 threads. You only need to set the corePoolSize core The number of threads, the maximum number of threads, and the size of the queue can reuse thread replacement tasks.
  2. Better thread management. ThreadPoolExecutor can control the number of threads, and set the number of queues and saturation policies according to actual application scenarios.

1.2 Parameters of the thread pool

parameter name Parameter meaning
corePoolSize number of core threads
maxinumPoolSize Maximum number of threads
keepAliveTime Idle thread lifetime
unit unit of survival time
workQueue Store thread task queue
threadFactory Thread factory, to create new threads
handler The thread pool rejects the processed task
  • Among the parameters shown above, the third and fourth parameters are regarded as one, a combination of time and unit
  • corePoolSize represents the number of core threads, that is, the number of threads that create work under normal circumstances. These threads will not be eliminated after creation, but a resident thread
  • maximumPoolSize represents the maximum number of threads, which corresponds to the number of core threads, and represents the maximum number of threads allowed to be created. For example, if there are many current tasks and the number of core threads is used up, and the demand cannot be met, it will be created at this time New threads, but the total number of threads in the thread pool will not exceed the maximum number of threads
  • keepAliveTime, unit indicates the idle survival time of threads beyond the number of core threads, that is, the core threads will not be eliminated, but some threads exceeding the number of core threads will be eliminated if they are idle for a certain period of time, we can set the idle time through setKeepAliveTime time
  • workQueue is used to store the tasks to be executed. Assuming that all the core threads have been used now, and there are tasks coming in, all of them will be put into the queue until the entire queue is full but the tasks continue to enter, and new threads will be created.
  • ThreadFactory is actually a thread factory used to produce threads to perform tasks. We can choose to use the default creation factory, and the generated threads are all in the same group, have the same priority, and are not daemon threads. Of course, we can also choose to customize the thread factory. Generally, we will formulate different thread factories according to the business.
  • Handler task rejection strategy, there are two cases, the first one is when we call shutdown and other methods to close the thread pool, even if there are unfinished tasks in the thread pool, but because the thread pool has been closed, we will If you continue to think about submitting tasks to the thread pool, you will be rejected. Another situation is that when the maximum number of threads is reached and the thread pool is no longer capable of processing newly submitted tasks, this is also rejected.

5. Which method of process communication is the fastest? Why?

Link: Remember an interview with Tencent: What communication methods are there between processes? How to communicate? ---- Say goodbye to rote memorization .

  1. Pipeline
    Let's look at a Linux statement
netstat -tulnp | grep 8080

Among them, "|" means pipeline, and its function is to use the output of the previous command as the input of the next command. Here is to use the output of netstat -tulnp as the input of grep 8080 command. If two processes want to communicate, they can use this pipe to communicate, and we can know that this vertical line has no name, so we call this communication method an anonymous pipe.

And this communication method is one-way, only the output of the first command can be used as the input of the second command. If the processes want to communicate with each other, two pipelines need to be created.

There are anonymous pipes, which means that there are named pipes. Let's create a named pipe.

mkfifo  test   # 这条命令创建了一个名字为 test 的命名管道。

Next, we use a process to write data into this pipe, and then another process reads the data inside.

echo "this is a pipe" > test   // 写数据

If the contents of the pipeline are not read out at this time, the command will stop here forever, and the command will end only when another process reads out the contents of test. Next we use another process to read.

cat < test  // 读数据

As can be seen from the above example, the notification mechanism of the pipeline is similar to caching , just like a process puts data in a certain cache area, and then waits for another process to get it, and the pipeline is a one-way transmission .

  1. Message queue
    If process a wants to send a message to process b, it only needs to put the message in the corresponding message queue, and process b will go to the corresponding message queue when it needs it.

  2. Shared memory
    Shared memory is to map a section of memory that can be accessed by other processes. This shared memory is created by one process, but multiple processes can access it. Shared memory is the fastest method of IPC, and it is specifically designed to run inefficiently when other methods of interprocess communication run. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.

  3. Semaphore
    A semaphore is a counter that can be used to control access to shared resources by multiple processes. It is often used as a locking mechanism to prevent other processes from accessing shared resources while a process is accessing the resource. Therefore, it is mainly used as a means of synchronization between processes and between different threads in the same process.

  4. Socket
    socket is also an inter-process communication mechanism. Unlike other communication mechanisms, it can be used for process communication between different processes.

6. Mutual exclusion and synchronization of threads:

Mutual exclusion : refers to a resource that only allows one visitor to access it at the same time, which is unique and exclusive. But mutual exclusion cannot limit the order in which visitors access resources, that is, access is out of order.

Synchronization : refers to the sequential access of visitors to resources through other mechanisms on the basis of mutual exclusion (in most cases). In most cases, synchronization already implements mutual exclusion, and in particular all writes to resources must be mutually exclusive. In rare cases, multiple visitors can be allowed to access resources at the same time.

7. What are the methods of thread synchronization?

  • Critical section : A critical section object can be used when multiple threads access an exclusive shared resource. A thread with a critical section can access protected resources or code segments. If other threads want to access, they will be suspended until the thread with the critical section gives up the critical section, so as to achieve the purpose of operating shared resources in an atomic manner.
  • Event : The event mechanism allows a thread to actively wake up another thread to perform the task after processing a task.
  • Mutex : Mutex objects are very similar to critical section objects, except that they are allowed to be used between processes, while critical sections are only limited to use between threads of the same process, but they are more resource-saving and efficient.
  • Semaphore : A "semaphore" object can be used when a counter is needed to limit the number of threads that can use a shared resource.

Difference : The function of a mutex is very similar to that of a critical section, but the mutex can be named, which means that the mutex can be used across processes, but more resources are required to create a mutex, so if it is only for the process If used internally, using critical sections will bring speed advantages and reduce resource usage. Because the mutex is a cross-process mutex, once it is created, it can be opened by name.
Mutexes, semaphores, and events can all be used across processes to synchronize data operations.

8. Process switching

Process switching is mainly divided into two steps:

  1. Switch the page table to use the new address space. Once the context is switched, all cached memory addresses in the processor are invalidated.
  2. Switch kernel stack and hardware context.

For Linux, the biggest difference between a thread and a process lies in the address space. For thread switching, the first step does not need to be done, and the second step is required for both process and thread switching.
Because each process has its own virtual address space, and threads share the virtual address space of the process, threads in the same process do not involve virtual address space conversion when switching threads.

9. Why is it time-consuming to switch between virtual address spaces?

A process has its own virtual address space. To convert a virtual address into a physical address, you need to look up the page table. Page table lookup is a very slow process. Therefore, Cache is usually used to cache common address mappings, which can speed up page table lookup. Cache is TLB (translation Lookaside Buffer, TLB is essentially a Cache, which is used to speed up page table lookup)

Since each process has its own virtual address space, obviously each process has its own page table, so when the process is switched, the page table must also be switched. After the page table is switched, the TLB will become invalid, and the cache failure will lead to a hit rate. If the virtual address is lowered, the conversion of virtual address to physical address will be slowed down, which means that the program will run slower, and thread switching will not cause TLB failure, because threads do not need to switch address spaces, so we usually say that thread switching should be compared with processes Switch blocks, and here's why.

10. What kind of switching does thread switching involve?

11. How is the thread awakened?

12. What is the difference between a daemon thread and a user thread?

10.1 User (User) thread:

Running in the foreground, performing specific tasks, such as the main thread of the program, the sub-threads connected to the network, etc. are all user threads

10.2 Daemon thread:

  • Runs in the background and serves other foreground threads (non-daemon threads). When all user threads finish running , the daemon thread will finish working along with the JVM. It can be seen that the daemon thread is dependent on the user thread. When all user threads exit, the daemon thread will also exit. Typical daemon threads are garbage collection threads. The user thread exists independently and will not exit because other user threads exit.

Precautions:

  • setDaemon(true) must be executed before the start() method, otherwise an IllegalThreadStateException will be thrown
  • A new thread spawned in a daemon thread is also a daemon thread
  • Not all tasks can be assigned to daemon threads for execution, such as read and write operations or calculation logic
  • The contents of the finally block cannot be relied upon in a Daemon thread to ensure that logic to close or clean up resources is executed. Because we also said above that once all user threads finish running, the daemon thread will finish working together with the JVM, so the finally statement block in the daemon thread may not be executed.

13. What are the process scheduling strategies?

  • First come, first served : Non-preemptive scheduling algorithm, scheduling according to the order of requests. It is good for long jobs, but it is not good for short jobs, because short jobs have to wait for the previous long jobs to be executed before they can be executed, and long jobs need to be executed for a long time, resulting in too long waiting time for short jobs. In addition, it is not good for I/O-intensive processes, because such processes have to be re-queued after each I/O operation.
  • Shortest job first : A non-preemptive scheduling algorithm that schedules in the order with the shortest estimated running time. Long jobs may starve to death and are in a state of waiting for short jobs to finish. Because if short jobs keep coming, long jobs will never be scheduled.
  • Shortest Remaining Time First : A preemptive version of shortest job first, scheduled in order of remaining run time. When a new job arrives, its overall running time is compared with the remaining time of the current process. If the new process requires less time, suspend the current process and run the new process. Otherwise the new process waits.
  • Time slice rotation : Arrange all ready processes into a queue according to the principle of FCFS, and allocate CPU time to the queue leader process each time it is scheduled, and this process can execute a time slice. When the time slice is used up, the timer sends a clock interrupt, and the scheduler stops the execution of the process and sends it to the end of the ready queue, while continuing to allocate CPU time to the process at the head of the queue.
    The efficiency of the time slice rotation algorithm has a lot to do with the size of the time slice : because process switching must save the process information and load the information of the new process, if the time slice is too small, the process switching will be too frequent. would take too much time. And if the time slice is too long, real-time performance cannot be guaranteed.
  • Priority scheduling : Assign a priority to each process and schedule according to the priority. To prevent low-priority processes from never waiting to be scheduled, the priority of waiting processes can be increased over time.

14. What are the states of a process?

There are 5 states in total for a process, which are created, ready, running (executed), terminated, and blocked.
insert image description here

  • The running state is that the process is running on the CPU. In a uniprocessor environment, at most one process is running at a time.
  • The ready state means that the process is ready to run, that is, the process has obtained all the required resources except the CPU, and can run once it obtains the CPU.
  • The blocked state is when a process is suspended while waiting for an event, such as waiting for a resource to become available or for I/O to complete. Even if the CPU is idle, the process cannot run.

Running state→blocking state : It is often caused by waiting for peripherals, waiting for resource allocation such as main memory, or waiting for manual intervention.
Blocked state→ready state : the waiting condition has been met, and it can run after being allocated to the processor.
Running state→ready state : It is not due to its own reasons, but because of external reasons, the process in the running state gives up the processor, and then it becomes the ready state. For example, the time slice is used up, or there is a higher priority process to preempt the processor, etc.
Ready state→Running state : The system selects a process in the ready queue to occupy the processor according to a certain strategy, and it becomes the running state at this time.

15. How to create a thread

Under Linux:

16. How to create a child process

Under windows:

Header file: #include <unistd.h>

Create a new process: pid_t fork(void)

If error returns -1

Fork calls once and returns twice, the original process returns the pid of the new process (parent process), and returns 0 in the new process (child process)

17. About the fork function

A process, including code, data, and resources allocated to the process. The fork() function creates a process that is almost identical to the original process through a system call, that is, two processes can do exactly the same thing, but if the initial parameters or the variables passed in are different, the two processes can also do different things .
After a process calls the fork() function, the system first allocates resources to the new process, such as space for storing data and code. Then copy all the values ​​from the original process to the new new process, only a few values ​​are different from the original process. It's like cloning yourself.

Why are the fpids of the two processes different? This is related to the characteristics of the fork function. A wonderful thing about the fork call is that it is only called once, but it can return twice. It may have three different return values:

  • In the parent process, fork returns the process ID of the newly created child process;
  • In the child process, fork returns 0;
  • If an error occurs, fork returns a negative value;

After the fork function is executed, if the new process is successfully created, two processes will appear, one is the child process and the other is the parent process. In the child process, the fork function returns 0, and in the parent process, fork returns the process ID of the newly created child process. We can judge whether the current process is a child process or a parent process by the value returned by fork.

There may be two reasons for the fork error: the number of processes has reached the upper limit specified by the system; the system memory is insufficient.

18. Three special processes

What is PID

There are many processes running under the OS. In order to better manage the processes,
a unique number (non-negative integer) is assigned to each process.
If the current process ends, this PID can be reused.

We check the PID on the linux platform:
command: ps -aux

Main view process PID through task manager under windows

Process with PID == 0: daemon process

  • When we check the process PID, we cannot see the process with PID 0
  • This process is also known as the scheduler process
  • The function is to realize the scheduling and switching between processes
  • Let the CPU execute all processes in rotation according to the scheduling algorithm
  • A process instruction address is placed in the PC. When the PC executes different processes, the CPU executes different processes, which is to realize process switching.

Process with PID == 1:
initialization, also known as the init process

  • To read various system files and use the data in the files to initialize the OS startup
  • Let the operating system enter the multi-user state, that is, let the OS support multi-user login

Manage Orphaned Processes: Take care of orphaned processes.

Original parent process:

  • Under the linux platform and windows platform, almost all processes are born through the parent process, and then the process with PID == 1 is the original parent process

Process with PID == 2: daemon process, responsible for paging operation of virtual memory

What is a paging operation?
When the OS supports the virtual memory mechanism, loading the application program into the memory does not copy the complete code, but only copies the part of the code to be run by the current process. After this part of the code is finished running, another part of the code that needs to be run will be copied into the memory. When copying, the operation is performed page by page, and each page is 4096 bytes.

19. Critical resources and critical sections

  • Critical resources :
    A critical resource is a shared resource that only one process is allowed to use at a time. Each process adopts a mutually exclusive method, and the shared resources are called critical resources. Hardware that is a critical resource includes printers, tape drives, etc.; software includes message queues, variables, arrays, buffers, etc. Mutual exclusion is adopted among the various processes to realize the sharing of this resource.
  • Critical section :
    The code that accesses critical resources in each process is called a critical section . Only one process is allowed to enter the critical section at a time. After entering, other processes are not allowed to enter. Regardless of whether it is a hardware-critical resource or a software-critical resource, multiple processes must mutually exclusive access to it. A critical section in which multiple processes refer to the same critical resource is called a dependent critical section . When using a critical section, it is generally not allowed to run for too long. As long as the thread running in the critical section has not left, all other threads entering this critical section will be suspended and enter the waiting state, which will affect the program to a certain extent. run performance.

2. Deadlock

1. What is deadlock?

Deadlock refers to a phenomenon in which two or more processes (threads) are blocked due to competition for resources or communication with each other during execution. If there is no external force, they will not be able to advance. At this time, the system is said to be in a deadlock state or the system has a deadlock, and these processes (threads) that are always waiting for each other are called deadlock processes (threads).

For example, there is only one printer and one input device in a certain computer system, process A is occupying the input device, and at the same time makes a request to use the printer, but at this time the printer is being occupied by process B, and B has not released the printer Earlier, a request was made to use the input device being occupied by A. In this way, the two processes wait endlessly for each other, and neither can continue to execute. At this time, the two processes fall into a deadlock state.

Multiple threads are blocked at the same time, one or all of them are waiting for a resource to be released. Since the thread is blocked indefinitely, it is impossible for the program to terminate gracefully.

As shown in the figure below, thread A holds resource 2, and thread B holds resource 1. They both want to apply for each other's resources at the same time, so the two threads will wait for each other and enter a deadlock state.

2. What are the four necessary conditions for a deadlock to form?

  1. Mutual exclusion: A certain resource can only be accessed by one process at a time, that is, once the resource is allocated to a process, other processes cannot access it until the process access ends.
  2. Possessing and waiting: A process itself occupies resources (one or more types), and at the same time, there are still unsatisfied resources, and it is waiting for other processes to release the resources.
  3. No preemption: Someone else has already occupied a certain resource, and you cannot snatch other people's resources just because you also need the resource.
  4. Circular waiting: There exists a chain of processes such that each process holds at least one resource needed by the next process.

When the above four conditions are met, a deadlock will inevitably result. On the contrary, as long as one of the above conditions is not satisfied, a deadlock will not occur.

Deadlocked processes cannot proceed, nor can the resources they hold be released. This will cause the throughput of the CPU to drop. Therefore, the deadlock situation will waste system resources and affect the performance of the computer.

3. How to deal with the deadlock problem

Commonly used methods to deal with deadlocks are: deadlock prevention, deadlock avoidance, deadlock detection, deadlock resolution, and ostrich strategy.

4. How can we avoid deadlock?

The basic idea of ​​deadlock avoidance: the system dynamically checks the resource application issued by the process, and decides whether to allocate resources according to the check results. If the system may deadlock after allocation, it will not be allocated, otherwise it will be allocated. This is a dynamic strategy to ensure that the system does not enter a deadlock state. The banker's algorithm is a classic deadlock avoidance algorithm.

5. Deadlock prevention

Understanding the causes of deadlocks, especially the four necessary conditions for deadlocks, can avoid, prevent and resolve deadlocks as much as possible. As long as one of the four necessary conditions is broken, deadlocks can be effectively prevented:

  1. Break mutual exclusion conditions: transform exclusive resources into virtual resources, and most resources can no longer be transformed.
  2. Break the non-preemption condition: When a process occupies an exclusive resource and then applies for an exclusive resource that cannot be satisfied, the originally occupied resource will be withdrawn.
  3. Break the possession and waiting condition: adopt the resource pre-allocation strategy, that is, apply for all resources before the process runs, run if it is satisfied, or wait, so that it will not occupy and apply.
  4. Break the circular waiting condition: realize the resource allocation strategy in an orderly manner, implement classification and numbering for all devices, and all processes can only apply for resources in the form of increasing serial numbers.

6. What is the difference between deadlock avoidance and deadlock prevention?

Deadlock prevention is to try to destroy at least one of the four necessary conditions for deadlock, and strictly prevent the occurrence of deadlock; while deadlock avoidance is not so strict to limit the existence of necessary conditions for deadlock, because even deadlock A necessary condition exists, and a deadlock does not necessarily occur. Deadlock avoidance is to pay attention to avoid the eventual occurrence of deadlock during system operation.

7. Use tools to troubleshoot deadlock problems?

If you want to check whether your Java program is deadlocked, you can use the jstack tool, which is a thread stack analysis tool that comes with jdk.
Under Linux, we can use pstack + gdb tools to locate deadlock problems.
The pstack command can display the stack trace information (function call process) of each thread. Then, when locating the deadlock problem, we can execute the pstack command multiple times to view the function call process of the thread, compare the results multiple times, and confirm which threads There has been no change, and it is because of waiting for a lock, so the high probability is caused by a deadlock problem.

3. Memory management

1. The function of memory management

Memory management is mainly to improve memory utilization, and memory can be logically expanded through virtual technology.

The functions of memory management are :

  • Allocation and recovery of memory space : The allocation and management of main memory space is completed by the operating system, freeing programmers from the trouble of memory allocation and improving programming efficiency.
  • Address translation : In a multiprogramming environment, the logical address in the program cannot be consistent with the physical address in the memory. Therefore, the storage management must provide an address translation function to convert the logical address into the corresponding physical address.
  • Expansion of memory space : Use virtual storage technology or automatic overlay technology to logically expand memory.
  • Storage protection : Ensure that each job runs in its own storage space without interfering with each other.

1. What is pagination?

Answer: Divide the memory space into equal-sized and fixed blocks as the basic unit of main memory. Because the program data is stored in different pages, and the pages are discretely distributed in the memory, a page table is needed to record the mapping relationship to realize the mapping from the page number to the physical block number.

Accessing the memory data in the paging system requires two memory accesses (one is to access the page table from the memory, find the specified physical block number from it, and add the offset in the page to obtain the actual physical address; the second is to obtain the actual physical address based on the first time) The physical address access memory to fetch data).
insert image description here

2. What is segmentation?

Answer: Paging is to improve memory utilization, and segmentation is to meet some logic requirements of programmers when writing code (such as data sharing, data protection, dynamic linking, etc.).

In segmented memory management, the address is two-dimensional, one-dimensional is the segment number, and two-dimensional is the address within the segment; the length of each segment is different, and the addressing of each segment starts from 0 . In segment management, each segment is allocated with continuous memory, but segments are allocated discretely, so there is also a mapping relationship between logical addresses and physical addresses, corresponding to the segment table mechanism.

3. What is the difference between paging and segmentation?

  • Paging is transparent to the programmer, but segmentation requires the programmer to explicitly divide each segment.
  • The address space of paging is a one-dimensional address space, and segmentation is two-dimensional.
  • The page size is immutable, and the segment size can be changed dynamically.
  • Paging is mainly used to implement virtual memory to obtain a larger address space; segmentation is mainly to enable programs and data to be divided into logically independent address spaces and to facilitate sharing and protection.

4. What is swap space?

The operating system divides physical RAM (physical RAM) into small blocks of memory, and each block of memory is called a page . When memory resources are insufficient, Linux transfers the contents of certain pages to a space on the hard disk to free up memory space . That space on the hard disk is called swap space, and this process is called swapping. The total capacity of physical memory and swap space is the available capacity of virtual memory.

use:

  • When the physical memory is insufficient, some infrequently used pages can be swapped out to the system.
  • Many memory pages are used for initialization when the program starts, and then they are no longer needed and can be swapped out.

5. Virtual memory technology

Virtual memory means that the physical memory is expanded into a larger logical memory, so that the program can obtain more available memory. Virtual memory uses partial loading technology to load certain pages of a process or resource into the memory, so that more processes can be loaded, and even processes larger than the memory can be loaded, so that it seems that the memory has become larger. This part of the memory In fact, it contains a disk or hard disk, and it is called virtual memory.

6. What are the implementation methods of virtual memory?

In virtual memory, it is allowed to load a job into memory multiple times. When the continuous allocation method is used, a considerable part of the memory space will be in a temporary or permanent idle state, resulting in a serious waste of memory resources, and it is also impossible to expand the memory capacity logically. Therefore, the implementation of virtual memory needs to be based on discrete allocation of memory management. There are three ways to implement virtual memory:

  • Request paging storage management.
  • Request fragmentation storage management.
  • Request segment paging storage management.

7. Page Replacement Algorithm

  • Optimal Replacement Algorithm (OPT) (Ideal Replacement Algorithm): Remove pages that will never be needed from main memory; if no such page exists, select the page that will not be accessed for the longest time. Because the selected eliminated pages will never be used in the future, or pages that will not be accessed for the longest time, it can ensure the lowest page fault rate.
  • First-in-first-out replacement algorithm (FIFO): It is the simplest page replacement algorithm. The basic idea of ​​this algorithm is: when a page needs to be eliminated, the page that resides in the main memory for the longest time is always selected for elimination, that is, the page that first enters the main memory is eliminated first. The reason is that the pages that were first transferred into main memory are most likely to be no longer used.
  • The least recently used (LRU) algorithm: The basic idea of ​​this algorithm is to use the principle of locality to speculate on future behavior based on the past page access history of a job during execution. It assumes that pages that have not been visited in the past may not be visited again in the immediate future. Therefore, this algorithm can be described as: when a page needs to be eliminated, the page that has not been used for the longest time in the most recent period is always selected to be eliminated.
  • Clock (CLOCK) replacement algorithm

8. Tell me about IO multiplexing?

IO multiplexing refers to the kernel - once it finds that one or more IO conditions specified by a process are ready to be read, it notifies the process.

IO multiplexing is applicable to the following occasions:

  • When a client deals with multiple descriptors (typically interactive input and network sockets), I/O multiplexing must be used.
  • While it is possible, but rare, for a client to handle multiple sockets simultaneously.
  • If a TCP server has to handle both listening sockets and connected sockets, I/O multiplexing is generally used.
  • If a server needs to process both TCP and UDP, it is generally necessary to use I/O multiplexing.
  • If a server needs to handle multiple services or multiple protocols, it is generally necessary to use /O multiplexing.
  • Compared with multi-process and multi-thread technology, the biggest advantage of I/0 multiplexing technology is that the system overhead is small, the system does not need to create processes/threads, and does not need to maintain these processes/threads, thus greatly reducing the system overhead.

9. What is the difference between hard links and soft links?

  • A hard link is to create an entry in the directory, recording the file name and inode number, and this inode is the inode of the source file. Delete any entry, the file still exists, as long as the number of references is not zero. But hard links have limitations, they cannot span file systems, nor can they link directories.
  • The symbolic link file saves the absolute path where the source file is located, and it will be located on the source file when it is read, which can be understood as a Windows shortcut. When the source file is deleted, the linked file cannot be opened. Since paths are recorded, symlinks can be created for directories.

10. What is the interrupt processing process?

  1. Protect the scene: save the relevant data of the currently executing program in the register, and then push it into the stack.
  2. Enable interrupt: to respond to higher-level interrupt requests when executing interrupts.
  3. interrupt handling
  4. Disable Interrupts: Guaranteed not to be disturbed by new interrupts when resuming the scene
  5. Restoring the scene: Take out the program data in order from the stack, and restore the execution state before the interruption.

11. What are user mode and kernel mode?

User state and system state are two operating states of the operating system:

  • Kernel mode: Programs running in the kernel mode can access any data and resources of the computer without restriction, including peripheral devices, such as network cards, hard drives, etc. A CPU in kernel mode can switch from one program to another without preempting the CPU.
  • User mode: Programs running in user mode can only access memory in a limited manner, and can only directly read the data of user programs, and are not allowed to access peripheral devices. The CPU in user mode is not allowed to be monopolized, which means that the CPU can be used by other programs. Obtain.

Dividing the running state of the operating system into user mode and kernel mode is mainly to limit access capabilities and prevent system crashes caused by random more dangerous operations, such as setting the clock and memory cleanup, all of which need to be in the kernel mode Finish.

12. How to switch between user mode and kernel mode?

All user processes run in user mode, but as we said above, user programs have limited access capabilities. Some important operations, such as reading data from the hard disk and obtaining data from the keyboard, can only be done in the kernel mode. Things, but these data are very important to the user program. So it involves the conversion in two modes, that is, user mode -> kernel mode -> user mode , and the only thing that can do these operations is the system call, and the only thing that can execute the system call is the operating system.

- Generally, the conversion from user mode to kernel mode is called trap into the kernel, also known as trap instruction (trap instruction).

Their workflow is as follows:
insert image description here

13. What is the difference between select, poll and epoll?

(1) select: time complexity O(m)

The core function of elect is to call the poll function of the tcp file system to query continuously. If there is no desired data, it will actively execute a schedule (to prevent the cpu from being occupied all the time) until there is a connection with the desired message. It can be seen from here that the execution method of select is basically to call poll differently until there is a required message.

(2) poll: time complexity O(n)

Poll is essentially the same as select. It copies the array passed in by the user to the kernel space, and then queries the device status corresponding to each fd. However, it does not have a limit on the maximum number of connections because it is stored based on a linked list.

(3) epoll: time complexity 0(1)

epoll can be understood as event poll, different from busy polling and indiscriminate polling, epoll will notify us of which stream has occurred and what I/O event has occurred. So epoll is actually event-driven (each event is associated with fd).

select, poll, and epoll are all IO multiplexing mechanisms. I/O multiplexing is to monitor multiple descriptors through a mechanism, and once a descriptor is ready (usually read ready or write ready), the program is notified to perform corresponding read and write operations. But select, poll, and epoll are essentially synchronous I/O, because they all need to be responsible for reading and writing after the read and write events are ready, that is to say, the process of reading and writing is blocked, while asynchronous I/O does not need to Responsible for reading and writing, the implementation of asynchronous I/O will be responsible for copying data from the kernel to user space.

14. What happens on a page fault

The pages in the process linear address space do not need to be resident in memory. When executing an instruction, if it is found that the page he wants to access is not in the memory (that is, the existence bit is 0), then the execution of the instruction is stopped and a page does not exist For the exception, the corresponding fault handler can eliminate the fault by loading the page from the external memory, and then the instruction that caused the exception can continue to execute without generating an exception.

However, it also interrupts the processing:
1. When the current execution process is in the kernel mode: _do_kernel_fault
2. The page fault interrupt of the user process: _do_page_fault

Guess you like

Origin blog.csdn.net/luanfenlian0992/article/details/116273300