One, three key components

CPU, memory, I/O control chip

Ii. History of Development

  • In the early days, cpus and memory were at the same frequency and were directly connected to the bus, while low-speed devices such as I/O devices were connected to the bus through I/O controllers.
  • The CPU core frequency is much higher than the memory, and the CPU uses frequency doubling mode to communicate with the system bus. The system bus has the same frequency as the memory.
  • The north bridge is connected to high-speed components (memory and graphics devices), while the south bridge is connected to low-speed devices such as keyboard, mouse and USB.
  • SMP: Each CPU has the same status and functions in the system.
  • Multicore processor: Multiple cores share a cache.

Computer system software architecture

“Any problem in computer science can be solved by another layer of indirection.”

  • The protocol through which the layers communicate with each other is called an interface.
  • The system call interface is provided as a software interrupt.
  • Hardware is the definition of interface, operating system kernel layer is the user of interface.

4. Operating system

Two big functions

  • Provide abstract interfaces
  • Manage hardware resources (CPU, memory, I/O devices)

CPU scheduling

  1. Multiprogram:
    • When the CPU is idle, it is allocated to other programs.
    • There is no priority between program schedules.
    • Critical tasks may not get a quick response.
  2. Time-sharing system:
    • Each program actively cedes the CPU after running for a certain period of time.
    • A dependent program actively cedes the CPU.
  3. Multi-tasking system:
    • The operating system takes over all hardware resources.
    • The OPERATING system (OS) has the highest rights and allocates cpus by the OS.
    • Programs run as processes, each with its own address space.
    • There are priorities between processes, and the operating system can force CPU privileges to be removed and assigned to a higher-priority process.

Hardware abstraction

  • The operating system provides a unified interface for developers.
  • Hardware generation vendors provide hardware drivers based on the framework provided by the operating system.

Memory allocation

  1. Direct physical memory allocation:

    1. Address Spaces are not isolated
    2. Low memory usage efficiency:
    You need to load the entire program into memory. There's a lot of memory switching in and out.Copy the code
    1. Program running memory address is uncertain
  2. Virtual Address mapping

    Each process has its own independent virtual address space. The actual physical address mapping is managed by the operating system.

Virtual address mapping method

  1. piecewise
    • Map the memory space required by the program to equal physical space.
    • Address space isolation and virtual space address fixation start at 0x00000000.
    • Unable to solve switch-in and switch-out, insufficient memory still requires a block switch-out.
  2. paging
    • The address space (virtual and physical) is artificially divided into pages of equal size (4KB or 4MB). The 32-bit virtual address space is 4GB, divided into 1 048 576 4KB pages.
    • Data is exchanged and accessed on a page-by-page basis between virtual, physical, and disk pages.

Memory Management Unit (MMU) replication processes the mapping of virtual addresses.

thread

A thread is the smallest unit of a program’s execution flow. A thread consists of a thread ID, the current instruction pointer (PC), a register set, and a stack. A process’s threads share the program’s memory space (including code segments, data segments, heaps, and so on) as well as some process-level resources (open files, and so on).

Thread private Sharing between threads (process owned)
* Local variables The global variable
* Arguments to the function Data on the heap
* Thread local storage (TLS) A static variable in a function
* Registers (basic data of the execution stream) The program code
Open file

Thread scheduling

Thread status:

  • run
  • ready
  • Waiting for the

Scheduling algorithm: priority scheduling, rotation method high-priority threads will execute earlier. IO – intensive threads are more easily prioritized than CPU – intensive threads. IO – intensive threads frequently enter the wait state, giving up CPU time. Starve to death: a thread has a low priority and never gets executed. To avoid starvation, the scheduling system promotes threads that have been waiting too long for execution. A thread is forcibly denied the right to continue execution after the time slice is exhausted. This is called preemption.

Multithreading in Linux

Linux refers to all executing entities, whether threads or processes, as tasks. Fork generates a new task, which executes the same image of the original task and reads a portion of memory. If either task changes memory, a copy is made for separate use (copy while writing). The new task executes the new executable file by calling exec.

Thread safety

When an operation is compiled into assembly code with more than one instruction, its execution may be interrupted by thread scheduling, causing shared global variables and heap data to be changed by other threads. For example, i++ is translated into three instructions by the clang compiler. Atomic operation: single instruction operation without interruption.

Synchronization with the lock

Multiple threads reading at the same time do not cause thread-safety issues, and only non-atomic operations that modify data require synchronization to avoid errors. The thread needs to acquire the lock before accessing the data, and release the lock after accessing the data. When a lock is occupied, the thread waits until the lock is available again.

  1. Semaphore:
    • Get semaphore, semaphore value -1.
    • If the semaphore value is less than 0, the thread blocks, otherwise execution continues.
    • The resource access is complete.
    • The semaphore value is +1.
    • A semaphore with a value greater than 1 wakes up a blocked thread.
    • Semaphores can be acquired by one thread and released by another.
  2. The mutex:
    • Similar to binary semaphores, resources are only allowed to be accessed by one thread.
    • Which thread gets the mutex, which thread frees it.
  3. The critical area:
    • Semaphore and mutex are visible between processes.
    • The scope of the critical value is limited to this process. Other processes cannot obtain the lock.
  4. Read and write locks:
    • Free state: Can be acquired and entered into a shared or exclusive state.
    • Shared state: It can be acquired in shared mode and can be acquired exclusively when all other threads have been freed and enter the exclusive state.
    • Exclusive state: Prevents any other thread from acquiring the lock.
  5. Conditional variable:
    • Threads can wait on condition variables.
    • Threads can wake up condition variables.
    • The condition variable is awakened, and all threads waiting for the condition variable resume execution.

DCL, Double Check Lock Singleton

volatile T* pInst = 0;      # Volatile prevents the compiler from caching variables into registers without writing them back
T* GetInstance(a)
{
    if(pInst == NULL)
    {
        lock(a); Lock only at initializationif(pInst == NULL)
            pInst = new T;
        unlock(a); }return pInst;
}
Copy the code

What does the new operation do

  • (1) Allocate memory for objects
  • (2) Call constructor to initialize object
  • (3) Return the memory address

Step (2) and step (3) are executed in reverse order after compiler optimization, so DCL singletons are not thread-safe.

  • [1] DDJ_Jul_Aug_2004_revised