“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”

Before the interview, I prepared 10 days of review content for myself. I don’t know if it is useful for you. I would like to share with you and welcome you to add.

Operating system evolution

In the early days there was no concept of an operating system. There is a CPU, and a program prepared by themselves, but CPU resources are very precious, only run a program every time, when the end of the run, the CPU is idle, which causes the waste of CPU resources. In order to make better use of CPU resources, a monitor program was written to monitor the CPU. If you find that the CPU is idle, start it up to run the program that is currently waiting to be executed, thus making full use of the CPU resources. This is called “Multiprogramming.”

Another problem with having multiple procedures is that they are carried out without regard to priorities. For example, when we use the browser, we want the page to load quickly and play the music at the same time. For a multi-channel program, it needs one to run out of CPU resources before it can execute another, which obviously cannot meet the requirements. So we improved several programs, so that each program will actively release CPU resources after running for a period of time, so that each program can run for a short period of time, so that we can meet our needs. This is called a time-sharing System.

New problems arose with the advent of time-sharing. If there is an error in a program, resulting in the memory running out of infinite loop, so that not only this program error, the entire system will crash. To solve this problem, the time-sharing system was improved. Use the operating system to take over all hardware resources from the lowest level. All applications run as “processes” on top of the operating system, with each Process having its own address space, isolated from each other. CPU is uniformly scheduled by the operating system, and every program has a chance to get CPU resources. If a process runs for more than a certain amount of time, the system will suspend it and give the CPU to other programs. This avoids a system-wide failure situation. This way of operation is the systems we use today, which are called “multi-tasking systems”.

The difference between threads and processes

Process: is the dynamic execution Process of a program to a data set. It is the basic unit of resource allocation. Each process has its own address space, which typically includes a text area, a data area, and a stack. The text area is used to store the code executed by the processor; The data area is used to store variables and dynamically allocated memory available during process execution; The stack area holds calling instructions and local variables.

Thread: A Lightweight Process (LWP), which is the smallest unit of program execution flow. A standard thread consists of a thread ID, the current instruction pointer (PC), registers, and a stack. In addition, the thread does not own system resources, but it must have some system resources that can be guaranteed to run independently.

The difference between:

1. Conceptual differences

  • Process: the dynamic execution of a data set by a program. It is the basic unit of resource allocation.
  • Thread: The basic unit of scheduling within a process. Threads are smaller than processes, and a process contains one or more threads.

2. Differences in execution process:

  • Processes: Have a separate memory space that provides efficiency by sharing memory with multiple threads.
  • Threads: Each independent thread has its own entry, exit, and execution order. Threads cannot run independently and must depend on the process, with the application providing control over the execution of multiple threads.
  • Context switch: For process context switch CPU execution process is generally: loading program context, execution program, save program context these three steps. Each program switch has to go through these three steps, so the switching overhead for the process is very high. Context switches are different for threads, such as A, B, and C in program A. Threads share process memory, and CPU execution becomes: context for loading program A, segment A for executing program A, segment B for executing program A, segment C for executing program A, and context for saving program A. Here, the execution of threads A, B and C shares the context of program A, and the CPU does not switch the context during execution. So thread context switching within the same process is fast and inexpensive

3. Logical differences:

  • Process: the smallest unit of resource allocation
  • Thread: Thread is the smallest unit of CPU scheduling. Multiple threads can execute simultaneously, but the operating system does not treat threads as multiple applications for process scheduling and resource management.

The system calls

System Call: A System Call is a program that requests services and privileges from the System kernel to access resources. It is an important interface between the program and the operating system. For example, create a new process, schedule other processes, and access hard disks

Context switch

What is the context?

  • Because the CPU is to switch between different applications, only one program at a time, before each program runs, the CPU needs to know where the currently executing program load, from where to start running (run last time where instruction), to ensure the running state of then last time, otherwise all the time to run the program, this program is never complete. This information is stored in the kernel of the system to keep the original state of the program intact and keep the task running continuously. This information is the context.

What is a context switch?

  • A scheduling mechanism that allocates CPU resources from one process to another. It ensures that all programs can get the CPU and run in unit time. Switching between programs because the CPU is so fast that we look like multiple programs are running at the same time.

Context switching between processes is different from context switching between threads within the same process

Context switching between processes: CPU loader context, CPU running context, CPU saving context.

Context switching between threads: THE CPU runs threads and the CPU holds thread-private resources (stacks, registers, etc.). Since virtual memory is shared by multiple threads within the same process, they do not need the context of the loader. They are already loaded when switching processes, and they only need to execute and save the thread’s own private resources. Thus, switching between threads consumes less resources than switching between processes.

Multi-process and multi-thread have their respective advantages

Multiple processes

Advantages:

  • Each process is independent of each other and does not affect the child main program, even if the child process dies
  • You can minimize thread locking/unlocking to provide performance
  • Suitable for multi-core multi-machine distributed, a machine is not enough to expand to multiple very simple
  • Simple programming and debugging

Disadvantages:

  • Data sharing complexity
  • It occupies a large memory and has high switching overhead
  • Create destroy, switch complex, slow speed

Process usage scenario: target sub-function interaction is less, if resources and performance permit, can be designed into multiple subroutines to complete the combination.

multithreading

Advantages:

  • Data sharing is simple because process data is shared
  • Low memory usage, simple switchover, and high CPU utilization
  • Create destroy, switch simple, fast

Disadvantages:

  • Programming is complicated, debugging is complicated
  • The failure of one thread causes the failure of the entire process
  • Synchronization and locking control between threads are troublesome

Usage scenarios of threads: When there are a lot of IO, network and other time-consuming operations or user interaction, multi-threading can better provide system parallelism and friendliness

Communication between processes

A process cannot read or write data from another process. The communication between the two processes is carried out through Inter-process communication (IPC). Process communication usually follows the producer-consumer model, which requires data exchange and synchronization. There are four possible implementations

1. Messaging

  • Pipe: a one-way, fifO, unstructured, fixed-size byte stream that connects the standard output of one process to the standard input of another process
  • Message queues: Message queues are linked lists of messages stored in the kernel and identified by message queue identifiers. The message queue overcomes the disadvantages of little signal transmission, pipe carrying only plain byte stream and limited buffer size

2. The synchronous

  • Semaphore: A semaphore is a counter that can be used to control access to a shared resource by multiple processes. It is often used as a locking mechanism to prevent other processes from accessing a shared resource while one process is accessing it. Therefore, it is mainly used as a means of synchronization between processes and between different threads within the same process
  • Condition variables,
  • Read-write lock

3. Shared memory

  • Shared memory maps a segment of memory that can be accessed by other processes. This segment of shared memory is created by one process but can be accessed by multiple processes

4. Remote procedure call Socket

The basic state of the process

  • Wait state: Waiting for an event to complete
  • Ready: Waiting for the system to allocate processors to run
  • Running: Indicates that the processor is running

Running state -> wait state: generally due to waiting for peripherals, waiting for main memory and other resource allocation or manual intervention caused

Wait state -> Ready state: The condition for waiting to run has been met and only needs to be allocated to the processor to run

Ready -> Running: the system selects a process in the ready queue to assign to the processor according to some policy and becomes running

Running -> Ready: a running process becomes ready when it cedes the processor for external reasons, not its own. For example, the time slice is running out, or a higher priority process is preempting the processor

Preemptive scheduling and non-preemptive scheduling

Preemptive scheduling: A mode in which the operating system forcibly suspends a running process and the scheduler allocates CPU to another ready process.

Non-preemptive scheduling: The allocator assigns a processor to a process and lets it run until the process is complete or blocked by a process scheduling event before assigning the processor to another process.

Process thread synchronization, mutex, deadlock

Mutual exclusion: A means of resolving competing relationships between processes (for example, multiple processes want the same resource, such as a printer). When multiple processes want to use a shared resource, a maximum of one process is allowed to use the resource at any time. Other processes must wait until the process that occupies the resource releases the resource.

Synchronization: A means of addressing collaboration between processes (for example, multiple processes working together to complete a task). Process synchronization is when two or more processes coordinate their activities based on a condition. The execution of a process depends on the message or signal from another cooperating process. When a process does not receive the message or signal from the other process, it needs to wait until the message or signal arrives and is woken up.

Means to achieve synchronization:

  • Critical section: access to a common resource or a piece of code through serialization of multiple threads, fast, suitable for controlling data access. At any time only allows a thread to access Shared resources, if there are multiple thread attempts to access the public resources, and then, after a thread into the other thread attempts to access the public resources will be suspended, and wait until the thread from entering a critical area, critical region after the release, other threads can be preempted
  • Mutually exclusive: Uses the mutually exclusive object mechanism.
  • Semaphore: It allows multiple threads to access the same resource at the same time, but you need to limit the maximum number of threads that can access the resource at the same time. Unlike the previous methods, semaphore objects synchronize threads, in that signals allow multiple threads to use a shared resource at the same time, the same as PV operations in an operating system. It specifies the maximum number of threads that can simultaneously access a shared resource. It allows multiple threads to access the same resource at the same time, but you need to limit the maximum number of threads that can access the resource at the same time
  • Events: By notifying operations to keep threads synchronized, but also to facilitate the implementation of multiple thread priority comparison operations.

Deadlock: Failure of multiple processes to execute because they are waiting for resources in a loop. Deadlocks cause processes to fail to execute and resources cannot be reclaimed.

There are four conditions that can cause a deadlock:

1. Mutually exclusive

  • Exclusive use of allocated resources by a process, that is, a resource is occupied by only one process in a period of time. If another process requests the resource at this time, the requester can only wait until the process holding the resource is released.

2. Do not preempt

  • A resource that has been acquired by a process and cannot be taken away until it is used up, but can only be released when it is used up.

3. Ask and hold

  • A process that has held at least one resource and then makes a new resource request that has been occupied by another process. In this case, the requesting process blocks but does not release other resources that it has obtained

4. Wait in loop

  • When deadlock occurs, there must be a process – resource ring chain, that is, process set {P0, P1, P2, ···, Pn} P0 is waiting for a resource occupied by P1; P1 is waiting for resources occupied by P2…… Pn is waiting for a resource that has been occupied by P0

How to avoid deadlock-Banker algorithm: Determine if this request causes a deadlock if it does, reject the request. Banker’s algorithm

Understand coroutines

Coroutines, also known as microthreads, fibers. English name Coroutine.

Coroutines can be understood as a user-level threads and coroutines and thread: is the difference between threads is a preemptive scheduling, and coroutines is a coordinated scheduling, coroutines avoid meaningless scheduling, which can improve performance, but also as a result, programmers have to bear the liability for scheduling, at the same time, it also lost the ability to use more CPU standard thread.

Network IO model

Network IO model