Operating Systems (R16 Iii B.Tech I Sem) Unit - VI
Operating Systems (R16 Iii B.Tech I Sem) Unit - VI
The figure represents various components that makeup a full UNIX system. The most
important thing is kernel mode.
Kernel component code executes in a special privileged mode caused 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. Kernel runs
1
OPERATING SYSTEMS (R16 III B.TECH I SEM) UNIT -
VI
each process and provides system services to processes, provides protected access to hardware to
processes.
Any operating system support code that does not need to run in kernel mode is placed in
system libraries than that code will be run in user mode. User mode has limited access to all the
resources of Linux system.
Inter Process Communication: Linux provides rich environment for process to communicate
with each other. Linux provide the following number of inter process communication mechanisms.
1. 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.
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 functinos
registered by the process or by the invoking one of the default signal dispositions supplied by
the kernel, for the said event.
2. Pipes: Pipe is a standard inter process communication mechanism which is used to transfer
data among processes. Pipe mechanism allows a child process to inherit a communication
channel from its parent. Data written to one end of the pipe can be read at the other end of the
pipe. Pipes are divided into two types.
Anonymous pipes: Anonymous pipes can be setup and used only between processes
that share parent-child relationship. 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.
Named Pipes: Named pipes (or FIFO) are variants of pipe that allow communication
between processes that are not related to each other. 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. The FIFO file on disk acts as the contract between the two processes that
wish to communicate.
3. 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. The advantage of message
queues over pipes/FIFOs is 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.
4. Shared Memory: As the name implies, this IPC mechanism allows one process to share a
region of memory in its address space with another. This allows two or more processes to
communicate data more efficiently amongst themselves with minimal kernel intervention.
5. Semaphores: 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 simpler and elegant implementation and thus are
most widely used when compared to SysV semaphores on Linux.
2
OPERATING SYSTEMS (R16 III B.TECH I SEM) UNIT -
VI
Synchronization: Linux provides many of the synchronization or locking mechanisms that are
available in the Linux kernel. Synchronization methods are necessary when the property of
concurrency exists. The following are some of the problems occurred in concurrency.
1. Critical Section: A Critical Section is a piece of code which should be executed under
mutual exclusion. That means when one process is executing in Critical section then no other
should enter into that region. If the process tries to enter then the problem occurs.
2. Race Condition: One of the main problems in synchronization is the race condition. This
condition occurs when two or more threads manipulate a shared data item and the result
depends upon the timing of the execution.
3. Deadlock: Another problem that occurs in operating system is the Deadlock. Deadlock is
occurred due to the improper usage of synchronization mechanism. Deadlock refers to a
specific condition when two or more processes are each waiting for another to release a
resource, in this situation deadlock may occur. The following synchronization mechanisms
are used to avoid the above problem.
Atomic Operation: Atomic operations are simple like add and subtract which perform
in one cycle. The atomic integer methods operate on a special data type atomic_t. A
common use of atomic integer operations is to implement counters which are updated
by multiple threads. Linux provides two sets of interfaces for atomic operations. One
that operates on integers and another that operates on individual bits. Some of atomic
methods are
Spin-locks are very much suitable to use in interrupts context because it does
not put process/thread in sleep. In the uni-processor system if the kernel acquires a
spin-lock, it would disable preemption first. If the kernel releases the lock, it would
enable preemption. There are two different types of spin-locks: Basic Spin Lock and
Reader Writer Spin Lock.
Sequence Lock: Sequence Lock maintains a counter for sequence of operations.
Sequence lock contains many readers and few writers. When the shared data is
written, a lock is obtained and sequence counter is incremented by 1.
The write operation makes the sequence counter to odd and when releasing it
makes the counter value even. In case of reading, sequence counter is read before and
after reading the data. If the values are same this indicates that a write did not begin in
the middle of the read. In addition to that if the values are even a write operation is
not going on. Sequence lock gives the higher priority to writers compared to readers.
4. Kernel Mutexes: Mutexes are available in kernel as a way to accomplish semaphore
behavior. The kernel mutex is available on top of the atomic API’s which is not visible to the
user. Mutex can hold only one task at a time and only this task can unlock the mutex. There
is no recursive locking or unlocking mechanism. Mutexes are more fast offer when
3
OPERATING SYSTEMS (R16 III B.TECH I SEM) UNIT -
VI
compared with semaphores. Mutexes provide five types of functions. Three are used for
locking and one for unlocking and one for testing. They are
mutex_trylock //It locks immediately
mutex_lock //It locks
mutex_lock_interuptable //lock purpose
mutex_unlock //to unlock
mutex_is_locked //for testing
Interrupt: An interrupt is an event that alters the normal flow of execution of a program and that
can be generated by hardware devices or even by the CPU itself. Interrupts are mainly divided into
two types.
1. Synchronous Interrupts
2. Asynchronous Interrupts
1. Synchronous Interrupts: Synchronous Interrupts can also be called as exceptions. These
interrupts are produced by the CPU while processing instructions. Eg: a Number divided by
zero.
2. Asynchronous Interrupts: Asynchronous Interrupts can also be called as Interrupt. These are
issued by the hardware devices or generated by an external event. Eg: Interrupt generated by
I/O device.
When an interrupt is occurred the CPU must stop the current instruction execution and
execute the newly arrived interrupts. When the interrupt occur then the CPU has to save the
current process state and resumes the process state after interrupt is handled.
Handling Interrupts: Interrupts can occur any time, and then the kernel tries to get it out of the way
as soon as possible when interrupt occurred. An interrupt can be interrupted by another interrupt.
Based on the interrupt requests, these interrupt request can be categorized into two types.
Maskable interrupts: Maskable interrupts are issued by the input-output devices. These
maskable interrupts can be in two states: masked and unmasked. Masked interrupts can be
ignored and will not create any problem where unmasked interrupts can be getting processed
by kernel.
Non maskable interrupts: These non maskable interrupts occurs because of critical
malfunctions like hardware failures. These types if interrupts are always processed by CPU.
Hardware programmable Interrupt Controller: Every hardware device has it's own Interrupt
Request (IRQ) line. The IRQs are numbered starting from 0. All IRQ lines are connected to a
Programmable Interrupt Controller (PIC). The PIC listens on IRQs and assigns them to the CPU. It
is also possible to disable a specific IRQ line.
Modern multiprocessing Linux systems generally include the newer Advanced PIC (APIC),
which distributes IRQ requests equally between the CPUs. The mid-step between an interrupt or
exception and the handling of it is the Interrupt Descriptor Table (IDT). This table associates each
interrupt or exception vector (a number) with a specified handler (f.e. Divide error gets handled by
the function divide_error ()).
4
OPERATING SYSTEMS (R16 III B.TECH I SEM) UNIT -
VI
A PIC usually has a set of ports used to exchange information with the CPU. When a device
connected to one of the PIC’s IRQ lines needs CPU attention the following flow happens:
When an interrupt occurs as a request from PIC to CPU then a common interrupt handler code is
executed. The common interrupt handler first saves the context of running process. This includes
Instruction Pointer, Stack pointer, and other process registers. This context is usually saved in stack.
Then context is changed to interrupt for its processing.
Exception and System call: An exception is nothing but an error. An exception can also be
called as a synchronous interrupts. These interrupts are produced by the CPU while processing
instructions. Exceptions are caused by the programming errors like divide by zero, page fault,
overflow etc…That must be handled by the kernel. There are two types of exceptions.
1. Processor detected exceptions: These type of exceptions are generated by the CPU while
detecting a anomalous conditions. These are divided into three groups.
Faults: These types of exceptions are generally corrected, once exception is corrected
then the program is allowed to restart with no loss of continuity.
Traps: Traps reported immediately following the executions of the trapping
instruction. After the trap is handled the kernel immediately returns controller to the
continuity of the program. The main use of trap is for debugging purpose.
Abort: A serious error occurred; the control unit is in trouble and is unable to store in
the register the precise location of the instruction causing the exception. Aborts are
used to report severe errors, such as hardware failures or inconsistent values.
5
OPERATING SYSTEMS (R16 III B.TECH I SEM) UNIT -
VI
o Divided by zero
o Debug
o Not used error
o Overflow
o Invalid opcode etc…
Introduction to Android: Android is a modified Linux based mobile operating system that was
originally started by android inc with the same name in 2005. Android was took over by Google and
made it as open source and free.
Features of Android: The following are some of the features of the android.
Storage: It uses SQLite, a light weight relational database for data storage.
Media Support: Android include support for large number of media formats for audio,
images as well as video like H.264, MPEG-4, AMR, AAC, AVI, MP3, MP4, JPEG, PNG,
GIF.
Messaging: Android supports both SMS and MMS.
Web Browser: It supports a web browser.
Connectivity: It supports large group of networks like Bluetooth, Wi-Fi, LTE, Wimax.
Multi-Touch: It supports multi touch screen.
Multi-Task: It supports Multi tasking applications.
Flash-support: It supports flash.
Hardware support: It supports camera, audio recording and video recording etc…
6
OPERATING SYSTEMS (R16 III B.TECH I SEM) UNIT -
VI
1. Linux Kernel: At the bottom of the layers is Linux - Linux 3.6 with approximately 115
patches. This provides a level of abstraction between the device hardware and it contains all
the essential hardware drivers like camera, keypad, display etc. Also, the kernel handles all
the things that Linux is really good at such as networking and a vast array of device drivers,
which take the pain out of interfacing to peripheral hardware.
2. Android Libraries: This category contains Java-based libraries that are specific to Android
development. Examples of libraries in this category include the application framework
libraries in addition to those that facilitate user interface building, graphics drawing and
database access. A summary of some key core Android libraries available to the Android
developer is as follows −
android.app: Provides access to the application model and is the cornerstone
of all Android applications.
android.content: Facilitates content access, publishing and messaging
between applications and application components.
android.database: Used to access data published by content providers and
includes SQLite database management classes.
android.opengl: A Java interface to the OpenGL ES 3D graphics rendering
API.
android.os: Provides applications with access to standard operating system
services including messages, system services and inter-process
communication.
android.text: Used to render and manipulate text on a device display.
7
OPERATING SYSTEMS (R16 III B.TECH I SEM) UNIT -
VI
android.view: The fundamental building blocks of application user
interfaces.
android.widget: A rich collection of pre-built user interface components
such as buttons, labels, list views, layout managers, radio buttons etc.
android.webkit: A set of classes intended to allow web-browsing capabilities
to be built into applications.
3. Android Runtime: This is the third section of the architecture and available on the second
layer from the bottom. This section provides a key component called Dalvik Virtual
Machine which is a kind of Java Virtual Machine specially designed and optimized for
Android.
The Dalvik VM makes use of Linux core features like memory management and
multi-threading, which is intrinsic in the Java language. The Dalvik VM enables every
Android application to run in its own process, with its own instance of the Dalvik virtual
machine.
The Android runtime also provides a set of core libraries which enable Android
application developers to write Android applications using standard Java programming
language.
8
OPERATING SYSTEMS (R16 III B.TECH I SEM) UNIT -
VI
Foreground services must display a Notification. Foreground services continue running even
when the user isn't interacting with the app.
2. Background: A background service performs an operation that isn't directly noticed by the
user. For example, if an app used a service to compact its storage, that would usually be a
background service.
3. Bound: A bound service offers a client-server interface that allows components to interact
with the service, send requests, receive results, and even do so across processes with
interprocess communication (IPC). A bound service runs only as long as another application
component is bound to it. Multiple components can bind to the service at once, but when all
of them unbind, the service is destroyed.
A service can essentially take two states:
Started : A service is started when an application component, such as an activity, starts it by
calling startService(). 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
bindService(). 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
interprocess communication (IPC).
A service has life cycle callback methods that you can implement to monitor changes in the
service's state and you 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. The Service base class defines various callback methods and the most important are
given below.
9
OPERATING SYSTEMS (R16 III B.TECH I SEM) UNIT -
VI
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(): The system calls this method when another component wants to bind with the
service by calling bindService(). If you implement this method, you must provide an
interface that clients use to communicate with the service, by returning an IBinder object.
You must always implement this method, but if you don't want to allow binding, then you
should return null.
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. Your service should implement this to clean up any resources such as threads,
registered listeners, receivers, etc.
Android Runtime Application Development: Runtime is used by the operating system
which takes care of converting high level language into machine level language done by assemblers,
compilers and interpreters. Android runtime is an application runtime environment used by the
android operating system. Android runtime consists of two impartment software components. They
are
1. Core libraries: This component contains Java-based libraries that are specific to Android
development. Some key core Android libraries available to the Android developer are
android.app, android.content, android.database, android.opengl etc…
2. Dalvik Virtual Machine (DVM): In android to develop applications user writes code in java
language which is converted into Dex (Dalvik executable Code) byte code. This Dex byte
code format is translated to native archive code via android runtime or Dalvik runtime. The
Dex code is independent of device architecture.
Here DVM just acts as Java virtual Machine. DVM is a JIT (Just In Time)
compilation based engine. The following diagram represents the Java virtual machine and
Dalvik virtual machine.
With the Dalvik JIT compiler, each time when the app is run, it dynamically translates
a part of the Dalvik byte code into machine code. As the execution progresses, more byte
code is compiled and cached. Since JIT compiles only a part of the code, it has a smaller
memory footprint and uses less physical space on the device.
ART is equipped with an Ahead-of-Time compiler. During the app’s installation
phase, it statically translates the DEX byte code into machine code and stores in the device’s
10
OPERATING SYSTEMS (R16 III B.TECH I SEM) UNIT -
VI
storage. This is a one-time event which happens when the app is installed on the device. With
no need for JIT compilation, the code executes much faster.
11
OPERATING SYSTEMS (R16 III B.TECH I SEM) UNIT -
VI
of its project hierarchy. The manifest file is an important part of our app because it defines
the structure and metadata of our application, its components, and its requirements.
2. Java folder: The Java folder contains the Java source code files. These files are used as a
controller for controlled UI (Layout file). It gets the data from the Layout file and after
processing that data output will be shown in the UI layout. It works on the backend of an
Android application.
3. res folder: res stands for resources of the application. It contains icons, images etc…related
to our application project. res directory contains different types of resource folders.
drawable: This folder contains xml, png, jpg, and jpeg files. In this folder we can
store images, which are used in our application and in other xml files.
layout: A layout defines the visual structure for a user interface, such as the UI for an
Android application. This folder stores Layout files that are written in XML
language. You can add additional layout objects or widgets as child elements to
gradually build a View hierarchy that defines your layout file.
Values: This folder contains default files like strings.xml, dimens.xml, colors.xml,
and styles.xml. we can define title of the screen and any other tags with strings.xml.
We can also define height and width of your application by dimen.xml. We can also
specify different styles with styles.xml.
4. build folder: This folder contains R.java file which is auto generated file. This file indexes
all the resources of android application project like layout, strings et…this folder contains
applications debug and release apk files.
5. libs folder: To use any external files copy and paste their archive files into the libs folder and
then you can directly use it in our java code libs.
6. gradle: Gradle is an automated build toolkit that allows the way in which projects are built to
be configured and managed through a set of build configuration files. This defines how a
project to be built, what dependencies need to be fulfilled for the project to build
successfully and the end result.
7. Activity class: This application occupies the entire device screen which needs at least one
class inherits from the activity class. This method initiates the application and builds the
layout page.
Application Process management: Every Android application runs in its own Linux process.
This process is created for the application when some of its code needs to be run, and will remain
running until it is no longer needed and the system needs to reclaim its memory for use by other
applications. A fundamental feature of Android is that an application process's lifetime is not
directly controlled by the application itself.
Android is managing all the apps and processes running on phone. For each application
android creates a new process with single thread of execution. In low memory situations a process is
decided to kill based on the number of inactive components of that process. A process which has
more number of inactive components will be killed first. There are five different processes available
for applications. They are described below
1. Foreground process: The app you were using is considered as the foreground process.
A process is considered to be in foreground if any of the following conditions hold.
12
OPERATING SYSTEMS (R16 III B.TECH I SEM) UNIT -
VI
It is running an activity at the top of the screen.
It has a broadcast receiver that is currently running that an event has occurred
or not.
2. Visible Process: A visible process is not in the foreground, but is still affecting what
you see on the screen. For example, the foreground process may be a dialog that
allows you to see an app. The app visible in the background would be a visible
process. Here activities will not interact with the user like pause, downloading etc…
3. Service process: The service process is doing something in the background to the
user such as playing music or downloading data in background. These services are
handled by service process.
4. Background process: Background processes are not currently visible to the user.
They have no impact on experience of using phone. At any given time many
background processes are running, you can think of these apps as paused apps.
5. Empty process: An empty process that does not contain any app data anymore. It
may be kept around for caching purposes to speed up app launches or the system may
kill it as necessary.
13