###### tags: `NTU`, `SP`, `TA`
# Programming Assignment 1 Report Criteria
1. What is busy waiting? How do you avoid busy waiting in this assignment? Is it possible to have busy waiting even with select() or poll() ? (0.3 pts)
**Sample answer:**
1. Busy waiting refers to a process doing nothing but continuously waiting for a specific condition/resource/event(e.g, I/O event, lock) to be ready. (0.1 pts)
2. select()/poll() (0.05 pts)
3. Yes. If we set timeout argument in select()/poll() as NULL/negative number, then the behavior is still busy waiting. (0.15 pts)
**Criteria:**
1. 定義寫對就給滿分
2. 有寫到 select()/poll() 就給滿分
3. 寫出 yes 給 0.05 分,解釋佔 0.1 分,可接受的解釋:
* timeout 設 NULL,設 0 或設很小
* 舉出可能需要 busy waiting 的 case, e.g, 在 select ready 後,busy wait for file lock
4. 可能被扣分的原因
* busy waiting 的定義有問題 -> -0.1 pts
* 沒有寫怎麼 avoid 或 avoid 的方法有問題 => -0.05 pts
* 3. 的解釋可能導致 blocking 而不是 busy waiting => -0.05 pts
* 3. 的解釋寫錯 => -0.1 pts
* 3. 的解釋太空泛 => -0.1 pts
* 只寫 buggy program, didn't write code properly, didn't set up arguments properly, etc.
> Note:
> Busy waiting 指的是一個 process **在它執行的時間中**,有很大部份的時間都用在檢查某個 condition/resource/event 是否 ready,不一定是一直佔著 CPU 不放,兩者的差別可以想成前者的分母是該 process 的 CPU time,後者的分母是從該 process 開始到結束為止的總時間長度。到了下學期的 OS 課你們應該會學到 preemption 這個概念,就是 kernel 會強制 context switch,而非等到 process 自願讓出 CPU 才做 context switch。在有 preemption 的情況下,後者的定義或許就不夠準確了。
> 因為程式作業一的 server 除了處理 client request 外沒有其他事情要處理,所以即使用了 select()/poll() + timeout 還是有點像 busy waiting(可以參考 [difference between polling and busy waiting](https://stackoverflow.com/questions/10594426/what-is-the-difference-between-busy-wait-and-polling#:~:text=Polling%20is%20sometimes%20used%20synonymously,point%20the%20device%20is%20accessed.)),spec 上要求同學不要 busy waiting 是提醒同學不要用 while loop + non-blocking I/O 這種常見的 busy waiting pattern),因此第三小題不管寫 timeout 設 NULL/negative number、設 0 或是設很小都可以拿到分數。
> 然而,我認為最標準的答案是 timeout 設 NULL/negative number,因為不管 timeout 設 0 或是設很小,我們都可以在兩次 loop 之間做其他 task (if any),而根據 [select](https://man7.org/linux/man-pages/man2/select.2.html) 和 [poll](https://man7.org/linux/man-pages/man2/poll.2.html) 的 manual,timeout 設 NULL/negative number 的話 select()/poll() 會一直執行到有 file descriptor ready 為止才 return,且 select 和 poll 的實作(以 linux 為例)實際上是用一個無窮迴圈去對 poll 每個在監測的 file descriptor,因此如果 timeout 設 NULL/negative number,又一直沒有 file descriptor ready,你的程式就會一直在 kernel mode 中做 busy waiting。
> 考慮到絕大多數同學的背景知識還不夠(i.e, 修過 OS 課),應該也沒辦法和沒時間去看 kernel code,且 report 也沒有打算給同學太大的 loading,所以即使在第二小題中提到把 timeout 設 NULL 或是在第三小題中的 timeout 不是寫 NULL/negative number 也都一併給分。
2. What is starvation? Is it possible for a request to encounter starvation in this assignment? Please explain. (0.3 pts)
**Sample answer:**
1. Starvation refers to a process who wants to write to a file cannot acquire lock due to the file has been continuously read by other process.
Starvation refers to a process cannot access specific resources occupied by other processes. (0.15 pts)
- 寫一般的 starvation 或是針對這次作業的 write starvation 都可以
- 寫 process/thread 都可以
2. Yes. For instance, if a user A wants to edit states of id 902001, and meanwhile on another read server, there are always some users checking states of id 902001, then A will not be able to edit states of 902001. (0.15 pts)
- 舉例有瑕疵(e.g, 只寫 write 的時候剛好有人在 read) => 0.1 pts
3. How do you handle a file’s consistency when multiple requests within a process access to it simultaneously? (0.2 pts)
**Sample answer:**
用 array 紀錄該 process 內對每個 id 的 access 狀況,write server 只需紀錄是否該 id 正在被 access,而 read server 則需紀錄當前有幾個 connection 正在 access 該 id。
- 如果有其他看起來 feasible 的答案也可以自己判斷是否給分
- 答案內有瑕疵(e.g, read server 只記錄某個 id 是否正在被 access)=> 0.1 pts
- 答案寫的東西明顯不會 work => 0 pts
4. How do you handle a file’s consistency when different process access to it simultaneously? (0.2 pts)
**Sample answer:**
用 fcntl() 對每個 connection 所 access 到的 bookingRecord 的區域上鎖,如果上鎖失敗代表有其他 process 正在存取。
- 如果有其他看起來 feasible 的答案也可以自己判斷是否給分
- 答案內有瑕疵 => 0.1 pts
- 答案寫的東西明顯不會 work => 0 pts