owned this note
owned this note
Published
Linked with GitHub
<style>
h2.part{color:#D92424;}
h3.part{color:#0099B0;}
h4.part{color:#005BB0;}
h5.part{color:#FD6F0A;}
h6.part{color:#4400B0;}
</style>
# Program, Process, Thread
contributed by <`f5120125`>
###### tags: `sysprog`
### **1. Memory Layout**
- **Text segment**
- 為object file中可執行指令的程式片段, 載入至memory中
- 通常為read-only
- **Initialized data segment**
- 一般稱為data segemnt
- 為programmer初始化的global, static變數
- read-write
- **Uninitialized data segment**
- 又稱為Block Started by Symbol (BSS) segment
- 存有global, static和在source code中沒有被明確初始化的變數
- **Stack**
- [stack frame](https://en.wikipedia.org/wiki/Call_stack)
- stack 在function結束後會消失
example swap要是不用pointer就會消失
- **Heap**
- 由[malloc](https://website-humblec.rhcloud.com/who-told-malloc-is-a-system-call/), realloc, free等C library 中的function管理, 他們可能利用brk(), sbrk()這些system call去調整data segment的大小
:::warning
:zap:**note**:
- malloc, realloc, free不為system call 且能夠allocate記憶體的system call還可能有mmap().
上述functions何時使用這些system call為C Library決定.
:::

### **2. Program**
- 儲存在secondary storage中的程式碼, 尚未讀取到memory中.
### **3. Process**
- 讀取到memory中的的program.
#### ►Scheduling
- Multitasking Operating System中可以同時運行多個Process然而一個CPU一次只能做一件事情, 但CPU的數量永遠少於運行中的Process數, 因此每個Process使用的時間需要被排程(Scheduling).
#### ►Memory Management
- 每個Process間在記憶體中,如果擺放的方式不當,就會在記憶體中產生很多沒辦法用到的碎片,因此Memory Management是一個問題.
#### ►Virtual Memory
- 每個Process所需要的記憶體總合,也可能大於實體記憶體,因此需要另外用二次儲存裝置充當虛擬記憶體(Virtual Memory).
#### ►Page Fault
- 但是二次儲存裝置的速度肯定很慢,因此如何做到對虛擬記憶體最小的依賴,盡量避免Page Fault(電腦在主記憶體中找不到資料,而要去二次記憶體找,就稱為Page Fault)防止Thrashing的發生(因為Virtual Memory演算法不當,造成幾乎每次存取都要依賴二次記憶體).
### **4. Thread**
- 在同一個Process底下,有許多自己的分身.
#### ►Single Threaded and Multi-threaded Processes
- 一個Process底下有數個Thread,而一個Process的Global Variable可以讓它的所有Thread共享,也就是所有Thread都可以存取同一個Process的Global Variable. 而每個Thread自己也有自己的專屬Variable.

#### ►Synchronization
- 如果有兩個Thread要存取同一個Global Variable,有可能發生問題,也就是說可能會存取到錯的值
#### ►Dead Lock
- 每一個Thread之間可能會互搶資源,而造成死結.
#### ►Thread的種類
[Operating System Multi-Threading](http://www.w3ii.com/en-US/operating_system/os_multi_threading.html)
[Kernel thread and User thread](https://en.wikipedia.org/wiki/Thread_(computing))
##### 1. kernel thread
- OS在kernel space上運行的thread
- 根據[Protection Rings](https://zh.wikipedia.org/wiki/%E5%88%86%E7%BA%A7%E4%BF%9D%E6%8A%A4%E5%9F%9F)所劃分的權限, kernel thread通常擁有最高特權,並且可以和最多的硬體直接溝通
##### 2. user thread
- 在user space上運行的thread
## References
- [Operating System Tutorials](http://www.w3ii.com/en-US/operating_system/os_multi_threading.html)
- [wiki - Protection Ring](https://zh.wikipedia.org/wiki/%E5%88%86%E7%BA%A7%E4%BF%9D%E6%8A%A4%E5%9F%9F)
- [wiki - 執行緒](https://en.wikipedia.org/wiki/Thread_(computing))
- [wiki - 輕量級程序](https://zh.wikipedia.org/wiki/%E8%BD%BB%E9%87%8F%E7%BA%A7%E8%BF%9B%E7%A8%8B)
- [GeeksForGeeks - Memory Layout](http://www.geeksforgeeks.org/memory-layout-of-c-program/)
- [鳥哥 - 什麼是程序(Process)](http://linux.vbird.org/linux_basic/0440processcontrol.php)
- [Program, Process, Thread差別](http://finalfrank.pixnet.net/blog/post/27781751-program---process---thread-%E7%9A%84%E5%B7%AE%E5%88%A5)
- [Multi-threads](http://sls.weco.net/node/21324)
- [Getting Started With POSIX Threads](http://www.csie.ntu.edu.tw/~r92094/c++/pthread.txt)