Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
7.2行程與執行緒 Game MP3 IrA multitasking • 為了方便管理應用程式,嵌入式作業系統會分別以一個執行單位來看待一項工作或是一個程式,這個執行單位在不同的嵌入式作業系統中會有不同的名稱與意義,大多為行程(PROCESS)與執行緒(THREAD) • 目的:讓不同的程式共同分享及使用有限的硬体資源 • 好像同時執行多個應用程式 嵌入式作業系統 RISC CPU 不同的任務分配執行時間
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
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
A single execution unit main() { ….. a_task(); ….. } void a_task() { ….. } 應用程式=執行單位 ONE Process Process WinCE Task or thread 子Process 子Process 子Process
Main() { PROGRAM 1 } Main() { PROGRAM 3 } Process 1 Process 2 Process 3 Main() { PROGRAM 2 } Main() { PROGRAM 4 } Process 4
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() { } 行程代表著不同的應用程式
Linux Palm OS ,Uc/OS Process 執行單位 應用程式 Execute thread or task task kernel
傳統 Embedded System 優點:速度快 缺點:不當的AP容易破壞OS OS Modern Embedded System USER AP 使用 模式 核心模式 作業系統核心執行的模式 MEMORY 執行AP FOR REAL TIME NEED
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 行程、記憶體與微處理器的關係
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 執行緒/工作、記憶體與微處理器的關係
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)
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
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
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
lightweight process: Commonly used in embedded system to avoid the cost and complexity of memory management unit (Provide strict separation between memory space)
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
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
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
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
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 */ }
Existing process New process New process Fork() execv New program same
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
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); }
7.2.2執行緒的狀態 • EMBEDDED SYSTEM MUST be 管理多工之間的執行與切換,且每個工作狀態都不相同,故以多執行緒(multi-thread)討論執行單元的狀態:
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
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
(1)First Come First Service Scheduling (FCFS)先來先做 FIFO(first in first out)演算法 最簡單的演算法 較早到達作業系統內部的行程將會較早取得CPU的資源以執行其運算 READY QUEUE佇列 CPU E D B A C Non-preemptive不可搶先排程演算法
(2)Priority First Scheduling(優先權排程演算法) 若優先權相同則採用FCFS演算法 (1)preemptive可搶先排程演算法或Non-preemptive不可搶先排程演算法 CPU E A B F C 高優先權 低優先權 Non-preemptive不可搶先排程演算法,OS將新到且有較高優先權的行程排入至執行佇列最前面的位置而不能暫停目前正在使用CPU運算的行程