3 points | 青训营笔记

94 阅读2分钟

1、deadlock

A deadlock is a situation in which two or more processes are unable to proceed because they are waiting for each other to release resources. Deadlocks can occur in multi-threaded or multi-process applications and can lead to a system freeze or crash.

To prevent deadlocks, a system can use techniques such as resource allocation graph, banker's algorithm, or timeouts. The resource allocation graph shows the resource allocation status of the system, which can be used to detect deadlocks. The banker's algorithm is used to check if a resource request will lead to a deadlock before granting it, and timeouts are used to interrupt a process that has been waiting for a resource for too long.

To avoid deadlocks, we can avoid circular waits, enforce a strict ordering of resource requests, and release resources as soon as possible. It's also essential to ensure that all resources are being used efficiently to minimize the chances of deadlock.

Interviewer: Can you give an example of a situation that can lead to a deadlock?

Answer: Sure, an example of a situation that can lead to deadlock is the dining philosophers problem. In this problem, there are five philosophers sitting at a table, and each philosopher needs two forks to eat. If all the philosophers pick up their left fork simultaneously, they will be unable to pick up their right fork, and a deadlock will occur.

Interviewer: How can you detect and resolve a deadlock?

Answer: Deadlocks can be detected by using techniques such as the resource allocation graph or the banker's algorithm. To resolve a deadlock, we can either abort one or more processes involved in the deadlock or preempt resources from one of the processes involved. However, both of these solutions may have some drawbacks, such as data loss or process starvation.

2、OOP

Object-oriented programming is a programming paradigm that is based on the concept of objects, which can contain data and code that manipulate the data. The essential features of object-oriented programming are encapsulation, inheritance, polymorphism, and abstraction.

Encapsulation is the technique of hiding the internal details of an object and exposing only the necessary interfaces. It helps in keeping the object state consistent and improves the maintainability of the code.

Inheritance is the mechanism by which a subclass can inherit properties and behavior from its superclass. It allows code reuse, reduces code duplication, and makes the code more modular. Polymorphism is the ability of an object to take on many forms. It allows objects of different classes to be treated as if they are of the same type, making the code more flexible and reusable. Abstraction is the technique of representing complex real-world entities using simplified models in code. It helps in managing complexity, reduces code duplication, and improves maintainability.

3、3-way handshake

The Three-way Handshake is a process used to establish a TCP connection between two devices. The process consists of three steps:

  1. The first device sends a SYN (synchronize) packet to the second device, indicating that it wants to initiate a connection.
  2. The second device responds with a SYN-ACK (synchronize-acknowledgement) packet, indicating that it received the SYN packet and is willing to establish a connection.
  3. The first device responds with an ACK (acknowledgement) packet, indicating that it received the SYN-ACK packet and the connection is established.

Once the Three-way Handshake is complete, data can be transmitted between the two devices.

The Four-way Wavehand is a process used to terminate a TCP connection between two devices. The process consists of four steps:

  1. The first device sends a FIN (finish) packet to the second device, indicating that it wants to terminate the connection.
  2. The second device responds with an ACK packet, indicating that it received the FIN packet.
  3. The second device then sends its own FIN-ACK packet to the first device, indicating that it is also terminating the connection.
  4. The first device responds with an ACK packet, indicating that it received the second FIN packet and the connection is terminated.