# Operating System HomeWork-Question ### ***The first week*** `Why is IOS "smoother" than Android devices int early 2010s?` **ios介面上觸控的優先權佇列會非常高,所以處理上會做觸控螢幕的事項,所以相對安卓來說會滑順很多。** `What does "No response" in Operating Systems mean?` **無回應,應該是使用者執行該程式時,程式沒有辦法跟使用者做互動,可能是程式執行有誤或是設計的bug之類的影響。** `When my computer is slow, which component should upgrade first?` **可以打開工作管理員來查看,如果cpu或是記憶體等等overload,則可以看是需要升級cpu還是RAM...disk drives 之類** ### ***The second week*** `What is polling?` **polling 是一種詢問式I/O,透過CPU定時去詢問每個裝置是否需要服務,如果有的話及時給予服務,沒有的話則下一個,缺點是效率很低,需要花大量的時間去輪詢每個裝置。** `What is the different between interrupt, trap and exception?` **interrupt 可以由硬體跟軟體產生,但是trap跟exception 只會由軟體產生,軟體產生的可能是程式中不合法的行為,或是作業系統有發生錯誤等等。** `What is DMA? what is the benefit to use DMA rather than interrupt?` **DMA 是提供裝置控制器,讓I/O能夠直接跟記憶體做資料的傳輸,就可以不用再透過CPU干涉其中,CPU也可以繼續執行其他的工作,而DMA能夠讓CPU一次就傳一整個block的資料,而不是一個byte就要中斷一次。** `What is the different between buffering, caching and spooling?` **buffering 是在記憶體中切割一塊緩衝區來放disk的資料,讓CPU存取時能夠直接取用,不用跑到較慢的disk中,caching則相較buffering快上許多,但容量又比buffering小,而spooling則是能夠讓不同之間的I/O能夠共用,spool可以讓某個工作的CPU與另一個工作的I/O同步進行,但buffering只能讓同個地方的cpu計算跟I/O操作同步執行。** `What is ther different between a multiprogramming and a timesharing system?` **multiprogramming是在記憶體內擺多個工作,再排成來決定順序,當完成一個作業的空檔後,進行另外一個作業,而timesharing則是CPU快速切換,因此response time會很小,同時每個使用者都至少會有一個程式在記憶體中執行,讓使用者覺得自己有專屬電腦。** `What is thee different between user mode and kernal mode?` **kernel mode相較於user mode萬能許多,像是有關CPU的硬體,都能用kernel mode的程式來操作machine code來操作該硬體,但是user mode無法存取硬碟,而有些特權特定的指令,也只能在kernel mode來執行** ### ***The third week*** `What is the difference between a batch system, and a time sharing system?` **batch system 可以不需要User直接跟電腦做互動,但是time sharing system需要,且time sharing system會在許多的作業之間做頻繁地來回切換,也導致了time sharing system的CPU閒置時間會比batch system少,batch system也有難以提供作業間的優先順序。** `What are the differences between program, process, and thread?` **program是在IDE或是文字編輯器所寫的code,都未被load到memory的code,而process所指的是已經執行,且load到memory的program,而process可能會被CPU所執行,同時每一個process都是互相獨立的,像是打開工作管理員中能看到某些process正在執行中,而process也是thread的容器,而一個thread也是一個執行單位,所代表的為單個控制流。** `Please briefly describe the differences between register, cache, main memory, and secondary memory.` **register, cache, main memory, secondary memory, 由左至右,越左邊的速度跟運算能力越快,但是相對容量大小就越小,而越右邊的則是速度給運算能力慢,但是儲存的容量空間很大。** `In cloud computing, there are three kinds of architectures, namely "Saas," "Paas," and "Iaas." Please briefly describe them.` **SaaS為軟體即服務,由一個或多個軟體組成,可以直接使用該軟體,不需要去自己去做架設或是管理系統,像是電子郵件就算是一種SaaS,PaaS為平台即服務,由軟體堆疊所組成,通常是已準備好作業環境的平台供使用,可以免除掉一些硬體跟作業系統上的需求,更能專注在應用程式上的部署跟管理,IaaS為基礎架設即服務,能提供互聯網,電腦跟資料庫的存取。** `What is the difference between "shell" and "kernel" in an operating system?` **shell是user&kernel所存在的介面,讓user能夠跟kernel來做溝通,而kernel則是典型作業系統的核心,而shell是一個CLI,能夠執行記憶體管理,也是作業系統中的外殼,kernel則是低階的程式來與一些應用程式跟硬體來做互動溝通,能夠控制系統的所有任務,主要也是執行行程的管理,更是作業系統中的中心內層。** ### ***The fourth week*** `Describe the difference between user mode and kernel mode in CPU execution and why it is important for the security and stability of an operating system.` **User Mode跟Kernel Mode會區分開來,User Kode像是當你在瀏覽網站時的模式,Kernel Mode則是在背後操控穩定你整個系統軟體跟硬體的運作,當把兩者區分開來的時,像是在瀏覽網頁時,能夠避免一些應用程式直接去你的軟硬體中去存取資料或是散播惡意程式,間接了更保護你的電腦,甚至當你的應用軟體崩潰時,不至於讓整個作業系統都崩潰,只需關掉該User Mode的東西就好,系統能照樣正常運作。** `what is the primary purpose of the kernel in an operating system, and how does it differ from other components?` **Kernel 主要充當硬體跟用戶的軟體的橋樑,負責與其溝通並傳送訊息,並管理系統資源,像是CPU, Memory, Disk等等,以及管理Process,可以控制且排定行程,分配Memory的內存空間和Virtual Memory等等諸如此類事項。** `What are system call, and why are they essential for user-level programs to interact with the kernel in an operating system?` **System Call能夠讓 User 像底層作業系統來請求服務,像是有一些資源請求時,需要讀入或是寫出檔案會需要Kernel來幫助允許存取,而利用System Call來請求Kernel幫助會相較安全許多,像是某程度上需要存取或是做到User Mode無法做到的事之時,都需要呼叫System Call來切換Kernel Mode幫助。** ### ***The sixth week*** `when two processes are communicating with each other, there are two approaches, namely "shared memory" and "message passing" , what is the different?` **Shared Memory 是透過分享部分記憶體,讓所有Process都能在共享記憶體中讀取跟寫入,優點是因為能直接存取Memory所以處理速度上較快,很適用於需要交換大型資料,但同時也因為允許多個Process同進入共享記憶體中,所以會可能導致同步裝置發生衝突;而Message Passing則是在多個Process之間建立溝通連接,讓Process間有自己專屬的路,能夠發送或接收訊息,優點上能夠更分離Process,可避免造成衝突,也相較容易許多,缺點上是開銷上會比較大一點。** `What is Context switching?` **Context Switching是CPU運行時,當要切換到另一個Process,能夠將之前的資訊給儲存起來,並再載入新的Process的相關資訊,Context 則是表示PCB中的流程,但Context Switching是會浪費效能的,因為在這過程中,系統會做不具有生產力的工作,而Context Switching 所耗費的時間則是依照硬體的效能支援程度,所以有些會加Register數來同時Load,以節省所耗費時間。** `Please draw the diagram for Process Status Transition.` **略** `What is the difference between Job Queue, Ready Queue, Device Queue?` **Jobs Queue 是包含在System中所有的Process, Ready Queue 則是已經被load到Main Memory的Process,且他們都是已準備好執行的,同時也會一直不斷的進來新的Process,Device Queue 則是等待I/O Devices而暫時停止等待的Process, 而Process則會在這幾個Queue中不斷遷移移動。** ### ***The seventh week*** `1. Between user thread and kernel thread, there are three methods of mapping, namely "one to one" "many to many" , and "many to one" approach. In what scenario will we use each of them? Please describe them respectively.` **Many to One為多個User Threads 對應到單一個User Thread,但如果其中一個Thread Block可能會導致整個Process都被Block;還有一次只有一個Thread可以被Access到Kernel,即使是Multiple Threads, Muticore System下仍然無法平行。** **One to One為每個User Thread 對應到一個 Kernel Thread,創造一個User Thread 會產生相對應的 Kernel Thread,因此也會Overhead 而需要限制Thread的數量,此種方法相對於Many to One 也有更有 Concurrency。** **Many to Many則是多個User Threads 對應到相同或是小於其數目的Kernel Threads,同時也會在Runtime間做Mapping,User Threads的數量不會做限制,而Kernel Threads也可以在Muticore System下平行執行;當某個 thread 被 block 時,可安排其他 kernel thread 來執行。** `2. Suppose we have a program with 40% serial and 60% parallel, how much time will it be faster to move from 1 core to 6 cores?` **將會加速 1/(40%+(60%/6)) = 1/50% = 2** `3. Write a program to create a child process, you can use any kind of platform.` ```clike= #include <Windows.h> #include <iostream> int main() { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); if (CreateProcess( NULL, "C:\\Path\\To\\Your\\ChildProgram.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi )) { std::cout << "Success , ID:" << pi.dwProcessId << std::endl; WaitForSingleObject(pi.hProcess, INFINITE); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); } else { // fail std::cerr << "Fail , Error ID:" << GetLastError() << std::endl; return 1; } return 0; } ``` ### ***The eighth week*** `If two processes need to access a shared global variable "" simultaneously while maintaining synchronization, please provide the code implementing the following algorithms for handling critical sections:` `1. Peterson's solution.` ```clike= #include <stdio.h> int turn = 0; int flag[2] = {0}; void process1() { flag[0] = 1; turn = 1; while (flag[1] == 1 && turn == 1); flag[0] = 0; } void process2() { flag[1] = 1; turn = 0; while (flag[0] == 1 && turn == 0); flag[1] = 0; } ``` `2. Mutex locks.` ```clike= #include <stdio.h> #include <pthread.h> int x = 0; pthread_mutex_t mutex; void* process(void* arg) { pthread_mutex_lock(&mutex); x++; pthread_mutex_unlock(&mutex); return NULL; } int main() { pthread_t thread1, thread2; pthread_mutex_init(&mutex, NULL); pthread_create(&thread1, NULL, process, NULL); pthread_create(&thread2, NULL, process, NULL); pthread_join(thread1, NULL); pthread_join(thread2, NULL); pthread_mutex_destroy(&mutex); return 0; } ``` `3. Semaphores.` ```clike= #include <stdio.h> #include <pthread.h> #include <semaphore.h> int x = 0; sem_t semaphore; void* process(void* arg) { sem_wait(&semaphore); x++; sem_post(&semaphore); return NULL; } int main() { pthread_t thread1, thread2; sem_init(&semaphore, 0, 1); pthread_create(&thread1, NULL, process, NULL); pthread_create(&thread2, NULL, process, NULL); pthread_join(thread1, NULL); pthread_join(thread2, NULL); sem_destroy(&semaphore); return 0; } ``` ### ***The tenth week*** `1. Please describe the following client-server process communications:` `A. Sockets` **Sockets 是一種在應用層跟傳輸層之間的一個抽象層,用於客戶端跟伺服器端溝通通訊,它們允許應用程序在不同的設備或是主機之間傳輸資訊或是數據,通常使用TCP或UDP協議來建立連接,來讓應用程序之間可以進行可靠的雙向傳輸。** `B. Remote Procedure Calls` **RPC遠端過程呼叫,允許客戶端的應用程式使用遠程伺服器上的Function或是程式,這讓分佈式系統的程式能夠共享和協作程式,客戶端發送遠程請求,伺服器執行遠端的請求,並回應結果回傳至客戶端。** `C. Pipes` **Pipe是一種在同一台主機上Process跟Process之間進行通訊的機制,像是一個Process允許將Data寫入Pipe,而另一個Process則從Pipe中可以從Pipe讀取該資料,通常Pipe為單向的,也就是一個Process 將資料寫入Pipe,另一個Pipe將其讀取或是做其他的操作,例如:''ls -la | more'',Pipe對於實現Process之間得協作跟資料傳遞很有用。** `2. Please describe the following special threads:` `A. Thread Pool` **Threads Pool 是一種管理和重複使用Threads的機制,他通常在應用程序啟用時創建一定數量的Threads,然後這些Threads在Pool中等待被執行,當應用程序有任務時,他就會從Threads Pool中獲取一個可用的Thread,並執行該任務,直到完成任務再將Thread返回到Pool之中,以便之後還可以再使用它,這種方式可以減少Threads的創建跟銷毀成本,更能提高效能。** `B. OpenMP` **OpenMP是一種支援多平台的並行編程API,他讓開發者可以在Code中標示並行區塊,以便利用多核心處理器的能力,開發者也能使用指令集或是標示來告訴Compiler哪些部分的Code可以並行處理,如此一來,開發者就能夠更容易地實現並行化,提高效能。** `C. Grand Central Dispatch` **GCD 是蘋果公司開發的一個並行編程框架,適用於IOS和masOC平台,並且使用了一種Dispatch Queues的機制,讓開發者可以以非同步的方式執行,而且GCD自動管理Threads的創建跟回收,讓並行編程變得更簡單,開發者可以把任務直接丟到Dispatch Queues中,系統會自動負責並確保這些任務在適當的時間和Threads上執行。** `3. In the producer-consumer problem, please practice to write the codes for the producer and consumer, respectively, with bounded buffer.` ```clike= #include <iostream> #include <queue> #include <thread> #include <mutex> #include <condition_variable> const int BufferSize = 5; std::queue<int> buffer; std::mutex mtx; std::condition_variable bufferNotEmpty, bufferNotFull; void producer() { for (int i = 1; i <= 10; ++i) { std::unique_lock<std::mutex> lock(mtx); bufferNotFull.wait(lock, [] { return buffer.size() < BufferSize; }); buffer.push(i); std::cout << "Produced: " << i << std::endl; lock.unlock(); bufferNotEmpty.notify_one(); } } void consumer() { for (int i = 1; i <= 10; ++i) { std::unique_lock<std::mutex> lock(mtx); bufferNotEmpty.wait(lock, [] { return !buffer.empty(); }); int item = buffer.front(); buffer.pop(); std::cout << "Consumed: " << item << std::endl; lock.unlock(); bufferNotFull.notify_one(); } } int main() { std::thread producerThread(producer); std::thread consumerThread(consumer); producerThread.join(); consumerThread.join(); return 0; } ``` ### ***The eleventh week*** `Schedule process using Round Robin Scheduling, and Shortest-Remain-Time Scheduling.` **略** ### ***The twelfth week*** `1.If there are three empty spaces in the memory: (1) 30~40 (2)60~90 (3)120~160 Suppose we have a process with size of 25, which position should it put using thefollowing respective strategies? (1) Firstfit (2), Bestfit, (3) Worstfit` **Firstfit:第一個符合的就直接配置,所以應該放在(2) 60~90。 Bestfit:搜尋全部找到最剛好的,所以放在(2) 60~90。 Worstfit:搜尋全部找到最大的那個,所以為(3) 120~160。** `2. If we use paging memory management, the page size is 5. Suppose we have a logical address 12, and the page table is as follows: What 15-12s coresponding physical address?` **Logical Address 為12,所以在page2的第三個位置,p=2, d=2, 再根據Page Table得知f=0, Physical Address = f\*(page_size)+d, 得知答案為2。** `3. Suppose we have a process with Base Address 60, and thelimit is 30, which of the following physical addresses it cannot access? (A) 60 (B) 75 (C) 100` **Base Address 為60加上限制的30後為90,允許存取的範圍為60~89, C 為100,已超出範圍。** ### ***The thirteenth week*** `1. Suppose we have the reference string 1, 2, 3, 4, 3, 2, 1, 5, 6, 2, 1 in sequence. If we have 3 frames only, Please use the place replacement algorithms as follows, and draw which pages are in the frames in the time sequence. (1) FIFO (2) LRU (3) Optimal` `2. Now we have 4 frames, please repeat the algorithms above.` **略** ### ***The fourteenth week*** `1. Calculate the Hard Disk Performance based on the following conditions: Transfer 8KB block on a 5,400pm disk, with a 10ms average seek time, 2GB/sec transfer rate, with a 0.2ms controller overhead.` ***hint:Average I/O Time = Average Access Time + (Amount To Transfer / Transfer Rate) + Controller Overhead.*** **∵5400rpm = 5.56ms Average Latency, Average Access Time = 10 ms + 5.56ms = 15.56ms. ∴Average I/O Time = 15.56ms + (8KB / 2GB/Sec) + 0.2ms = 15.764ms** `2. Suppose we have the following queue: starting at 53, 180, 119, 11, 123, 62, 64, 95, 34 please use: (1) SSTF, (2) C-SCAN to do disk scheduling.` **SSTF = 53 -> 62 -> 64 -> 34 -> 11 -> 95 -> 119 -> 123 -> 180 C-SCAN = 53 -> 62 -> 64 -> 95 -> 119 -> 123 -> 180 -> 11 -> 34** `3. What does RAID O ~ RAID 6 stand for?` **RAID 0 : 在存放資料時,分段後分散儲存在這些磁碟中,因為讀寫時都可以並列處理,所以在所有的級別中,RAID 0的速度是最快的。但是RAID 0既沒有冗餘功能,也不具備容錯能力,如果一個磁碟損壞,所有資料都會遺失。<br> RAID 1 : 兩組以上的N個磁碟相互作鏡向,在一些多執行緒作業系統中能有很好的讀取速度,理論上讀取速度等於硬碟數量的倍數,與RAID 0相同。另外寫入速度有微小的降低。只要一個磁碟正常即可維持運作,可靠性最高。其原理為在主硬碟上存放資料的同時也在鏡像硬碟上寫一樣的資料。當主硬碟損壞時,鏡像硬碟則代替主硬碟的工作。因為有鏡像硬碟做資料備份,所以RAID 1的資料安全性在所有的RAID級別上來說是最好的。但無論用多少磁碟做RAID 1,僅算一個磁碟的容量,是所有RAID中磁碟利用率最低的一個級別。<br> RAID 2 : 這是RAID 0的改良版,以漢明碼(Hamming Code)的方式將資料進行編碼後分割為獨立的位元,並將資料分別寫入硬碟中。因為在資料中加入錯誤修正碼(ECC,Error Correction Code),所以資料整體的容量會比原始資料大一些,RAID 2最少要三台磁碟機方能運作。<br> RAID 3 : 採用 Bit-interleaving(資料交錯儲存)技術,它需要通過編碼再將資料位元分割後分別存在硬碟中,而將相同位元檢查後單獨存在一個硬碟中,但由於資料內的位元分散在不同的硬碟上,因此就算要讀取一小段資料資料都可能需要所有的硬碟進行工作,所以這種規格比較適於讀取大量資料時使用。<br> RAID 4 : 採用塊交織技術(Block interleaving)。它與RAID 3不同的是它在分割時是以區塊為單位分別存在硬碟中,但每次的資料存取都必須從同位元檢查的那個硬碟中取出對應的同位元資料進行核對,由於過於頻繁的使用,所以對硬碟的損耗可能會提高。<br> RAID 5 : 是一種儲存效能、資料安全和儲存成本兼顧的儲存解決方案,它使用的是 Disk Striping(硬碟分割)技術,使用奇偶校驗位(Parity Bit or Check Bit),與 RAID 4 一樣,有效大小是 N-1 個磁碟的大小, 然而,由於奇偶校驗資訊也在 N 個驅動器之間均勻分布,因此避免了每次寫入都必須更新奇偶校驗磁碟的瓶頸。防止單個磁碟故障,而且訪問速度快。<br> RAID 6 : 與RAID 5相比,RAID 6增加第二個獨立的奇偶校驗資訊塊。兩個獨立的奇偶系統使用不同的演算法,資料的可靠性非常高,任意兩塊磁碟同時失效時不會影響資料完整性。RAID 6需要分配給奇偶校驗資訊更大的磁碟空間和額外的校驗計算,相對於RAID 5有更大的IO操作量和計算量,其寫效能強烈取決於具體的實現方案,因此RAID 6通常不會通過軟體方式來實現,而更可能通過硬體方式實現**