[TOC]
-
This is a operating system learning notebook
-
Reference:
-
os
Ubuntu 16.04 LTS
-
compiler
gcc version 9.4.0
-
This is os lab notebook
git clone [email protected]:DC-Jade/c_notebook.git
## PA1
-
Turing machine, TRM
- while (1) { get instruction; run instruction; update instruction; }
- 状态机模型
$多元组(PC, R_1, ..., R_n, mermory)$ - Program counter, PC
$R_i(i = 1, ..., n), register$
-
ISA
- 指令集架构, 硬件无关的机器指令集的集合
- 地址映射
- pmem
### Intrinsic 本质
-
$tuple(pc, r_1, ..., r_n, mermory)$ -
$r_i(i = 1, ..., n), register$ x86 rip -> pc ; r[abcd]x ; rbp -> begin ptr of stack ; rsp -> stop ptr of stack, controlling stack
-
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
.c(src) -> (preprocessing, gcc -I) -> .I -> (compiling, gcc -E) -> .S(assembly code)
-> (assembling, gcc -c) -> linking(ld || gcc -o) -> .o(ELF, object code)
-
Expand #include macro(marked with #)
-
rm comments
-
save #pragma compiling instructions, required for compiling(step2)
transfer c/c++ code to assembly code, producing assmebly file(.S)
transfer assembly code to machine instruction(binary code), producing intermediate object file(.o)
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)
硬件:C程序应用程序: 系统调用API的集合
- machine
C program
- application program
syscall APIs
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
-
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
mgcc () {
arg1=$1; # .c file
arg2=${@:2:10} # argments
# -g - debug information
gcc $arg1 -Wall -g $arg2 -o ../bin/${arg1%\.c}
}
-
Main memory
主存是一个临时存储设备,在处理器执行程序时,用来存放程序和程序处理的数据,主存是由一组动态随机存取存储器(DRAM)芯片组成的; 从逻辑上来说,存储 器是一个线性的字节数组,每个字节都有其唯一的地址(数组索引)
-
CPU
中央处理单元(CPU),简称处理器,解释(或执行)存储在主存中二进制指令。主要由ALU (算术逻辑单元)和 PC(程序计数器)
ALU处理指令,进行状态计算,如;PC是一个大小为一个字的存储设备(或寄存器),指向主存中的某条机器语言指令(即含有该条指令的地址;
-
Memory Hierarchy
- Bit,二进制数字
- Byte,字节;8位bit,最小寻址的内存单位
- Word,字; 数据传输单元,和寄存器长度一致
- interruption
- trap
- falut
- Halt
control flow runs in different cpus by concurrency
all status of a process
ont type of thread_safe function
- don’t use gloabl or static variables in peer threads
global and static variables and function
symbol name - name of globale and static variables and function
one c/cpp file, always same as an object file
-
used by obejct file and executable file , relocatable or executable
-
three types
- relocatable file(.o)
- execuable file(a.out)
- 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
a symbol decoration mechanism in C++, used to void symbol name confict
int a = 100, b = 1;
void swap(int *a, int *b);
swap(&a, &b);
void swap(int *a, int *b) {
*a ^= *b ^= *a ^= *b;
}