系列文章 : [6.1810] 跟著 MIT 6.1810 學習基礎作業系統觀念
大綱
- find (moderate)
- exec (moderate)
find (moderate)
作業要求
- This exercise explores pathnames and directories.
- Learn how to use the system calls open, read, and fstat.
- Write a simple version of the UNIX find program for xv6: find all the files in a directory tree with a specific name. Your solution should be in the file user/find.c.
- 給予一個名稱,要搜尋該目錄,以及所有子目錄下,有著同樣名稱的檔案
作業提示
- 可以看
user/ls.c 來學習怎麼讀取一個 directory
- Don't recurse into "." and "..".
作業內容
- user/find.c
- BFS ? DFS ?
- 用 DFS,這樣子就不用自己實作 Queue 了。DFS 可以直接利用 call-stack,對我來說更容易實作。有 C++ 的話,就能用 std::queue 了 !
- 大部分的程式碼其實都是從 user/ls.c 偷來的。
exec (moderate)
作業要求
- 這個作業可以練習 system calls :
fork, exec, and wait
- Add a "-exec cmd" to find, which executes the program "cmd file" for each file f that
find finds, instead of printing matching file names.
作業內容
- 複習一下
fork
- return value
- parent 會收到 child process 的 PID
- child 會收到 0
- 複習一下
exec
-
(arg) char *path
-
(arg) char **argv
- 給予新的 program 的 command-line arguments。
- return value
- value == -1 : 該 function 失敗
- value == argc : 該 function 執行成功,會 return argc ( 給予新的 program 的參數的個數 )。
- 複習一下
wait
-
(arg) addr
- 第 0 個參數為 address,這個參數代表 child-process 的 exit status 要複製到哪個地方。
- 若該 address == 0,則不會複製 exit status 給 parent-process。
- user/find.c
- 使用
grade-lab-util 進行測試
== Test find, in current directory == find, in current directory: OK (1.4s)
== Test find, in sub-directory == find, in sub-directory: OK (0.9s)
== Test find, recursive == find, recursive: OK (1.3s)
== Test exec, recursive find == exec, recursive find: OK (1.1s)
== Test exec == exec: OK (1.7s)
== Test exec, multiple args == exec, multiple args: OK (1.0s)
== Test exec, recursive find == exec, recursive find: OK (1.2s)
Reference