Operating System Interview Summary

operating system

interrupt

  1. Type of interrupt:
    1. An interrupt caused by an abnormality or failure of computer hardware is called an internal abnormal interrupt.
    2. The interrupt caused by the execution of the interrupted instruction in the program is called a soft interrupt.
    3. Interrupts caused by external devices are called external interrupts.
  2. The process of interrupt handling:
    1. Protect the CPU scene
    2. Analyze the cause of the outage
    3. execute interrupt handler
    4. Restoring the CPU scene

inode

The inode (inode) is where the metadata of the file is stored, including:

  1. creator of the file
  2. Date the file was created
  3. file size
  4. File read, write and execute permissions
  5. The number of links to the file, how many files point to this inode

A directory file contains a series of directory entries, consisting of the filename and the number of the corresponding inode

列出文件目录中所有文件名:
	ls ./code
列出整个目录文件:
	ls -i ./code
列出文件的详细信息:
	ls -l ./code
创建硬链接(删除其中一个文件不会影响另外的文件名,但是修该的话会影响到所有文件名):
	ln source target
创建软链接(文件A和文件B的内容不同,但是文件A的内容是文件B的路径,系统会帮忙打开文件B)ln -s source target

multiprogramming

  1. Process: A process is a running activity of a program about a data set, and it is the basic unit of system scheduling and resource allocation.

  2. Process Control Block (PCB): Process identifier, processor mechanism, process scheduling information (process status, process priority), process control information (program data address, resource list).

  3. Thread: A thread is a part of a process and is the basic unit of CPU scheduling. Threads only reserve some resources necessary for program operation, such as program counters, registers, and stacks.

    1. Shared process resources
    2. Can execute concurrently
    3. Basic unit of independent scheduling and dispatch
    4. Thread entities include program, data, and TCP
  4. Coroutines are scheduled by the programmer in the code of the coroutine

  5. Parallelism: can be executed by multiple CPUs at the same time

    Concurrency: alternately executed by the same CPU

  6. The difference between multiprocessing and multithreading

    multi-Progress Multithreading
    Data is separated, sharing is complex, synchronization is simple Data is shared, sharing is simple, synchronization is complex
    Occupies a lot of memory, switches more, and CPU utilization is low Less memory usage, less switching, and high CPU utilization
    Processes do not affect each other One thread hangs, the whole process hangs
    Create, destroy, switch complex Create, destroy, switch easily
  7. Process scheduling algorithm:

    1. FCFS (First Come First Served Algorithm): Fair, simple, not suitable for interaction, average waiting time can be optimized.

    2. SJF (Short Job First Algorithm): The shortest job is scheduled first, which can guarantee the minimum waiting time.

      Use the exponential averaging method to predict the size of the next CPU interval.

    3. Priority scheduling: Each job is associated with a priority, and the running priority will occupy the CPU first, but there will be a starvation state.

    4. RR scheduling: Set a time slice and schedule according to the length of the time slice, but there are many context switches.

    5. Multi-level queue scheduling: Set up multiple process queues, each process queue has a different priority and uses different scheduling algorithms, but there is still a certain degree of starvation.

    6. Multi-level feedback queues: Tasks can move between queues, and queues can be moved based on CPU time to prevent starvation.

  8. Deadlock: The phenomenon in which multiple processes cannot execute due to circular waiting.

    Four necessary conditions for deadlock:

    1. Mutual exclusion
    2. No preemption
    3. Hold and wait
    4. Circular wait

    Deadlock Detection Method: Banker's Algorithm

    Deadlock release method: deprive resources, terminate the process


interprocess communication

  1. Pipeline: Pipeline is a half-duplex communication method, data can only flow in one direction, and it can only be used in related processes.
    1. Anonymous pipe: apply for a fixed-size buffer in memory, and the program has write and read permissions. Generally, the fork() function is used for inter-process communication.
    2. Named pipe: apply for a fixed-size buffer in memory, the program has write and read permissions, and processes without blood relationship can also communicate between processes.
    3. Features:
      1. byte stream
      2. Lifecycle varies with the kernel
      3. Built-in synchronization and mutual exclusion mechanism
      4. Half-duplex, one-way communication, two pipes can realize two-way communication
  2. Message queue: A queue is created in the kernel. The elements in the queue store data packets. Different processes can access this queue through handles.
  3. Semaphore communication: Control the access of multiple processes to shared resources. A semaphore is essentially a counter that records the access of a resource.
    1. Detect semaphore usage.
    2. P operation: If the value of the semaphore is positive, indicating that the resource is available, the process will set the value of the semaphore to -1 (representing the number of resources of the CPU).
    3. If the value of the semaphore is 0, it means that the resource is unavailable, and the process goes to sleep until the value of the semaphore is greater than 0, and the process is woken up.
    4. V operation: When the process no longer uses the resource, release the resource, the value of the semaphore +1, if there are other sleeping processes waiting, wake it up.
    5. For the associated thread, keep a pointer to the first address of the thread array.
  4. Signal: A signal is a relatively complex communication method used to notify the receiving process that an event has occurred.
  5. Shared memory (Share Memory): Shared memory is to map a piece of memory that can be accessed by other processes. This shared memory is created by one process, but can be accessed by multiple processes.
  6. Sockets: Sockets are also a type of inter-process communication, which can be used for communication between processes on different machines.
  7. Spin lock: When resources are occupied, other processes can only constantly detect the usage of resources, that is, busy waiting, which is easy to cause deadlock and waste of resources.
  8. Mutex: When the resource is occupied, if other processes want to request the resource, the request will be suspended.
  9. Monitor: At the same time, only one process/thread enters the critical section, so that Monitor can achieve the effect of synchronization, with elements:
    1. Monitor objects and locks
    2. Condition variables and wait, signal defined on the monitor object

memory management

  1. Program executable file structure:

    1. .text: store the source code
    2. .rodata: store constants
    3. .data: store initialized global variables and static variables
    4. .bss: Stores uninitialized global variables and static variables
    5. .heap: store variables controlled by functions such as malloc, realloc, free, etc.
    6. .stack: The stack is used to save the function scene when the function is called, and the local variables are also stored in the stack
  2. Virtual memory: When the program is loaded, only part of the program data is loaded into the memory, and only when the program needs it, the other parts are loaded into the memory, realizing the separation of logical memory and physical memory. Demand paging, also known as demand paging, if a particular page does not exist in memory, it is called only when the program needs it. (Virtual memory technology consists of MMU and page table, MMU is a mapping algorithm that maps virtual memory addresses to physical memory addresses)

    1. Check the address in the page table for valid bits and legality.
    2. If the address is invalid, the program is terminated. If the address is legal and valid, call the corresponding page, if invalid, enter page fault interrupt.
    3. If an idle frame is found in the memory, the page is loaded from the disk through the scheduling algorithm. If there is no idle frame, the corresponding page replacement algorithm is called.
    4. Update the page table.
    5. Re-execute the paging procedure.
  3. page replacement algorithm

    1. FIFO: first in first out algorithm
    2. OPT algorithm: not available in practice
    3. LRU algorithm: select the page that has not been used for the longest time recently and eliminate it
    4. LFU algorithm: Eliminate by frequency of use (least frequently used)
  4. The advantages and disadvantages of memory paging: (page number P + page offset W)

    Advantages: No outer fragments, each inner fragment does not exceed the size of the page.

    Disadvantage: If the program is fully loaded into memory, corresponding hardware support is required.

  5. Memory segmentation: the mapping process of logical addresses to physical addresses:

    1. Get the segment number, which is the ss to the left of the logical addresss -bit data
    2. Use the segment number to index to find the starting physical address of the corresponding segment in the process segment table
    3. The rightmost mm of the logical addressm , indicating offset, bit cheap and segment length comparison, illegal if it exceeds the length
    4. The physical address is the starting physical address plus the segment offset
  6. Disk scheduling algorithm

    1. FCFS (First Come First Served Algorithm)
    2. SSTF (Shortest Seek First)
    3. SCAN (elevator algorithm): the head only moves in one direction, and the locality is not good
    4. C-SCAN: The head moves directly to the other side, and requests at both ends can be processed quickly
    5. LOOK, C-LOOK: It is an improvement of the elevator algorithm, that is, it does not need to reach the end of the disk
  7. Linux memory management:

Operation

malloc allocates memory on the heap space, and the heap space will maintain a break pointer. The addresses before the break pointer are allocated, the memory that the process can access, and the space behind the heap space is unmapped. If the process accesses If this space is exceeded, an error will be reported, so malloc is to repair the break pointer and move it to a high address. When malloc allocates, it first checks whether there is a suitable block from the allocated block chain, and if no new block is created.


Linux

  1. top to view system resource usage

    1. VIRT: virtual memory usage
    2. RES: physical memory usage
    3. SHR: Shared memory usage
    4. %MEM: memory usage
  2. ps : View currently running processes

    1. ps -A lists all processes
    2. ps -elf to see which processes are currently
  3. **netstat **

    1. netstat -lnp to see which ports are opened by the system
    2. netstat -an to view network connection status
  4. w and uptime view the system load, load averages represent the average number of tasks in 1 minute, 5 minutes, and 10 minutes.

  5. vmstat

  6. grep is mainly used to find text content and supports regular expressions

    grep [option] pattern filename
    e.x. grep "List" List.cpp
    
    # 查找关于 mysql的进程
    ps -ef | grep mysql
    
  7. sed for editing text content

    sed [option] 'command' filename
    
  8. awk for analytical processing

  9. Components of Linux:

    1. Linux system kernel
    2. shell
    3. File system
    4. application
  10. stat example.txt View the inode information of the file.

  11. **gdb **

    gcc gdb-sample -o gdb-sample -g
    
    1. The role of -o gdb-sample is to generate an executable file named gdb-sample
    2. -g means compile source code information into executable file
    gdb
    
    (gdb) file gdb-sample
    (gdb) r
    (gdb) b main
    (gdb) r
    (gdb) s
    (gdb) p n
    (gdb) b tempfunction
    (gdb) c
    (gdb) display /i $pc
    (gdb) si
    (gdb) d
    (gdb) i r
    (gdb) i r eax
    (gdb) q
    
    1. r run
    2. b set breakpoints
    3. s execute one step
    4. pn View the value of variable n
    5. c continue
    6. display /i $pc display assembly code
    7. si executes a piece of assembly code
    8. d delete all breakpoints
    9. ir displays the value of the register
    10. ir eax displays the value of any specified register
    11. q quit
  12. fork : fork() first allocates resources to the new process, and then copies the remaining resources in the parent process to the child process. The fork() function has two return values:

    1. The pid returned in the parent process is the pid of the new child process
    2. The value of pid returned in the child process is 0
    3. If pid < 0, an error occurs
    4. In vfork(), the parent process shares the data segment with the child process, and vfork() ensures that the child process runs first.
  13. Under the Linux system, when editing a file, the power is suddenly cut off. Is the file saved?

    When Vim opens a file, it will generate a .swap file to save data. If it exits normally, the .swap file will be deleted. If it exits abnormally, it can be replied through the .swap file.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324146448&siteId=291194637