---
# System prepended metadata

title: Chapter 10：Shell

---

Chapter 10：Shell
===

:::info 

終於來到最後一章了，最後我們要完成一個`shell`供使用者輸入的介面。
最重要手要完成的就是 print log，開發過程中這是最重要的一環，接著我們要 support pid ，讓每個 process 擁有唯一的 number，然後是 process management: `fork()`、`exec()`、`wait()`、`exit()`、`pipe()`，然後再來是要讓使用者輸入的 keyboard 實作，最後才是完整的`shell`。
&emsp; 1. Print Log
&emsp; 2. pid
&emsp; 3. fork
&emsp; 4. exec
&emsp; 5. wait and exit
&emsp; 6. pipe
&emsp; 7. Keyboard
&emsp; 8. My Shell

:::
>[time=Thu, Dec 18, 2025 2:32 AM]
---
https://youtu.be/BHXdv1iA31s
<iframe width="560" height="315" src="https://www.youtube.com/embed/BHXdv1iA31s?si=Zk_sN6Q2koyazf53" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

https://youtu.be/k3__qZBkzl0
<iframe width="560" height="315" src="https://www.youtube.com/embed/k3__qZBkzl0?si=Oj46a2YNo4xIoqiz" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>


![截圖 2026-01-14 下午6.17.05](https://hackmd.io/_uploads/BkbBT1BB-e.png)


# Print Log

>[!Note] System Call
>![截圖 2025-12-19 凌晨12.31.33](https://hackmd.io/_uploads/HyQKhiWm-l.png)


## Source Code
https://github.com/srhuang/a-os/commit/f643094b1c03ef5d09e1feb38cba3f65ac3f39a2

## Compile
```
make all
```

## Put on hard disk
```
sh gen.sh
```

## Checkpoint
![截圖 2025-12-19 凌晨12.32.42](https://hackmd.io/_uploads/H1w6nj-XWe.png)
















# pid

![截圖 2025-12-27 凌晨3.37.06](https://hackmd.io/_uploads/r1E-ND3Q-l.png)


## Source Code
https://github.com/srhuang/a-os/commit/21fe8b02f7561383b1ff5bc84de4f8c129cee4ec

## Compile
```
make all
```

## Put on hard disk
```
sh gen.sh
```

## Checkpoint
![截圖 2025-12-27 凌晨3.38.22](https://hackmd.io/_uploads/SkJ8EP27We.png)














# fork
![截圖 2026-01-14 晚上7.40.44](https://hackmd.io/_uploads/ryhCe-BBWx.png)

>[!Note] User Process Resource
>![截圖 2026-01-14 晚上7.42.20](https://hackmd.io/_uploads/B1xRS-WBrWg.png)

>[!Note] fork kstack
>![截圖 2026-01-14 晚上7.45.57](https://hackmd.io/_uploads/rJpmMbSBZl.png)

## Source Code
https://github.com/srhuang/a-os/commit/2e47b238bd0d3b260e46dc74602fe72ea4d1e9ce

## Compile
```
make all
```

## Put on hard disk
```
sh gen.sh
```

## Checkpoint
![截圖 2026-01-14 晚上7.37.09](https://hackmd.io/_uploads/ByWMeWrHbe.png)




















# exec

>[!Note] Hard Disk Layout
>![截圖 2026-01-15 凌晨12.08.54](https://hackmd.io/_uploads/S1B-xBHBbl.png)

>[!Note] C Runtime Library (CRT)
>![截圖 2026-01-15 凌晨12.11.07](https://hackmd.io/_uploads/BkBBxSHSbe.png)


## Source Code
https://github.com/srhuang/a-os/commit/053aa01835c86eab0b8f6d750f370edf6e6b6abb

## Compile
```
make all
```

## Put on hard disk
```sh=
dd if=../code/out/mbr.bin of=./60mb.img bs=512 count=1 conv=notrunc
dd if=../code/out/loader.bin of=./60mb.img bs=512 count=4 seek=1 conv=notrunc
dd if=../code/out/kernel.bin of=./60mb.img bs=512 count=200 seek=5 conv=notrunc
dd if=../code/out/usr.bin of=./60mb.img bs=512 count=100 seek=205 conv=notrunc
dd if=../code/out/prog of=./60mb.img bs=512 count=100 seek=305 conv=notrunc
```

```
sh gen.sh
```

## Checkpoint
![截圖 2025-12-28 下午6.05.12](https://hackmd.io/_uploads/ByyG-K07-x.png)




















# wait and exit

![截圖 2026-01-15 凌晨3.04.49](https://hackmd.io/_uploads/S1xZYDrSZe.png)

## Source Code
https://github.com/srhuang/a-os/commit/770eb9e4b597a03db3fed5f457a8ac763fa67956

## Compile
```
make all
```

## Put on hard disk
```
sh gen.sh
```

## Checkpoint
![截圖 2026-01-01 晚上10.19.09](https://hackmd.io/_uploads/Hkr9z-44Zg.png)




















# pipe

![截圖 2026-01-15 清晨7.08.41](https://hackmd.io/_uploads/HJmXMoBHZl.png)

>[!Note] IO Queue
>![截圖 2026-01-15 下午5.41.50](https://hackmd.io/_uploads/B19tUNIB-x.png)


## Source Code
https://github.com/srhuang/a-os/commit/9bb5d704f6f54169c4da0f14899513802e75a131

## Compile
```
make all
```

## Put on hard disk
```
sh gen.sh
```

## Checkpoint
![截圖 2026-01-03 下午4.12.48](https://hackmd.io/_uploads/r1UiyI8V-g.png)

test ioqueue
![截圖 2026-01-15 下午5.29.18](https://hackmd.io/_uploads/rJviXNIrZl.png)
![截圖 2026-01-15 下午5.30.30](https://hackmd.io/_uploads/HJzRQVLBWe.png)














# Keyboard

> Reference:
> [Bochs' map of I/O ports to functions](https://bochs.sourceforge.io/techspec/PORTS.LST)
> ![截圖 2026-01-04 晚上7.08.51](https://hackmd.io/_uploads/HkMvqpPV-l.png)

>[!Note] Scan Code
>https://wiki.osdev.org/PS/2_Keyboard#Scan_Code_Set_1
>![截圖 2026-01-04 晚上7.44.42](https://hackmd.io/_uploads/BkK6GRPEWl.png)


## Source Code
https://github.com/srhuang/a-os/commit/722021b66b2dde6e178327ff2d9f634a6f346803

## Compile
```
make all
```

## Put on hard disk
```
sh gen.sh
```

## Checkpoint
![截圖 2026-01-05 下午6.31.24](https://hackmd.io/_uploads/HkXmmfYNbx.png)












# My Shell

![截圖 2026-01-15 晚上7.41.00](https://hackmd.io/_uploads/ByvvfU8Sbx.png)

>[!Note] Absolute Path
>![截圖 2026-01-15 晚上8.10.57](https://hackmd.io/_uploads/rka_F8IBWg.png)


>[!Note] Command Length
>![截圖 2026-01-15 晚上7.42.26](https://hackmd.io/_uploads/SJvRML8HZl.png)

 
## Source Code
https://github.com/srhuang/a-os/commit/9fec70088b00fffb1ea9f7b3686375f529007248

>[!Warning] Bug Fix
>https://github.com/srhuang/a-os/commit/e631516f2db8ef0db418886650c762d117bbddbb

## Compile
```
make all
```

## Put on hard disk
```
sh gen.sh
```

## Checkpoint
![截圖 2026-01-15 晚上7.33.18](https://hackmd.io/_uploads/HkzilIUSbe.png)
![截圖 2026-01-15 晚上7.34.27](https://hackmd.io/_uploads/rkb1b8UHWg.png)
![截圖 2026-01-16 凌晨3.25.13](https://hackmd.io/_uploads/rk9EJTLHZx.png)
