---
# System prepended metadata

title: 'CH 15 : 系統互動'

---

Chapter 15：系統互動
===

:::info
這是讀書筆記

![679544](https://hackmd.io/_uploads/HJB-ZFKV-l.jpg =30%x)

作者：鄭鋼
出版社：佳魁資訊股份有限公司
出版日期：2017/05/31
:::
>[time=2025-08-11]
---

# fork

* fork 的作用就是複製處理程序。
* 複製以下處理程序資源
    * PCB
    * 程式碼
    * 堆疊，包含使用者堆疊和核心堆疊。
    * 分頁，獨立的記憶體空間。

## Source Code

http://xn--github-v94euhq75w.com/yifengyou/os-elephant/tree/master/code/c15/a

## Result

![截圖 2025-08-06 凌晨3.00.16](https://hackmd.io/_uploads/r1k1HCk_gg.png)

# system call: read/putchar/clear

## Source Code

https://github.com/yifengyou/os-elephant/tree/master/code/c15/b

## Result

![截圖 2025-08-06 下午4.36.08](https://hackmd.io/_uploads/BJ4mN5l_ge.png)

# 實現一個簡單的 shell

* Ctrl+u: 清除輸入
* Ctrl+l: 清除螢幕

## Source Code

https://github.com/yifengyou/os-elephant/tree/master/code/c15/f

## Result

![截圖 2025-08-07 上午10.43.25](https://hackmd.io/_uploads/Sy-g7q-ugl.png)

![截圖 2025-08-07 下午3.54.44](https://hackmd.io/_uploads/SyFkn0W_xe.png)

# 載入使用者處理程序

## Source Code

https://github.com/yifengyou/os-elephant/tree/master/code/c15/g

https://github.com/yifengyou/os-elephant/tree/master/code/c15/h

```shell=
CFLAGS="-c -m32 -fno-stack-protector -fno-builtin"

OBJS="../build/string.o \
    ../build/syscall.o \
    ../build/stdio.o \
    ../build/assert.o \
    ../build/start.o"

x86_64-linux-gnu-gcc prog_a.c $CFLAGS -I ../include -o prog_a.o
x86_64-linux-gnu-gcc prog_b.c $CFLAGS -I ../include -o prog_b.o
x86_64-linux-gnu-ld -m elf_i386 -e main -z noexecstack prog_a.o $OBJS -o ../out/prog_a
x86_64-linux-gnu-ld -m elf_i386 -z noexecstack prog_b.o $OBJS -o ../out/prog_b

```

```
dd if=../code/out/mbr.bin of=./sr_hd60m.img bs=512 count=1 conv=notrunc
dd if=../code/out/loader.bin of=./sr_hd60m.img bs=512 count=4 seek=2 conv=notrunc
dd if=../code/out/kernel.bin of=./sr_hd60m.img bs=512 count=200 seek=9 conv=notrunc

SEC_CNT=$(ls -l ../code/out/prog_a|awk '{printf("%d", ($5+511)/512)}')
dd if=../code/out/prog_b of=./sr_hd60m.img bs=512 count=$SEC_CNT seek=300 conv=notrunc

SEC_CNT=$(ls -l ../code/out/prog_b|awk '{printf("%d", ($5+511)/512)}')
dd if=../code/out/prog_b of=./sr_hd60m.img bs=512 count=$SEC_CNT seek=400 conv=notrunc

```

## Result

![截圖 2025-08-08 晚上10.57.51](https://hackmd.io/_uploads/HkGqx5muxg.png)

![截圖 2025-08-09 凌晨12.00.45](https://hackmd.io/_uploads/SJlUyimulx.png)


# 實現系統呼叫 wait and exit

## Source Code

https://github.com/yifengyou/os-elephant/tree/master/code/c15/i

## Result

![截圖 2025-08-11 凌晨12.33.10](https://hackmd.io/_uploads/H1Tk9BU_ll.png)

# 管線

## Source Code

https://github.com/yifengyou/os-elephant/tree/master/code/c15/j

https://github.com/yifengyou/os-elephant/tree/master/code/c15/k

## Result

![截圖 2025-08-11 凌晨1.28.42](https://hackmd.io/_uploads/B16JvULOgg.png)

![截圖 2025-08-11 凌晨1.54.36](https://hackmd.io/_uploads/HyuWTUI_ex.png)

