sysprog2019
dev_record
contributed by < HTYISABUG
>
為了滿足 q_insert_tail
& q_size
時間複雜度 的要求
在 structure 內補上 tail & size 以取得 queue 的尾端與長度
依據註解補上 allocation error 的判斷式
以及 member 的初始化
先確認 queue 不為 NULL
再以迴圈將各個 element 釋放
最後才釋放 queue
依序判斷 queue 不為 NULL
、malloc 成功與否
如果字串複製失敗,就將 element 釋放並回傳錯誤
最後更新 queue 的內容
第四行的寫法在 commit 時會報錯 memory leak
檢查後發現是括號導致的執行順序錯誤
OR operator 兩側都以括號包裝即可解決
以 tail 直接取得尾端
其餘操作與 q_insert_tail
相同
第四行的問題同 q_insert_head
以括號包裝可以解決
補上 queue 不為 NULL
或空的判斷
更新 queue 的 member data
新增字串複製以及記憶體釋放的部份
以 queue 的 member q_size
直接取得長度
以達成時間複雜度 的要求
同樣補上 queue 不為 NULL
或空的判斷
用兩個新指標來儲存新的 head & tail
再循序將 elements 重新指向
實作方法示意圖如下
使用 cscope
幫助追蹤程式流程
queue_init
init_cmd
console_init
run_console
皆為 trigger_exception
的 wrapper
trigger_exception
使用 siglongjmp
返回至 main loop
返回點則是在設定 shell 指令時使用 sigsetjmp
紀錄當下的 stack pointer 、 instruction pointer 等資訊
之所以不使用 setjmp
、 longjmp
是因為這裡牽涉到 signal 遮罩
如果使用以上兩個 function 會導致 signal handler 失效
從這幾個 function 的行為來看
我猜想它們在 kernel 內的定位是用在 context switch 的部份
設定中以 singly linked list 儲存新增的項目
為一方便增加程式結束時的處理的方法
用這方法能避免增加處理時需要修改多個檔案的問題
相比一般等待使用者輸入會用 scanf
等來自 standard I/O 的 function
qtest
中使用了 select
作為替代方案
select
的用途是監視 file descriptor 是否可用
包含 input, output, except 皆可同時監視
雖然在 qtest
中只用於監視單一 input 來源
但這種監視多個 file descriptor 的功能
我猜想或許在任務排程時會有作用
在了解程式運作流程的時候
因為打錯檔名而意外發現檔案不存在時會產生 memory leak
Trace 程式碼發現是這種情況下會因為 run_console
回傳 false
而使 ok
變為 false
進而導致下一行的 finish_cmd
不被執行
修改 finish_cmd
與 ok
的順序來修復這個問題
嘗試提交 pull request
jserv
以下節錄自 ISOIEC 9899:201x
6.7.6.2 節 陣列宣告
If the expression is a constant expression,
it shall have a value greater than zero.
而 harness.c
中這個 struct 違反這段的規定
不知道是不是我搞錯什麼了
追加:
看來這個寫法來自 GCC extension
作為動態指定 struct member 大小的方法使用
這樣同時還能確保記憶體的連續
對應 C99 的 flexible array member
操作方法基本相同,需要預留足夠的記憶體
但如果沒有預留任何記憶體給 flexible array member
這樣會導致 undefined behavior
If this array would have no elements,
it behaves as if it had one element but the behavior is undefined
if any attempt is made to access that element
or to generate a pointer one past it.