Skip to content

Latest commit

 

History

History
315 lines (184 loc) · 6.54 KB

README.md

File metadata and controls

315 lines (184 loc) · 6.54 KB

[TOC]

os_notebook

Introduction

Supported

  • os

    Ubuntu 16.04 LTS

  • compiler

    gcc version 9.4.0

  • This is os lab notebook

Start

git clone [email protected]:DC-Jade/c_notebook.git

## PA1

  • Turing machine, TRM
    • while (1) { get instruction; run instruction; update instruction; }
  • 状态机模型
  • ISA
    • 指令集架构, 硬件无关的机器指令集的集合
  • 地址映射
    • pmem

Program 程序

### Intrinsic 本质

1. state machine

  • $tuple(pc, r_1, ..., r_n, mermory)$

  • program counter, pc

  • $r_i(i = 1, ..., n), register$ x86

    ​ rip -> pc ; r[abcd]x ; rbp -> begin ptr of stack ; rsp -> stop ptr of stack, controlling stack

2. computing + syscall

  • computing program

    up to 99%

  • syscall

    store process status and managing process

  • function

    • intrinsic

      stack and frame

    • function call

      push frame

    • function return

      pop frame

cd src/
mgcc hanoi.c  # -g is required for gdb
gdb ../bin/hanoi
	layout src
	wa $rax  # watch point, trace register rax

# min_hello.S
gcc -c min_hello.S && ld min_hello.o -o ../bin/min_hello

Compile process 编译过程

.c(src) -> (preprocessing, gcc -I) -> .I -> (compiling, gcc -E) -> .S(assembly code)

-> (assembling, gcc -c) -> linking(ld || gcc -o) -> .o(ELF, object code)

1. preprocessing

  • Expand #include macro(marked with #)

  • rm comments

  • save #pragma compiling instructions, required for compiling(step2)

2. compiling

transfer c/c++ code to assembly code, producing assmebly file(.S)

3. assembling

transfer assembly code to machine instruction(binary code), producing intermediate object file(.o)

4. linking

linking object file, incluing shared object file(.so), producing executable file(a.out)

dereference of symbols in different modules, adjust symbols address in different modules(object file)

linking_process

operating system, OS 操作系统

  • 硬件:C程序
  • 应用程序: 系统调用API的集合

Intrinsic

  1. machine

​ C program

  1. application program

​ syscall APIs

Virtualisation 虚拟化

Abstraction

Abstraction makes it possible to write a large program by dividing it into small and under- standable pieces

  • CPU

  • Virtual Memory

    • abstraction of main memory
  • File

    • abstraction of I/O devices
  • Protection

  • Isolation

Concurrency 并发

  • Definition

    running instructs of process/thread by turns

    Control flow of a process overlaped with others in time

  • Multiple threads

  • Atomic

  • Plot status machine

  • 互斥

cd ../include
wget http://jyywiki.cn/pages/OS/2022/demos/thread.h
cd ../src
mgcc multi_thread.c -l pthread && ../bin/multi_thread

Persistence 持久性

Scripts(.sh)

mgcc () {
  arg1=$1;  # .c file
  arg2=${@:2:10}  # argments
  # -g - debug information
  gcc $arg1 -Wall -g $arg2 -o ../bin/${arg1%\.c}
}

Basics

Hardware Composite Model

composite_model

cache_model

  • Main memory

    主存是一个临时存储设备,在处理器执行程序时,用来存放程序和程序处理的数据,主存是由一组动态随机存取存储器(DRAM)芯片组成的; 从逻辑上来说,存储 器是一个线性的字节数组,每个字节都有其唯一的地址(数组索引)

  • CPU

    中央处理单元(CPU),简称处理器,解释(或执行)存储在主存中二进制指令。主要由ALU (算术逻辑单元)和 PC(程序计数器)

    ALU处理指令,进行状态计算,如;PC是一个大小为一个字的存储设备(或寄存器),指向主存中的某条机器语言指令(即含有该条指令的地址;

  • Memory Hierarchy

    memory_hierarchy_model

Process

process_virtual_memory_model

Bit

  • Bit,二进制数字
  • Byte,字节;8位bit,最小寻址的内存单位
  • Word,字; 数据传输单元,和寄存器长度一致

Exception

  1. interruption
  2. trap
  3. falut
  4. Halt

Parallel

control flow runs in different cpus by concurrency

Context

all status of a process

Reentrant function(可重入函数)

ont type of thread_safe function

  • don’t use gloabl or static variables in peer threads

Symbol

global and static variables and function

symbol name - name of globale and static variables and function

module

one c/cpp file, always same as an object file

ELF(executable linkable format)

  • used by obejct file and executable file , relocatable or executable

  • three types

    1. relocatable file(.o)
    2. execuable file(a.out)
    3. shared object file(.so)
  • .bss and .data

    .bss - uninitialized global and static variables, (static int i = 0, seen as unitialized, located at the .bss)

    .bss allocated by the linker, due to weak symbols size is undetermined in compiling process

    .data - initialized global and static variables

  • cmd

    objdmp -h -s -x .o
    readelf -h -S -s .o
    nm .o
    strip .o # remove symbol info(including debug info)
    size .o

elf_format

name mangling/decoration

a symbol decoration mechanism in C++, used to void symbol name confict

Stack model

stack_model

Trick

swap

int a = 100, b = 1;
void swap(int *a, int *b);
swap(&a, &b);
void swap(int *a, int *b) {
  *a ^= *b ^= *a ^= *b;
}