contributed by <bauuuu1021
>
從 linux-list 學習裡頭的技巧,包含裡頭 unit test 的設計,透過給定的 linked list 操作,實作出 merge sort
qtest
得以支援平均效能分析 (用 random_shuffle_array()
所產生的數列)
特例效能分析
根據過去在演算法所學,這三種 sorting algorithm 之最佳及最差時間複雜度如下
排序名稱 | 最佳 | 平均 | 最差 |
---|---|---|---|
insert sort | |||
quick sort | |||
merge sort |
而當 input data 為 1,2,3,… (sequential data)時,恰為 insert sort 之 best case 及 quick sort 之 worst case
新增 macro sequential_array()
,並利用 #if
及 #elif
搭配 #define select
即可選擇使用哪種 input data:
實驗結果圖形如下(fig2)
結論:
以下引述 jserv 老師在社團的發文
在 Linux 核心中,建議採用 heap sort 或 merge sort,有個重要的考量是,許多要排序的原始序列,已是幾乎排序好的 (例如個別 process 佔用 CPU 的比例,原本已排序好,只是新增新的 process 後,又需要重排序),或是期望順序剛好相反,這對於 quick sort 都是很大的衝擊。
為何 Linux 採用 macro 來實作 linked list?一般的 function call 有何成本?
Making a function an inline function suggests that calls to the function be as
fast as possible. The extent to which such suggestions are effective is
implementation-defined. (6.7.4 Function specifiers)
在 header 中使用 inline
時,常搭配 static
出現,這樣的 static inline
函式搭配編譯器最佳化,則有 macro 的效果,並得以進行參數形態檢查
Linux 應用 linked list 在哪些場合?舉三個案例並附上對應程式碼,需要解說,以及揣摩對應的考量
GNU extension 的 typeof 有何作用?在程式碼中扮演什麼角色?
根據 C 語言規格書,使用 ==
或 !=
必須滿足下列四種條件之一
— both operands have arithmetic type
— both operands are pointers to qualified or unqualified versions of compatible types
— one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void
— one operand is a pointer and the other is a null pointer constant
(6.5.9 Equality operators)
其中對於 compatible type 定義如下
Two types have compatible type if their types are the same
(6.2.7 Compatible type and composite type)
因此當 x 的資料型態與 type 不相符時,例如:
執行結果
這是因為 _dummy
及 _dummy2
不是一樣的 data type,所以此指向這兩者的 pointer 不是 compatible type
typecheck_fn
中將 add() 的 return value (int) 指派給 (char)解釋以下巨集的原理
offsetof()
的定義如下
offsetof()
可以算出某 struct member 與 &struct 相對位址,例如:
除了你熟悉的 add 和 delete 操作,list.h
還定義一系列操作,為什麼呢?這些有什麼益處?
LIST_POISONING
這樣的設計有何意義?
node->prev
及 node->next
是非法的行為node->prev
及 node->next
指向不可存取的位址,之後在沒有賦予新值的情況下存取到 node->prev
或 node->next
就會被視為非法操作而發出 exceptionlinked list 採用環狀是基於哪些考量?
什麼情境會需要對 linked list 做排序呢?舉出真實世界的應用,最好在作業系統核心出現
什麼情境會需要需要找到 第 k 大/小元素 呢?又,你打算如何實作?
*
list_for_each_safe
和 list_for_each
的差異在哪?"safe" 在執行時期的影響為何?
for_each 風格的開發方式對程式開發者的影響為何?
foreach
這種可針對整個 array(list) 操作的語法程式註解裡頭大量存在 @
符號,這有何意義?你能否應用在後續的程式開發呢?
原本根據提示以為 linux 也是利用 Doxygen 來產生文件,但是安裝並執行後卻沒有找到預期的結果文件。後來參考 0xff07 同學的共筆,發現原本了理解有誤。bauuuu1021
The Linux kernel uses Sphinx to generate pretty documentation from reStructuredText files under Documentation. To build the documentation in HTML or PDF formats, use make htmldocs or make pdfdocs. The generated documentation is placed in Documentation/output.
tests/
目錄底下的 unit test 的作用為何?就軟體工程來說的精神為何?
In computer programming, unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use
tests/
目錄的 unit test 可如何持續精進和改善呢?
bauuuu1021
, sysprog
,2019spring