Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android OS 許軒中 李紀萱 鄧國盛 Hi

Similar presentations


Presentation on theme: "Android OS 許軒中 李紀萱 鄧國盛 Hi"— Presentation transcript:

1 Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 Hi

2 Outline Overview of Android OS Multiprogramming and Multitasking
Hi Outline Overview of Android OS Multiprogramming and Multitasking Scheduling employed Threading supported Programming languages and IDEs supported 許軒中 李紀萱 鄧國盛

3 Android Architecture java C 1.有五大架構。 2.將整個架構分為兩大部分
上層用java寫的,下層是用C寫的 (但有一塊android runtime很特別) Application C 許軒中 李紀萱 鄧國盛

4 Android Architecture(Cont)
The applications are at the topmost layer of the Android stack. An average user of the Android device would mostly interact with this layer (for basic functions, such as making phone calls, accessing the Web browser etc.) Application 一般我們從GOOGLE PLAY下載下來的一個可以賣的APP,像是GOOGLE MAP ,LINE啊等就是屬於這一層的 而一般的智慧型手機使用者其實也只會接觸到這層 在APP層下面一點的就是 Application framework 當開發APP的時候,開發者主要就是在使用這一層BASIC FUNCTION EX:GOOGLE MAP要可以知道使用者的位置、LINE要可以取得手機聯絡人 而這些BASIC TOOL就是由這層來提供 而一般來說,開發者也只需要知道app framework和app之間的運作關係,以及如何利用java來實作就好,並不太需要探究更內部的系統環境。 但如果是要移植Android到嵌入式系統(例如用android來設計家電的操作介面) ,就必須更深入理解Android系統內部的函式庫、Android執行環境與Linux核心的細節。 Application framework includes the programs that manage the phone's basic functions. Think of the application framework as a set of basic tools with which a developer can build much more complex tool. 許軒中 李紀萱 鄧國盛

5 Android Architecture(Cont)
Dalvik VM Dalvik VM Dalvik VM Dalvik VM app app app app app app Core libraries (Java) These are different from Java SE and Java ME libraries. However these libraries provides most of the functionalities defined in the Java SE libraries. Dalvik VM It is a type of JVM used in android devices to run apps and is optimized for low processing power and low memory environments. Dalvik VM is Register based and each application runs on its own Dalvik VM, that is, It has been designed to allow multiple VM instances to run at once Operates on DEX files. 而前面有提到上面這兩層是用java寫的 下面的是用C寫 那java是如何run 在上面的呢? 其實有寫過Java的應該都對Jvm不陌生,java之所以可以成為跨平台的開發軟體也是因為jvm負責溝通java和下面的host OS 而android OS也有類似JVM的東西叫做Dalvic VM DVM跟JVM很像,也是一種java VM,但是他有特別對效能和記憶體資源的運用做過優化 他跟JVM最大的不同點也在這 JVM是Stack-based的,DVM是Register based的,JVM在程式運行的時候,所有的變數皆需push,pop到stack裏去,所以就會多出不少指令,而Delvik VM則改用register based的方式,變數皆存放於暫存器中,相較之下Delvik VM的指令就會少一點,速度也就會再加快一點。 另外要注意的是每一個app會有專自己專屬的D VM來執行,換句話說,不是一個dalvic上有很多app在跑,不是,而是一次可以有很多個DVM在run,而一個DVM只會跑一個app 還有DVM跟JVM有另一個不同的地方就是 DVM是跑dex檔 Andorid應用程式開發者在運用Java程式語言編寫完原始程式碼後,其儲存的檔案副檔名為 .java,.java檔案接著會被Java程式語言編譯器(Compiler)編譯成副檔名為 .class的檔案(其為二進位碼形式(bytecode))。 如 果是一般的Java應用程式,大概到這一步就可以直接透過Java虛擬機器(Java virtual machine, JVM)在作業系統之中執行。只不過,因為在Android系統之中並不是使用JVM,而是Dalvik虛擬機器(Dalvik virtual machine, Dalvik VM)。所以,副檔名.class的檔案還要再被Android SDK(軟體開發工具包)當中的dx工具再轉換成為Dalvik VM可以執行的檔案(副檔名為 .dex, Dalvik Executable,同為二進位碼形式)。 最後,Android SDK會將所有檔案壓縮包裏成單一的Android封裝檔(副檔名為 .apk,Android application package),以供手機用戶直接下載執行。 .java compile .class convert .dex package .apk 許軒中 李紀萱 鄧國盛

6 許軒中 李紀萱 鄧國盛

7 Android Architecture(Cont)
Libraries (C/C++) Android’s native libraries carry a set of instructions to guide the device in handling different types of data. These libraries are written in c or c++ language and are specific for a particular hardware. Libraries Android裡的系統元件其實都是由這一層的C libraries寫成的,負責處理各種類型的資料,但是要注意的是開發者還是必須要透過上一層的framework來使用這些功能 Kernel ndroid作業系統的核心屬於Linux核心的一個分支,具有典型的Linux排程和功能,[36]除此之外,Google為了能讓Linux在移動設備上良好的執行,對其進行了修改和擴充。Android去除了Linux中的本地X Window System,也不支援標準的GNU庫,這使得Linux平台上的應用程式移植到Android平台上變得困難[37] Kernel The basic layer is the Linux kernel. The whole Android OS is built on top of the Linux 2.6 Kernel with some further architectural changes made by Google.  It is this Linux that interacts with the hardware and contains all the essential hardware drivers. It also  acts as an abstraction layer between the hardware and other software layers. 許軒中 李紀萱 鄧國盛

8 Anatomy of an Android Application
Application components are the essential building blocks of an Android application. Each component is a different point through which the system can enter your application. Not all components are actual entry points for the user and some depend on each other, but each one exists as its own entity and plays a specific role—each one is a unique building block that helps define your application's overall behavior. Activities Services Content Providers Broadcast receivers 許軒中 李紀萱 鄧國盛

9 Activities Activity 1 Activity 2 Activity 3
An activity represents a single screen with a user interface. For example, an application might have one activity that shows a list of new s, another activity to compose an , and another activity for reading s. Although the activities work together to form a cohesive user experience in the application, each one is independent of the others. As such, a different application can start any one of these activities (if the application allows it). For example, a camera application can start the activity in the application that composes new mail, in order for the user to share a picture. Activity 1 Activity 2 Activity 3 Show list of new s Compose s Reading s Application 許軒中 李紀萱 鄧國盛

10 Services Content Providers Broadcast receivers
A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. Example: Playing music in the background Services A content provider manages a shared set of application data. You can store the data in the file system, an SQLite database, on the web, or any other persistent storage location your application can access. Through the content provider, other applications can query or even modify the data (if the content provider allows it). For example, the Android system provides a content provider that manages the user's contact information.  Content Providers A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the system—for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. Applications can also initiate broadcasts—for example, to let other applications know that some data has been downloaded to the device and is available for them to use.  Broadcast receivers 許軒中 李紀萱 鄧國盛

11 Photo Editing Application
A unique aspect Any application can start another application’s component. Photo Editing Application Camera Application Android OS Component 1 Component 1 Intent Component 2 Component 2 Component 3 Component 3 許軒中 李紀萱 鄧國盛

12 Multitasking The Android system tries to maintain an application process for as long as possible……. but eventually needs to remove old processes to reclaim memory for new or more important processes. To determine which processes to keep and which to kill, the system places each process into an "importance hierarchy" based on the components running in the process and the state of those components. Importance Foreground Process Visible Process Service Process Background Process Empty Process 許軒中 李紀萱 鄧國盛

13 It hosts an Activity that the user is interacting with.
Foreground Process It hosts an Activity that the user is interacting with. It hosts a Service that's bound to the activity that the user is interacting with. It hosts a BroadcastReceiver that's executing its onReceive() method. Visible Process Visible Process A process that doesn't have any foreground components, but still can affect what the user sees on screen.  It hosts a Service that's bound to a visible (or foreground) activity. 許軒中 李紀萱 鄧國盛

14 A process holding an activity that's not currently visible to the user
Service Process Service Process They are generally doing things that the user cares about (such as playing music in the background or downloading data on the network), Background Process Background Process A process holding an activity that's not currently visible to the user Empty Process A process that doesn't hold any active application components. The only reason to keep this kind of process alive is for caching purposes, to improve startup time the next time a component needs to run in it. 許軒中 李紀萱 鄧國盛

15 When deciding which processes to kill, the Android system weighs their relative importance to the user. For example, it more readily shuts down a process hosting activities that are no longer visible on screen, compared to a process hosting visible activities. The decision whether to terminate a process, therefore, depends on the state of the components running in that process. If a user later returns to an application that's been killed, Android preserves the "all applications are running all of the time" experience by keeping track of the parts of the application the user is aware of (the Activities), and re-starting them with information about the last state they were seen in. Android's process management can be seen as a form of swap space: application processes represent a certain amount of in-use memory; when memory is low, some processes can be killed (swapped out); when those processes are needed again, they can be re-started from their last saved state (swapped in). 許軒中 李紀萱 鄧國盛

16 Scheduling Since Android is based on the Linux kernel, it shares many aspects with Linux, including how processes and threads are scheduled. The Linux kernel uses the CFS, or the Completely Fair Scheduler which picks out the next process dynamically based on vruntime values and “nice values” 許軒中 李紀萱 鄧國盛

17 CFS Scheduler Scheduler Latency - the period in which all run queue tasks are scheduled at least once. Now consider a case where two threads share the CPU, one with niceness 0 and the other with niceness 5. CFS assigns these niceness levels the weights of 1024 and 335 respectively. The time that the threads get is therefore proportional to 1024/( ) and 335/( ). The thread with niceness 0 will receive approximately 75ms out of each 100ms and the thread with niceness 5 will receive approximately 25ms out of each 100ms Thread 1 Nice Value 0 Weight 1024 Thread 2 Nice Value 5 Weight 335 1024/( ) 335/( 許軒中 李紀萱 鄧國盛

18 Vruntime To pick out the next process to run, Linux picks the process with the lowest vruntime (virtual run time) Thread 1 Nice Value 0 Weight 1024 Vruntime += 1 Run for 1 ns Thread 2 Nice Value 5 Weight 335 Vruntime += 3 Run for 1 ns 許軒中 李紀萱 鄧國盛

19 The CPU thinks that each process was given the same run time!
Time Slice Given to each process = sched_latency * (process_weight / all weights) Vruntime added to each process = Actual Runtime / (process_weight) Vrun time = sched_latency / all weights The CPU thinks that each process was given the same run time! However as we can see, processes with higher priorities (lower nice values) actually get larger time slices to run! 許軒中 李紀萱 鄧國盛

20 許軒中 李紀萱 鄧國盛

21 Imagine if we had 10 background threads and 1 foreground thread.
But……. Imagine if we had 10 background threads and 1 foreground thread. Are nice levels enough to guarantee that the response time of our foreground thread is unaffected? Background Cgroup Default/Foreground Cgroup 5% of all CPU cycles 95% of all CPU cycles 許軒中 李紀萱 鄧國盛

22 Linux CFS Nice Values Hard on the CPU Nicer to the CPU -20 -19 -18 -17
-16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 THREAD_PRIORITY_DEFAULT THREAD_PRIORITY_LOWEST THREAD_PRIORITY_URGENT_AUDIO Default Cgroup – 95% Background Cgroup – 5% 許軒中 李紀萱 鄧國盛

23 Threading in Android When an Android application is first started, the runtime system creates a single thread in which all application components will run by default. This thread is generally referred to as the main thread.  許軒中 李紀萱 鄧國盛

24 public class MyThread extends Thread{ @Override public void run() {
Android also supports java.lang.thread. There are basically two main ways of having a Thread execute application code. Create a new class that extends Thread and override its run() method. MyThread t = new MyThread(); t.start(); public class MyThread extends Thread{ @Override public void run() { super.run(); }//run }//class 許軒中 李紀萱 鄧國盛

25 public class MyRunnableClass implements Runnable { @Override
Create a new Runnable instance passing to it a Thread object. Runnable myRunnable1 = new MyRunnableClass(); Thread t1 = new Thread(myRunnable1); t1.start(); public class MyRunnableClass implements Runnable { @Override public void run() { }//run }//class 許軒中 李紀萱 鄧國盛

26 Programming Language Android development is java-based (most of the times), because a large portion of Java libraries is supported in Android along with the Android libraries. Java.lang Java.math Javax.sql Javax.xml.parsers Java.text Java.security 許軒中 李紀萱 鄧國盛

27 Android SDK 許軒中 李紀萱 鄧國盛

28 Hi Android SDK 許軒中 李紀萱 鄧國盛


Download ppt "Android OS 許軒中 李紀萱 鄧國盛 Hi"

Similar presentations


Ads by Google