Os Unit 6 Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

UNIT-VI Linux System & Android Software Platform

Linux System:
Components of LINUX
Linux is one of popular version of UNIX operating System.

It is open source as its source code is freely available.


Linux was designed considering UNIX compatibility. Its functionality list is quite
similar to that of UNIX.

Components of Linux System

Linux Operating System has primarily three components

Kernel :
Kernel is the core part of Linux.
It is responsible for all major activities of this operating system.
It consists of various modules and it interacts directly with the underlying
hardware.
Kernel provides the required abstraction to hide low level hardware details to
system or application programs.
System Library −
System libraries are special functions or programs using which application
programs or system utilities accesses Kernel's features.

These libraries implement most of the functionalities of the operating system and
do not requires kernel module's code access rights.
System Utility −
System Utility programs are responsible to do specialized, individual level tasks.

Kernel Mode vs User Mode

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

Kernel component code executes in a special privileged mode called kernel mode
with full access to all resources of the computer.

This code represents a single process, executes in single address space and do not
require any context switch and hence is very efficient and fast.

Support code which is not required to run in kernel mode is in System Library.

User programs and other system programs works in User Mode which has no
access to system hardware and kernel code.

User programs/ utilities use System libraries to access Kernel functions to get
system's low level tasks.

Basic Features
Following are some of the important features of Linux Operating System.

Portable − Portability means software can works on different types of hardware in


same way. Linux kernel and application programs supports their installation on any
kind of hardware platform.

Open Source − Linux source code is freely available and it is community based
development project. Multiple teams work in collaboration to enhance the
capability of Linux operating system and it is continuously evolving.

Multi-User − Linux is a multiuser system means multiple users can access system
resources like memory/ ram/ application programs at same time.

Multiprogramming − Linux is a multiprogramming system means multiple


applications can run at same time.

Hierarchical File System − Linux provides a standard file structure in which


system files/ user files are arranged.

Shell − Linux provides a special interpreter program which can be used to execute
commands of the operating system. It can be used to do various types of
operations, call application programs. etc.

Security − Linux provides user security using authentication features like


password protection/ controlled access to specific files/ encryption of data.

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

Interposes Communication Mechanisms On Linux

Inter Process Communication (IPC) is a mechanism that involves communication


of one process with another process. This usually occurs only in one system.

Communication can be of two types −

Between related processes initiating from only one process, such as parent and
child processes.

Between unrelated processes, or two or more different processes.

The Linux kernel provides the following IPC mechanisms:

Pipes − Communication between two related processes. The mechanism is half


duplex meaning the first process communicates with the second process. To
achieve a full duplex i.e., for the second process to communicate with the first
process another pipe is required.

Anonymous Pipes
Anonymous pipes (or simply pipes, for short) provide a mechanism for one process
to stream data to another.

A pipe has two ends associated with a pair of file descriptors - making it a one-to-
one messaging or communication mechanism.

One end of the pipe is the read-end which is associated with a file-descriptor that
can only be read, and the other end is the write-end which is associated with a file
descriptor that can only be written.

This design means that pipes are essentially half-duplex.

Anonymous pipes can be setup and used only between processes that share parent-
child relationship.

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

Generally the parent process creates a pipe and then forks child processes.
Each child process gets access to the pipe created by the parent process via the file
descriptors that get duplicated into their address space.

This allows the parent to communicate with its children, or the children to
communicate with each other using the shared pipe.

Pipes are generally used to implement Producer-Consumer design amongst


processes - where one or more processes would produce data and stream them on
one end of the pipe, while other processes would consume the data stream from the
other end of the pipe.

Named Pipes Or FIFO

Named pipes (or FIFO) are variants of pipe that allow communication between
processes that are not related to each other.

Communication between two unrelated processes

The processes communicate using named pipes by opening a special file known as
a FIFO file.

One process opens the FIFO file from writing while the other process opens the
same file for reading.

Thus any data written by the former process gets streamed through a pipe to the
latter process.

FIFO is a full duplex, meaning the first process can communicate with the second
process and vice versa at the same time.

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

MESSAGE QUEUES

Message Queues are synonymous to mailboxes.

One process writes a message packet on the message queue and exits. Another
process can access the message packet from the same message queue at a latter
point in time.

Communication between two or more processes with full duplex capacity. The
processes will communicate with each other by posting a message and retrieving it
out of the queue.

Once retrieved, the message is no longer available in the queue.

The advantage of message queues over pipes/FIFOs are that the sender (or writer)
processes do not have to wait for the receiver (or reader) processes to connect.

Think of communication using pipes as similar to two people communicating over


phone,

while message queues are similar to two people communicating using mail or other
messaging services.

There are two standard specifications for message queues.

SysV message queues.

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

The SysV message queues support message channeling.

Each message packet sent by senders carry a message number.

The receivers can either choose to receive message that match a particular message
number, or receive all other messages excluding a particular message number or all
messages.

POSIX message queues.

The POSIX message queues support message priorities.

Each message packet sent by the senders carry a priority number along with the
message payload.

The messages get ordered based on the priority number in the message queue.

When the receiver tries to read a message at a later point in time, the messages
with higher priority numbers get delivered first.

POSIX message queues also support asynchronous message delivery using threads
or signal based notification.

Linux support both of the above standards for message queues.

Shared Memory
As the name implies, this IPC mechanism allows one process to share a region of
memory in its address space with another.

Communication between two or more processes is achieved through a shared piece


of memory among all processes.

This allows two or more processes to communicate data more efficiently amongst
themselves with minimal kernel intervention.

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

There are two standard specifications for Shared memory.

SysV Shared memory.


POSIX Shared memory.

SEMAPHORES

Semaphores are meant for synchronizing access to multiple processes.

When one process wants to access the memory (for reading or writing), it needs to
be locked (or protected) and released when the access is removed.

This needs to be repeated by all the processes to secure data.

Semaphores are locking and synchronization mechanism used most widely when
processes share resources.

Linux supports both SysV semaphores and POSIX semaphores. POSIX


semaphores provide a more simpler and elegant implementation and thus is most
widely used when compared to SysV semaphores on Linux.

Signals
Signals are the cheapest forms of IPC provided by Linux

Their primary use is to notify processes of change in states or events that occur
within the kernel or other processes

Signal is a mechanism to communication between multiple processes by way of


signaling. This means a source process will send a signal (recognized by number)
and the destination process will handle it accordingly.

We use signals in real world to convey messages with least overhead - think of
hand and body gestures.

On Linux, the kernel notifies a process when an event or state change occurs by
interrupting the process's normal flow of execution and invoking one of the signal
handler functions registered by the process or

by the invoking one of the default signal dispositions supplied by the kernel, for
the said event.

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

Synchronization in Linux

Process Synchronization means sharing system resources by processes in a such a way that,
Concurrent access to shared data is handled thereby minimizing the chance of inconsistent data.
Maintaining data consistency demands mechanisms to ensure synchronized execution of
cooperating processes.

Process Synchronization was introduced to handle problems that arose while multiple process
executions. Some of the problems are discussed below.

Critical Section Problem


A Critical Section is a code segment that accesses shared variables and has to be executed as an
atomic action. It means that in a group of cooperating processes, at a given point of time, only
one process must be executing its critical section. If any other process also wants to execute its
critical section, it must wait until the first one finishes.

Solution to Critical Section Problem


A solution to the critical section problem must satisfy the following three conditions:

1. Mutual Exclusion
Out of a group of cooperating processes, only one process can be in its critical section at a given
point of time.

2. Progress
If no process is in its critical section, and if one or more threads want to execute their critical
section then any one of these threads must be allowed to get into its critical section.

3. Bounded Waiting
After a process makes a request for getting into its critical section, there is a limit for how many
other processes can get into their critical section, before this process's request is granted. So after
the limit is reached, system must grant the process permission to get into its critical section.

Interrupt in Linux
Interrupts

An event external to the currently executing process that causes a change in the normal flow of
instruction execution; usually generated by hardware devices external to the CPU

• Asynchronous w.r.t current process


• External & internal devices need CPU service
• CPU must detect devices that require attention!

Interrupt types

• Hardware: An event/electronic signal from external device that needs CPU attention
– Mouse moved, keyboard pressed

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

– Printer ready, modem, etc


• Software:
– exceptions (traps) in the processor: divide by zero exception, page faults, etc.
– special software interrupt instructions (e.g., request disk reads/writes to disk controller)

Exceptions in Linux
Exceptions and Interrupts *

Exceptions are often discussed at the same time as interrupts. Unlike interrupts, exceptions occur
synchronously with respect to the processor clock; they are often called synchronous interrupts.
Exceptions are produced by the processor while executing instructions either in response to a
programming error (e.g. divide by zero) or abnormal conditions that must be handled by the
kernel (e.g. a page fault). Because many processor architectures handle exceptions in a similar
manner to interrupts, the kernel infrastructure for handling the two is similar.

Simple definitions of the two:

Interrupts: asynchronous interrupts generated by hardware.

Exceptions: synchronous interrupts generated by the processor.

System calls (one type of exception) on the x86 architecture are implemented by the issuance of
a software interrupt, which traps into the kernel and causes execution of a special system call
handler. Interrupts work in a similar way, except hardware (not software) issues interrupts.

System Call in Linux


System calls can be roughly grouped into five major categories:

Process Control

load

execute

end, abort

create process (for example, fork on Unix-like systems,)

terminate process

get/set process attributes

wait for time, wait event, signal event

allocate, free memory

File Management

create file, delete file

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

open, close

read, write, reposition

get/set file attributes

Device Management

request device, release device

read, write, reposition

get/set device attributes

logically attach or detach devices

Information Maintenance

get/set time or date

get/set system data

get/set process, file, or device attributes

Communication

create, delete communication connection

send, receive messages

transfer status information

attach or detach remote devices

******************************************************************************

What is Android?

Android is a software package and linux based operating system for mobile devices such as
tablet computers and smart phones.

It is developed by Google and later the OHA (Open Handset Alliance). Java language is mainly
used to write the android code even though other languages can be used.

There are many code names of android such as Lollipop, Kitkat, Jelly Bean, Ice cream
Sandwich, Froyo, Ecliar, Donut etc which is covered in next page.

Android offers a unified approach to application development for mobile devices which means
developers need only develop for Android, and their applications should be able to run on
different devices powered by Android.

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

Features of Android

After learning what is android, The important features of android are given below:

1) It is open-source.

2) Anyone can customize the Android Platform.

3) There are a lot of mobile applications that can be chosen by the consumer.

4) It provides many interesting features like weather details, opening screen, live RSS (Really
Simple Syndication) feeds etc.

Android Software Platform:

Android Architecture
Android is an open source, Linux-based software stack created for a wide array of devices and
form factors.

Android operating system is a stack of software components which is roughly divided into five
sections and four main layers as shown below in the architecture diagram.

the Linux Kernel provides a level of abstraction between the device hardware and the upper
layers of the Android software stack.

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

Linux Kernel
It is the heart of android architecture that exists at the root of android architecture. Linux kernel
is responsible for device drivers, power management, memory management, device management
and resource access.

This layer is the foundation of the Android Platform.

 Contains all low level drivers for various hardware components support.
 Android Runtime relies on Linux Kernel for core system services like,
o Memory, process management, threading etc.
o Network stack
o Driver model
o Security and more.

Hardware Abstraction Layer (HAL)

Provides Abstraction between hardware and rest of the software stack.


Android Runtime (ART)
Designed to run apps in a constrained environment that has limited muscle power in terms of
battery, processing and memory.

ART uses DEX files, which is a type of byte code, specially designed for Android, which helps
ART to manage memory more efficiently.

Prior to Android 5.0, Dalvik was used as Android runtime.

ART is capable of both Ahead-of-time (AOT) and Just-in-time (JIT) compilation.

It also has a very efficient garbage collection.

Libraries
Exposed to developers through Android Application Framework.Contains C/C++ libraries used
by components of Android Systems.

Few features include

SQLite Library used for data storage and light in terms of mobile memory footprints and task
execution.

WebKit Library mainly provides Web Browsing engine and a lot more related features.

The surface manager library is responsible for rendering windows and drawing surfaces of
various apps on the screen.

The media framework library provides media code for audio and video.

The OpenGl (Open Graphics Library) and SGL(Scalable Graphics Library) are the graphics
libraries for 3D and 2D rendering, respectively.

Application Framework

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

It is a collection of APIs written in Java, which gives developers access to the complete feature
set of Android OS.

Developers have full access to the same framework APIs used by the core applications, so that
they can enhance more in terms of functionalities of their application.

Enables and simplify the reuse of core components and services, like:

Activity Manager: Manages the Lifecycle of apps & provide common navigation back stack.

Window Manager: Manages windows and drawing surfaces, and is an abstraction of the surface
manager library.

Content Providers: Enables application to access data from other applications or to share their
own data i.e it provides mechanism to exchange data among apps.

View System: Contains User Interface building blocks used to build an application's UI,
including lists, grids, texts, boxes, buttons,etc. and also performs the event management of UI
elements(explained in later tutorials).

Package Manager: Manages various kinds of information related to the application packages
that are currently installed on the device.

Telephony Manager: Enables app to use phone capabilities of the device.

Resource Manager: Provides access to non-code resources (localized Strings, bitmaps,


Graphics and Layouts).

Location Manager: Deals with location awareness capabilities.

Notification Manager: Enable apps to display custom alerts in the status bar.

Android Runtime - Dalvik Virtual Machine


the Linux kernel provides a multitasking execution environment allowing multiple processes to
execute concurrently.

It would be easy to assume, therefore, that each Android application simply runs as a process
directly on the Linux kernel.

The Dalvik Virtual Machine (DVM) is an android virtual machine optimized for mobile
devices.

It optimizes the virtual machine for memory, battery life and performance.

The Dex compiler converts the class files into the .dex file that run on the Dalvik VM. Multiple
class files are converted into one dex file.

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

Dalvik virtual machine (VM).

Running applications in virtual machines provides a number of advantages.

1.applications are essentially sandboxed, in that they cannot detrimentally interfere (intentionally
or otherwise) with the operating system or other applications, nor can they directly access the
device hardware.

2. this enforced level of abstraction makes applications platform neutral in that they are never
tied to any specific hardware.

The Dalvik virtual machine was developed by Google and relies on the underlying Linux kernel
for low-level functionality.

It is more efficient than the standard Java VM in terms of memory usage, and specifically
designed to allow multiple instances to run efficiently within the resource constraints of a mobile
device.

In order to execute within a Dalvik VM, application code must be transformed from standard
Java class files to the Dalvik executable (.dex) format, which has a 50% smaller memory
footprint than standard Java bytecode.

Android Runtime – Core Libraries

The Android Core Libraries (also referred to as the Dalvik Libraries) fall into three main
categories, each of which merits an individual description:

Dalvik VM Specific Libraries

This is a set of libraries used predominantly for interacting directly with an instance of the
Dalvik VM and is unlikely to be used by most Android application developers.

Java Interoperability Libraries

Android applications are predominantly developed using the Java programming language.

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

The Standard Java development environment includes a vast array of classes that are contained
in the core Java runtime libraries.

These libraries provide support for tasks such as string handling, networking and file
manipulation (to name but a few) and are both familiar to, and widely used by Java developers
regardless of platform.

The Java Interoperability Libraries are an open source implementation (based on the Apache
Harmony project) of a subset of the Standard Java core libraries that have been adapted and
transformed for use by applications running within a Dalvik VM.

Android Operating System Services or Android Application Life Cycle

Services
 Services performs the action without user interaction in the background, but does not get
initiated without user invocation.
 It does not require a user interface.
 It is an android application component which runs in a background and has no visual UI.
 It is used to perform the processing part of your application in the background.
For example, music player application. When the music station is playing the song, the user
can open another application and the song plays in the background.

A service is a component that runs in the background to perform long-running operations without
needing to interact with the user and it works even if application is destroyed.

A service can essentially take two states −


 Started
 Bound

Started
A service is started when an application component, such as an activity, starts it by calling
startService().

A service is started when component (like activity) calls startService() method, now it runs in the
background indefinitely. It is stopped by stopService() method.

The service can stop itself by calling the stopSelf() method.

Once started, a service can run in the background indefinitely, even if the component that started
it is destroyed.

Bound
A service is bound when an application component binds to it by calling bind Service(). A
bound service offers a client server interface that allows components to interact with the service,
send requests, get results, and even do so across processes with inter process communication
(IPC).

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

A service is bound when another component (e.g. client) calls bindService() method. The client
can unbind the service by calling the unbindService() method.

A service has life cycle call back methods that can implement to monitor changes in the
service's state and can perform work at the appropriate stage.

The following diagram on the left shows the life cycle when the service is created with
startService() and the diagram on the right shows the life cycle when the service is created with
bindService():

To create an service, create a Java class that extends the Service base class or one of its
existing subclasses.

onStartCommand()

The system calls this method when another component, such as an activity, requests that the
service be started, by calling startService().

If you implement this method, it is your responsibility to stop the service when its work is done,
by calling stopSelf() or stopService() methods.

onBind()

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

The system calls this method when another component wants to bind with the service by calling
bindService().

onUnbind()

The system calls this method when all clients have disconnected from a particular interface
published by the service.

onRebind()

The system calls this method when new clients have connected to the service, after it had
previously been notified that all had disconnected in its onUnbind(Intent).

onCreate()

The system calls this method when the service is first created using onStartCommand() or
onBind(). This call is required to perform one-time set-up.

onDestroy()

The system calls this method when the service is no longer used and is being destroyed. service
should implement this to clean up any resources such as threads, registered listeners, receivers,
etc.

Android system services

Functionality exposed by application framework APIs communicates with system services to


access the underlying hardware. There are two groups of services that application developers
may interact mostly with. They are system (services such as window manager and notification
manager) and media (services involved in playing and recording media). These are the services
that provide application interfaces as part of the Android framework.

Besides these services, there are also native services supporting these system services, such as
SurfaceFlinger, netd, logcatd, rild, and so on. Many of them are very similar to Linux daemons
that you may find in a Linux distribution. In a complicated hardware module, such as graphic, ...

Structure of Android Application


Developing an Android project, you have to install the Android plug-ins for Eclipse and at least
have some knowledge of Java programming.

After installing all the plug-ins for an Android file, you can begin to develop an Android
application.

Android uses packages not only to arrange the code in an application but to manage the
application themselves.

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

The above diagram shows the basic building blocks of an Android application. Android
application in Eclipse or in any development tool have a pre-defined structure with code and
resource organized into a number of folders.

Every Android project contains several folders, like:

Folder Name Description

src: The 'src' stands for Source Code. It contains the Java Source files.

gen: The 'gen' stands for Generated Java Library. This library is for Android internal use only.

Android 2.2: The Android Framework Library is stored here

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

assets: It is used to store raw asset files.

libs: It contains private libraries.

res:
The 'res' stands for Resource file. It can store resource files such as pictures, XML files, etc. It
contains some additional folders such as Drawable, Layout and Values.

anim: It is used for XML files that are compiled into animation objects.
color: It is used for XML files that describe colors.
drawable: It is used to store various graphics files. In Android project structure,

there are three types of drawable folders,

1.drawable-mdpi
2.drawable-hdpi
3.drawable-ldpi

The above drawable folders are required in order to adapt to different screen resolutions.

layout: It is used for placing the XML layout files, which defines how various Android objects
such as textbox, buttons, etc. are organized on the screen.

menu: It is used for defining the XML files in the application menu.

raw: The 'raw' stands for Raw Asset Files. These files are referenced from the application using
a resource identifier in the R class.
For example, good place for media is MP3 or Ogg files.

values: It is used for XML files which stores various string values, such as titles,labels,etc.

xml: It is used for configuring the application components.

AndroidManifest.xml: This file indicates the Android definition file. This file contains the
information about the Android application such as minimum Android version, permission to
access Android device capabilities such as Internet access permission, phone permission etc.

default.properties: This file contains the project settings, such as build the target. Do not edit
this file manually. It should be maintained in a Source Revision Control System.

Proguard.cfg: This file defines how ProGuard optimizes and makes your code unclear.

MainLayout.xml: This file describes the layout of the page. So all the components such as
textboxes, labels, radio buttons, etc. are displaying on the application screen.

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

Activity class: The application occupies the entire device screen which needs at least one class
inherits from the Activity class. OnCreate() method initiates the application and loads the layout
page.

Application Process management

Processes and threads

When an application component starts and the application does not have any other components
running, the Android system starts a new Linux process for the application with a single thread
of execution.

By default, all components of the same application run in the same process and thread (called the
"main" thread).

If an application component starts and there already exists a process for that application (because
another component from the application exists), then the component is started within that process
and uses the same thread of execution.

However, you can arrange for different components in your application to run in separate
processes, and you can create additional threads for any process.

This document discusses how processes and threads work in an Android application.

Processes
By default, all components of the same application run in the same process and most applications
should not change this. However, if you find that you need to control which process a certain
component belongs to, you can do so in the manifest file.

The manifest entry for each type of component element—<activity>, <service>, <receiver>, and
<provider>—supports an android: process attribute that can specify a process in which that
component should run.

process so that components of different applications run in the same process—provided that the
applications share the same Linux user ID and are signed with the same certificates.

The <application> element also supports an android: process attribute, to set a default value that
applies to all components.

Android might decide to shut down a process at some point, when memory is low and required
by other processes that are more immediately serving the user.

Application components running in the process that's killed are consequently destroyed.

A process is started again for those components when there's again work for them to do.

Prepared by D MADHUBABU
UNIT-VI Linux System & Android Software Platform

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.

The details of the process lifecycle and its relationship to application states are discussed in
Processes and Application Lifecycle.

Threads
When an application is launched, the system creates a thread of execution for the application,
called "main." This thread is very important because it is in charge of dispatching events to the
appropriate user interface widgets, including drawing events.

It is also almost always the thread in which your application interacts with components from the
Android UI toolkit (components from the android.widget and android.view packages).

As such, the main thread is also sometimes called the UI thread. under special circumstances, an
app's main thread might not be its UI thread;

The system does not create a separate thread for each instance of a component.

All components that run in the same process are instantiated in the UI thread, and system calls to
each component are dispatched from that thread.

Consequently, methods that respond to system callbacks (such as onKeyDown() to report user
actions or a lifecycle callback method) always run in the UI thread of the process.

For instance, when the user touches a button on the screen, your app's UI thread dispatches the
touch event to the widget, which in turn sets its pressed state and posts an invalidate request to
the event queue.

******************************************************************************

Prepared by D MADHUBABU

You might also like