or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
LINUX 核心設計 課程用書筆記
contributed by <
rwe0214
>tags:
Linux
,rwe0214
,CS:APP
,NCKU
CS:APP Ch10 System-level I/O
Unix
中的file
是一序列的bytes
所組成,所有的 I/O ( e.g. networks, disks, terminals, … ) 皆 model asfile
。因為這樣
Unix kernel
就可提供一個simple, low-level application interface (Unix I/O
),達到驅動所有 I/O 一個統一和方便的方法。10.4 Robust Reading and Writing with the Rio Package
因為
read
和write
的回傳值為已讀寫完的bytes
數量,但在連線不穩定(例如網路 socket connect 等等)的情況下,不一定只執行一次read
/write
就可達到預期讀寫完n bytes
的結果( i.e.short count > 0
),所以需要基於使用read
/write
來設計更穩定的 I/O function,可分成unbuffered
和buffered
兩種。Unbuffered I/O Function
利用回傳
short count
數量來判斷是否已經讀取完所需的 n bytes,若遇到EOF
則回傳0
,rio_readn
andrio_writen
如下:可將
替換成
即完成 buffered 版本,在下部分的 buffered I/O 會說明。
Buffered I/O Function
如果要計算
file
中有幾行資料(number of'\n'
),上述unbuffered
的作法利用多次read
一次讀取一個byte
並檢查是否為'\n'
,但此方法會需要多次的system call
所以極度沒有效率,使用
rio_readlineb
來減少使用read
的次數。透過 subroutine
rio_read
呼叫一次read
讀取長度為buflen
的資料後,再一個char
一個char
比對是否為\n
,呼叫system call
的數量便少了buflen
倍,同樣的,可以將
rio_readn
擴展成rio_readnb
,流程如下,為了達到以上的目標,定義了一個 rio_p 的結構體,
和一個初始化的
rio_readinitb
,再來就是介在中間的
rio_read
,有了以上,就能完成
rio_readlineb
。自我檢查清單
Ans: 因為
STDIO
在socket
有 documented restriction,所以在 network socket 上進行 I/O 用 RIO package 會更適合。fork
系統呼叫後,child process 和 parent process 的檔案分享狀況為何?(提示:閱讀 man-pages)Ans: child 和 parent 會共享同一個 file descriptor