##### tags `sp`
# <font color = blue>SP_CH14</font> **Advanced I/O**
---
## **Blocking / UnBlocking:**
---
> - **Blocking :**
>> - Process in suspended until <font color = red>all bytes</font> in the count field are read or written.
> - **UnBlocking :**
>> - The OS only reads or writes <font color = red>as many bytes as possible</font> without suspending the process.
>> - **"Slow System Calls"** are those that can block forever ex. read or write on pipes, terminal devices, and network devices.
---
## **Terminal Devices:**
---

> - Each terminal devices has it's <font color = red>input and ouput queue</font>
> - The Shell redirects standard input to the terminal (in canonical mode)
> - When the ouput queue is starting to fill up:
>> - Blocking: put process to sleep until room is available.
>> - UnBlocking: polling (busy waiting on CPU)
---
## **I/O Multiplexing:**
---
> - **Select:**
>> ```
>> totalFds = select(nfds, readfds, writefds, errorfds, timeout)
>> ```
>> - **nfds:** the range (0, nfds-1) of file descriptors to check
>>> - getdtablesize(): get file descriptor table size
>> - **readfds:** bit map of file desc. Set the bit X to check if file desc. X is ready for reading.
>> - **writefds:** bit map if file desc. Y is ready for writing
>> - **errorfds:** bit map to check for error
>> - **timeout:** how long to wait before un-suspending the process (microseconds)
>> - **totalFds:** number of ready descriptors, 0 on timeout, negative for error
> - **Poll:**
>> ```
>> totalFds = poll(fdarray[], nfds, timeout)
>> ```
>> - struct pollfd {
>> int fd; // file descriptor to check, or <0 to ignore
short events; // bit mask: each bit indicates an event of interest on fd
short revents; // bit mask: each bit indicates an event that occurred on fd
}
---
## **Select v.s. Poll:**
---
> - differenc:
> - |select()|poll()
> ---|----|-----
> bit mask:| rewrite| resume
> efficiency:|inefficient if one of fd is high|combines multiple bitmasks into one
> event types:|3|more
>