Summary of common web programming interview questions

One, foreword

The previous article for you to introduce the current background development post some requirements, please check: back-end development post requirements summary, I hope to help you;

Today this common network interview question is not a supplement to the network part of the job introduced in front, these interview questions are collected and the network, if the answer is wrong, please contact me in time to correct, avoid the error of knowledge caused by everyone

Second, online interview questions

Differences between TCP and UDP

(1) TCP connection-oriented (such as dialing to establish a connection before making a phone call); UDP is connectionless, that is, no connection needs to be established before sending data. (2) TCP provides reliable service. That is to say, data transmitted through the TCP connection is error-free, not lost, not repeated, and in order to arrive; (3) TCP byte stream oriented, in fact TCP treats data as a series of unstructured byte streams; UDP is packet-oriented and does not have congestion control. Therefore, network congestion does not reduce the sending rate of the source host (useful for real-time applications, such as IP phone calls and real-time video conferences). (4) Each TCP connection can only be point-to-point. UDP supports one-to-one, one-to-many, many-to-one and many-to-many interactive communication (5) TCP header cost 20 bytes; The header of UDP has a small overhead of only 8 bytes. (6) TCP logical communication channels are full-duplex reliable channels, while UDP is unreliable channels.

Flow control and congestion control

Congestion control Network congestion refers to the phenomenon that the number of packets arriving at a certain part of the communication subnet is too large for the network to handle. As a result, the performance of this part or the entire network deteriorates. In serious cases, network communication services may stop, that is, deadlock occurs. Congestion control is a mechanism to deal with network congestion. Flow control Data transmission and receiving process is likely to be too late to receive the situation, then need to control the sender to avoid data loss.

How to synchronize multiple threads

windows

There are four ways to synchronize threads: critical sections, kernel objects, mutex, and semaphores.

Linux

The most common examples of thread synchronization are mutex, condition variables, and semaphores.

What are the methods of interprocess communication, and what are the advantages and disadvantages of each

Interprocess communication

Linux interprocess communication (IPC) has evolved from early UNIX interprocess communication, System V based interprocess communication, Socket based interprocess communication, and POSIX based interprocess communication. UNIX interprocess communication includes: pipe, FIFO, signal. System V interprocess communication modes include: System V message queue, System V semaphore, System V shared memory POSIX interprocess communication modes include: POSIX message queue, POSIX semaphore, and POSIX shared memory. Interprocess communication currently used in Linux: (1) Pipes and Named pipes (FIFO) (2) Signals (3) message queues (4) Shared memory (5) Semaphores (6) Sockets

A TCP connection is established with a three-way handshake and disconnects with a four-way handshake

The 3-way handshake protocol used to establish the connection refers specifically to: The first handshake is when the client connects to the server, and the server accepts the request from the client, and sends a message to the client saying, “I’m ready, you’re connected to me.” This is the second handshake, and the third handshake is sent by the client to the server, It’s a confirmation of the second handshake. The client and server then communicate. The four handshakes of disconnection are as follows: The disconnected end sends the close request for the first handshake, the other end receives the disconnection request and needs to confirm the close, send a message, this is the second handshake, send the confirmation message and then send the close message to the other end, to close the connection to the other end, this is the third handshake, After the original disconnected end receives the message, it enters the important state time_wait, which is often asked by the interviewer. The last handshake is after the original disconnected end receives the message. Confirmation of the message.

Differences between epoll and SELECT

(1) the maximum number of concurrent fd (32*32=1024) is 32*32=1024. 2. Low efficiency: the whole FD_set will be linearly scanned each time, and the larger the set, the slower the speed; 3. Kernel/user space memory copy problem.

(2) Improvement of epoll: 1. It is not limited by the maximum concurrent connections, but only by the maximum number of files that can be opened by processes in the system; 2. Efficiency improvement: Only active sockets will actively call callback functions; 3. Eliminating unnecessary memory copy: Epoll is implemented through the same memory block as user-space Mmap by the kernel.

The difference and implementation principle of ET and LT in epoll (important)

(1) Epoll two working modes are triggered horizontally: the socket receiving buffer in the kernel is not empty, and there is data to read, and the read event keeps triggering. If the socket sending buffer in the kernel is insufficient, data can be continued to be written, and the write event keeps triggering

Edge triggering: the socket receive buffer in the kernel changes from null to non-null, data changes from unreadable to readable, and an event is triggered (only once).

The socket send buffer in the kernel changes from full to insufficient, data changes from unwritable to writable, and an event is triggered (only once).

(2) When selecting the working mode, LT mode will always trigger readable and writable events, resulting in low efficiency. In ET mode, data may be lost because read/write events are notified only once. (3) 1. In ET mode, when multiple connections arrive at the server at the same time, epoll_WAIT will return multiple descriptors. In ET mode, the ready state is returned only once. 2. In ET mode, read/write events trigger only once. If all data is not processed at a time, data will be lost. The solution is to set the socket of the connection to non-blocking when accept receives the connection and loop read/write until the data is fully processed (that is, an EAGAIN error is returned) while reading or writing data.

  1. In LT mode, if epoll Out events are monitored, epoll_wait will always return because the kernel buffer is always writable at the beginning, which reduces efficiency. Write data until the write buffer is full (return an EAGAIN error), then listen for out events. When all data is written, unlisten for out events

Summary of other network problems

1. The CONNECT method will block. Is there any way to avoid long time blocking? Answer: the most common method is the most effective is to add timer; Non-blocking mode can also be used.

2, in the network, if the client suddenly dropped or restarted, how can the server immediately know? A: If the client is disconnected or restarted, the server will receive a reset signal. Each TCP/IP implementation is different and the control mechanism is different.

3. How many available addresses are available in subnet 210.27.48.21/30? What are they? A :30 indicates that the network number is 30 bits, 11 is a broadcast address, 00 is a multicast address, and only 01 and 10 can be used as host addresses. 210.27.48.21/30 indicates that the network number of the subnet is 30 bits, that is, the network number is 210.27.48.21&255.255.255.251 =210.27.48.20. The address space of the subnet is two bits, that is, it can have four addresses: 210.27.48.20, 210.27.48.21, 210.27.48.22, 210.27.48.23. The host number/ ID of the first address is 0, which represents a multicast address. The last two digits of the last address are 11, and each 1 of the host number indicates a broadcast address. So only the middle two addresses can be used by the host. In fact, the question itself is not accurate, broadcast or multicast addresses are also available, so answer 4 should also be correct, of course, the person asking may want you to answer 2. I personally think the best answer is one broadcast address, one multicast address, and two unicast addresses.

4. What is TTL? What does it do? What tools do they use it for? (ping? traceroute? ifconfig? Netstat?) A :TTL is Time To Live, usually hUP count. It is subtracted by one for each route. If it becomes 0, the packet is discarded. Its main purpose is to prevent packet dead turn on looped network and waste network resources. Ping and traceroute use it.

TTL is Time To Live, currently hUP count. Each Time a packet passes through a router, it is subtracted by one. If it becomes zero, the router will throw the packet away. IP networks often have loops. For example, subnet A and subnet B are connected by two routers, which is A loop. The main purpose of TTL is to prevent packets from dying on a looped network, because the TTL of the packet eventually turns to 0 and the packet disappears from the network. (In this case, the router will send an ICMP packet back, which is what Traceroute does.) Ping sends the packet out, so it’s in there, but ping doesn’t have to have it. Traceroute is all about it. Ifconfig is used to configure network cards and netstat-rn is used to list routing tables, so it is not needed

5. What is the routing representation used for? How to configure a default route in Linux? A: Routing tables are used to determine how packets are sent from one subnet to another, or in other words, to which network card a packet received from one network card should go. On Linux, you can run route add default gw < default router IP address > to configure a default route.

Routing tables are used to determine how packets are sent from one subnet to another, or in other words, to which network card packets received from one network card should be sent. Each row of the routing table has at least the target network number, netmask, and network card that should be used to reach the subnet. When a router receives a packet from a network card, it scans each row of the routing table and does a logical arithmetic (&) between the netmask in the packet and the destination IP address in the packet to find the destination network number. If the network number is the same as the network number in the row, it reserves the route as an alternate route. If there is an alternate route, save the longest network number of the two routes, discard the other route, and then scan the next line until the end. If no route is found at the end of the scan, the default route is used. After determining the route, the packet is directly sent to the corresponding network card. In a concrete implementation, the routing table may contain more information for the details of the routing algorithm. Off-topic: Routing algorithms are actually very inefficient and not scalable, and the solution is to use IP switches such as MPLS. On Linux, you can run route add default gw < default router IP address > to configure a default route.

6. On the network, two hosts A and B are connected to another switch device through A router. If the physical connection is correct, how do I check whether the two hosts are connected? If no, how to determine the fault point? How to troubleshoot? A: Test whether the two machines are connected: Ping from one machine to the other if the ping fails, use Traceroute to determine which router is disconnected, and then find the fault between the switching device/HUP /cable, etc.

7 network programming design concurrent server, using multi-process and multi-thread, what is the difference? The child process is a copy of the parent process. The child process gets a copy of the parent’s data space, heap, and stack. 2. Thread: Compared with process, thread is a concept closer to the execution body. It can share data with other threads of the same process, but has its own stack space and independent execution sequence. Both can improve program concurrency, efficiency and response time. Thread and process have their own advantages and disadvantages: thread execution costs little, but is not conducive to resource management and protection; The process is the opposite. At the same time, threads are suitable for running on SMP machines, while processes can be migrated across machines.

Each process has its own address space, while threads share the address space. All other differences come from this: 1. Speed: Threads generate faster, communicate faster, switch faster, etc., because they are in the same address space. 2. Resource utilization: Threads also have better resource utilization because they are in the same address space. 3. Synchronization issues: threads using common variables/memory need to use synchronization because they are in the same address space.

General steps of network programming

For TCP connections:

1) Create socket create; 2) Bind the port number. 3) Monitor connection listen; 4) Accept the connection request and return the new socket; 5) Use the newly returned socket recv/send; 6) Close the socket.

2. Client 1) Create socket create; 2) Initiate connection request CONNECT; 3) Send/receive data send/ recV 4) Close the socket.

TCP summary:

Server端:create — bind — listen– accept– recv/send– close

The Client side: create — — — — — — — conncet — — — — — – the send/recv — — — — — – the close.

For UDP connections:

1. On the server :1) Create socket create; 2) Bind the port number. 3) Receive /send message recvfrom/sendto; 4) Close the socket.

2. Client :1) Create socket create; Send/receive sendto/ recvFROM; 3) Close the socket.

UDP summary:

On the Server: create—-bind —-recvfrom/sendto—-close

Client端:create—- sendto/recvfrom—-close.

How is the TCP retransmission mechanism implemented

1. The sliding window mechanism establishes the sending and receiving boundary, which enables the sender to know how many bytes have been sent (confirmed), the number of bytes that have not been confirmed, and the number of bytes that are yet to be sent; Let the receiver know (how many bytes have been acknowledged).

2. Select Retransmission to retransmit the sequence that fails to be transmitted.

Why isn’t TCP a double connection? Three handshakes?

If two processes A and B communicate, if only two connections. After user A sends the request packet, network congestion occurs due to poor network conditions. That is, user B receives the packet after A long delay. In this case, User A considers the packet invalid. After receiving the packet, USER B initiates A connection to user A. After the two handshakes are complete, USER B considers that A connection has been established and can communicate with user A. User B waits for the connection request sent by user A, but User A does not process the invalid packet. B is busy and so on, resulting in a waste of resources.

The connect method will block. Is there any way to prevent it from blocking for a long time?

The asynchronous transmission mechanism can be considered. The main difference between synchronous transmission and asynchronous transmission is that in synchronous transmission, if recvFROM is called, the operation will be blocked uniformly, resulting in the suspension of the calling thread. The asynchronous transmission mechanism does not, and returns immediately.

Network programming in the design of concurrent server, the use of multi-process and multi-thread, what is the difference?

Answer a:

1. Processes: Child processes are copies of parent processes. The child process gets a copy of the parent’s data space, heap, and stack.

2. Thread: Compared with process, thread is a concept closer to the execution body. It can share data with other threads of the same process, but has its own stack space and independent execution sequence. Both can improve program concurrency, efficiency and response time.

Thread and process have their own advantages and disadvantages: thread execution costs little, but is not conducive to resource management and protection; The process is the opposite. At the same time, threads are suitable for running on SMP machines, while processes can be migrated across machines.

Answer 2:

The basic difference is that each process has its own address space, while threads share the address space. All the other differences come from this:

1. Speed: the speed generated by threads is fast, communication between threads is fast, switching is fast, etc., because they are in the same address space.

2. Resource utilization: Threads also have better resource utilization because they are in the same address space.

3. Synchronization issues: Threads using common variables/memory need to use synchronization because they are in the same address space.

Connect will block. How to solve the problem

When a connect call is made, if the server is closed while the active connection is being made, the process will be blocked by CONNECT and wait a long time to return. If CONNECT is defined as non-blocking directly, there is no way to determine whether connect succeeded. 1. Set up Socket 2. Set the socket to non-blocking mode 3. Call connect() 4. Use select() to check whether the socket descriptor is writable to 5. Judge connect() result 6 based on the result returned by select(). Set the socket to blocking mode

What is the concurrency model of Linux

(1) There are three concurrency models in Linux, including: multi-process concurrency, multi-thread concurrency and IO reuse model. (2) Multi-process concurrency: When accept returns success, a process is forked for the connection, which is dedicated to processing the data on the connection. When the connection is finished, the process is terminated. Advantages: Compared with multi-process mode, it can save some resources and be more efficient. Disadvantages: Compared to the multi-process approach, increased programming complexity, because of the need to consider data synchronization and lock protection. Do not start too many threads in another process. In Linux, threads are actually processes inside the system, and thread scheduling is performed in accordance with the process scheduling method. There is a thread dedicated to listening on the port, and when accept returns, it puts the descriptor into the descriptor set fd. One thread uses SELECT to train the descriptor set to receive data on the connection with the data, and the other thread is dedicated to sending data. Of course you can also receive and send with one thread. Epoll mode: a thread listens on ports. When receiving a connection, it sets the connection to non-blocking mode and adds epoll events to epoll management by setting epoll events to edge trigger mode. The receiving thread blocks the wait event function in epoll. Another thread is dedicated to sending data.

What is the time_wait state? Why is there a time_wait state? Which party has the TIME_WAIT state and how can I prevent the time_WAIT state from occupying resources?

TCP ends the connection with four waves of the hand. TIME_WAIT: indicates that the FIN packet is received and an ACK packet is sent. TCP connections in TIME_WAIT state wait 2*MSL and then return to CLOSE. Max Segment Lifetime: indicates the maximum Lifetime of a TCP packet on the Internet. Each specific TCP implementation must select a definite MSL value. RFC 1122 recommends 2 minutes, but the BSD traditional implementation uses 30 seconds. Linux can see this value on the native machine by cat /proc/sys/net/ipv4/tcp_fin_timeout. If the FIN_WAIT_1 state receives a packet with both the FIN flag and ACK flag, you can enter the TIME_WAIT state without going through the FIN_WAIT_2 state. The TIME_WAIT state is the state of the end that initiates the disconnection.

The meanings of TIME_WAIT state are as follows: 1. A reliable full-duplex connection is implemented: The peer end may want to reset the ACK packet to resend it after the last ACK packet is sent. 2. Allow old repeating segments to disappear from the network.

Third, summary

Network related interview questions, background development students need to seriously study and strengthen, compare this is generally more complex, encountered more situations, we need to learn to sum up, so as to be able to work with ease ~

Four, past wonderful summary

GDB multithreading journey

Liver! Dynamic programming

The use of locks in C++

Painstaking recursion

Muduo source analysis study summary

Welcome to pay attention to the public number – background server development, more exciting wait for you to see ~

First ———— wechat public number — background server development