Unit 3 - Operating System Tunning CPT
Unit 3 - Operating System Tunning CPT
hardware. An operating system is a software which performs all the basic tasks like file
management, memory management, process management, handling input and output,
and controlling peripheral devices such as disk drives and printers.
Some popular Operating Systems include Linux Operating System, Windows
Operating System, VMS, OS/400, AIX, z/OS, etc.
Definition
An operating system is a program that acts as an interface between the user and the
computer hardware and controls the execution of all kinds of programs.
Memory Management
Processor Management
Device Management
File Management
Security
Control over system performance
Job accounting
Error detecting aids
Coordination between other software and users
Memory Management
Memory management refers to management of Primary Memory or Main Memory.
Main memory is a large array of words or bytes where each word or byte has its own
address.
Main memory provides a fast storage that can be accessed directly by the CPU. For a
program to be executed, it must in the main memory. An Operating System does the
following activities for memory management −
Keeps tracks of primary memory, i.e., what part of it are in use by whom, what part are not in
use.
In multiprogramming, the OS decides which process will get memory when and how much.
Allocates the memory when a process requests it to do so.
De-allocates the memory when a process no longer needs it or has been terminated.
Processor Management
In multiprogramming environment, the OS decides which process gets the processor
when and for how much time. This function is called process scheduling. An
Operating System does the following activities for processor management −
Keeps tracks of processor and status of process. The program responsible for this task is
known as traffic controller.
Allocates the processor (CPU) to a process.
De-allocates processor when a process is no longer required.
Device Management
An Operating System manages device communication via their respective drivers. It
does the following activities for device management −
Keeps tracks of all devices. Program responsible for this task is known as the I/O
controller.
Decides which process gets the device when and for how much time.
Allocates the device in the efficient way.
De-allocates devices.
File Management
A file system is normally organized into directories for easy navigation and usage.
These directories may contain files and other directions.
An Operating System does the following activities for file management −
Keeps track of information, location, uses, status etc. The collective facilities are often
known as file system.
Decides who gets the resources.
Allocates the resources.
De-allocates the resources.
Process
A process is basically a program in execution. The execution of a process must
progress in a sequential fashion.
A process is defined as an entity which represents the basic unit of work to be implemented in the
system.
To put it in simple terms, we write our computer programs in a text file and when we
execute this program, it becomes a process which performs all the tasks mentioned in
the program.
When a program is loaded into the memory and it becomes a process, it can be
divided into four sections ─ stack, heap, text and data. The following image shows a
simplified layout of a process inside main memory −
S.N. Component & Description
1
Stack
The process Stack contains the temporary data such as method/function parameters, return
address and local variables.
2
Heap
This is dynamically allocated memory to a process during its run time.
3
Text
This includes the current activity represented by the value of Program Counter and the
contents of the processor's registers.
4
Data
This section contains the global and static variables.
Program
A program is a piece of code which may be a single line or millions of lines. A computer
program is usually written by a computer programmer in a programming language. For
example, here is a simple program written in C programming language −
#include <stdio.h>
int main() {
printf("Hello, World! \n");
return 0;
}
A computer program is a collection of instructions that performs a specific task when
executed by a computer. When we compare a program with a process, we can
conclude that a process is a dynamic instance of a computer program.
A part of a computer program that performs a well-defined task is known as
an algorithm. A collection of computer programs, libraries and related data are
referred to as a software.
1
Start
This is the initial state when a process is first started/created.
2
Ready
The process is waiting to be assigned to a processor. Ready processes are waiting to have
the processor allocated to them by the operating system so that they can run. Process may
come into this state after Start state or while running it by but interrupted by the scheduler to
assign CPU to some other process.
3
Running
Once the process has been assigned to a processor by the OS scheduler, the process state is
set to running and the processor executes its instructions.
4
Waiting
Process moves into the waiting state if it needs to wait for a resource, such as waiting for user
input, or waiting for a file to become available.
5
Terminated or Exit
Once the process finishes its execution, or it is terminated by the operating system, it is
moved to the terminated state where it waits to be removed from main memory.
1
Process State
The current state of the process i.e., whether it is ready, running, waiting, or whatever.
2
Process privileges
This is required to allow/disallow access to system resources.
3
Process ID
Unique identification for each of the process in the operating system.
4
Pointer
A pointer to parent process.
5
Program Counter
Program Counter is a pointer to the address of the next instruction to be executed for this
process.
6
CPU registers
Various CPU registers where process need to be stored for execution for running state.
7
CPU Scheduling Information
Process priority and other scheduling information which is required to schedule the process.
8
Memory management information
This includes the information of page table, memory limits, Segment table depending on
memory used by the operating system.
9
Accounting information
This includes the amount of CPU used for process execution, time limits, execution ID etc.
10
IO status information
This includes a list of I/O devices allocated to the process.
Switching context
Jumping to the proper location in the user program to restart that program from where it
The dispatcher should be as fast as possible, given that it is invoked during every
process switch. The time taken by the dispatcher to stop one process and start another
process is known as the Dispatch Latency. Dispatch Latency can be explained using
the below figure:
2. When a process switches from the running state to the ready state (for example, when
an interrupt occurs).
3. When a process switches from the waiting state to the ready state(for example,
completion of I/O).
Non-Preemptive Scheduling
Under non-preemptive scheduling, once the CPU has been allocated to a process, the
process keeps the CPU until it releases the CPU either by terminating or by switching to
the waiting state.
This scheduling method is used by the Microsoft Windows 3.1 and by the Apple
Macintosh operating systems.
It is the only method that can be used on certain hardware platforms, because It does
not require the special hardware(for example: a timer) needed for preemptive
scheduling.
Preemptive Scheduling
In this type of Scheduling, the tasks are usually assigned with priorities. At times it is
necessary to run a certain task that has a higher priority before another task although it
is running. Therefore, the running task is interrupted for some time and resumed later
when the priority task has finished its execution.
CPU Scheduling: Scheduling Criteria
There are many different criterias to check when considering the "best" scheduling
algorithm, they are:
CPU Utilization
To make out the best use of CPU and not to waste any CPU cycle, CPU would be
working most of the time(Ideally 100% of the time). Considering a real system, CPU
usage should range from 40% (lightly loaded) to 90% (heavily loaded.)
Throughput
It is the total number of processes completed per unit time or rather say total amount of
work done in a unit of time. This may range from 10/second to 1/hour depending on the
specific processes.
Turnaround Time
It is the amount of time taken to execute a particular process, i.e. The interval from time
of submission of the process to the time of completion of the process(Wall clock time).
Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a process has
been waiting in the ready queue to acquire get control on the CPU.
Load Average
It is the average number of processes residing in the ready queue waiting for their turn
to get into the CPU.
Response Time
Amount of time it takes from when a request was submitted until the first response is
produced. Remember, it is the time till the first response and not the completion of
process execution(final response).
In general CPU utilization and Throughput are maximized and other factors are reduced
for proper optimization.
Scheduling Algorithms
To decide which process to execute first and which process to execute last to achieve
maximum CPU utilisation, computer scientists have defined some algorithms, they are:
3. Priority Scheduling
Introduction
Priority based scheduling enables us to give better service to certain processes. In our
discussion of multi-queue scheduling, priority was adjusted based on whether a task
was more interactive or compute intensive. But most schedulers enable us to give any
process any desired priority. Isn't that good enough?
Priority scheduling is inherently a best effort approach. If our task is competing with
other high priority tasks, it may not get as much time as it requires. Sometimes best
effort isn't good enough:
There are many computer controlled applications where delays in critical processing
can have undesirable, or even disastrous consequences.
What are Real-Time Systems
A real-time system is one whose correctness depends on timing as well as
functionality.
timeliness ... how closely does it meet its timing requirements (e.g. ms/day of
accumulated tardiness)
predictability ... how much deviation is there in delivered timeliness
It sounds like real-time scheduling is more critical and difficult than traditional time-
sharing, and in many ways it is. But real-time systems may have a few characteristics
that make scheduling easier:
We may actually know how long each task will take to run. This enables much
more intelligent scheduling.
Starvation (of low priority tasks) may be acceptable. The space shuttle
absolutely must sense attitude and acceleration and adjust spolier positions
once per millisecond. But it probably doesn't matter if we update the
navigational display once per millisecond or once every ten seconds.
Telemetry transmission is probably somewhere in-between. Understanding
the relative criticality of each task gives us the freedom to intelligently shed
less critical work in times of high demand.
The work-load may be relatively fixed. Normally high utilization implies long
queuing delays, as bursty traffic creates long lines. But if the incoming traffic
rate is relatively constant, it is possible to simultaneously achieve high
utilization and good response time.
In more complex real-time system, with a larger (but still fixed) number of tasks that
do not function in a strictly pipeline fashion, it may be possible to
do static scheduling. Based on the list of tasks to be run, and the expected completion
time for each, we can define (at design or build time) a fixed schedule that will ensure
timely execution of all tasks.
But for many real-time systems, the work-load changes from moment to moment,
based on external events. These require dynamic scheduling. For dynamic scheduling
algorithms, there are two key questions:
For the least demanding real time tasks, a sufficiently lightly loaded system might be
reasonably successful in meeting its deadlines. However, this is achieved simply
because the frequency at which the task is run happens to be high enough to meet its
real time requirements, not because the scheduler is aware of such requirements. A
lightly loaded machine running a traditional scheduler can often display a video to a
user's satisfaction, not because the scheduler "knows" that a frame must be rendered
by a certain deadline, but simply because the machine has enough cycles and a low
enough work load to render the frame before the deadline has arrived.
To support these applications Linux now supports a real-time scheduler, which can be
enabled with sched_setscheduler(2), and is described in this Linux Journal Article.
This real-time scheduler does not provide quite the same level of response-time
guarantees that more traditional Real-Time-OSs do, but they are adequate for many
soft real-time applications.
What about Windows? Conventional wisdom states that Windows is not well suited
for real time needs, offering no native real time scheduler and too many ways in
which desired real time deadlines might be missed. Windows favors general purpose
throughput over meeting deadlines, as a rule. With sufficiently low load, a Windows
system may, nonetheless, provide fast enough service for some soft real time
requirements, such as playing music or video. One should be careful in relying on
Windows for critical real time operations, however, as it is not designed for that
purpose.
What is Thread?
A thread is a flow of execution through the process code, with its own program counter
that keeps track of which instruction to execute next, system registers which hold its
current working variables, and a stack which contains the execution history.
A thread shares with its peer threads few information like code segment, data segment
and open files. When one thread alters a code segment memory item, all other threads
see that.
A thread is also called a lightweight process. Threads provide a way to improve
application performance through parallelism. Threads represent a software approach to
improving performance of operating system by reducing the overhead thread is
equivalent to a classical process.
Each thread belongs to exactly one process and no thread can exist outside a process.
Each thread represents a separate flow of control. Threads have been successfully
used in implementing network servers and web server. They also provide a suitable
foundation for parallel execution of applications on shared memory multiprocessors.
The following figure shows the working of a single-threaded and a multithreaded
process.
3 In multiple processing environments, each process executes the same code but All threads
has its own memory and file resources. can share
same set
of open
files, child
processes.
4 If one process is blocked, then no other process can execute until the first While one
process is unblocked. thread is
blocked
and
waiting, a
second
thread in
the same
task can
run.
Advantages of Thread
Threads minimize the context switching time.
Use of threads provides concurrency within a process.
Efficient communication.
It is more economical to create and context switch threads.
Threads allow utilization of multiprocessor architectures to a greater scale and efficiency.
Types of Thread
Threads are implemented in following two ways −
User Level Threads − User managed threads.
Kernel Level Threads − Operating System managed threads acting on kernel, an operating
system core.
Kernel can simultaneously schedule multiple threads from the same process on multiple
processes.
If one thread in a process is blocked, the Kernel can schedule another thread of the same
process.
Kernel routines themselves can be multithreaded.
Disadvantages
Kernel threads are generally slower to create and manage than the user threads.
Transfer of control from one thread to another within the same process requires a mode
switch to the Kernel.
Multithreading Models
Some operating system provide a combined user level thread and Kernel level thread
facility. Solaris is a good example of this combined approach. In a combined system,
multiple threads within the same application can run in parallel on multiple processors
and a blocking system call need not block the entire process. Multithreading models
are three types
1 User-level threads are faster to create and manage. Kernel-level threads are slower to create
and manage.
3 User-level thread is generic and can run on any Kernel-level thread is specific to the
operating system. operating system.
1
Symbolic addresses
The addresses used in a source code. The variable names, constants, and instruction labels
are the basic elements of the symbolic address space.
2
Relative addresses
At the time of compilation, a compiler converts symbolic addresses into relative addresses.
3
Physical addresses
The loader generates these addresses at the time when a program is loaded into main
memory.
Virtual and physical addresses are the same in compile-time and load-time address-
binding schemes. Virtual and physical addresses differ in execution-time address-
binding scheme.
The set of all logical addresses generated by a program is referred to as a logical
address space. The set of all physical addresses corresponding to these logical
addresses is referred to as a physical address space.
The runtime mapping from virtual to physical address is done by the memory
management unit (MMU) which is a hardware device. MMU uses following mechanism
to convert virtual address to physical address.
The value in the base register is added to every address generated by a user process,
which is treated as offset at the time it is sent to memory. For example, if the base register
value is 10000, then an attempt by the user to use address location 100 will be dynamically
reallocated to location 10100.
The user program deals with virtual addresses; it never sees the real physical addresses.
Swapping
Swapping is a mechanism in which a process can be swapped temporarily out of main
memory (or move) to secondary storage (disk) and make that memory available to
other processes. At some later time, the system swaps back the process from the
secondary storage to main memory.
Though performance is usually affected by swapping process but it helps in running
multiple and big processes in parallel and that's the reason Swapping is also known
as a technique for memory compaction.
The total time taken by swapping process includes the time it takes to move the entire
process to a secondary disk and then to copy the process back to memory, as well as
the time the process takes to regain main memory.
Let us assume that the user process is of size 2048KB and on a standard hard disk
where swapping will take place has a data transfer rate around 1 MB per second. The
actual transfer of the 1000K process to or from memory will take
2048KB / 1024KB per second
= 2 seconds
= 2000 milliseconds
Now considering in and out time, it will take complete 4000 milliseconds plus other
overhead where the process competes to regain main memory.
Memory Allocation
Main memory usually has two partitions −
Low Memory − Operating system resides in this memory.
High Memory − User processes are held in high memory.
Operating system uses the following memory allocation mechanism.
1
Single-partition allocation
In this type of allocation, relocation-register scheme is used to protect user processes from
each other, and from changing operating-system code and data. Relocation register contains
value of smallest physical address whereas limit register contains range of logical addresses.
Each logical address must be less than the limit register.
2
Multiple-partition allocation
In this type of allocation, main memory is divided into a number of fixed-sized partitions where
each partition should contain only one process. When a partition is free, a process is selected
from the input queue and is loaded into the free partition. When the process terminates, the
partition becomes available for another process.
Fragmentation
As processes are loaded and removed from memory, the free memory space is broken
into little pieces. It happens after sometimes that processes cannot be allocated to
memory blocks considering their small size and memory blocks remains unused. This
problem is known as Fragmentation.
Fragmentation is of two types −
1
External fragmentation
Total memory space is enough to satisfy a request or to reside a process in it, but it is not
contiguous, so it cannot be used.
2
Internal fragmentation
Memory block assigned to process is bigger. Some portion of memory is left unused, as it
cannot be used by another process.
The following diagram shows how fragmentation can cause waste of memory and a
compaction technique can be used to create more free memory out of fragmented
memory −
Paging
A computer can address more memory than the amount physically installed on the
system. This extra memory is actually called virtual memory and it is a section of a
hard that's set up to emulate the computer's RAM. Paging technique plays an
important role in implementing virtual memory.
Paging is a memory management technique in which process address space is broken
into blocks of the same size called pages (size is power of 2, between 512 bytes and
8192 bytes). The size of the process is measured in the number of pages.
Similarly, main memory is divided into small fixed-sized blocks of (physical) memory
called frames and the size of a frame is kept the same as that of a page to have
optimum utilization of the main memory and to avoid external fragmentation.
Address Translation
Page address is called logical address and represented by page number and
the offset.
Logical Address = Page number + page offset
Frame address is called physical address and represented by a frame number and
the offset.
Physical Address = Frame number + page offset
A data structure called page map table is used to keep track of the relation between a
page of a process to a frame in physical memory.
When the system allocates a frame to any page, it translates this logical address into a
physical address and create entry into the page table to be used throughout execution
of the program.
When a process is to be executed, its corresponding pages are loaded into any
available memory frames. Suppose you have a program of 8Kb but your memory can
accommodate only 5Kb at a given point in time, then the paging concept will come into
picture. When a computer runs out of RAM, the operating system (OS) will move idle or
unwanted pages of memory to secondary memory to free up RAM for other processes
and brings them back when needed by the program.
This process continues during the whole execution of the program where the OS keeps
removing idle pages from the main memory and write them onto the secondary
memory and bring them back when required by the program.
Advantages and Disadvantages of Paging
Here is a list of advantages and disadvantages of paging −
Paging reduces external fragmentation, but still suffer from internal fragmentation.
Paging is simple to implement and assumed as an efficient memory management technique.
Due to equal size of the pages and frames, swapping becomes very easy.
Page table requires extra memory space, so may not be good for a system having small
RAM.
Segmentation
Segmentation is a memory management technique in which each job is divided into
several segments of different sizes, one for each module that contains pieces that
perform related functions. Each segment is actually a different logical address space of
the program.
When a process is to be executed, its corresponding segmentation are loaded into
non-contiguous memory though every segment is loaded into a contiguous block of
available memory.
Segmentation memory management works very similar to paging but here segments
are of variable-length where as in paging pages are of fixed size.
A program segment contains the program's main function, utility functions, data
structures, and so on. The operating system maintains a segment map table for every
process and a list of free memory blocks along with segment numbers, their size and
corresponding memory locations in main memory. For each segment, the table stores
the starting address of the segment and the length of the segment. A reference to a
memory location includes a value that identifies a segment and an offset.
Virtual Machine Memory Management
Virtual Machine Memory
Each virtual machine consumes memory based on its configured size, plus additional overhead memory for
virtualization.
The configured size is a construct maintained by the virtualization layer for the virtual machine. It is the amount of
memory that is presented to the guest operating system, but it is independent of the amount of physical RAM that is
allocated to the virtual machine, which depends on the resource settings (shares, reservation, limit) explained below.
For example, consider a virtual machine with a configured size of 1GB. When the guest operating system boots, it
detects that it is running on a dedicated machine with 1GB of physical memory. The actual amount of physical host
memory allocated to the virtual machine depends on its memory resource settings and memory contention on
the ESXi host. In some cases, the virtual machine might be allocated the full 1GB. In other cases, it might receive a
smaller allocation. Regardless of the actual allocation, the guest operating system continues to behave as though it is
running on a dedicated machine with 1GB of physical memory.
Shares Specify the relative priority for a virtual machine if more than the reservation is available.
Reservation Is a guaranteed lower bound on the amount of physical memory that the host reserves for the virtual machine,
even when memory is overcommitted. Set the reservation to a level that ensures the virtual machine has sufficient
memory to run efficiently, without excessive paging.
After a virtual machine has accessed its full reservation, it is allowed to retain that amount of memory and this
memory is not reclaimed, even if the virtual machine becomes idle. For example, some guest operating systems
(for example, Linux) might not access all of the configured memory immediately after booting. Until the virtual
machines accesses its full reservation, VMkernel can allocate any unused portion of its reservation to other virtual
machines. However, after the guest’s workload increases and it consumes its full reservation, it is allowed to keep
this memory.
Limit Is an upper bound on the amount of physical memory that the host can allocate to the virtual machine. The virtual
machine’s memory allocation is also implicitly limited by its configured size.
Overhead memory includes space reserved for the virtual machine frame buffer and various virtualization data
structures.
Kernel in OS
What is a Kernel in OS
Now that we know that its a core program in the OS, one should also know it is also the
first program that loads after the bootloader. It then does all the talking between the
hardware and the software or applications. So if you launch a program, the user
interface sends a request to Kernel. The Kernel then sends a request to CPU, Memory to
assign processing power, memory, and other things so the application can run smoothly
in the front end.
You can imagine Kernel as a translator. It converts input/output requests from software
into an instruction set for the CPU and GPU. In simple words, its a layer between the
software and the hardware which makes everything possible. The kernel manages the
following:
1. CPU/GPU
2. Memory
3. Input/Output or IO devices
4. Resource management
5. Memory management
6. Device management
7. System calls.
User processes can access kernel-space only through the use of system calls. If a
program tries to access directly, it will result in a fault.