1. Why does the memory need a segmentation and paging mechanism?
In early-rising computers, programs run directly on the physical memory. There are several problems to do this:
1) The address space is not isolated, and the security and stability of the computer cannot be guaranteed. Because all programs can access the physical memory, malicious programs can easily modify the content of other programs, achieve the purpose of destruction.
2) memory usage efficiency is low. The currently executed Program (included in process a) must be fully loaded into the memory for execution. If you need to execute another program, you will find that the memory space is insufficient, the data of process a needs to be swapped out to the disk as a whole.
3) The program running address is unknown.
To solve the preceding three problems, the concept of virtual address, segment, and paging is introduced.
With virtual addresses, each process has its own virtual address space. Combined with the segmentation technology, the virtual addresses of different processes are mapped to different physical addresses without overlap, the problem of address space isolation is solved.
The basic idea of paging is to divide the memory into fixed pages. The size of each page is determined by the hardware or operating system. At present, the operating systems on almost two PCs are all 4 kb pages. The paging mechanism increases the granularity of memory usage and is more efficient in memory swap-in and swap-out.
2. What resources are shared by different processes? What resources are shared between different threads in a process?
1)
2) thread PRIVATE: Stack (local variables, function parameters), TLS data, registers. Thread sharing: global variables, heap, static variables, program code, opened files, and signals.
3. Thread Security Mechanism
1) atomic operation
2) semaphores
3) mutex
4) critical section
5) read/write lock
6) condition Variables
7) reentrant function
8 *) Execution of CPU in disorder, Barrier
4. Compilation and link
1) pre-compile: gcc-e hello. C-O hello. I
2) Compile: generate an assembly code file. Gcc-s hello. I-O hello. s
3) Assembly: converts Assembly commands into binary machine commands. Gcc-C hello. S-O hello. O or as hello. S-O hello. o
4) Link: LD command
5. Target File
The target file is the intermediate files (in Windows) that are compiled by the source code but are not linked. in OBJ and Linux. o), it is similar to the content and structure of the executable file, so it is generally stored in the same format as the executable file (Windows PE-COFF and elf in Linux)
The content in the target file includes the compiled code, Data, symbol table, and debugging information.
* What are the advantages of separate storage of programs and commands? 1) The command area is read-only and secure. 2) Cache Usage helps increase the cache hit rate of the CPU. 3) program sharing: when multiple copies of the same program are run, only one copy of code is required.
. BSS segment: stores static variables and uninitialized global variables without occupying disk space.
. Data Segment: initialized global variable
. Text: code segment
. Rodata: const variable and String constant
Custom segment:
_ Attribute _ (Section ("foo") int global = 42;
_ Attribute _ (Section ("bar") void Foo (){}