Skip to content

Instantly share code, notes, and snippets.

@nobuh
Last active June 23, 2024 04:58
Show Gist options
  • Save nobuh/10e8f71f883d5cca42d832d9f0087056 to your computer and use it in GitHub Desktop.
Save nobuh/10e8f71f883d5cca42d832d9f0087056 to your computer and use it in GitHub Desktop.
Unix と C のメモ
Hello World
コンパイラ
+---------+ +------------+
| main.c | ------> | a.out |
+---------+ +------------+
ライブラリ関数、シンボル、リンカ
+--------------------------------------------+
| |
| +----------+ |
| | <stdio> | 0x.... +-------------+ |
| | | <-------- |symbol table | |
| +----------+ | | |
| | main | --------> | | |
| | | printf +-------------+ |
| +----------+ |
| |
+--------------------------------------------+
a.out
return 200 # 0xc8 を返すだけの main
gcc -v <source>.c # なんか一杯リンクしている
ldd <exe> # 案の定 libc
dpkg -S <file> # libc6 パッケージ
apt info <package> # その説明
readelf -W -a <exe> # エントリーポイント、text セグメント、_start と main
objdump -M intel -D a.out # 実際のコード
Unix でプログラム内のメモリ以外一切触らないプログラムのメモリマップ
+----------+
Context Switch = Memory Switch |CPU assist|
<-----------------------> +----------+
+-----------+------------+ +---------------------+
| stack | (func call)| ^ | Kernel |
| | | | | Context |
| v | | | |
| | | +---------------------+
| ^ | | system call
| | | | read, write
| heap | (malloc) | | fork, exec
+-----------+------------+ |
| block storage space | | read write
+------------------------+ |
| init rw | ^ v
+------------------------+ |
| init ro | | ^
+------------------------+ | load | read only
| text | | program |
| | | | +-----------+
| | v v |CPU assist |
+------------------------+ +-----------+
Unix User Memory
+---------------+
|Kernel |
| sys call |
| ^ |
| | |
+--------+------+ +---------------+
| User | | |Service Process|
| | +---------------+
| Stack | ^
| | |
| | | Message
| Heap | |
| | v
| | +-------------+
| Text | |User Process |
+---------------+ +-------------+
user process
Monolithic Kernel <--------> Micro Kernel
直感イメージの OS = 実はマイクロカーネル型でセキュアと言われる理由
実際の OS = ユーザープロセスがカーネルになって HW アクセス
unistd sleep(3600)
/proc/<pid>/
pidstat -p <pid> # -w, -R, -r, -v, ...
init 1
musllibc と alpine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment