Java interview must-test points--Lecture 02 (Part 1): Operating system and computer network

Let's take a look at TCP disconnection, as shown in the figure below.

To close the TCP link, both parties in the communication can initiate it first. For the time being, we regard the party that initiates first as the Client. As can be seen from the figure, the links at both ends of the communication, Client and Server, are in the ESTABLISHED state, and then the Client actively initiates the closing of the link. request, the Client sends a FIN packet to the Server, indicating that the Client has no data to send, and then the Client enters the FIN_WAIT_1 state.

After receiving the FIN, the server returns ACK and then enters the CLOSE_WAIT state. At this time, the Server is in a semi-closed state, because the Client will no longer send data to the Server, but the Server may still have data to send to the Client.

When the server side data is sent, the server side will send a FIN to the client side, indicating that the server side has no data to send. At this time, the server side enters the LAST_ACK state and waits for the client's response to close the connection.

After receiving the FIN from the server, the client replies with ACK and then enters the TIME_WAIT state. In the TIME_WAIT state, you need to wait for 2 times the maximum segment survival time to ensure reliable closing of the link before entering the CLOSED state. After receiving the ACK, the server directly enters the CLOSED state.

Here the interviewer may ask why it is necessary to wait for 2 times the maximum segment survival time before closing the link. There are two reasons:

  1. Ensure that the full-duplex connection of the TCP protocol can be reliably closed;

  2. Ensure that the duplicate data segments of this connection disappear from the network to prevent possible data confusion when the port is reused.

It can be seen from this interaction process that whether it is establishing a connection or breaking the link, it needs to be carried out in two directions. However, when establishing a connection, the SYN and ACK on the server side are combined into one transmission, while when the link is broken, both directions are sent. The time when data transmission stops may be different, so FIN and ACK cannot be sent together. This is why it takes three handshakes to establish a connection and four to break the link.

In addition, when answering the question of broken links, you can mention that in actual applications, you may encounter the problem of a large number of Sockets in the TIME_WAIT or CLOSE_WAIT state. Generally, turning on tcp_tw_reuse and tcp_tw_recycle can speed up the recycling of TIME-WAIT Sockets; and a large number of CLOSE_WAIT may be caused by a code bug in the passive closing party and failure to close the connection correctly.

This class mainly introduces basic computer knowledge and Java language features that are often tested in interviews. Among them, basic computer knowledge reflects the basic abilities of engineers and is also a part that must be firmly mastered before the interview.

The lesson structure is as follows:

  1. A summary of knowledge points frequently examined during interviews to facilitate systematic review;

  2. Detailed explanation of TCP protocol, design patterns, and basic knowledge of Java;

  3. From the interviewer’s perspective, summarize the inspection points of some of the above contents during the interview;

  4. Provide some real interview questions and solutions to key questions.

Operating system knowledge points

Let’s first look at the summary of operating system related knowledge, as shown in the figure below. Operating system knowledge is very important for troubleshooting and locating service problems. Interviews generally focus on understanding and application, and the proportion of interview questions is generally not too high.

Processes and Threads

The process and thread part in the upper left corner of the above picture is a very important inspection point.

  1. First, you need to understand the differences and connections between processes and threads:

    1. The process is the smallest unit of system resource allocation, and the thread is the smallest unit of program execution;
    2. Processes use independent data spaces, while threads share the process's data space.
  2. Thread scheduling, simply understand several thread scheduling algorithms. For example, time slice rotation scheduling, first come first served scheduling, priority scheduling, multi-level feedback queue scheduling and high response ratio priority scheduling.

  3. The steps of thread switching are mainly to understand the context switching of threads and understand the cost of thread switching. The knowledge about threads will be explained in detail in the multi-threading course later, so we will skip it here.

  4. Another common inspection point in the process and thread section is inter-process communication, which is IPC. This part is often examined when interviewing for positions related to middleware development. As shown in the above knowledge point summary diagram, you need to understand the principles and applicable scenarios of these six process communication methods. For example, the scenario of inter-process data sharing can use shared memory; the scenario of inter-process data exchange can use Unix Socket or message queue.

  5. Finally, for the coroutine part, simply understand that coroutines are more lightweight and are scheduled in user mode. The cost of switching is much lower than thread context switching. You can also learn about Java's third-party coroutine frameworks, such as Kilim, Quasar, etc. .

Linux commonly used commands

Most Internet companies' services run on Linux systems, so Linux commands are also a common test point during interviews. In fact, this part mainly tests whether the candidate has experience in troubleshooting online problems, focusing on learning AWK, top, netstat, Frequently used tools such as grep.

There are also some knowledge points that are not often tested and should be properly understood, such as memory paging management and Swap mechanism, task queue and CPU Load, etc. This knowledge is very useful in analyzing online problems.

Expand knowledge

Finally, there are expanded knowledge points, such as memory barriers, instruction reordering, branch prediction, NUMA and CPU affinity, etc. If you have the opportunity to talk about it during the interview, you will leave a better impression on the interviewer in terms of depth of knowledge.

Computer network knowledge points

Computer network is also a very important basic knowledge. Services interact through different network protocols, such as HTTP protocol, RPC protocol, etc. The probability of network knowledge being tested in Java interviews is very high. The network knowledge points are summarized in the figure below.

First of all, you should have a deep understanding of the 4/7 layer model of the network, which is the basis of network knowledge.

The other two very important network protocols are HTTP and TCP. These two protocols are also the most commonly used protocols in service interaction. Let’s look at the TCP protocol first. The three-way handshake to establish a connection and the four-way handshake to disconnect is a high-frequency test point in the TCP protocol, which will be introduced in detail later.

  • TCP message status flags and link status are very important when troubleshooting network problems. You must understand the protocol status to facilitate packet capture and analysis.

  • Another knowledge point is the Nagel algorithm and ACK delay. You need to understand the background of its occurrence, which is to solve the small packet problem and improve the data load ratio. Know that the Nagel algorithm can be turned off in scenarios that are more sensitive to latency and send data less frequently.

  • Regarding TCP Keepalive, it is a mechanism for TCP to keep the link available when no data is sent for a long time. You need to know how to turn on and set up TCP Keepalive.

  • Finally, you need to understand how TCP implements flow control through the sliding window mechanism.

Let’s look at the HTTP protocol part again.

  • You need to master the specifications of the HTTP protocol, know the Method, Header, and Cookies in the protocol, and understand the meaning of common status codes, such as 404, 503, 302, etc.

  • There is also an interactive process of HTTPS.

  • HTTP2 is still relatively new, and understanding the HTTP2 protocol can reflect the level of attention to new technologies to a certain extent. You can pay attention to: HTTP2 multiplexing, Stream streaming interaction, flow control, server push, header compression and other new features

In addition to HTTP and TCP, UDP is also a relatively common transport layer protocol. UDP is characterized by non-link and unreliable transmission, but it is very efficient.

Finally, you can learn something about the QUIC protocol, which has been standardized as the HTTP3 protocol. QUIC is based on the UDP protocol, but QUIC provides reliability guarantees and flow control similar to TCP. QUIC can effectively avoid the preamble packet blocking problem of the HTTP2 protocol, achieve zero RTT connection establishment, and provide FEC forward error correction capabilities.

Detailed explanation of TCP protocol characteristics

TCP is a transport layer protocol, corresponding to the fourth transport layer of the OSI network model. Its characteristics are as follows.

  • The TCP protocol is based on links, that is, before transmitting data, a link needs to be established first and then transmitted.

  • Once a TCP link is established, bidirectional communication can occur on the link.

  • TCP transmission is based on byte streams rather than messages. The data is numbered according to byte size. The receiving end uses ACK to confirm the received data number. Through this mechanism, the TCP protocol can ensure the orderliness and orderliness of the received data. Integrity, so TCP can provide reliable transmission.

  • TCP can also provide flow control capabilities to control the data sending rate through sliding windows. The essence of the sliding window is a dynamic buffer. The receiving end dynamically adjusts the window size in the TCP Header according to its own processing capabilities, and notifies the sending end through an ACK response packet. The sending end adjusts the sending speed according to the window size.

  • It is not enough to have flow control capabilities. The TCP protocol also takes into account that network problems may lead to a large number of retransmissions, which will further worsen the network situation. Therefore, the TCP protocol also provides congestion control. TCP mainly uses four algorithms for processing congestion control: slow start, congestion avoidance, congestion occurrence, and fast recovery. Interested students can learn more.

In addition to the characteristics of the TCP protocol, you can also learn more about the packet status of the TCP protocol, the workflow of the sliding window, the parameter settings of Keepalive, and the rules of the Nagel algorithm.

There are also typical TCP protocol issues. For example, in certain scenarios, when Nagel and the ACK delay mechanism are used together, there may be a delay of 40ms before replying to the ACK packet.

Detailed explanation of three-way handshake to establish connection

Next, look at the three-way handshake of TCP connection establishment. TCP is based on links, so a link needs to be established before data is transmitted. TCP uses duplex transmission and does not distinguish between the client and the server. For ease of understanding, we call the end that actively initiates the connection establishment request the client. , the end that passively establishes the link is called the Server end.

As shown in the figure below, the timing of establishing a connection is from top to bottom. The green characters on the left and right represent the current link status of the Client and Server respectively.

First, before establishing a link, the server needs to listen to the port first, so the initial state before the server establishes the link is the LISTEN state. At this time, the client prepares to establish the link and first sends a SYN synchronization packet. After sending the synchronization packet, the client's link status changes to It has become SYN_SENT state. After receiving the SYN, the Server agrees to establish the link and will reply an ACK to the Client.

Since TCP is a duplex transmission, the server will also send a SYN to the client at the same time, requesting the server to establish a link to the client. After sending the ACK and SYN, the link status on the server side becomes SYN_RCVD.

After the Client receives the ACK from the Server, the client's link status changes to the ESTABLISHED state. At the same time, the Client sends an ACK to the Server in reply to the Server's SYN request.

After the Server receives the ACK from the Client, the link status of the Server changes to the ESTABLISHED status. At this time, the connection is established and both parties can transmit data at any time.

During the interview, you need to understand that the three-way handshake is to establish a two-way link, and you need to remember the link status changes between the Client and Server. In addition, when answering the question about establishing a connection, you can mention that the reason for the SYN flood attack is that after the server receives the SYN request from the client, it sends ACK and SYN, but the client does not reply, resulting in a large number of links on the server. SYN_RCVD status, thereby affecting the connection establishment of other normal requests. You can set tcp_synack_retries = 0 to speed up the recycling of half links, or increase tcp_max_syn_backlog to deal with a small number of SYN flood attacks.

Detailed explanation of waving four times to disconnect

Guess you like

Origin blog.csdn.net/g_z_q_/article/details/129739750