- simple shell
- demo pic
- 以C實作的簡易shell,支援一般指令、背景執行(&)、multiple pipeline。
- 以double fork防止zombie process(主要用於背景執行)。
- 以recursive實現multiple pipeline的fd處理。
- 不支援
cd
、redirect dataflow等操作。
- chatroom
- demo pic
- 用pthread跟socket實現的多人線上聊天室,但礙於那時對socket了解有限,部分架構不夠優美。
- 後續分別在BBS、BBS with single thread上用pthread、select實現了不同用戶對話的server-client model。
- matrix production
- demo pic
- 分別用multithread跟multiprocess實現平行化的矩陣乘法,目的在於直觀感受平行化的加速。
- 由於使用機器cpu有限,超過8個thread/process後沒有顯著改善。
- thread的表現略優於process,猜測是process switch成本更高的原因。
- BBS
感謝Jason提醒多線程中,
strtok
有共用記憶體的問題,改用strtok_r
實現- demo pic
- 以OOP、multithread、mutex的概念實作BBS。
- 為了在多人交互時,不同時修改同一個記憶體導致資料損毀,使用排他鎖及讀寫鎖維護。
- server端操作:
- 編譯指令:
g++ *.cpp
./a.out <IP> <port> <max user>
- IP僅能使用本機上網卡的IP,或以0.0.0.0代表本地IP
- Port須大於1024(1024前為通用port)
- max user不為負數
- 編譯指令:
- client端操作:
nc <IP> <port>
:連線serverregister <username> <password>
:註冊用戶login <username> <password>
:登入用戶whoami
:查詢當前帳號名稱list-user
:列出所有用戶send <username> <message>
:給指定用戶訊息list-msg
:分別列出從各用戶收到的信息數量receive <username>
:列出從用戶收到的所有訊息logout
:登出exit
:中斷連線
- BBS with single thread
- demo pic
- 與BBS功能類似,但實做方式不同,使用select在單一thread上handle多個socket(file descipter)的交互,使程式更加scalable,但與此同時,使用論詢的select在效率上較差(可以用epoll改善)。
- Makefile
make
:編譯server、client執行檔make clean
:刪除server、client執行檔
- server端操作:
./server <port>
- client端操作:
./client <IP> <port>
:連線到servername <username>
:重新命名user,默認為匿名使用者(anonymous)who
:查看在線使用者,以及各使用者的IP、port跟名稱tell <username> <message>
:私訊另一名使用者,匿名使用者無權限發送、接收私訊yell <message>
:廣播訊息,匿名使用者無權限發送廣播exit
:離開server
1am9trash / os_and_network_programming Goto Github PK
View Code? Open in Web Editor NEW紀錄學習OS跟網路程式時的筆記跟demo
License: MIT License