0% found this document useful (0 votes)
163 views10 pages

Resources Allocation

The main job of an operating system is to allocate and manage system resources such as CPU time, memory, and I/O devices. The Linux kernel provides basic functions for resource scheduling, memory management, and process management. It serves as the foundation for the Android operating system. The Android kernel uses a combination of priority-based and time-sharing scheduling algorithms to allocate resources to processes. Context switching allows a CPU to share resources by storing the state of a process so it can resume execution later.

Uploaded by

Tedla Melekot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views10 pages

Resources Allocation

The main job of an operating system is to allocate and manage system resources such as CPU time, memory, and I/O devices. The Linux kernel provides basic functions for resource scheduling, memory management, and process management. It serves as the foundation for the Android operating system. The Android kernel uses a combination of priority-based and time-sharing scheduling algorithms to allocate resources to processes. Context switching allows a CPU to share resources by storing the state of a process so it can resume execution later.

Uploaded by

Tedla Melekot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Resources allocation

The main job of an operating system is to allocate and manage the resource.resources allocation refers
to the process of assigning system resources such as CPU time ,memory, and I/O devices to different
tasks or processes running on operating system

Linux Kernel
The innermost layer of software that interacts with the device hardware is kernel. Kernel
provides the basic architectural model for resource scheduling, memory management, process
management, network management, and security. Being the first layer of software, Linux kernel
is the foundation of the Android platform. It allows Android to take advantage of major OS
functions and security features of a well-known kernel. It also helps device manufacturers to
develop hardware drivers more easily in a well-known environment.

ndroid Software Stack


The kernel uses a combination of priority based and time sharing scheduling algorithm to allocate
resources to processes.

ome functions of the kernel are:

 A kernel may access a variety of computer resources such as the CPU, I/O devices, and
other resources. It serves as a link between the user and the system's resources.
 Resource Management: It is the responsibility of a kernel to share resources across
multiple processes in such a way that each process has uniform access to the resources.
 Memory Management: Every process necessitates a certain amount of memory. As a
result, memory must be allocated and released for it to run. A kernel is in charge of
memory management.
 Device Management: The procedures take advantage of the peripheral devices linked to
the system. As a result, the kernel oversees device allocation.
 the Linux kernel is the most significant component of a Linux operating system, since it
serves as the primary interface between a computer's hardware and its functions (OS). It
connects the two, allowing for the most efficient use of resources. Like a seed inside a
hard shell, it resides within the OS and conducts all the hardware's critical tasks, whether
it's a phone, laptop, server, or any other sort of computer.
 The kernel is responsible for low-level operations such as disk management, memory
management, task management, and so on. It connects the user to the hardware
components of the system. When a process requests anything from the kernel, this is
called a System Call.

To make the Linux kernel more suited for smartphones, Android adds several additional capabilities to it.
Low Memory Killer, a process that checks memory status and reacts to high memory demands by killing
the least critical programs while keeping the system functioning, and wake locks, a means for apps to
inform the kernel that the device needs to stay on, are two examples.

https://emteria.com/learn/linux-kernel

the android operating system also uses a memory management system called the dalvik virtual machine
(DVM).the DVM allocates memory to different applications and manages garbage collection,which
frees up memory that is no longer needed android also uses a file system called YAFFS , which manages
the allocation and deletion of files on the devices

What is Dalvik Virtual Machine?

The Dalvik Virtual Machine (DVM) is a virtual machine that runs on the Android operating
system. It is designed to run multiple virtual machines on a single device, allowing for efficient
use of resources. The DVM is based on the CPU instruction set of the ARM architecture, and is
optimized for low-power devices. It is also designed to be memory-efficient, allowing for more
applications to run on a single device.

The DVM has a number of advantages over other virtual machines. It is designed to be efficient, and is
optimized for low-power devices. It is also designed to be memory-efficient, allowing for more
applications to run on a single device.

Android Dalvik Virtual Machine got its name after the name of Dalvik, which is the name of a
place in Iceland. Dan Bornstein developed it. Android Dalvik Virtual Machine is very similar to
Java Virtual Machine(JVM).

JVM itself is widely used and provides quite a high performance and better memory
management. However, JVM is not compatible with working optimally with low-powered
handheld devices like smartphones, tablets, smartwatches, etc.

Due to this reason, the Android Dalvik Virtual Machine came into existence. A virtual machine
is a software representation of an actual physical device. It implies that you don’t have a physical
device but have a model in your system. The representation works quite similarly to how an
actual physical device would work.
Dalvik Virtual Machine is configured to work on low-powered devices optimally. Dalvik Virtual
Machine provides high-performance features, better memory management, and battery life for
low-powered handheld devices. It was developed exclusively for android devices and allowed
several apps to execute on the virtual machine.

Context switching

Context Switching involves storing the context or state of a process so that it can be reloaded when
required and execution can be resumed from the same point as earlier. This is a feature of a multitasking
operating system and allows a single CPU to be shared by multiple processes.

Context Switching Triggers


There are three major triggers for context switching. These are given as follows −

 Multitasking: In a multitasking environment, a process is switched out of the CPU so


another process can be run. The state of the old process is saved and the state of the new
process is loaded. On a pre-emptive system, processes may be switched out by the
scheduler.
 Interrupt Handling: The hardware switches a part of the context when an interrupt
occurs. This happens automatically. Only some of the context is changed to minimize the
time required to handle the interrupt.
 User and Kernel Mode Switching: A context switch may take place when a transition
between the user mode and kernel mode is required in the operating system.

Context Switching Steps


The steps involved in context switching are as follows −

 Save the context of the process that is currently running on the CPU. Update the process
control block and other important fields.
 Move the process control block of the above process into the relevant queue such as the
ready queue, I/O queue etc.
 Select a new process for execution.
 Update the process control block of the selected process. This includes updating the
process state to running.
 Update the memory management data structures as required.
 Restore the context of the process that was previously running when it is loaded again on
the processor. This is done by loading the previous values of the process control block
and registers.

Tutorial point

Context Switching in Operating System


Context Switching in operating systems allows a single CPU to be shared by multiple processes.
It helps in storing each process’s task status so they can be resumed back from the state where
they were left. 

In simpler words, it is a process that involves switching between processes by a CPU and
returning to those functions with the same state.

How is Context Switching Performed?

Context Switching in Operating System


Context Switching in operating systems allows a single CPU to be shared by multiple
processes. It helps in storing each process’s task status so they can be resumed back from
the state where they were left. 

In simpler words, it is a process that involves switching between processes by a CPU and
returning to those functions with the same state.

How is Context Switching Performed?

Each time a process is removed from access to the processor, sufficient information
on its current operating state must be stored such that when it is again scheduled to
run on the processor it can resume its operation from an identical position. This
operational state data is known as its context and the act of removing the process's
thread of execution from the processor (and replacing it with another) is known as a
process switch or context switch.

 The distinction is made here between the condition where a process is removed from
the processor completely and is replaced by another process (a process switch) and the
case where the process state is stored while execution is interrupted temporarily (a
context switch). Note that a process switch performs a superset of the operations
required for the context switch. The latter case may be when an external interrupt is
serviced or when a system call necessitates a switch from user mode to system mode.
In the case of the process switch much more information must be saved in order to
later restore the process context than in the second case where the process remains
resident in memory while it's thread of execution is interrupted.

 The context of a process includes its address space, stack space, virtual address space,
register set image (e.g. Program Counter (PC), Stack Pointer (SP), Instruction
Register (IR), Program Status Word (PSW) and other general processor registers),
updating profiling or accounting information, making a snapshot image of its
associated kernel data structures and updating the current state of the process (waiting,
ready, etc).

 This state information is saved in the process's process control block which is then
moved to the appropriate scheduling queue. The new process is moved to the CPU by
copying the PCB info into the appropriate locations (e.g. the program counter is
loaded with the address of the next instruction to execute).

‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
’’’’’’

Context Switching is the mechanism that allows multiple processes to use a single CPU. Context
Switching stores the status of the ongoing process so that the process can be reloaded from the
same point from where it was stopped.

Scope
 This article explains What is Context Switching, Steps involved in Context Switching,
Example of Context Switching, and the advantages and disadvantage of Context
Switching.
 This article does not cover any practical implementation of performing Context
Switiching.

What is Context Switching?


Alice and Bob have been working hard to learn the concepts of an operating system, and they are
here to learn context switching in an operating system.

Professor: Bob, can you explain what is multitasking system?

Bob: Yes, Professor, multiprocessing systems are the type of systems where multiple processes
can be executed even if the system contains only one CPU.

Professor: Very good, Bob! But have you ever wondered how multiple processes are executed
on the system having a single CPU?
Bob: No, but I am curious to learn that.

Alice: Yes, Professor!

Professor: The magic of executing multiple processes on a single CPU takes place with the help
of Context Switching.

When any process is utilizing CPU, and some other process requires the CPU, Context Switching
is carried out before passing the control of CPU from the old process to the new process.

Alice: Professor, what does context switching actually do, and why is Context Switching
necessary?

Professor: Let's have a look.

Context Switching stores the state of the current process, and the state includes the information
about the data that was stored in registers, the value of the program counter, and the stack
pointer. Context Switching is necessary because if we directly pass the control of CPU to the
new process without saving the state of the old process and later if we want to resume the old
process from where it was stopped, we won't be able to do that as we don't know what was the
last instruction the old process executed. Context Switching overcomes this problem by storing
the state of the process.

Alice: Oh! Now I understand why Context Switching is essential.

Bob: Yes, imagine if Context Switching is not there, and you are taking notes in a word file, and
each time you browse something on the web and switch back, you need to rewrite the entire
notes. Giggles

Professor: Haha! That's a nice way to put the concept. Now let's see in which situations the
context switch takes place.

Steps Involved in Context Switching


Bob: Professor, sorry to interrupt you, but what is PCB in the above image?

Professor: No worries, Bob. It's good you are asking the question.

PCB stands for Process Control Block. PCB is a special data structure that is used by the
operating system to store all the process-relevant data in it. PCB also stores the state of the
process, which depicts whether the process is ready, running, or waiting.

As we can see in the image, there are two processes, namely P1 and P2. P1 is being executed by
the CPU currently. If process P1 requires performing some I/O(Input-Output) task or if any
interrupt takes place, the state of the process needs to change, but before changing the state, the
Context Switching takes place.

Following are the steps which takes place while switching process P1 and process P2:

1. The data present in the register and program counter will be stored in PCB of process P1,
let's say PCB1, and change the state in PCB1 .
2. Process P1 will be moved to the appropriate queue, which can be either ready queue, I/O
queue, or waiting queue.
3. The next process will be picked from the ready queue, let's say P2.
4. The state of the process P2 will be changed to running state, and if P2 was the process
that was previously executed by CPU, it would resume the execution from where it was
stopped.
5. If we need to execute the process P1, we need to carry out the same tasks mentioned in
step 1 to step 4.

Alice: Professor, I understood the entire steps involved in context switching but can you provide
one example for simplicity?

Professor: Sure, Alice.

Context Switching Triggers


Following are the three main triggers for Context Switching:

1. Multitasking: In the multitasking environment, when one process is utilizing CPU, and
there is a need for CPU by another process, Context Switching triggers. Context
Switching saves the state of the old process and passes the control of the CPU to the new
process.
2. Interrupt Handling: When an interrupt takes place, the CPU needs to handle the
interrupt. So before handling the interrupt, the Context Switching gets triggered, which
saves the state of the process before handling the interrupt.
3. User and Kernel Mode Switching: The user mode is the normal mode in which the user
application can execute with limited access, whereas kernel mode is the mode in which
the process can carry out the system-level operations that are not available in the user
4. mode. So, whenever the switching takes from user mode to kernel mode, mode switching
triggers the Context Switching, which stores the state on an ongoing process.

Note: Interrupt is the event raised by hardware or software which needs immediate attention.

Professor: As we have seen when the Context Switching triggers, we can now look at the steps
involved in context switching.

Example of Context Switching:


Let's say there is a process present in the system which is under CPU execution, and the ready
queue is empty. Now, one new process is created and placed in the ready queue. The system has
the mechanism of allotting the CPU to the process with the highest priority number. So, when the
newly created process enters the ready queue, the priority number of the newly created process
will be compared with the priority number of the older process. Here the higher priority number
needs first attention than the lower priority number.

If the priority number of the new process is less than the older process, the execution of the older
process continues. But if the priority number of a new process is higher than the older process,
Context Switching will get triggered. During the context switch, the data of registers, program
counter, and state of the process is stored in the Process Control Block. The older process is
placed in the waiting queue, while the state of the newly created process will be changed to a
running state.

Once the execution of the newly created process is over, the older process present in the waiting
queue will be fetched, and it will resume the execution from where it was stopped.

Alice: Thank you, Professor. I clearly understood the working of context switching.

Professor: Now, we will learn about the advantages of context switching.

Advantage of Context Switching


 The main advantage of context switching is even if the system contains only one CPU, it
gives the user an illusion that the system has multiple CPUs due to which multiple
processes are being executed. The context switching is so fast that the user won't even
realize that the processes are switched to and fro.

The Disadvantage of Context Switching


 Though the context switching time is very less, still during that time, the CPU remains
idle and does not do any fruitful work.
 Also, due to Context Switching, there is a frequent flush of TLB(Translation Lookaside
Buffer) and Cache.
Professor: Let's conclude whatever we have learned so far.

Conclusion
 During a context switch, the context of the old process is saved, and the CPU is allocated
to the new process. Once the new process gets completed and the CPU is returned to the
old process, it can continue the execution from where it was stopped.
 Context Switching gets triggered during multiprocessing, interrupt handling, and
switching from user mode to kernel mode.
 During a context switch, the data and state of the old process are stored in PCB, and the
CPU is allotted to the new process.
 Context Switching gives the impression to the user that the system has multiple CPUs by
executing multiple processes.
 Context Switching is considered as overhead as the CPU remains idle during that time.
 Context Switching also leads to frequent flushing of TLB(Translation Lookaside Buffer)
and Cache.

You might also like