Embedded some interview questions

Retrieved from: https://mp.weixin.qq.com/s/N4IfREMrZuW1K9x3BQb2EA

Send another wave of super benefits, want to know?

txp playing Linux 3 days ago

Before sharing the summary of the classic questions of this interview, let’s make a preview. The summary of the interview questions shared in the next issue will complete the answer to an interview question shared by netizens in the group:

The sources of interview questions are some classic and frequently asked questions on the Internet. The answers are for reference only. If you don’t understand, you can check the books to clarify the principles inside and answer them with confidence. ; At the same time, I personally suggest that you must understand the principle and memorize the standard answer. This effect is very good. Even when it comes to the interview, due to nervousness, the standard answer may be forgotten, but if you understand the principle, you can tell one or two things. ; Of course, the same is true for the written test, so you don’t know how to write at all.

1. Linux startup process:

(1) Load BIOS (2) Read MBR (3) Run BootLoader (4) Load the kernel (5) User layer Init sets the run level according to the inittab file (6) The init process executes rc.sysinit (7) Starts the kernel module (8) Execute script programs of different running levels (9 execute /etc/rc.d/rc.local (10) execute /bin/login program to enter the login state

2. The difference between malloc and new

(1) The new keyword is a part of C++, and new is an operator that can be overloaded.

(2) Malloc is provided by the c library function.

(3) The new keyword allocates memory in units of specific types.

(4) The malloc function allocates memory in bytes.

(5) The new key can be initialized when applying for a single type variable.

(6), malloc cannot initialize memory.

(7), malloc returns a null pointer, new returns an object type pointer

(8), malloc allocation failure returns null, new allocation failure returns exception

3. Please describe the IO multiplexing mechanism?

There are 4 IO models: synchronous blocking IO, synchronous non-blocking IO, asynchronous blocking IO, and asynchronous non-blocking IO; IO multiplexing belongs to the asynchronous blocking IO model in the IO model, and is often used in the construction of server high-performance IO.

Synchronous and asynchronous means the server side, blocking non-blocking means the user side, so it can explain why IO multiplexing (asynchronous blocking) is often used on the server side; file descriptor (FD, also called file handle): a descriptor is just one Number, it points to a structure in the kernel (file path, data area and other attributes). Specific source: The Linux kernel treats all external devices as a file to operate. The operation of the file will call the system command provided by the kernel and return a fd (file descriptor). Let's introduce IO multiplexing:

(1) I/O multiplexing technology multiplexes multiple I/O blocks onto the same select, poll or epoll block, so that the system can process multiple clients simultaneously in a single thread request. Compared with the traditional multi-thread/multi-process model, the biggest advantage of I/O multiplexing is that the system overhead is small, and the system does not need to create new additional processes or threads.

(2) Select, poll, and epoll are essentially synchronous I/O, because they all need to be responsible for reading and writing after the read and write event is ready, which means that the reading and writing process is blocked, while asynchronous I/O is You don't need to be responsible for reading and writing yourself. The implementation of asynchronous I/O will be responsible for copying data from the kernel to user space.

(3) The main application scenarios of I/O multiplexing are as follows: the server needs to process multiple sockets in the monitoring state or multiple connection states at the same time; the server needs to process sockets of multiple network protocols at the same time;

(4) The system calls that currently support I/O multiplexing include select, poll, epoll, epoll and select. The principles are similar, but epoll has made many major improvements, which are summarized as follows:

①The number of file handles FD opened by a process is not limited (why the number of select handles is limited: select uses bit fields to transfer the file descriptors of interest, because bit fields have a maximum length, which is 1024 under Linux , So there is a quantity limit);

②I/O efficiency will not decrease linearly as the number of FDs increases;

③The API of epoll is simpler;

(5) Three kinds of interface call introduction:

①Select function call format:

#include <sys/select.h>
#include <sys/time.h>
int select(int maxfdp1,fd_set *readset,fd_set *writeset,fd_set 
*exceptset,const struct timeval *timeout)

//Return value: the number of ready descriptors, 0 is returned when timeout, -1 is returned when error occurs ②The poll function call format:

# include <poll.h>
int poll ( struct pollfd * fds, unsigned int nfds, int timeout);

③epoll function format (the operation process includes three functions):

#include <sys/epoll.h>
int epoll_create(int size);
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int 
timeout);

(6) Function: Replace multi-thread/multi-process to a certain extent, reduce resource occupation, and ensure high efficiency of system operation

4. Handwritten bubbling implementation code?

void bubble_sort(int a[], int n)
{
      for(int i =0; i<n-1; i++)
      {
          for(int j =0; j<n-1-i;j++)
          {
              if(a[j]>a[j+1]
              {
                  int temp = a[j];
                  a[j]=a[j+1];
                  a[j+1]=temp;
            }
          }
      }
}

5. const and volatile:

(1) Explain the following meaning:

const int * p; //p is variable, the content pointed to by p is immutable

int const * p; //p is variable, the content pointed to by p is immutable

int * const p; //p is immutable, the content pointed to by p is variable

const int * const p; //The contents pointed to by p and p are immutable

(2) Write down the application scenarios you know about volatile:

a. Hardware registers of parallel devices (such as status registers)

b. Non-automatic variables that will be accessed in an interrupt service subroutine (Non-automatic variables)

c. Variables shared by several tasks in multithreaded applications

 

The above topics come from web blogs and related documents, I hope it will be helpful to everyone!

Guess you like

Origin blog.csdn.net/sinat_16643223/article/details/108645186