1 / 102

第七章

第七章. 嵌入式作業系統. 本章學習重點. 嵌入式作業系統原理 即時排程演算法 WinCE 、 Symbian 、 VxWorks 和 Nucleus 嵌入式作業系統介紹. 嵌入式作業系統架構的演進. 嵌入式作業系統特色. 記憶體空間需求小且整體效率高 良好的省電功能 與 PC 溝通的功能 開放應用程式撰寫環境與動態載入應用程式 通訊功能. 嵌入式作業系統. 有一核心 (Kernel) 及系統服務 提供系統程式庫給應用程式呼叫 系統程式庫 ( 1)file system(2) 記憶体配置

Télécharger la présentation

第七章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第七章 嵌入式作業系統

  2. 本章學習重點 • 嵌入式作業系統原理 • 即時排程演算法 • WinCE、Symbian、VxWorks和Nucleus嵌入式作業系統介紹

  3. 嵌入式作業系統架構的演進

  4. 嵌入式作業系統特色 • 記憶體空間需求小且整體效率高 • 良好的省電功能 • 與PC溝通的功能 • 開放應用程式撰寫環境與動態載入應用程式 • 通訊功能

  5. 嵌入式作業系統 • 有一核心(Kernel)及系統服務 • 提供系統程式庫給應用程式呼叫 • 系統程式庫(1)file system(2)記憶体配置 • (3)I/O存取服務(4)中斷服務(5)工作控制服務(Task)(5)時間服務

  6. 7.2行程與執行緒 Game MP3 IrA multitasking • 為了方便管理應用程式,嵌入式作業系統會分別以一個執行單位來看待一項工作或是一個程式,這個執行單位在不同的嵌入式作業系統中會有不同的名稱與意義,大多為行程(PROCESS)與執行緒(THREAD) • 目的:讓不同的程式共同分享及使用有限的硬体資源 • 好像同時執行多個應用程式 嵌入式作業系統 RISC CPU 不同的任務分配執行時間

  7. Processes • A process is a unique execution of a program. • Several copies of a program may run simultaneously or at different times. • Process is defined by both its code and its data • A process has its own data set: • CPU registers; • memory. • The operating system manages interaction between the processes

  8. process A unique execution of a program Written in terms of CPU Instructions Executed sequentially Stop it Process 1 Start it CPU Execute only one process at a time Process 2 Process 3 Context switch Process 4

  9. A single execution unit main() { ….. a_task(); ….. } void a_task() { ….. } 應用程式=執行單位 ONE Process Process WinCE Task or thread 子Process 子Process 子Process

  10. Main() { PROGRAM 1 } Main() { PROGRAM 3 } Process 1 Process 2 Process 3 Main() { PROGRAM 2 } Main() { PROGRAM 4 } Process 4

  11. Process 1 /* program 1*/ main() { program1(); } void program1() { } /* program 2*/ main() { program2(); } void program2 () { } Process 2 /* program 3*/ main() { program3(); } void program3() { } Process 3 Process 4 /* program 4*/ main() { program4(); } void program4() { } 行程代表著不同的應用程式

  12. Linux Palm OS ,Uc/OS Process 執行單位 應用程式 Execute thread or task task kernel

  13. 傳統 Embedded System 優點:速度快 缺點:不當的AP容易破壞OS OS Modern Embedded System USER AP 使用 模式 核心模式 作業系統核心執行的模式 MEMORY 執行AP FOR REAL TIME NEED

  14. 行程(process)/ 執行緒(thread)/工作(task) • 通常作業系統會配給行程(process)裡一塊記憶體空間,放著該行程所有的程式碼與資料,以及一些系統服務的紀錄資料,例如檔案開啟的紀錄表、虛擬記憶對應表等,當然還包含了該行程每次執行時,微處理機的執行狀態 • 但對於這個行程內的執行緒(thread)/工作(task)而言,分配到的可能是位於該行程內的一段記憶體空間,紀錄著該執行緒/工作的程式碼與資料,以及每次微處理機的執行狀態,而那些屬於全域性的紀錄資料,並不會複製到每一個執行緒/工作內,像是上述的檔案開啟的紀錄表、虛擬記憶對應表等。

  15. Memory CPU Process 1 data, code Program counter Process 2 data, code Register Files Process 3 data, code Process 4 data, code Process 1 record Process 2 record Process 3 record Process 4 record 行程、記憶體與微處理器的關係

  16. Memory CPU Status in Thread 1 of Process 1 Process 1 code, data Program counter Register Files CPU Status in Thread 2 of Process 1 Program counter Process 1 record Register Files 執行緒/工作、記憶體與微處理器的關係

  17. Linux使用繼承的方法作資源分配 6.輕權值行程(light weight process) 在Linux也稱為執行緒(thread) Paranet process 3.From Paranet process 記憶空間中把all data Copy 過來 1.繼承一部份系統資源 New process 4.再透過參數的設定決定是否與父行程分享資源 2.基本上是child process分配到另外一塊獨立的記憶空間 5.參數的設定決定新產生的子行程是屬於(1)重權值行程(heavy weight process)或(2)輕權值行程(light weight process)

  18. Activation record: record process state. Context switch: current CPU context goes out; new CPU context goes in. Processes and CPUs Current execute process 2 Code,data PC registers process 2 Activation record copy CPU ... process 3 Code,data Contain the data used to reactivate the process Nonexecuting process Record CPU internal state (when process were Stop) process 3 Activation record memory

  19. One commonly used form of process in embedded system lightweight process: a process that shares memory space with other processes. Also known as a THREAD. memory lightweight process Register Cohabit the same main memory space Have Distinct sets of value for CPU register lightweight process Register One lightweight process may destory the data of another lightweight process executing on the machine lightweight process Register

  20. General Purpose computing platform 1.Run heavyweight process on Full protection of process memory space User code that has not been tested for correctness and safety distinct logical memory space map Ensure processes do not interfere with each other Physical memory MMU Embedded computing platform run Use a lightweight process model A few program That can be tested before deployment Save the cost of MMU

  21. lightweight process: Commonly used in embedded system to avoid the cost and complexity of memory management unit (Provide strict separation between memory space)

  22. What is POSIX? Two mechanism processes satisfy More complexity system Timing requirement OS To build Embedded software build More complexity & flexibility application Real-time OS (RTOSs) Provide satisfying Real time requirements POSIX for example

  23. POSIX is a version of the Unix OS created by a standards organization Unix OS version compatible POSIX Based OS Many RTOSs Source code compatible In include unistd.h Defined in The existence of feature is determined by C preprocessor variable POSIX standard Many option

  24. Linux Based on POSIX Standard Open source code

  25. Process in POSIXcreating process require somewhat cumbersome code Existing process New process Fork() Copy created Runs the code of new process The complicated is ensuring Run same code What is fork()? Continue the work of old process A process makes a copy of itself by calling the fork() function

  26. Fork() ensure the OS to create a new process

  27. Create a process with fork: parent process keeps executing old program; child process executes new program. Processes in POSIX process a Returned the process ID # of child parent process b process c child Returned the 0 Share the same code and same data value

  28. fork() Returned value Decided which process is child • The fork process creates child: childid = fork(); if (childid == 0) {/*Must be child,do child process here*/ /* child operations */ } else { /* parent operations */ }

  29. Existing process New process New process Fork() execv New program same

  30. Both processes contain all the code for both the parent and child process is clumsy,so use execv() let new process have own program execv() • Overlays child code: childid = fork(); if (childid == 0) { execv(“mychild”,childargs); perror(“execv”); exit(1); } Can overloading the code in a process(child) file with child code( main()) The process call with an overlay of the child’s code on the child process Care the where execv fail and return to the parent process

  31. Parent process use Wait function: it Return child process’s status ,and make sure the child’s resource are freed childid = fork(); if (childid == 0) { execv(“mychild”,childargs); perror(“execv”); exit(1); } else{/* is the parent*/ parent_stuff();/*execute parent functionality*/ wait(&cstatus);/*wait child process ,set integer cstatus variable to the return value of the child process */ exit(0); }

  32. 7.2.2執行緒的狀態 • EMBEDDED SYSTEM MUST be 管理多工之間的執行與切換,且每個工作狀態都不相同,故以多執行緒(multi-thread)討論執行單元的狀態:

  33. 7.2.2執行緒的狀態 支援多執行緒的作業系統可分為下列五種狀態: (1)Dormant:執行緒在被建立、執行、結束或是刪除之後所處的狀態 (2)READY:執行緒一切就緒,等待系統分配CPU資源以便執行

  34. 7.2.2執行緒的狀態 支援多執行緒的作業系統可分為下列五種狀態: (3)Running:正在使用CPU資源 (4)Waiting:當執行緒正在等待某個事件發生的時侯所處的閂鎖狀態 (5)Delayed: :當執行緒暫停執行一段時間終了,才會進入Ready狀態

  35. Idle thread • 優先權最低 • 系統開機就被建立 • 當所有的執行緒都不存在或是處於waiting或是delay的時候CPU執行的執行緒即為idle thread • Idle thread在執行時會(1)檢查整個系統有那些資源應該回收(2)做預設系統的動作 • 如果沒有其它的工作,idle thread會將CPU切入睡眠模式,保持省電的狀態

  36. TCB(thread control block) • (1)task context • (2)記錄識別碼 • (3)記錄優先權 • (4)記錄一個執行緒的附屬資料 • 目的:做為排程器(scheduler)排程時所需的資訊

  37. µC/OS-II’s Task Control Block 資料結構 typedef struct os_tcb { OS_STK *OSTCBStkPtr; #if OS_TASK_CREATE_EXT_EN void *OSTCBExtPtr; OS_STK *OSTCBStkBottom; INT32U OSTCBStkSize; INT16U OSTCBOpt; INT16U OSTCBId; #endif struct os_tcb *OSTCBNext; struct os_tcb *OSTCBPrev; #if (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_MBOX_EN || OS_SEM_EN OS_EVENT *OSTCBEventPtr; #endif #if (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_MBOX_ EN void *OSTCBMsg; #endif INT16U OSTCBDly; INT8U OSTCBStat; INT8U OSTCBPrio; INT8U OSTCBX; INT8U OSTCBY; INT8U OSTCBBitX INT8U OSTCBBitY; #if OS_TASK_DEL_EN BOOLEAN OSTCBDelReq; #endif } OS_TCB;

  38. 7.3排程(scheduling) • 排程可以說是作業系統核心中最重要工作之一,因為CPU只有一個,所以在一個多工的系統中,核心必須適當的安排分配每個程式的執行時間,讓整各系統看起來就像是有許許多多的程式同時在執行一樣。通常核心會以分時(Time Slicing)的觀念來讓大家共享CPU資源。把CPU執行的時間,分成一段段的,每個程式就分配到這些時間來執行任務。這一段一段的時間,在不同的作業系統中,有的長,可能是200個分時,有的短,也有只有20個分時,有的是固定的,也有可以調整的,而每一段的執行時間到了,就必須輪替換下一個程式執行,那要輪到哪一個,就必須要由核心的排程器來決定。

  39. scheduler Determine which process to be execute next. Determine which process to call Save it’s the state 3 2 Process1 call 1 scheduler Process 2 4 CPU state Set CPU state to the new process Main different:not directly determine which process run next,we use scheduler (more flexibility) Like co-routine structure

  40. Process 1 Process 2 Process 4 Process 3 排程

  41. 7.3.1排程演算法 • (1)First Come First Service Scheduling (FCFS) • (2)Priority First Scheduling • (3)Round-Robin Scheduling • (4)Priority First with Round-Robin Scheduling

  42. (1)First Come First Service Scheduling (FCFS)先來先做 FIFO(first in first out)演算法 最簡單的演算法 較早到達作業系統內部的行程將會較早取得CPU的資源以執行其運算 READY QUEUE佇列 CPU E D B A C Non-preemptive不可搶先排程演算法

  43. (2)Priority First Scheduling(優先權排程演算法) 若優先權相同則採用FCFS演算法 (1)preemptive可搶先排程演算法或Non-preemptive不可搶先排程演算法 CPU E A B F C 高優先權 低優先權 Non-preemptive不可搶先排程演算法,OS將新到且有較高優先權的行程排入至執行佇列最前面的位置而不能暫停目前正在使用CPU運算的行程

  44. (3)Round-Robin Scheduling 每個行程分配一段時間(100MS)CPU使用權 CPU A B C D

  45. (4)Priority First with Round-Robin Scheduling循環式排程演算法 • 優先權高者採用優先權排程演算法 • 優先權相同者採用循環式排程演算法

  46. 什麼時候呼叫排程器? • 1被動式:行程或執行緒被切換時,也就是每一個時間中斷的時侯

  47. 排程時機 • 在被動的情況下,行程或是執行緒被切換,這個時機就是在每一個時間中斷的時候。決定是否排程以及排程器的程式碼,通常會放在計時器的中斷服務函式裡。 • 行程或執行緒主動呼叫作業系統的系統服務。有些系統服務會讓行程或是執行緒本身的狀態改變,如exit、sleep、wait event、wait semaphore等,或是give up CPU的系統呼叫時候,都會去呼叫排程機制。

  48. 好的排程器要有即時的效果 • 搶先及優先等權等特性的排程器會具有即時的效果 • SEE範例7-3,7-4,7-5

  49. 什麼是嚴格區域(Critical Section)? 缺點:會造成死結(dead lock),及優先權的逆轉,低優先權的執行緒正在執行Critical Section的程式而使高優先權的執行緒無法取得CPU的執行權 Critical Section 1.正在執行 執行緒 CPU 中斷服務 中斷服務常式 2.切換至 執行緒的工作 3.危險 不能被排程 Critical region: section of code that cannot be interrupted by another process.Examples(1)writing shared memory;(2)accessing I/O device.

  50. 7.4即時系統排程演算法 • QNX的QNS OS • Windriver的Vxworks • Microware 的OS9 POS

More Related