0% found this document useful (0 votes)
77 views35 pages

Unit 3 - Operating System Tunning CPT

An operating system is software that acts as an interface between users and computer hardware. It performs basic tasks like file management, memory management, process management, input/output control, and device management. Popular operating systems include Linux, Windows, VMS, OS/400, AIX, and z/OS. An operating system controls execution of programs, allocates system resources, provides security and error detection, and coordinates other software and users.

Uploaded by

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

Unit 3 - Operating System Tunning CPT

An operating system is software that acts as an interface between users and computer hardware. It performs basic tasks like file management, memory management, process management, input/output control, and device management. Popular operating systems include Linux, Windows, VMS, OS/400, AIX, and z/OS. An operating system controls execution of programs, allocates system resources, provides security and error detection, and coordinates other software and users.

Uploaded by

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

An Operating System (OS) is an interface between a computer user and computer

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.

Following are some of important functions of an operating System.

 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.

Other Important Activities


Following are some of the important activities that an Operating System performs −
 Security − By means of password and similar other techniques, it prevents unauthorized
access to programs and data.
 Control over system performance − Recording delays between request for a service and
response from the system.
 Job accounting − Keeping track of time and resources used by various jobs and users.
 Error detecting aids − Production of dumps, traces, error messages, and other debugging
and error detecting aids.
 Coordination between other softwares and users − Coordination and assignment of
compilers, interpreters, assemblers and other software to the various users of the computer
systems.

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.

Process Life Cycle


When a process executes, it passes through different states. These stages may differ
in different operating systems, and the names of these states are also not
standardized.
In general, a process can have one of the following five states at a time.

S.N. State & Description

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.

Process Control Block (PCB)


A Process Control Block is a data structure maintained by the Operating System for
every process. The PCB is identified by an integer process ID (PID). A PCB keeps all
the information needed to keep track of a process as listed below in the table −

S.N. Information & Description

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.

The architecture of a PCB is completely dependent on Operating System and may


contain different information in different operating systems. Here is a simplified diagram
of a PCB −
The PCB is maintained for a process throughout its lifetime, and is deleted once the
process terminates.
CPU Scheduling

What is CPU Scheduling?


CPU scheduling is a process which allows one process to use the CPU while the
execution of another process is on hold(in waiting state) due to unavailability of any
resource like I/O etc, thereby making full use of CPU. The aim of CPU scheduling is to
make the system efficient, fast and fair.
Whenever the CPU becomes idle, the operating system must select one of the
processes in the ready queue to be executed. The selection process is carried out by
the short-term scheduler (or CPU scheduler). The scheduler selects from among the
processes in memory that are ready to execute, and allocates the CPU to one of them.

CPU Scheduling: Dispatcher


Another component involved in the CPU scheduling function is the Dispatcher. The
dispatcher is the module that gives control of the CPU to the process selected by
the short-term scheduler. This function involves:

 Switching context

 Switching to user mode

 Jumping to the proper location in the user program to restart that program from where it

left last time.

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:

Types of CPU Scheduling


CPU scheduling decisions may take place under the following four circumstances:
1. When a process switches from the running state to the waiting state(for I/O request or

invocation of wait for the termination of one of the child processes).

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).

4. When a process terminates.

In circumstances 1 and 4, there is no choice in terms of scheduling. A new process(if


one exists in the ready queue) must be selected for execution. There is a choice,
however in circumstances 2 and 3.
When Scheduling takes place only under circumstances 1 and 4, we say the scheduling
scheme is non-preemptive; otherwise the scheduling scheme is preemptive.

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:

1. First Come First Serve(FCFS) Scheduling


2. Shortest-Job-First(SJF) Scheduling

3. Priority Scheduling

4. Round Robin(RR) Scheduling

5. Multilevel Queue Scheduling

6. Multilevel Feedback Queue Scheduling

Real Time 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:

 During reentry, the space shuttle is aerodynamically unstable. It is not actually


being kept under control by the quick reflexes of the well-trained pilots, but
rather by guidance computers that are collecting attitude and acceleration
input and adjusting numerous spoilers hundreds of times per second.
 Scientific and military satellites may receive precious and irreplaceable sensor
data at extremely high speeds. If it takes us too long to receive, process, and
store one data frame, the next data frame may be lost.
 More mundanely, but also important, many manufacturing processes are run
by computers nowadays. An assembly line needs to move at a particular
speed, with each step being performed at a particular time. Performing the
action too late results in a flawed or useless product.
 Even more commonly, playing media, like video or audio, has real time
requirements. Sound must be produced at a certain rate and frames must be
displayed frequently enough or the media becomes uncomfortable to deal
with.

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.

When we discussed more traditional scheduling algorithms, the metrics we looked at


were turn-around time (or throughput), fairness, and mean response time. But real-
time systems have very different requirements, characterized by different metrics:

 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

And we introduce a few new concepts:

 feasibility ... whether or not it is possible to meet the requirements for a


particular task set
 hard real-time ... there are strong requirements that specified tasks be run a
specified intervals (or within a specified response time). Failure to meet this
requirement (perhaps by as little as a fraction of a micro-second) may result in
system failure.
 soft real-time ... we may want to provide very good (e.g. microseconds)
response time, the only consequences of missing a deadline are degraded
performance or recoverable failures.

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.

Real-Time Scheduling Algorithms


In the simplest real-time systems, where the tasks and their execution times are all
known, there might not even be a scheduler. One task might simply call (or yield to)
the next. This model makes a great deal of sense in a system where the tasks form a
producer/consumer pipeline (e.g. MPEG frame receipt, protocol decoding, image
decompression, display).

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:

1. how they choose the next (ready) task to run


o shortest job first
o static priority ... highest priority ready task
o soonest start-time deadline first (ASAP)
o soonest completion-time deadline first (slack time)
2. how they handle overload (infeasible requirements)
o best effort
o periodicity adjustments ... run lower priority tasks less often.
o work shedding ... stop running lower priority tasks entirely.

Preemption may also be a different issue in real-time systems. In ordinary time-


sharing, preemption is a means of improving mean response time by breaking up the
execution of long-running, compute-intensive tasks. A second advantage of
preemptive scheduling, particularly important in a general purpose timesharing
system, is that it prevents a buggy (infinite loop) program from taking over the CPU.
The trade-off, between improved response time and increased overhead (for the added
context switches), almost always favors preemptive scheduling. This may not be true
for real-time systems:
 preempting a running task will almost surely cause it to miss its completion
deadline.
 since we so often know what the expected execution time for a task will be,
we can schedule accordingly and should have little need for preemption.
 embedded and real-time systems run fewer and simpler tasks than general
purpose time systems, and the code is often much better tested ... so infinite
loop bugs are extremely rare.

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.

Real-Time and Linux


Linux was not designed to be an embedded or real-time operating system, but many
tasks that were once-considered embedded applications now require the capabilities
(e.g. file systems, network protocols) of a general purpose operating system. As these
requirements have increased and processors have gotten faster, increasingly many
embedded and real-time applications have moved to Linux.

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.

Difference between Process and Thread


S.N. Process Thread

1 Process is heavy weight or resource intensive. Thread is


light
weight,
taking
lesser
resources
than a
process.

2 Process switching needs interaction with operating system. Thread


switching
does not
need to
interact
with
operating
system.

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.

5 Multiple processes without using threads use more resources. Multiple


threaded
processes
use fewer
resources.

6 In multiple processes each process operates independently of the others. One


thread can
read, write
or change
another
thread's
data.

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.

User Level Threads


In this case, the thread management kernel is not aware of the existence of threads.
The thread library contains code for creating and destroying threads, for passing
message and data between threads, for scheduling thread execution and for saving
and restoring thread contexts. The application starts with a single thread.
Advantages

 Thread switching does not require Kernel mode privileges.


 User level thread can run on any operating system.
 Scheduling can be application specific in the user level thread.
 User level threads are fast to create and manage.
Disadvantages

 In a typical operating system, most system calls are blocking.


 Multithreaded application cannot take advantage of multiprocessing.

Kernel Level Threads


In this case, thread management is done by the Kernel. There is no thread
management code in the application area. Kernel threads are supported directly by the
operating system. Any application can be programmed to be multithreaded. All of the
threads within an application are supported within a single process.
The Kernel maintains context information for the process as a whole and for individuals
threads within the process. Scheduling by the Kernel is done on a thread basis. The
Kernel performs thread creation, scheduling and management in Kernel space. Kernel
threads are generally slower to create and manage than the user threads.
Advantages

 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

 Many to many relationship.


 Many to one relationship.
 One to one relationship.

Many to Many Model


The many-to-many model multiplexes any number of user threads onto an equal or
smaller number of kernel threads.
The following diagram shows the many-to-many threading model where 6 user level
threads are multiplexing with 6 kernel level threads. In this model, developers can
create as many user threads as necessary and the corresponding Kernel threads can
run in parallel on a multiprocessor machine. This model provides the best accuracy on
concurrency and when a thread performs a blocking system call, the kernel can
schedule another thread for execution.
Many to One Model
Many-to-one model maps many user level threads to one Kernel-level thread. Thread
management is done in user space by the thread library. When thread makes a
blocking system call, the entire process will be blocked. Only one thread can access
the Kernel at a time, so multiple threads are unable to run in parallel on
multiprocessors.
If the user-level thread libraries are implemented in the operating system in such a way
that the system does not support them, then the Kernel threads use the many-to-one
relationship modes.
One to One Model
There is one-to-one relationship of user-level thread to the kernel-level thread. This
model provides more concurrency than the many-to-one model. It also allows another
thread to run when a thread makes a blocking system call. It supports multiple threads
to execute in parallel on microprocessors.
Disadvantage of this model is that creating user thread requires the corresponding
Kernel thread. OS/2, windows NT and windows 2000 use one to one relationship
model.
Difference between User-Level & Kernel-Level Thread
S.N. User-Level Threads Kernel-Level Thread

1 User-level threads are faster to create and manage. Kernel-level threads are slower to create
and manage.

2 Implementation is by a thread library at the user Operating system supports creation of


level. Kernel threads.

3 User-level thread is generic and can run on any Kernel-level thread is specific to the
operating system. operating system.

4 Multi-threaded applications cannot take advantage Kernel routines themselves can be


of multiprocessing. multithreaded.

Memory management is the functionality of an operating system which handles or


manages primary memory and moves processes back and forth between main
memory and disk during execution. Memory management keeps track of each and
every memory location, regardless of either it is allocated to some process or it is free.
It checks how much memory is to be allocated to processes. It decides which process
will get memory at what time. It tracks whenever some memory gets freed or
unallocated and correspondingly it updates the status.
This tutorial will teach you basic concepts related to Memory Management.

Process Address Space


The process address space is the set of logical addresses that a process references in
its code. For example, when 32-bit addressing is in use, addresses can range from 0 to
0x7fffffff; that is, 2^31 possible numbers, for a total theoretical size of 2 gigabytes.
The operating system takes care of mapping the logical addresses to physical
addresses at the time of memory allocation to the program. There are three types of
addresses used in a program before and after memory is allocated −

S.N. Memory Addresses & Description

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.

Static vs Dynamic Loading


The choice between Static or Dynamic Loading is to be made at the time of computer
program being developed. If you have to load your program statically, then at the time
of compilation, the complete programs will be compiled and linked without leaving any
external program or module dependency. The linker combines the object program with
other necessary object modules into an absolute program, which also includes logical
addresses.
If you are writing a Dynamically loaded program, then your compiler will compile the
program and for all the modules which you want to include dynamically, only
references will be provided and rest of the work will be done at the time of execution.
At the time of loading, with static loading, the absolute program (and data) is loaded
into memory in order for execution to start.
If you are using dynamic loading, dynamic routines of the library are stored on a disk
in relocatable form and are loaded into memory only when they are needed by the
program.

Static vs Dynamic Linking


As explained above, when static linking is used, the linker combines all other modules
needed by a program into a single executable program to avoid any runtime
dependency.
When dynamic linking is used, it is not required to link the actual module or library with
the program, rather a reference to the dynamic module is provided at the time of
compilation and linking. Dynamic Link Libraries (DLL) in Windows and Shared Objects
in Unix are good examples of dynamic libraries.

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.

S.N. Memory Allocation & Description

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 −

S.N. Fragmentation & Description

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 −

External fragmentation can be reduced by compaction or shuffle memory contents to


place all free memory together in one large block. To make compaction feasible,
relocation should be dynamic.
The internal fragmentation can be reduced by effectively assigning the smallest
partition but large enough for the process.

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.

Kernel Security & Protection


The kernel also protects the hardware. If there is no protection, any program will be able
to carry out any task on the computer, including crashing your computer, corrupting
data, etc.
In modern-day computers, security is implemented on the hardware level. For example,
Windows will not load drivers which are not from a trusted source and certified using
signature. Secure Boot and Trusted Bootare classic examples.
Secure Boot: It is a security standard developed by members of the PC industry. It helps you
protect your system from malicious programs by not allowing any unauthorized applications to
run during the system start-up process. The feature makes sure that your PC boots using only
software that is trusted by the PC manufacturer. So, whenever your PC starts, the firmware
checks the signature of each piece of boot software, including firmware drivers (Option ROMs)
and the operating system. If the signatures are verified, the PC boots and the firmware gives
control to the operating system.
Trusted Boot: It uses the Virtual Trusted Platform Module (VTPM) to verify the digital
signature of the Windows 10 kernel before loading it. In turn, it confirms every other component
of the Windows startup process, including the boot drivers, startup files, and ELAM. If a file has
been altered or changed to any extent, the bootloader detects it and refuses to load it by
recognizing it as the corrupted component. In short, it provides a chain of trust for all the
elements during boot.

What are the types of Kernel


The Kernel can also talk to hardware on a secure line. So companies can develop a Kernel which
can talk to their hardware through a set of buttons. Take the washing machine for an example.
Depending on the knobs you move, and time you set – a basic level of Kernel should be
enough. That said, Kernel themselves grow complicated with time, which results in types of
Kernel.
1. Monolithic Kernel: Here, the OS and Kernel both run in the same memory space and
suitable where security is not a significant concern. It results in faster access, but if there is a
bug in the device driver, the entire system crashes.
2. Microkernel: Its a stripped-down version of Monolithic Kernel where the Kernel itself can
do most of the job done, and there is no need of an extra GUI. They should be used where
security and the crashing system isn’t or will not happen.
3. Hybrid Kernel: This Kernel is what we see most. Windows, Apple’s macOS. They are a
mix of Monolithic Kernel and Microkernel. It moves out drivers but keeps system services
inside the Kernel – similar to how drivers are loaded when Windows Starts the bootup
process.
4. Nano Kernel: If you need to have a kernel, but its majority of function is set up outside, then
this comes into the picture.
5. Exo Kernel: This kernel only offers process protection and resource handling. However it is
mostly used when you are testing out an inhouse project, and you upgrade to a better Kernel
type.
There is a lot more to a Kernel than what we have talked about. As you dig deeper, the definition
of Kernel gets broader and more profound.

You might also like