C + + server interview preparation (4) Linux and multithreading

Time:2021-7-16

Statement: the content of this article is only for reference. If there is any mistake, the blogger strongly hopes you to point out. If you are the original blogger of a certain knowledge point, if necessary, you can contact me and add a link. The content of this article will be updated according to the needs of bloggers. I hope you will pay more attention to it.

File IO related knowledge points

  • Seven file types: common file (-), directory (d), symbolic link (L), pipeline (P), socket (s), character device (c), block device (b)
  • The color of the file in shell: White – normal file, green – executable file, red – compressed file, blue – directory, cyan – linked file, yellow – block device character device pipeline, gray – other file
  • PCB: process control block, storing file descriptor table, is a structure in essence
  • PCB internal members: process ID, process status, current working directory, file descriptor table, user ID group ID, signal related information, etc
  • A process has a file descriptor table with the size of 1024 bytes. The first three are occupied, namely stdin_ FILENO、STDOUT_ Fileno and stderr_ FILENO
  • File descriptor function: find disk file
  • View disk command:df -h

Concurrency and parallelism

  • Concurrent:

It means that multiple tasks are executed at the same time in the same time period, and none of them is finished. Concurrent tasks emphasize simultaneous execution in a period of time, and a period of time is accumulated by multiple units of time, so multiple concurrent tasks may not be executed at the same time in a unit of time.

  • Parallel:

It means that multiple tasks are executed at the same time in a unit time.

In the practice of multithreading programming, the number of threads is often more than the number of CPU, so it is generally called multithreading concurrent programming rather than multithreading parallel programming.

Process related knowledge points

  • View process:ps auxps ajxIt can display the parent process and check the memory usagefree -hTo view the current CPU:top
  • MMU: virtual memory mapping unit, unit is 4K, virtual memory is mapped to physical memory through MMU
  • Register 4 bytes, 1 byte 8 bits
  • Process basic state: initial state, ready state, running state, suspended state, terminated state

        Running state – > suspended state: waiting for resources to be used, such as waiting for peripheral transmission, manual intervention, etc
        Running state – > ready state: a process with higher priority appears

  • Parent child process sharing: file descriptor, MMAP mapping area, global variable read time sharing, write time copying
  • Orphan process: the parent process ends before the child process. The child process is called the orphan process. The init process becomes its parent process and takes over the orphan process
  • Zombie process: when the child process ends earlier than the parent process, and the parent process does not recycle the child process, the process descriptor of the child process is still saved in the system. At this time, the child process will become a zombie process

Knowledge points related to interprocess communication

  • There are four common communication methods between processes: pipeline (easy to use), signal (low overhead), shared mapping area (no blood relationship available), local socket (stable)
  • The Conduit:

In essence, it is a pseudo file. The kernel uses the ring queue mechanism and the kernel buffer (4K). Once the data is read, the pipeline does not exist

  • Shared mapping area:

MMAP, a method of memory mapping files, maps a file or other objects into memory. Unrelated processes can use shared mapping area for communication, which is commonly used in high-performance applications. MMAP mapping memory must be an integral multiple of the page size. Stream oriented devices can’t carry out MMAP. The implementation of MMAP is related to hardware

  • Signal:

        Simple but can not carry a lot of information, meet the conditions to send, equivalent to interrupt, has a strong delay, all signals are sent and processed by the core (PCB)
        Signal 4 elements: number, name, event and default processing action
        Signal processing mode: execute default actions (terminate process, ignore signal, terminate process and generate core file, pause process, continue running process), ignore and capture

  • To view the signal list:kill -l

What should we pay attention to when creating a session

  • The process creating the session cannot be the process group leader, otherwise an error will be returned
  • The process becomes the first process of the new session and a new process group leader
  • Root permission is required (not required for Ubuntu)
  • The new session discards the original control terminal, and the session has no control terminal
  • When creating a session, call fork first, the parent process terminates, and the child process calls setsid ()

Create a daemon

  1. Execute fork in the parent process and exit;
  2. The setsid function is called in the child process to create a new session.
  3. The chdir function is called in the child process, so that the root directory “/” becomes the working directory of the sub process.
  4. The umask function is called in the sub process, and the umask of the process is set to 0.
  5. Close any unnecessary file descriptors (close (FD)) or redirect / dev / null in the child process

Thread related knowledge points

  • View all threads:top -Hps -LfProcess number, view the thread of the process
  • Thread definition: LWP, lightweight process, independent PCB and shared address space
  • Why is thread safety: when a thread is operating on a shared resource, other threads participate in it before the execution is completed, which leads to the safety problem of the shared resource.
  • Methods to protect shared data area: mutex, read-write lock, condition variable, semaphore (C language), atomic operation (usually used to protect single variable), windows critical area
  • Deadlock: repeatedly lock a lock, or two threads request the other party’s lock after locking one lock respectively
  • Deadlock solution: lock in the same order, C + + 11 uses STD:: lock (mutex1, mutex2,…)

Use of conditional variables

  1. Create a mutex and use lock_ Guard or unique_ Lock encapsulation mutex
  2. Create a condition variable and use the wait member function to wait for the condition to be satisfied
  3. Another thread uses notify_ One or notify_ Activate wait after all
  4. Wait determines whether the second parameter is true after holding the lock. If it is true, the following code will be executed. If it is false, it indicates that the condition is not satisfied and continues to wait for the next activation

The difference between sleep() and wait()

  • Sleep() contains < windows. H > header file and wait() contains < wait. H > file
  • Sleep() takes up the CPU sleep and will not unlock. Wait() releases the lock and frees up CPU resources to wait for wake-up and get the lock

The difference between process and thread

  1. Process is the basic unit of resource allocation; Thread is the basic unit of system scheduling and dispatching.
  2. A process can have multiple threads. Threads belonging to the same process have the same address space. The heap is shared and the stack is private.
  3. The process cost is large and the thread cost is small
  4. The communication between threads is relatively convenient, and the communication between processes is relatively complex
  5. Multi process is more robust and multi thread is prone to error
  6. Process is a container of resources, which provides shared resources for all threads in the process. It is a static description of the program. Thread is the smallest scheduling and running unit of the computer, and it is a dynamic description of the program

Why multithreading

  1. Avoid obstruction

Programs in a single thread are executed sequentially. If the previous operation is blocked, the later operation will be affected. In this case, multithreading can be used

  1. Avoid CPU idling

Sometimes processing a request involves database access, disk IO and other operations. The speed of these operations is much slower than that of the CPU. While waiting for these responses, the CPU cannot process new requests and does not make full use of resources. In this case, multithreading will play a role

  1. Improve performance

        Multithreading can improve performance on the premise of satisfying the conditions
        First, tasks are concurrent, and there must be no dependency on the order of the subtasks. In addition, there must be no resource competition
        Second, multithreading can improve performance only when CPU is the bottleneck.
        Thirdly, multi-core CPU is required. If all the above conditions are met, there is an empirical formula to calculate the performance improvement ratio, which is called amdal’s law
       Speed increase ratio = 1 / [(1-p) + (P / N)]
        Where p is the proportion of parallelizable tasks and N is the number of CPU cores

GDB basic debugging

Compile: GCC plus – G, – G keep function name and variable name
Starting: GDB executable
Parameter transfer: set args parameter 1 Parameter 2

operation command
Look at the up and down programs on line n l n
Set to view n-line up and down programs set listsize n
View other file programs L file name: n or function name
Break points in N lines b n
View breakpoints i b
Remove Breakpoint D N, n is the breakpoint number
Set an invalid or valid breakpoint DIS / ENB n, n is the breakpoint number
Conditional breakpoint B N if variable = = value
function R or start, R directly to the breakpoint, start into the first line of the program
Single step execution n
Go to the next breakpoint c
View variable information P variable name
View variable types Ptype variable name
Always show variable information Display variable name
Suppress variable display First I display to view the variable number n, and then undisplay n
Enter function s
Departure function finish
Jump out of the loop Until, you need to delete the breakpoint
Leave GDB q

GDB can also directly debug the core file to see the error:

1. When a program has segment error, the following prompt will appear:
       Segmentation fault (core dumped)
        Core refers to the memory distribution of the process space when the program is running
        Dumped indicates that the kernel has thrown the core
        Usually, when a segment error occurs, a core file should be automatically generated in the program running directory to store the core thrown by the kernel. However, because the core file is limited to 0 by default in Linux environment, the core file cannot be generated in general.

2. View the core file size limit:ulimit -c

3. Change it to unlimited:ulimit -c unlimited

4. Operation command:GDB executable file name core

5. GDB input   where

GDB multi thread debugging

Command line view thread information:

  • To view the currently running process:PS aux|grep executable file name
  • To view the currently running lightweight processes:PS - al|grep executable file name
  • To view the relationship between the main thread and the new thread:Pstree - P main thread ID

View thread stack structure:

  • Get thread ID
  • View the stack structure through the command:PS stack thread ID

Using GDB to view thread information:

  • Attach the running process to the GDB debugger to see if a new thread has been createdGDB attach mainline ID

Enter GDB debugging:

  • info inferiors: view current process
  • info threads: view current thread
  • bt: view the current thread stack structure
  • thread n/ID: switch threads (n represents the number of threads)
  • Break line number / function name: set the current thread breakpoint
  • break file.c:100 thread all: set breakpoints at line 100 of the file. C for all threads passing through.
  • set scheduler-locking off/on/step
    When using the step or continue command to debug the currently debugged thread:
    Off: no thread is locked, that is, all threads are executed. This is the default value.
    On: only the currently debugged program will execute.
    Step: in single step, only the current thread will execute, except when next passes a function (people familiar with the situation may know that this is actually a behavior of setting breakpoints and then continue).
  • thread apply ID1 ID2 command: let one or more threads execute GDB command
  • thread apply all command: let all debugged threads execute GDB command

Makefile Foundation

  • Search down the content and judge which file needs to be updated according to the time
  • Target: dependence
            command
  • Find the specified file type in the directory:Variable = $(wildcard file path)
  • Match replacement:Variable = $(patsubst original name, replaced name, source file)
  • notdir: directory function to remove file names
  • basename: take file name function
  • [email protected]: target in rule$<: the first dependency in the rule$^: all dependencies in the rule
  • /: line feed
  • Custom command:
    name:
            command
  • Declare false target:
    . phone: name

Let’s talk about the difference between Linux and windows

  1. Compatibility: windows is backward compatible, Linux kernel upgrade may not be compatible, and windows is superior in software
  2. Process: a single process cannot be executed by windows, but can be executed by Linux
  3. Performance: Windows graphical interface, a lot of rendering, low performance, less Linux, work with the command line, good performance
  4. File system: Linux has a variety of file systems, the management of files and devices is dominant, windows is poor
  5. Interrupt priority: windows has 32 levels, Linux seems to have only 5 levels. Exceptions are divided into fault, trap and abort
  6. Software startup: windows use registry, Linux use configuration file, high flexibility

Recommended Today

STM32 network circuit design

In previous tweets《SMI interface of STM32 network》《MII and RMII interfaces of STM32 network》, all interfaces of STM32 Ethernet and external PHY are introduced. If some students are not familiar with SMI, MII and RMII interfaces, it is recommended to read the two articles mentioned above, otherwise they may not understand the following. ​ Zone 1:We […]