Against the big factory! "Golden Nine Silver Ten" and autumn recruits pass rate of 90% Linux interview tips

Preface

Considering that there are many people in the critical period of interview preparation, I may continue to update some Java interview-related articles for everyone to learn and communicate to prepare for the interview. If you have anything to add, you can also comment below the article. Your encouragement is my biggest motivation!

Linux interview pointsInsert picture description here

1. What are the main types of kernel locks in Linux?

The synchronization mechanism of Linux has been continuously developed and improved since 2.0 to 2.6. From the initial atomic operation, to the later semaphore, from the big kernel lock to today's spin lock. The development of these synchronization mechanisms is accompanied by the transition of Linux from a single processor to a symmetric multi-processor; with the transition from a non-preemptive kernel to a preemptive kernel. Linux's lock mechanism is becoming more effective and more complex. Linux's kernel locks are mainly spin locks and semaphores.

The spin lock can only be held by one executable thread at most. If an execution thread tries to request a spin lock that has been contented (has been held), then this thread will continue to be busy loop-spin- Wait for the lock to be available again. If the lock is not contended, the thread of execution that requested it can immediately obtain it and continue. Spin locks can prevent more than one execution thread from entering the critical section at any time.

The semaphore in Linux is a sleep lock. If there is a task trying to obtain a semaphore that has been held, the semaphore will push it into the waiting queue and then put it to sleep. The processor is now free to execute other code. When the process holding the semaphore releases the semaphore, a task in the waiting queue will be awakened, so that the semaphore can be obtained. The sleep characteristics of the semaphore make the semaphore suitable for situations where the lock will be held for a long time; it can only be used in the process context, because it cannot be scheduled in the interrupt context; in addition, when the code holds the semaphore, it cannot Hold the spin lock again.

Synchronization mechanisms in the Linux kernel: atomic operations, semaphores, read-write semaphores, and spin lock APIs, and other synchronization mechanisms, including large kernel locks, read-write locks, large reader locks, RCU (Read-Copy Update, as the name implies It is read-copy modification), and sequence lock.

2. What is the meaning of user mode and kernel mode in Linux?

Operating systems such as MS-DOS run in a single CPU mode, but some Unix-like operating systems use dual modes, which can effectively realize time sharing. On Linux machines, the CPU is either in the trusted kernel mode or in the restricted user mode. Except that the kernel itself is in kernel mode, all user processes are running in user mode. Kernel-mode code has unlimited access to all processor instruction sets and all memory and I/O space. If a user-mode process wants to enjoy this privilege, it must issue a request to a device driver or other kernel-mode code through a system call. In addition, user-mode code allows page faults, while kernel-mode code does not. In 2.4 and earlier kernels, only user-mode processes can be context switched out and preempted by other processes.

Unless the following two situations occur, kernel mode code can always monopolize the CPU:

(1) It voluntarily abandons the CPU; (2) An interrupt or exception occurs.
The 2.6 kernel introduces kernel preemption, and most kernel mode codes can also be preempted.

3. How to apply for a large chunk of kernel memory?

In the Linux kernel environment, the success rate of applying for a large block of memory decreases with the increase of system running time.Although it is possible to apply for physical discontinuous but continuous virtual address memory through the vmalloc series of calls, its use efficiency is not high and it is in 32 The memory address space of vmalloc on bit systems is limited. Therefore, the general recommendation is to apply for a large block of memory during the system startup stage, but the probability of success is only relatively high, not 100%. If the program really cares about the success of this application, it can only retreat to "Boot Memory".

The following is a sample code to apply for and export the startup memory:

> void* x_bootmem = NULL; EXPORT_SYMBOL(x_bootmem); unsigned long
> x_bootmem_size = 0; EXPORT_SYMBOL(x_bootmem_size); static int __init
> x_bootmem_setup(char *str) {
    
     x_bootmem_size = memparse(str, &str);
> x_bootmem = alloc_bootmem(x_bootmem_size); printk(“Reserved %lu bytes
> from %p for x\n”, x_bootmem_size, x_bootmem); return 1; }
> __setup(“x-bootmem=, x_bootmem_setup);

It can be seen that its application is relatively simple, but the advantages and disadvantages are always symbiosis, and it inevitably has its own limitations: the memory application code can only be connected to the kernel, and cannot be used in the module. The allocated memory will not be used and counted by the page allocator and slab allocator, which means it is outside the visible memory of the system, even if you release it somewhere in the future. General users will only apply for a large block of memory, if you need to implement complex memory management on it, you need to implement it yourself. When memory allocation failure is not allowed, reserving memory space by starting the memory will be our only choice.

4. What are the main ways of communication between user processes?

(1) Pipe ** (Pipe) : Pipes can be used for communication between processes with kinship, allowing communication between a process and another process that has a common ancestor with it.
(2) Named pipe ** (named pipe)
: Named pipe overcomes the limitation that pipes have no name. Therefore, in addition to the functions of pipes, it also allows communication between unrelated processes. Named pipes have corresponding file names in the file system. The named pipe is created by the command mkfifo or the system call mkfifo.
(3) Signal (Signal): Signal is a more complex communication method, used to notify the receiving process that a certain event has occurred. In addition to inter-process communication, the process can also send signals to the process itself; linux supports early Unix signals In addition to the semantic function sigal, it also supports the signal function sigaction whose semantics conform to the Posix.1 standard (in fact, this function is based on BSD. In order to achieve a reliable signal mechanism, BSD can unify the external interface and reimplement the signal function with the sigaction function) .
(4) Message (Message) queue : Message queue is a linked list of messages, including Posix message queue system V message queue. Processes with sufficient permissions can add messages to the queue, and processes with read permissions can read messages in the queue. The message queue overcomes the lack of signal carrying information, the pipeline can only carry unformatted byte streams, and the buffer size is limited.
(5) Shared memory : allows multiple processes to access the same memory space, which is the fastest available IPC form. It is designed for the low efficiency of other communication mechanisms. It is often used in combination with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes.
(6) Semaphore : mainly used as a means of synchronization between processes and between different threads of the same process.
(7) Socket : A more general inter-process communication mechanism that can be used for inter-process communication between different machines. It was originally developed by the BSD branch of the Unix system, but now it can generally be ported to other Unix-like systems: Linux and System V variants support sockets.

5. What are the functions to apply for kernel memory through the partner system?

A zone based buddy system is implemented on the physical page management. A separate buddy system is used to manage the memory in different areas, and free pages are monitored independently. The corresponding interface alloc_pages(gfp_mask, order),_ _get_free_pages(gfp_mask, order), etc.

6. What are the key data structures of the Linux virtual file system?

(Write at least four) struct super_block, struct inode, struct file, struct dentry;

7. In which data structure is the operation function of the file or device stored?

struct file_operations

8. What are the files in Linux?

Execution files, ordinary files, catalog files, link files and device files, pipeline files.

9. What are the system calls to create a process?

clone(), fork(), vfork(); system call service routines: sys_clone, sys_fork, sys_vfork;

10.How many ways to call schedule() for process switching?

1. The system call do_fork();
2. Timed interrupt do_timer();
3. Wake up the process wake_up_process
4. Change the scheduling strategy of the process setscheduler();
5. The system call polite sys_sched_yield();

11. Does the Linux scheduler schedule processes based on their dynamic priority or static priority?

The Liunx scheduler schedules processes according to the dynamic priority of the process, but the dynamic priority is calculated according to the algorithm based on the static priority, and the two are two related values. Because high priority processes are always better than low priority processes

The process is scheduled first. In order to prevent multiple high-priority processes from occupying CPU resources, other processes cannot occupy the CPU, so the concept of dynamic priority is used
Insert picture description here

12. What is the core data structure of process scheduling?

struct runqueue

13. How to load and unload a module?

insmod load, rmmod unload

14. Where are the modules and applications running in?

The module runs in the kernel space, and the application runs in the user space

15. Is the floating point operation in Linux implemented by the application or by the kernel?

Application program implementation, floating-point operations in Linux are implemented using mathematical library functions. Library functions can be called after the application is linked, but cannot be called by the kernel link. These calculations are run in the application, and then the results are fed back to the system.

If the Linux kernel must perform floating-point operations, you need to select math-emu when building the kernel, and use software to simulate floating-point operations. It is said that there are two costs for this: The user needs to rebuild the kernel when installing the driver, which may affect To other applications, these applications also use math-emu when doing floating-point operations, greatly reducing efficiency.

16. Can module programs use linkable library functions?

The module program runs in the kernel space and cannot link library functions.

17. What is cached in the TLB?

TLB, page table cache, when a linear address is converted to a physical address for the first time, the correspondence between the linear address and the physical address is placed in the TLB, which is used to speed up the conversion when the linear address is accessed next time.

18. What kinds of devices are there in Linux?

Character devices and block devices. The network card is an exception. It does not directly correspond to the device file. The mknod system call is used to create the device file.

19. What is the key data structure of the character device driver?

Character device descriptor struct cdev, cdev_alloc() is used to dynamically allocate cdev descriptors, cdev_add() is used to register a cdev descriptor, cdev contains a struct kobject type data structure, which is the core data structure

20. What functions does the device driver include?

open(),read(),write(),llseek(),realse();

21. How to uniquely identify a device?

Linux uses a device number to uniquely identify a device. The device number is divided into: major device number and minor device number. Generally, the major device number indicates the driver corresponding to the device, and the minor device number corresponds to the device pointed to by the device file, which is used in the kernel. dev_t represents the device number, generally it is 32 bits in length, of which 12 bits are used to represent the major device number, 20 bits are used to represent the minor device number, using MKDEV (int major, int minor); used to generate a dev_t type object .

22. In what way does Linux implement system calls?

It is realized by software interrupt. First, the user program sets the parameters for the system call. One of the numbers is the system call number. After the parameter setting is completed, the program executes the system call instruction. The soft interrupt on x86 is generated by an int. This instruction will cause An exception generates an event, which will cause the processor to jump to kernel mode and jump to a new address. And start to deal with the exception handler there, the exception handler at this time is the system call program.

23. What is the function of Linux soft interrupt and work queue?

The soft interrupt and work queue in Linux are interrupt processing.

1. Soft interrupt is generally a general term for "delayable function", it cannot sleep, cannot block, it is in interrupt context, cannot process switching, soft interrupt cannot be interrupted by itself, but can only be interrupted by hardware interrupt (top half) , Can run concurrently on multiple CPUs. So the soft interrupt must be designed as a reentrant function, so it also needs a spin lock to protect its data structure.
2. The function in the work queue is in the process context. It can sleep or be blocked, and can switch between different processes. Different tasks have been completed. Neither the deferrable function nor the work queue can access the user's process space. The deferrable function cannot have any running process when it is executed. The function of the work queue is executed by the kernel process, and he cannot access the user space address.

At last

Welcome everyone to learn and exchange together. If you like the article, remember to follow me and give me a like, thank you for your support!

If you need more Linux interview questions, you can follow me and reply to "learn" to get more information

Guess you like

Origin blog.csdn.net/weixin_50333534/article/details/108433315