Eight-legged essay for interview - computer network and operating system

Table of contents

What is an operating system? please give a brief overview

What are the classifications of operating systems?

What is kernel mode and user mode?

How to switch between kernel mode and user mode?

The difference between concurrency and parallelism

What is a process?

Basic operations of the process

Briefly describe the inter-process communication method

How processes communicate through pipes

How do processes communicate through shared memory?

what is a signal

How to write correct and safe signal handler functions

Timing of process scheduling

Situations where process scheduling cannot be performed

Process Scheduling Policy

Basic Design Indicators for Process Scheduling Strategies

Process states and state transitions

What is an orphan process? Zombie process?

What are threads?

Why do you need threads?

Briefly describe the difference and connection between threads and processes

Basic API for processes and threads

multi-threaded model

Method of Process Synchronization

Method of Thread Synchronization

What is the difference between process synchronization and thread synchronization

How does a deadlock arise?

How to solve the deadlock problem?

What is a virtual address and what is a physical address?

What is virtual memory?

Why introduce virtual memory?

Common Page Replacement Algorithms

Please tell me what is copy-on-write?

The concept of a real-time operating system

What is priority inversion? How to solve

A brief description of the TCP/IP five-layer protocol

What does the physical layer do

What is the role of the data link layer

What does the network layer do

What does the transport layer do

What does the session layer do

What does the presentation layer do

What does the application layer do

The difference between TCP and UDP

Why TCP is reliable

Why UDP is unreliable

Briefly describe the phenomenon of TCP sticky packets

How to deal with TCP sticky packet phenomenon

Briefly describe the sliding window of TCP protocol

A Brief Introduction to Congestion Control of TCP Protocol

Brief description of fast retransmission

TCP three-way handshake process

Why does the TCP handshake need three times, can it be done twice?

Briefly describe the semi-join queue

Briefly describe SYN attack

TCP four wave process

Why does it take 4 times for TCP to wave

Why do I need to wait 2MSL when I wave four times to release the connection

A brief description of the DNS protocol

Briefly describe the DNS resolution process

A brief description of the HTTP protocol

A brief description of cookies

Brief session

Briefly describe the http status code and corresponding information

The difference between forwarding and redirection

Brief description of http1.0

Briefly describe the improvement of http1.1

Briefly describe the difference between HTTP short connection and long connection

Briefly describe the improvement of http2.0

The difference between http and https

Briefly describe the relationship between TLS/SSL, HTTP, and HTTPS

HTTPS connection process

The difference between Get and Post

Is there a size limit for Get method parameters?

Do you understand REST API?

What exactly happens when a URL is entered into a browser


What is an operating system? please give a brief overview

An operating system is a computer program that manages computer hardware and software resources and provides an interface between computer users and computer hardware systems.

Provide an interface to the user program upwards, and take over hardware resources downwards.

The operating system is essentially a piece of software. As the system software closest to the hardware, it is responsible for processor management, memory management, device management, file management, and providing user interfaces.

What are the classifications of operating systems?

Operating systems can be generally divided into batch operating systems, time-sharing operating systems, and real-time operating systems.

If an operating system takes into account both batch operations and time-sharing functions, the system is called a general-purpose operating system.

Common general-purpose operating systems include: Windows, Linux, MacOS, etc.

What is kernel mode and user mode?

In order to prevent the operating system and key data from being destroyed by user programs, the execution state of the processor is divided into kernel state and user state.

The kernel state is the state in which the operating system management program is executed. It can execute all instructions including privileged instructions, and can access all storage spaces in the system.

The user state is the state of the processor when the user program is executed. It cannot execute privileged instructions and can only access the user address space.

User programs run in user mode, and the operating system kernel runs in kernel mode.

How to switch between kernel mode and user mode?

There are three ways for the processor to switch from user mode to kernel mode: system calls, exceptions, and external interrupts.

  1. The system call is the smallest functional unit of the operating system and the user interface provided by the operating system. The system call itself is a soft interrupt.
  2. Exceptions, also called internal interrupts, are caused by errors, such as file corruption, page faults, etc.
  3. The external interrupt is to notify the processor of the state change of the peripheral through two signal lines, which is a hard interrupt.

The difference between concurrency and parallelism

  1. Concurrency: Refers to the macro view that two programs are running at the same time, such as multitasking on a single-core CPU. But from a microscopic point of view, the instructions of the two programs are interleaved and executed, and only one instruction is executed in a single cycle. This kind of concurrency does not improve the performance of the computer, but only improves efficiency (such as reducing the response time of a certain process).
  2. Parallelism: refers to the simultaneous operation in the strict physical sense, such as multi-core cpu, two programs run on two cores respectively, and the two do not affect each other. Each program runs its own instructions in a single cycle. That is, two instructions are executed. In this way, parallelism does improve the efficiency of the computer. So the current cpu is developing towards multi-core.

What is a process?

Process is one of the most important abstract concepts in the operating system, the basic unit of resource allocation, and the basic unit of independent operation.

The classic definition of a process is an instance of a program in execution. Every program in the system runs in the context of a process.

A context is made up of the state a program needs to run correctly. This state includes the program's code and data stored in memory, its stack, the contents of general-purpose registers, the program counter, environment variables, and a collection of open file descriptors.

A process generally consists of the following parts:

  1. The process control block PCB is the only sign of the existence of the process, including the process identifier PID, the current state of the process, the address of the program and data, the priority of the process, the CPU site protection area (for process switching), the list of resources occupied, etc.
  2. program segment
  3. data segment

Basic operations of the process

Take the Unix system as an example:

  1. Process creation: fork(). The newly created child process is almost but not identical to the parent process. The child process gets a copy of the same (but separate) user-level virtual address space as the parent process, including code and data segments, heap, shared libraries, and the user stack. The child process also gets an identical copy of any open file descriptors of the parent process, which means that when the parent process calls fork, the child process can read and write to any files that were open in the parent process. The biggest difference between the parent process and the newly created child process is that they have different PIDs. The fork function is interesting (and often confusing) because it is called once and returns twice: once in the calling process (the parent process) and once in the newly created child process. In the parent process, fork returns the PID of the child process. In a child process, fork returns 0. Because the PID of the child process is always non-zero, the return value provides an unambiguous way to tell whether the program is executing in the parent process or in the child process.
    pid_t fork(void);
     
  2. Recycling child processes: When a process terminates for some reason, the kernel does not immediately clear it from the system. Instead, the process is kept in a terminated state until it is reaped by its parent process. When a parent process recycles a terminated child process, the kernel passes the child's exit status to the parent process, and then discards the terminated process. A process can wait for its child processes to terminate or stop by calling the waitpid function.
    pid_t waitpid(pid_t pid, int *statusp, int options);
     
  3. Load and run program: The execve function loads and runs a new program in the context of the current process.
    int execve(const char *filename, const char *argv[], const char *envp[]);
     
  4. Process terminated:
    void exit(int status);
     

Briefly describe the inter-process communication method

Each process has a different user address space, and the global variables of any process cannot be seen in another process, so the exchange of data between processes must pass through the kernel, and a buffer area is opened in the kernel, and process A puts the data Copy from user space to the kernel buffer, and process B reads the data from the kernel buffer. This mechanism provided by the kernel is called inter-process communication.

The nature of communication between different processes: a common resource can be seen between processes; and the form or provider of this resource is different, resulting in different communication methods.

Inter-process communication mainly includes pipes, system IPC (including message queues, semaphores, signals, shared memory, etc.), and sockets.

How processes communicate through pipes

Pipeline is the most basic IPC mechanism, which acts between blood-related processes to complete data transfer. A pipe can be created by calling the pipe system function. Has the following qualities:

  1. Its essence is a pseudo-file (actually a kernel buffer)
  2. Referenced by two file descriptors, one for the read side and one for the write side.
  3. It is stipulated that data flows into the pipeline from the write end of the pipeline and flows out from the read end.

The principle of the pipeline: The pipeline is actually a circular queue mechanism used by the kernel and implemented with the help of the kernel buffer.

Limitations of pipelines:

  1. The data cannot be written by itself.
  2. Once the data is read, it does not exist in the pipeline and cannot be read repeatedly.
  3. Because the pipeline adopts half-duplex communication mode. Therefore, data can only flow in one direction.
  4. Pipes can only be used between processes that share a common ancestor.

How do processes communicate through shared memory?

It allows multiple processes to access the same memory space, and different processes can see the updates of data in the shared memory in the other process in time. This approach needs to rely on some kind of synchronization operations, such as mutexes and semaphores.

Features:

  1. Shared memory is the fastest type of IPC, because the process directly operates on the memory to achieve communication, avoiding data copying back and forth between user space and kernel space.
  2. Synchronization is required because multiple processes can operate simultaneously.
  3. Semaphores and shared memory are usually used together, and semaphores are used to synchronize access to shared memory.

what is a signal

A signal is a small message that notifies a process that an event of some type has occurred in the system. There are 30 different types of signals supported on Linux systems. Each signal type corresponds to a certain system event. Low-level hardware exceptions are handled by kernel exception handlers and are normally invisible to user processes. Signals provide a mechanism to notify users of processes that these exceptions occurred.

  1. Sending a signal: The kernel sends (delivers) a signal to the target process by updating a state in the context of the target process. Signals can be sent for two reasons:
  • The kernel has detected a system event, such as a division by zero error or a child process termination.
  • A process calls the kill function, explicitly requesting the kernel to send a signal to the target process. A process can send signals to itself.
  1. Receiving a signal: A destination process receives a signal when it is forced by the kernel to respond in some way to the sending of the signal. A process can ignore this signal, terminate, or catch this signal by executing a userland function called a signal handler.

How to write correct and safe signal handler functions

  1. The handler should be as simple as possible. The best way to avoid trouble is to keep the handler as small and simple as possible. For example, a handler might simply set a global flag and return immediately; all processing related to receiving a signal is performed by the main program, which periodically checks (and resets) this flag.
  2. Only call async-signal-safe functions in handlers. A so-called async-signal-safe function (or simply a safe function) can be safely called by a signal handler for two reasons: either it is reentrant (e.g. only accesses local variables), or it cannot be interrupted by a signal handler.
  3. Save and restore errno. Many Linux async-signal-safe functions return with errno set on error. Calling such functions in handlers may interfere with other dependent components in the main program. The solution is to save errno in a local variable on entry to the handler and restore it before the handler returns. Note that this is only necessary if the handler is to return. This is not needed if the handler calls _exit to terminate the process.
  4. Block all signals, protecting access to shared global data structures. If your handler and main program or other handlers share a global data structure, your handler and main program should temporarily block all signals while accessing (reading or writing) that data structure. The reason for this rule is that accessing a data structure d from the main program usually requires a sequence of instructions, and if the sequence of instructions is interrupted by a handler accessing d, the handler may find the state of d inconsistent, with unpredictable results. Temporarily blocking the signal while accessing d ensures that the handler does not interrupt the sequence of instructions.
  5. Declare global variables with volatile. Consider a handler and a main function that share a global variable g. The handler updates g, and main periodically reads g. To an optimizing compiler, the value of g in main never appears to change, so using a copy of g cached in a register to satisfy each reference to g is very safe. If so, the main function may never see the value updated by the handler. You can define a variable with the volatile type qualifier to tell the compiler not to cache the variable. For example: the volatile qualifier forces the compiler to read the value of g from memory every time g is referenced in the code. In general, as with all other shared data structures, you should temporarily block signals to protect each access to a global variable.
    volatile int g;
     
  6. Declare the flag with sig_atomic_t. In common handler design, handlers write global flags to record receipt of a signal. The main program periodically reads this flag, responds to the signal, and then clears the flag. For flags that are shared in this way, C provides an integer data type sig_atomic_t whose reads and writes are guaranteed to be atomic (uninterruptible).
  7. One counter-intuitive aspect of signals is that unhandled signals are not queued. Since there is only one bit for each type of signal in the pending bit vector, there can be at most one outstanding signal of each type. The key idea is that the presence of an unprocessed signal indicates that at least one signal has arrived.

Timing of process scheduling

  1. The currently running process ends.
  2. The currently running process blocked for some reason.
  3. Return to the user process after executing the system program such as the system call.
  4. In systems using preemptive scheduling, when a process with a higher priority becomes ready.
  5. In a time-sharing system, the time slice allocated to the current process runs out.

Situations where process scheduling cannot be performed

  1. While the interrupt handler is executing.
  2. In the critical section of the kernel program of the operating system.
  3. During other atomic operations that require complete masking of interrupts.

Process Scheduling Policy

  1. First come first serve scheduling algorithm
  2. Short Job First Scheduling Algorithm
  3. priority scheduling algorithm
  4. Time slice round-robin scheduling algorithm
  5. High Response Ratio Priority Scheduling Algorithm
  6. Multi-level Queue Scheduling Algorithm
  7. Multilevel Feedback Queue Scheduling Algorithm

Basic Design Indicators for Process Scheduling Strategies

  1. CPU utilization
  2. System throughput, that is, the number of jobs completed by the CPU per unit time.
  3. Response time.
  4. Turnaround time. Refers to the time interval from submission to completion of a job. From the perspective of each job, the time to complete each job is also critical
  • average turnaround time
  • Turnaround time with entitlement
  • average entitlement turnaround time

Process states and state transitions

A process has three basic states at runtime: ready state, running state, and blocked state.

  1. Running state: A state in which a process occupies the processor and is running. The process has acquired the CPU and its program is executing. In a uniprocessor system, only one process is executing; in a multiprocessor system, multiple processes are executing.

2. Ready (ready) state: the process has the conditions to run, waiting for the system to allocate a processor to run the state. When the process has been allocated all the necessary resources except the CPU, it can be executed immediately as long as the CPU is obtained again. The state of the process at this time is called the ready state. There may be multiple processes in the ready state in a system, and they are usually arranged in a queue, called the ready queue.

3. Blocking (wait) state: also known as waiting state or sleep state, refers to the state that the process does not have the conditions to run and is waiting for a certain time to complete.

Transitions between states:

  1. Ready → Execute the process in the ready state. When the process scheduler allocates a processor for it, the process changes from the ready state to the execution state.
  2. Execution → Ready A process in the execution state has to give up the processor because a time slice allocated to it has been used up during its execution, so the process changes from the execution state to the ready state.
  3. Execution → Blocking When an executing process cannot continue to execute because it is waiting for some event to occur, it changes from the executing state to the blocking state.
  4. Blocking→Ready For a process in the blocked state, if the event it is waiting for has occurred, the process changes from the blocked state to the ready state.

What is an orphan process? Zombie process?

  1. Orphan process: The parent process exits, and the child processes that are still running are all orphan processes. The orphan process will be adopted by the init process (process No. 1), and the init process will complete the state collection work for them.

2. Zombie process: The process uses fork to create a child process. If the child process exits, but the parent process does not call wait to obtain the status information of the child process, the processes whose process descriptors of the child process are still stored in the system are zombie processes.

What are threads?

  1. It is a task divided by a process, a schedulable entity in a process, and the basic unit of CPU scheduling, which is used to ensure the real-time performance of the program and realize the concurrency within the process.
  2. A thread is the smallest execution and scheduling unit recognized by the operating system. Each thread has its own virtual processor: its own register set, instruction counter, and processor state.
  3. Each thread performs a different task, but different threads belonging to the same process share the same address space (that is, the same dynamic memory, mapped files, object code, etc.), open file queues and other kernel resources.

Why do you need threads?

The reason for thread generation: a process can enable multiple programs to execute concurrently to improve resource utilization and system throughput; but it has some disadvantages:

  1. A process can only do one task at a time, and CPU resources cannot be fully utilized in many cases.
  2. If the process is blocked during execution, the entire process will hang, even if other tasks in the process do not depend on the waiting resources, the process will still be blocked.

The introduction of threads is to solve the shortcomings of the above processes. Threads have the following advantages:

  1. In terms of resources, the resources required to open up a thread are much smaller than a process.
  2. In terms of switching efficiency, multiple threads running in one process use the same address space between them, and the time required to switch between threads is much shorter than the time required to switch between processes (this time difference Mainly due to a large number of cache misses).
  3. In terms of communication mechanism, it is a convenient communication mechanism between threads. For different processes, they have independent address spaces, and the transfer of data can only be carried out through inter-process communication. Threads are not the case. Different threads belonging to the same process share the same address space, so the data of one thread can be perceived by other threads, and the threads can directly read and write process data segments (such as global variables) to communicate (need some synchronization measure).

Briefly describe the difference and connection between threads and processes

  1. A thread can only belong to one process, and a process can have multiple threads, but at least one thread. Threads depend on processes to exist.
  2. A process has an independent address space during execution, and multiple threads share the address space of a process. (Resources are allocated to a process, and all threads of the same process share all resources of the process. Multiple threads in the same process share code segments (code and constants), data segments (global variables and static variables), extended segments (heap storage) .But each thread has its own stack segment, which is also called the runtime, and is used to store all local variables and temporary variables.)
  3. A process is the smallest unit of resource allocation, and a thread is the smallest unit of CPU scheduling.
  4. Communication: Since multiple threads in the same process have the same address space, the realization of synchronization and communication between them becomes easier. Inter-process communication IPC, threads can directly read and write process data segments (such as global variables) to communicate (need some synchronization methods to ensure data consistency).
  5. Process programming and debugging are simple and reliable, but the cost of creation and destruction is high; on the contrary, threads have low overhead and fast switching speed, but programming and debugging are relatively complicated.
  6. Processes do not affect each other; a thread in a process that hangs will cause the entire process to hang.
  7. Processes are suitable for multi-core and multi-machine distribution; threads are suitable for multi-core.

Basic API for processes and threads

The process API takes the Unix system as an example, and the thread-related API belongs to the Posix thread (Pthreads) standard interface.

process primitive thread primitives describe

multi-threaded model

  1. Many-to-one model. Map multiple user-level threads onto a single kernel-level thread. Under this model, threads are managed in user space, which is more efficient. The disadvantage is that if one thread is blocked, all threads in the entire process will be blocked. Few systems continue to use this model.
  2. One-to-one model. One-to-one correspondence between kernel threads and user threads. The advantage is that when a thread is blocked, it will not affect the execution of other threads. This model has better concurrency. The disadvantage is that the number of kernel threads generally has an upper limit, which will limit the number of user threads. A higher number of kernel threads also imposes an additional burden on thread switching. Both the Linux and Windows operating system families use a one-to-one model.
  3. Many-to-many model. Map multiple user-level threads onto multiple kernel-level threads. It combines the characteristics of the many-to-one model and the one-to-one model.

Method of Process Synchronization

In the operating system, processes have different address spaces, and two processes cannot perceive the existence of each other. Sometimes, multiple processes are required to cooperate to complete some tasks. When multiple processes need to operate on the same kernel resource, these processes are in a competitive relationship. The operating system must coordinate the resource occupation of each process. Mutual exclusion of processes is a method to solve the competition relationship between processes. Process mutual exclusion means that when several processes want to use the same shared resource, at most one process is allowed to use it at any time, and other processes that want to use the resource must wait until the process that owns the resource releases the resource. When multiple processes cooperate to complete some tasks, the execution progress of different processes is inconsistent, which leads to the problem of process synchronization. The intervention of the operating system is required to synchronize all processes at a specific synchronization point. This coordination relationship between cooperative processes waiting for each other's messages or signals is called process synchronization. Process mutual exclusion is essentially a kind of process synchronization. The synchronization method of the process:

  1. mutex
  2. read-write lock
  3. condition variable
  4. record locking
  5. amount of signal
  6. barrier

Method of Thread Synchronization

In the operating system, threads belonging to the same process have the same address space, and sharing data between threads becomes simple and efficient. When encountering the problem that competing threads modify the same data at the same time or cooperating threads set synchronization points, some thread synchronization methods need to be used to solve these problems.

The method of thread synchronization:

  1. mutex
  2. read-write lock
  3. condition variable
  4. amount of signal
  5. spin lock
  6. barrier

What is the difference between process synchronization and thread synchronization

The address spaces between processes are different, and the existence of each other cannot be sensed. When synchronizing, locks need to be placed in the space shared by multiple processes. The same address space is shared between threads, and the lock can be placed in the same process space to which they belong during synchronization.

How does a deadlock arise?

Deadlock refers to the phenomenon that two or more processes wait for each other due to competition for resources during execution. A deadlock needs to meet the following four conditions:

  1. Mutual exclusion condition: The process does not allow other processes to access the allocated resources. If other processes access the resource, they can only wait until the process that occupies the resource finishes using it and releases the resource.
  2. Occupy and wait condition: After a process obtains a certain resource, it sends a request for other resources, but the resource may be occupied by other processes. At this time, the request is blocked, but the process will not release the resources it has already occupied.
  3. Non-preemptive conditions: The resources obtained by the process cannot be deprived before they are used, and can only be released after use.
  4. Circular waiting condition: After a deadlock occurs in a process, there must be a circular chain between processes and resources.

How to solve the deadlock problem?

The method to solve the deadlock is to destroy one of the four necessary conditions for deadlock. The main methods are as follows:

  1. The resource is allocated once, so that there will be no more requests (breaking the request condition).
  2. As long as one resource cannot be allocated, no other resources will be allocated to this process (destruction of possession and waiting conditions).
  3. Preemptible resources: that is, when the new resources of the process are not satisfied, the resources already occupied are released, thereby breaking the non-preemptive conditions.
  4. Orderly resource allocation method: the system assigns a sequence number to each type of resource, and each process requests resources in increments according to the number, and releases the opposite, thus destroying the condition of loop waiting

What is a virtual address and what is a physical address?

An address space is an ordered collection of non-negative integer addresses.

In a system with virtual memory, the CPU generates virtual addresses from an address space with N=pow(2,n) addresses. This address space is called the virtual address space (virtual address space). Modern systems usually support 32 bit or 64-bit virtual address space.

A system also has a physical address space, which corresponds to M bytes of physical memory in the system.

The concept of an address space is important because it clearly distinguishes data objects (bytes) from their attributes (addresses).

Once this distinction is recognized, it can be generalized to allow each data object to have multiple independent addresses, each selected from a different address space. This is the basic idea of ​​virtual memory.

Each byte in main memory has a virtual address selected from the virtual address space and a physical address selected from the physical address space.

What is virtual memory?

In order to manage memory more efficiently and with fewer errors, modern systems provide an abstraction of main memory called virtual memory (VM). Virtual memory is the perfect interaction of hardware exceptions, hardware address translation, main memory, disk files, and kernel software, which provides each process with a large, consistent, and private address space. Through a very clear mechanism, virtual memory provides three important capabilities:

  1. It treats main memory as a cache of address spaces stored on disk, keeps only active regions in main memory, and transfers data back and forth between disk and main memory as needed. In this way, it efficiently Main memory is used.
  2. It provides a consistent address space for each process, which simplifies memory management.
  3. It protects the address space of each process from being corrupted by other processes.

Why introduce virtual memory?

  1. Virtual memory as a tool for caching
  • Virtual memory is organized as an array of N consecutive byte-sized units stored on disk.
  • Virtual memory utilizes DRAM to cache pages from a generally larger virtual address space.
  1. Virtual memory is used as a tool for memory management. The operating system provides each process with an independent page table, that is, an independent virtual address space. Multiple virtual pages can be mapped to the same physical page.
  • Simplified linking:  Separate address spaces allow each process' memory image to use the same basic format, regardless of where code and data actually reside in physical memory.
    • For example: every process on a given linuxsystem uses a similar memory format, for a 64-bit address space, the code segment always starts at a virtual address) 0x400000, data segment, code segment, stack, heap, etc.

    • Simplified loading:  Virtual memory also makes it easy to load executables and shared object files into memory. To load the .text and .data sections of the target file into a newly created process, the Linux loader allocates virtual page VPs for the code and data segments, marks them as invalid (not cached), and points the page table entries to the  target The starting position of the file.
      • The loader never actually copies any data from disk to memory, and the virtual memory system automatically loads data pages as needed when each page is first referenced.
    • Simplified sharing:  Separate address spaces provide the OS with a consistent mechanism for managing sharing between user processes and the operating system itself.
      • General: Each process has its own private code, data, and stack, which are not shared with other processes, so that the OS creates a page table to map virtual pages to discontinuous physical pages.
      • In some cases, processes are required to share code and data. For example, each process calls the same operating system kernel code, or C standard library functions. The OS will map appropriate virtual pages in different processes to the same physical pages.

    • Simplified memory allocation:  Virtual memory provides users with an easy mechanism for allocating additional memory. When a program running in a user process requests additional heap space (for example malloc), the OS allocates an appropriate number of k contiguous pages of virtual memory and maps them to k arbitrary physical pages anywhere in physical memory, so The operating system does not need to allocate k consecutive physical memory pages, and the pages can be randomly scattered in the physical memory .
  • Virtual memory is used as a tool for memory protection. A user process should not be allowed to modify its read-only segment, nor should it modify any kernel code and data structures, read or write private memory of other processes, or modify any virtual pages shared with other processes. Every time the CPU generates an address, MMUit reads one PTE, and PTEcontrolling access to the contents of a virtual page is simple enough by adding a few extra permission bits to it.

Common Page Replacement Algorithms

When accessing a page that does not exist in the memory and the memory is full, you need to call out a page from the memory or send the data to the disk swap area to replace a page. This phenomenon is called page fault replacement. The most commonly used page fault replacement algorithm in the current operating system is as follows:

  • First in first out (FIFO) algorithm:
    • Idea: Replace the page that is transferred into the memory first, that is, replace the page that resides in the memory for the longest time.
    • Realization: Arrange into queues according to the order of entering the memory, enter from the tail of the queue, and delete from the head of the queue.
    • Features: simple to implement; poor performance, the page called out may be frequently accessed

  • Least recently used ( LRU) algorithm:
    • Idea: Replace the page that has not been visited for the longest time in the most recent period. According to the principle of program locality, the page that has just been accessed may be accessed again soon; and the page that has not been accessed for a long time may not be accessed recently.
    • Implementation: When a page is missing, calculate the last access time of each logical page in the memory, and select the page with the longest time from the last use to the current time
    • Features: It is possible to achieve the optimal effect, and the cost of maintaining such an access list is relatively large

Algorithms are the most commonly used at present LRU.

  • Least Common Algorithm ( Least Frequently Used, LFU)
    • Idea: When a page is missing, replace the page with the least number of visits
    • Realization: Set an access count for each page. When a page is accessed, the access count is increased by 1. When a page is missing, the page with the smallest replacement count is replaced.
    • Features: The algorithm is expensive, and it is frequently used at the beginning, but it is difficult to replace pages that are not used in the future

Please tell me what is copy-on-write?

  • If multiple processes are going to read their own copy of that sector's resource, then duplication is unnecessary. Each process only needs to save a pointer to this resource. As long as no process is going to modify its own "copy", there is the illusion that each process seems to have exclusive access to that resource. This avoids the burden of copying. If a process wants to modify its own "copy" of a resource, then that resource is copied, and the copied copy is given to the process. However, the copying is transparent to the process. This process can modify the copied resource, while other processes still share the unmodified resource. So that's where the name comes from: copy on write.
  • The main benefit of copy-on-write is that if the process never needs to modify the resource, no copying is required. The benefit of lazy algorithms is that they try to postpone expensive operations until they are necessary.
  • In the case of virtual memory, copy-on-write (Copy-On-Write) is performed on a page basis. Therefore, as long as the process does not modify its entire address space, it is not necessary to copy the entire address space. After the fork() call ends, both the parent process and the child process believe that they have their own address space, but in fact they share the original pages of the parent process, and then these pages can be shared by other parent processes or child processes.

The concept of a real-time operating system

A real-time operating system (RTOS), also known as an instant operating system, runs in sequence, manages system resources, and provides a consistent basis for developing applications. Compared with the general operating system, the biggest feature of the real-time operating system is "real-time". If there is a task to be executed, the real-time operating system will execute the task immediately (in a short period of time) without a long delay. hour. This feature ensures the timely execution of each task.

What is priority inversion? How to solve

Because multiple processes share resources, the process with the highest priority is blocked by the low-priority process, but the process with the medium priority is executed before the high-priority process, resulting in a system crash. This is the so-called priority inversion (Priority Inversion). In fact, priority inversion is blocked when a high-priority task (assumed to be A) wants to access a resource occupied by a low-priority task (assumed to be C). At this time, the priority is higher than that of the occupied resource. When the task (C) of the task (C) is lower than the priority task (assumed to be B) of the blocked task (A), then the task that occupies the resource is suspended (the occupied resource is still occupied by it), because the resource is occupied The priority of the task A is very low, so it may be suspended by other tasks all the time. And the resources it occupies can not be released, so that the task A has been unable to execute. The task with a lower priority than it can be executed .

There are currently many ways to solve priority inversion. Two methods are commonly used: one is called priority inheritance; the other is called priority ceilings.

  1. Priority inheritance (priority inheritance) Priority inheritance refers to raising the priority of low-priority tasks to the priority of the highest priority tasks waiting for the resources it occupies. When high-priority tasks are blocked due to waiting for resources, At this time, the priority of the resource owner will be automatically raised.
  2. Priority ceilings (priority ceilings) priority ceiling refers to the priority of the task that applies for a resource is raised to the priority of the highest priority task among all tasks that may access the resource. (This priority is called the priority of the resource ceiling).
     

A brief introduction to OSI seven-layer protocol

The OSI seven-layer protocol includes: physical layer, data link layer, network layer, transport layer, session layer, presentation layer, application layer

A brief description of the TCP/IP five-layer protocol

TCP/IP five-layer protocol includes: physical layer, data link layer, network layer, transport layer, application layer

What does the physical layer do

It mainly solves the communication between two physical machines, and realizes it through the transmission of binary bit stream. The binary data is expressed as the strength of current and voltage, and then converted into binary machine code after arriving at the destination. Network cards and hubs work on this layer.

What is the role of the data link layer

Provide reliable transmission on unreliable physical media, receive data in the form of bit stream from the physical layer, encapsulate it into frames, and transmit it to the upper layer; similarly, disassemble the data frames from the upper layer into bit stream form The data is forwarded to the physical layer. Based on the bit stream provided by the physical layer, this layer makes the physical lines with errors into error-free data links through error control and flow control methods. Provide physical address addressing function. Switches work at this layer.

What does the network layer do

Translate the network address into the corresponding physical address, and decide how to route the data from the sender to the receiver, and use the routing selection algorithm to select the best path for the packet to pass through the communication subnet. Routers work at this layer.

What does the transport layer do

The transport layer provides logical communication between processes, and the transport layer shields high-level users from the core details of the underlying network layer, making the application appear as if there is an end-to-end logical communication channel between two transport layer entities.

What does the session layer do

Establish a session: authentication, authentication, etc.; Maintain a session: maintain the session, and both parties can use this session at any time during the session maintenance period; Disconnect the session: when the timeout specified by the application or application layer expires After that, the OSI session layer will release the session.

What does the presentation layer do

Compile the data format, process the received or sent data according to the characteristics of the application layer, such as processing text, pictures, audio, video, documents, etc., and can also decompress compressed files and decrypt encrypted files, etc. .

What does the application layer do

Provide application layer protocols, such as HTTP protocol, FTP protocol, etc., to facilitate communication between applications.

The difference between TCP and UDP

As a stream-oriented protocol, TCP provides reliable, connection-oriented transport services and point-to-point communication. UDP, as a message-oriented protocol, does not provide reliable delivery and does not require connections. Not only point-to-point, but also multicast and broadcast

Why TCP is reliable

TCP has a three-way handshake to establish a connection, and four handshakes to close the connection. In addition, there are sliding window and congestion control algorithms. The most important thing is to retain the mechanism of timeout retransmission. There is also a checksum for each message to ensure the reliability of each message.

Why UDP is unreliable

UDP is connectionless for datagrams. When datagrams are sent out, data backups are not kept. Only checksum multiplexing is added to the header of the IP datagram. UDP has no concept of server and client. If the UDP message is too long, it will be handed over to IP to cut it into small segments. If a certain segment is discarded, the message will be discarded.

Briefly describe the phenomenon of TCP sticky packets

TCP is a stream-oriented protocol, and the sending unit is a byte stream, so it is possible to encapsulate multiple small-sized data in one tcp message and send it out. It can be simply understood that the client calls send twice, and the server reads all the information with one recv.

How to deal with TCP sticky packet phenomenon

Fixed sending message length, or add delimiter between two messages.

Briefly describe the sliding window of TCP protocol

The sliding window is a measure of flow control at the transport layer. The receiver controls the sender's sending speed by notifying the sender of its own window size, preventing the sender from being overwhelmed by sending too fast.

A Brief Introduction to Congestion Control of TCP Protocol

Congestion means that one or more switching points are overloaded with datagrams, and TCP will have a retransmission mechanism, resulting in overload. In order to prevent network congestion caused by excessive growth of the congestion window cwnd, it is also necessary to set a slow start threshold ssthresh state variable.

When cwnd < ssthresh, the slow start algorithm is used. When cwnd > ssthresh, stop using the slow start algorithm and use the congestion avoidance algorithm instead. When cwnd = ssthresh, either the slow start algorithm or the congestion avoidance algorithm can be used.

Slow start: gradually increase the size of the congestion window from small to large, and the cwnd index increases each time a packet is received.

Congestion avoidance: cwnd increases slowly, that is, the sender's congestion window cwnd is increased by 1 every time a round-trip time RTT passes.

The strategy before fast recovery: the sender judges that the network is congested, and sets ssthresh to half of the sender's window value when congestion occurs, continues to perform slow start, and then performs congestion avoidance.

Fast recovery: The sender determines that the network is congested, and sets ssthresh to half of the sender's window value when congestion occurs, and sets cwnd to half of ssthresh, and then performs congestion avoidance.

Brief description of fast retransmission

If three consecutive redundant ACKs are received before the timeout retransmission timer expires, the sender will know which segment was lost during transmission, and resend the segment without waiting for timeout retransmission The timer overflows and then sends the message.

TCP three-way handshake process

  1. The first handshake: the client sets the flag bit SYN to 1, randomly generates a value sequence number seq=x, and sends the data packet to the server, and the client enters the syn_sent state, waiting for the server to confirm.
  2. The second handshake: After the server receives the data packet, the flag bit SYN=1 knows that the client requests to establish a connection, the server sets the flag bits SYN and ACK to 1, ack=x+1, and randomly generates a value seq= y, and send the data packet to the client to confirm the connection request, and the server enters the syn_rcvd state.
  3. The third handshake: the client checks after receiving the confirmation. If it is correct, the flag bit ACK is set to 1, ack=y+1, and the data packet is sent to the server. The server checks that if it is correct, the connection is established successfully. The client and the server enter the established state, complete the three-way handshake, and then the client and the server can start transmitting data

Why does the TCP handshake need three times, can it be done twice?

no. The key to TCP's reliable transmission is to maintain a serial number. The process of three-way handshake is to inform each other of the initial value of the serial number and confirm that the other party has received the initial value of the serial number.

If there are only two handshakes, at most only the initial serial number of the client can be confirmed, and the serial number of the server cannot be confirmed.

Briefly describe the semi-join queue

In the TCP handshake, when the server is in the SYN_RCVD state, the server will put the request connection in this state in a queue, which is called a semi-connected queue.

Briefly describe SYN attack

The SYN attack uses the TCP protocol defect to send a large number of semi-connection requests, occupying the semi-connection queue, and consuming CPU and memory resources.

Optimization method:

  1. Shorten the SYN Timeout time
  2. Record the IP. If you receive repeated SYN packets from a certain IP continuously, the packets from this IP address will be discarded.

TCP four wave process

  1. The first wave: the client sends a FIN to close the data transmission from the client to the server, and the client enters the fin_wait_1 state.
  2. The second wave: After receiving the FIN, the server sends an ACK to the client, confirming that the serial number is the received serial number + 1, and the server enters the Close_wait state. At this time, the TCP connection is in a half-closed state, that is, the client has no data to send, but if the server sends data, the client still needs to receive it.
  3. The third wave: the server sends a FIN to close the data transmission from the server to the client, and the server enters the Last_ack state.
  4. The fourth wave: After the client receives the FIN, the client enters the Time_wait state, and then sends an ACK to the server. After confirmation, the server enters the Closed state and completes four waved hands.

Why does it take 4 times for TCP to wave

The main reason is that after the server receives the FIN data packet from the client, the server may still have data to send and will not close immediately.

So the server will first send the ACK to tell the client that I have received your disconnection request, but please give me a little more time, this time is used to send the remaining data packets, and then send the FIN packet Send it to the client to indicate that it can be broken now. After that, the client needs to send ACK to confirm the disconnection information to the server after receiving the FIN packet.

Why do I need to wait 2MSL when I wave four times to release the connection

MSL is the maximum packet lifetime. Setting 2MSL can ensure that the message of the last connection has disappeared in the network, and there will be no conflict with the new TCP connection message.

A brief description of the DNS protocol

The DNS protocol is an application layer protocol based on UDP. Its function is to resolve the IP address corresponding to the domain name according to the domain name entered by the user, so as to provide access to the client.

Briefly describe the DNS resolution process

1. The client computer sends out a query request and searches it in the local computer cache. If it is not found, it will send the request to the dns server

2. The local dns server will search in its own area, and if found, it will be parsed according to this record. If it is not found, it will be searched in the local cache

3. If the local server does not find the information queried by the client, it will send this request to the root domain name dns server

4. The root domain name server resolves the root domain part of the client's request, and it returns the address of the next-level dns server contained in it to the client's dns server address

5. The dns server of the client computer then accesses the dns server of the next level according to the returned information

6. This recursive method approaches the target of the query step by step, and finally obtains the corresponding IP information on the server with the target domain name

7. The client's local dns server will return the query result to our client

8. The client accesses the target host according to the obtained ip information and completes the parsing process

A brief description of the HTTP protocol

The http protocol is a hypertext transfer protocol. It is an application layer transmission protocol based on the TCP protocol, that is, a rule for data transmission between the client and the server. The protocol itself HTTP is a stateless protocol.

A brief description of cookies

The HTTP protocol itself is stateless. In order to enable it to handle more complex logic, HTTP/1.1 introduces cookies to save state information.

The cookie is generated by the server, and then sent to the client for storage. When the client visits again, the server can identify which client is based on the cookie, so as to make personalized push, login without account and password, etc.

Brief session

Session is used to mark specific client information and exists in a file on the server. Generally, the client accesses the server with a cookie, and the information about the client recorded by the server can be queried from the entire session through the session id in the cookie.

Briefly describe the http status code and corresponding information

1XX: The received message is being processed

2XX: The request has been processed normally

3XX: Redirection

4XX: Client Error

5XX: server error

Common error codes: 301: Permanent redirection 302: Temporary redirection 304: The resource has not been modified, just use the previous cache 400: The message requested by the client has an error 403: Indicates that the server forbids access to the resource 404: Indicates that the requested resource is on the server does not exist or was not found

The difference between forwarding and redirection

Forwarding is a server behavior. The server directly accesses the URL to the target address, reads the corresponding content and sends it to the browser. The URL in the address bar of the user's browser remains unchanged, and the forwarded page and the forwarded page can share the data in the request.

Redirection is achieved by using the status code returned by the server. If the server returns 301 or 302, the browser will automatically jump to the new URL to request resources again after receiving the new message. The user's address bar url will change and data cannot be shared.

Brief description of http1.0

Specifies the request header and request tail, response header and response tail (get post)

Each request is a separate connection, and connection reuse is not possible

Briefly describe the improvement of http1.1

HTTP1.1 enables persistent connections by default, and multiple HTTP requests and responses can be transmitted on one TCP connection. The way of using TCP long connection improves the performance overhead caused by HTTP/1.0 short connection.

It supports pipeline network transmission. As long as the first request is sent out, the second request can be sent out without waiting for it to come back, which can reduce the overall response time.

The server cannot actively push

Briefly describe the difference between HTTP short connection and long connection

The long connection and short connection in HTTP refer to the underlying TCP connection of HTTP.

Short connection: When the client and server perform an HTTP connection operation, a TCP connection is performed, and when the connection ends, the TCP closes the connection.

Long connection: If the HTTP header has the parameter keep-alive, that is, after the long connection web page is opened, the underlying TCP connection for data transmission will not be closed directly, but will be kept connected according to the hold time set by the server, and the connection will be made after the hold time expires closure.

Briefly describe the improvement of http2.0

Multiplexing is proposed. Before multiplexing, the files are transmitted serially, the file a is requested, the file b can only wait, and there are too many connections. Introducing multiplexing, file a and file b can be transmitted at the same time.

Introduced binary dataframes. The frame identifies the data sequentially, and with the sequence id, the server can transmit data in parallel.

The difference between http and https

All the content transmitted by http is plain text, and neither the client nor the server can verify the identity of the other party. https has a secure ssl encryption transmission protocol, and the encryption adopts symmetric encryption. The https protocol needs to apply for a certificate from CA. Generally, there are few free certificates and you need to pay.

Briefly describe the relationship between TLS/SSL, HTTP, and HTTPS

The full name of SSL is Secure Sockets Layer, which is the secure socket layer. Its successor is TLSTransport Layer Security, which is used to provide security support for data communication at the transport layer.

The HTTPS protocol can be simply understood as the HTTP protocol + TLS/SSL

HTTPS connection process

  1. The browser sends the supported encryption algorithm information to the server
  2. The server selects a set of encryption algorithms supported by the browser and sends it back to the browser in the form of a certificate
  3. The client (SSL/TLS) parses the certificate to verify the validity of the certificate and generates a symmetric encryption key. We call this key the client key, that is, the client key. The client key is encrypted with the server's public key. Symmetric encryption.
  4. The client will initiate the second HTTP request in HTTPS, and send the encrypted client symmetric key to the server
  5. After the server receives the ciphertext sent by the client, it will use its own private key to asymmetrically decrypt it. The decrypted plaintext is the client key, and then use the client key to symmetrically encrypt the data, so that the data is into ciphertext.
  6. The server sends the encrypted ciphertext to the client
  7. The client receives the ciphertext sent by the server, uses the client key to symmetrically decrypt it, and obtains the data sent by the server. In this way, the second HTTP request in HTTPS ends and the entire HTTPS transmission is completed

The difference between Get and Post

Get: Specify the resource request data, refresh is harmless, the data requested by Get will be appended to the URL, and the size of the transmitted data is limited by the URL.

Post: Submit data to be processed to the specified resource. Refreshing will cause the data to be submitted repeatedly. Post sends the request header to the server for confirmation before sending the data, and then actually sends the data.

Is there a size limit for Get method parameters?

Generally, the HTTP protocol does not limit the parameter size limit. But generally, because the get request is directly attached to the address bar, and because the browser address bar has a length limit, the GET request will have a length limit at the browser implementation level.

Do you understand REST API?

The full name of REST API is Representational State Transfer (REST), which uses get, post, put, delete and other HTTP methods in HTTP to form the addition, deletion, modification and query operations of data resources in REST:

  • Create : POST
  • Read : GET
  • Update : PUT/PATCH
  • Delete: DELETE

What exactly happens when a URL is entered into a browser

  1. Perform DNS resolution operation, and find the server IP address according to the result of DNS resolution
  2. Through ip addressing and arp, find the server, and use the three-way handshake to establish a TCP connection
  3. The browser generates an HTTP message, sends an HTTP request, and waits for the server to respond
  4. The server processes the request and returns it to the browser
  5. According to whether HTTP has opened long connection or not, perform TCP wave process
  6. The browser renders the page based on the received static resources

Guess you like

Origin blog.csdn.net/qq_39953312/article/details/126415318