Summary of some recent server problems encountered

      As the business became familiar, more time was spent on technology. If you encounter a problem, make a note.

1. How to check the current network status of the server, how to check whether the socket is disconnected, and how to check the connection status of each port.

Use ifconfig to display the details of all network interfaces, such as: ip address, subnet mask, etc.

Use netstat -na to display the connection information of each port of the current network, and display the status of each connection (a all, n shows the address, you can add t to specify tcp, and l to specify the socket to be monitored).

 

2. How to ensure that there is no error when linux fd (file descriptor) is reused.

 

 

3. How does the client connect to the server asynchronously.

Use multiple threads or multiple processes to connect to the server (thread pool)

 

4. How to find files containing specified output in one directory and multiple directories

Use  grep'xxxx' [directory 1] [directory 2] -rn to search for files in directory 1, directory 2 where'xxxx' appears, and show which line appears.

 

5. Skynet accidentally used call in the for loop and another service did not handle it. How to solve this situation.

 

6. How is Lua's table realized.

I haven't read this yet. I only know that Lua's table is realized by array and hash together. One interesting thing is that there is a table, table[1000] = 1. In this case, it is an array or a hash (although it is an array in our opinion, it is actually a hash. Simply think, if it is an array, Not practical, create 1000 addresses and only store one address). Take the length of the table, it will find a plastic key, and meet the condition

  • table[key] != nil
  • table[key+1] == nil

Therefore, if there is a nil table in the middle, an error will occur when calculating the length.

 

7. How to judge whether the server has an infinite loop.

  1. top : find out programs that take up too much cpu
  2. top -H -p : record the thread number occupying the cpu
  3. gdb attach <process number> : Gdb debugging of programs that occupy too much CPU
  4. info thread  : list thread status
  5. thread <thread number>  : switch to a thread according to the thread number
  6. bt  : output stack
  7. l  : View the current code
  8. print <variable name>  : output the necessary variable content
  9. detach : detach thread
  10. q Quit gdb debugging
  11. Deal with the infinite loop and restart the process/thread .

 

8. How to check how many people can be hosted by your server.

Check the memory of the linux server, check how much memory a player needs, and calculate.

 

9. During socket write, if the other party is closed and the kernel buffer is not full yet, what will happen if you try to continue writing.

Write to the buffer first, and then return an error and trigger the signal.

 

10. The efficiency of dynamic library and static library is high. Why do C++ dynamic libraries declare extern c. Call the method of the dynamic library.

Static library

When the program is connected to the static library, the machine code of all functions contained in the object file in the library that will be used by the program are copied to the final executable file. This will lead to a relatively large amount of executable code generated in the end, which is equivalent to the compiler completing the code. Advantages , so that it runs relatively faster. But there is a disadvantage : it takes up disk and memory space. The static library will be added to every program connected to it, and when these programs run, they will be loaded into the memory. Invisibly consumes more memory space.

Dynamic library

The executable file connected with the shared library only contains the reference table of the functions it needs, not all the function codes. Only when the program is executed, the required function codes are copied to the memory. Advantages , this makes executable files smaller, saves disk space, and further, the operating system uses virtual memory, so that a shared library resides in memory and is used by multiple programs, which also saves memory. Disadvantages , but because it will take a certain amount of time to link the library at runtime, the execution speed will be relatively slow. In general, static libraries sacrifice space efficiency in exchange for time efficiency, and shared libraries sacrifice time efficiency in exchange for space Efficiency, there is no difference between good and bad, just look at the specific needs.

In addition, after a program is compiled, some modifications and optimizations are sometimes needed. If the library function we want to modify happens to be a library function, under the premise that the interface remains unchanged, the program that uses the shared library only needs to recompile the shared library. , And the program that uses the static library needs to recompile the static library, and then recompile the program again. This is also the difference in the use process. Take the current project as an example. During the remote update, if only the *.so dynamic library package content changes, then you only need to update *.so.

The variables and functions modified by extern "C" are compiled and linked according to the C language

 

11. What is the mode of epoll used by skynet? What is the difference between epoll horizontal trigger and edge trigger

Level trigger (Level_triggered ) , epoll is triggered horizontally by default, and skynet has not been modified.

Level-triggered (Level_triggered ) : For the socket to read it, read the kernel buffer associated file descriptor is not empty, the data can be read, it will trigger ready for reading, writing for the socket, the descriptor associated kernel write buffer Dissatisfaction can trigger write readiness.

Edge trigger (Edge_triggered) : For socket read, once a message arrives, a read ready is triggered. If the buffer is not read at one time, the remaining messages will stay in the buffer and will not trigger read ready. For socket writing, the write ready is triggered only once. If the buffer is full, the write ready will be triggered again when the buffer is sent. If it is not full, it will not trigger write ready.

 

12. How to realize the C++ virtual function.

For classes with virtual functions, the compiler will allocate an additional virtual function table, which records the address of the virtual function. When this class is inherited, if the subclass also writes a virtual function, it will be in the virtual function of the subclass The function address of the parent class is overwritten in the table, otherwise the virtual function address of the parent class is inherited.

After instantiation, the object has a virtual function pointer, which points to the virtual function table, so that when the program runs, the virtual function table found through the virtual function pointer is pointed to according to the type of the object.

 

13. How is skynet timer realized.

Guess you like

Origin blog.csdn.net/banfushen007/article/details/108699760