OS Handout notes
OS Handout notes
(AUTONOMOUS)
NAMAKKAL- TRICHY MAIN ROAD, THOTTIAM,TRICHY
ARTIFICIAL INTELLIGENCE AND DATA SCIENCE
20IT401-OPERATING SYSTEMS
HANDOUT NOTES
UNIT 1
INTRODUCTION AND OPERATING SYSTEM STRUCTURES
A computer system can be divided roughly into four components: the hardware,
the operating system, the application programs, and a user (Figure 1.1).
The hardware—the central processing unit (CPU), the memory, and the
input/output (I/O) devices—provides the basic computing resources for the
system. The application programs—such as word processors, spreadsheets,
compilers, and web browsers—define the ways in which these resources are
used to solve users’ computing problems.
The operating system controls the hardware and coordinates its use among the
various application programs for the various users.
Operating systems exist because they offer a reasonable way to solve the
problem of creating a usable computing system.
The fundamental goal of computer systems is to execute programs and to make
solving user problems easier.
Computer hardware is constructed toward this goal. Since bare hardware alone
is not particularly easy to use, application programs are developed.
These programs require certain common operations, such as those controlling
the I/O devices.
MAINFRAME SYSTEMS
It is the system where the first computer used to handle many commercial
scientific applications. The growth of mainframe systems traced from simple
batch system where the computer runs one and only one application to time
shared systems which allowed for user interaction with the computer system.
Early computers were physically large machine. The common input devices
were card readers, tape drivers. The common output devices were line printers,
tape drivers and card punches.
In these systems the user did not interact directly with the computer system.
Instead the user preparing a job which consists of programming data and some
control information and then submitted it to the computer operator after some
time the output is appeared.
The disadvantages of batch system are that in this execution environment the
CPU is often idle because the speed up of I/O devices is much slower than the
CPU.
Multi-programmed System:
Multi-programming concept increases CPU utilization by organization jobs so
that the CPU always has one job to execute the idea behind multiprogramming
concept. The operating system keeps several jobs in memory simultaneously as
shown in below figure.
This set of job is subset of the jobs kept in the job pool. The operating system
picks and beginning to execute one of the jobs in the memory. In this
environment the operating system simply switches and executes another job.
When a job needs to wait the CPU is simply switched to another job and so on.
The disadvantages of the multi-programmed system are It does not provide user
interaction with the computer system during the program execution. The
introduction of disk technology solved these problems rather than reading the
cards from card reader into disk. This form of processing is known as spooling.
The time sharing system is also known as multi user systems. The CPU executes
multiple jobs by switching among them but the switches occurs so frequently
that the user can interact with each program while it is running.
The time sharing system allows many users to share the computer
simultaneously. Since each action in this system is short, only a little CPU time
is needed for each user.
The system switches rapidly from one user to the next so each user feels as if the
entire computer system is dedicated to his use, even though it is being shared by
many users.
PERSONAL COMPUTER SYSTEM / DESKTOP SYSTEM:
Personal computer appeared in 1970‘s. They are microcomputers that are
smaller & less expensive than mainframe systems.
Instead of maximizing CPU & peripheral utilization, the systems opt for
maximizing user convenience & responsiveness.
At first file protection was not necessary on a personal machine. But when other
computers 2nd other users can access the files on a pc file protection becomes
necessary.
The lack of protection made if easy for malicious programs to destroy data on
such systems. These programs may be self replicating& they spread via worm or
virus mechanisms.
MULTIPROCESSOR SYSTEMS
Symmetric Multiprocessor
In this system, every processors have own identically copy of operating system,
and they can make communication in between each other. In which all
processors are connected each other with peer to peer relationship nature, it
means no master & slave relation.
Examples – Windows NT, Solaris, Digital UNIX, OS/2 & Linux.
Asymmetric Multiprocessor
In this system, every processor is allotted predefined tasks, and master processor
has power for controlling entire system. In which, It use the master- slave
relationship.
Examples – SunOS Version 4, IOS
Shared Memory Multiprocessor
In this system, each CPU contains sharable common memory.
Distributed Memory Multiprocessor
In this system, all types of processors consists own private memory.
UMA Multiprocessor
UMA Multiprocessor stands for “Uniform Memory Access Multiprocessor”. In
which, it allows to access all memory at the uniform speed rate for all
processors.
NUMA Multiprocessor
NUMA Multiprocessor stands for “Non Uniform Memory Access
Multiprocessor”. In this system, it involves some areas of the memory for
accessing at the faster rate, and left parts of memory are utilized for other tasks.
DISTRIBUTED SYSTEMS
Real time system is used when there are rigid time requirements on the
operation of a processor or flow of data. Sensors bring data to the computers.
The computer analyzes data and adjusts controls to modify the sensors inputs.
System that controls scientific experiments, medical imaging systems and some
display systems are real time systems.
Real time system is of two types such as,
HANDHELD SYSTEMS
Handheld systems include Personal Digital Assistants (PDAs), such as Palm-
Pilots or Cellular Telephones with connectivity to a network such as the
Internet.
They are usually of limited size due to which most handheld devices have a
small amount of memory, include slow processors, and feature small display
screens.
HARDWARE PROTECTION
If a computer system has multiple users and allows the concurrent execution of
multiple processes, then access to data must be regulated.
For that purpose, mechanisms ensure that files, memory segments, CPU, and
other resources can be operated on by only those processes that have gained
proper authorization from the operating system.
SYSTEM COMPONENTS
Processor-memory
Processor-I/O
Data processing
I/O Function
MEMORY HIERARCHY:
To achieve greatest performance, the memory must be able to keep up with the
processor.
As the processor is executing instructions, we would not want it to have to pause
waiting for instructions or operands.
Thus the cost of memory must be reasonable in relationship to other
components.
OPERATING SYSTEMS OVERVIEW:
An OS is defined as a System program that controls the execution of application
programs and acts as an interface between applications and the computer
hardware.
OPERATING-SYSTEM SERVICES
An operating system provides an environment for the execution of programs. It
makes certain services available to programs and to the users of those programs.
The specific services provided, of course, differ from one operating system to
another, but we can identify common classes. Figure 1.11 shows one view of the
various operating-system services and how they interrelate.
Types of Services
1) User- oriented service
2) System-oriented service
Types of System Calls
Systems calls are grouped into five categories such as
Process control
File management
Device management
Information maintenance
Communications
SYSTEM PROGRAMS
Another aspect of a modern system is its collection of system programs. In the
logical computer hierarchy, At the lowest level is hardware. Next is the
operating system, then the system programs, and finally the application
programs.
They can be divided into these categories:
1) File management
2) Status information
3) File modification
4) Programming language support
5) Program loading and execution
6) Communications
7) Application programs
OPERATING SYSTEM STRUCTURE
A system as large and complex as a modern operating system must be
engineered carefully if it is to function properly and be modified easily.
A common approach is to partition the task into small components, or modules,
rather than have one monolithic system. Each of these modules should be a well-
defined portion of the system, with carefully defined inputs, outputs, and
functions.
Various ways to structure an operating system:
o Monolithic structure
Simple structure – MS-DOS
More complex – UNIX
More complex – Linux
o Layered – An abstraction
o Microkernel – Mach
Microkernels
We have already seen that as UNIX expanded, the kernel became large and
difficult to manage.
In the mid-1980s, researchers at Carnegie Mellon University developed an
operating system called Mach that modularized the kernel using the microkernel
approach.
VIRTUAL MACHINES
Traditionally, applications have run directly on an OS on a PC or a server. Each
PC or server would run only one OS at a time. Thus, the vendor had to rewrite
parts of its applications for each OS/platform they would run on. An effective
strategy for dealing with this problem is known as virtualization.
Virtualization technology enables a single PC or server to simultaneously run
multiple operating systems or multiple sessions of a single OS. A machine with
SYSTEM DESIGN AND IMPLEMENTATION
In this section, we discuss problems we face in designing and implementing an
operating system. There are, of course, no complete solutions to such problems, but
there are approaches that have proved successful.
Design Goals
The first problem in designing a system is to define goals and specifications. At
the highest level, the design of the system will be affected by the choice of
hardware and the type of system: batch, time sharing, single user, multiuser,
distributed, real time, or general purpose.
Beyond this highest design level, the requirements may be much harder to
specify. The requirements can, however, be divided into two basic groups:
User goals
System goals.
Mechanisms and Policies
One important principle is the separation of policy from mechanism.
Mechanisms determine how to do something; policies determine what will be
done.
For example, the timer construct is a mechanism for ensuring CPU protection,
but deciding how long the timer is to be set for a particular user is a policy
decision.
The separation of policy and mechanism is important for flexibility. Policies are
likely to change across places or over time. In the worst case, each change in
policy would require a change in the underlying mechanism.
A general mechanism insensitive to changes in policy would be more desirable.
A change in policy would then require redefinition of only certain parameters of
the system. For instance, consider a mechanism for giving priority to certain
types of programs over others.
Implementation
Once an operating system is designed, it must be implemented. Because
operating systems are collections of many programs, written by many people
over a long period of time, it is difficult to make general statements about how
they are implemented.
Early operating systems were written in assembly language. Now, although
some operating systems are still written in assembly language, most are written
in a higher-level language such as C or an even higher-level language such as
C++. Actually, an operating system can be written in more than one language.
The lowest levels of the kernel might be assembly language. Higher-level
routines might be in C, and system programs might be in C or C++, in
interpreted scripting languages like PERL or Python, or in shell scripts. In fact, a
given Linux distribution probably includes programs written in all of those
languages.
The first system that was not written in assembly language was probably the
Master Control Program (MCP) for Burroughs computers. MCP was written in a
variant of ALGOL. MULTICS, developed at MIT, was written mainly in the
system programming language PL/1.
The Linux and Windows operating system kernels are written mostly in C,
although there are some small sections of assembly code for device drivers and
for saving and restoring the state of registers.
The advantages of using a higher-level language, or at least a systems
implementation language, for implementing operating systems are the same as
those gained when the language is used for application programs: the code can
be written faster, is more compact, and is easier to understand and debug.
UNIT 2
PROCESS MANAGEMENT
PROCESS CONCEPTS
A process can be thought of as a program in execution.
A process is the unit of work in a modern time-sharing system.
A process generally includes the process stack, which contains temporary data
(such as method parameters, return addresses, and local variables), and a data
section, which contains global variables.
Process States:
1. New: The process is being created.
2. Running: Instructions are being executed.
3. Waiting: The process is waiting for some event to occur (such as an I/O
completion or reception of a signal).
4. Ready: The process is waiting to be assigned to a processor.
5. Terminated: The process has finished execution.
The creating process is called a parent process, whereas the new processes are
called the children of that process.
2. Process Termination
A process terminates when it finishes executing its final statement and asks the
operating system to delete it by using the exit system call.
At that point, the process may return data (output) to its parent process (via the
wait system call).
A process can cause the termination of another process via an appropriate
system call.
COOPERATING PROCESSES
The concurrent processes executing in the operating system may be either
independent processes or cooperating processes.
A process is independent if it cannot affect or be affected by the other processes
executing in the system.
A process is cooperating if it can affect or be affected by the other processes
executing in the system.
Benefits of Cooperating Processes
1. Information sharing
2. Computation speedup
3. Modularity
4. Convenience
INTER-PROCESS COMMUNICATION
Operating systems provide the means for cooperating processes to communicate
with each other via an inter process communication (PC) facility.
IPC provides a mechanism to allow processes to communicate and to
synchronize their actions. IPC is best provided by a message passing system.
Inter process communication is achieved through,
Shared Memory
Message Passing
Basic Structure:
If processes P and Q want to communicate, they must send messages to and
receive messages from each other; a communication link must exist between
them.
Physical implementation of the link is done through a hardware bus , network
etc,
There are several methods for logically implementing a link and the operations:
1. Direct or indirect communication
2. Symmetric or asymmetric communication
3. Automatic or explicit buffering
4. Send by copy or send by reference
5. Fixed-sized or variable-sized messages
1. Direct Communication
Each process that wants to communicate must explicitly name the recipient or
sender of the communication.
A communication link in this scheme has the following properties:
i. A link is established automatically between every pair of processes that want
to communicate. The processes need to know only each other's identity to
communicate.
ii. A link is associated with exactly two processes.
iii. Exactly one link exists between each pair of processes.
THREADS – OVERVIEW
A thread is the basic unit of CPU utilization.
It is sometimes called as a lightweight process.
It consists of a thread ID, a program counter, a register set and a stack.
It shares with other threads belonging to the same process its code section, data
section, and resources such as open files and signals.
Benefits of multithreaded programming
Responsiveness
Resource Sharing
Economy
Utilization of MP Architectures
MULTITHREADING MODELS
1. Many-to-One
2. One-to-One
3. Many-to-Many
1. Many-to-One:
Many user-level threads mapped to single kernel thread.
Used on systems that does not support kernel threads.
One thread blocking causes all to block
Multiple threads may not run in parallel on muticore system because only one
may be in kernel at a time. Few systems currently use this model
2.One-to-One:
Each user-level thread maps to kernel thread
Creating a user-level thread creates a kernel thread
CPU SCHEDULING
CPU scheduling is the basis of multi-programmed operating systems.
The objective of multiprogramming is to have some process running at all times,
in order to maximize CPU utilization.
Scheduling is a fundamental operating-system function.
Almost all computer resources are scheduled before use.
CPU Scheduler
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 ready queue is not necessarily a first-in, first-out (FIFO) queue. It may be a
FIFO queue, a priority queue, a tree, or simply an unordered linked list.
Preemptive Scheduling
CPU scheduling decisions may take place under the following four
circumstances:
Non-Preemptive Scheduling
In non preemptive scheduling, once the CPU has been allocated a process, the
process keeps the CPU until it releases the CPU either by termination or by
switching to the waiting state.
This scheduling method is used by the Microsoft windows environment.
Dispatcher
The dispatcher is the module that gives control of the CPU to the process
selected by the short-term scheduler.
This function involves:
1. Switching context
2. Switching to user mode
3. Jumping to the proper location in the user program to restart that program.
When a task becomes runnable, it is added to the tree. If a task on the tree is not runnable
(for example, if it is blocked while waiting for I/O), it is removed.
Generally speaking, tasks that have been given less processing time (smaller values of
vruntime) are toward the left side of the tree, and tasks that have been given more processing
time are on the right side.
According to the properties of a binary search tree, the leftmost node has the smallest key
value, which for the sake of the CFS scheduler means that it is the task with the highest
priority.
Because the red-black tree is balanced, navigating it to discover the leftmost node will
require O(logN) operations (where N is the number of nodes in the tree).
However, for efficiency reasons, the Linux scheduler caches this value in the variable rb
leftmost, and thus determining which task to run next requires only retrieving the cached
value.
Let’s examine the CFS scheduler in action: Assume that two tasks have the same nice
values. One task is I/O-bound and the other is CPU-bound.
Typically, the I/O-bound task will run only for short periods before blocking for additional
I/O, and the CPU-bound task will exhaust its time period whenever it has an opportunity to
run on a processor.
Therefore, the value of vruntime will eventually be lower for the I/O-bound task than for the
CPU-bound task, giving the I/O-bound task higher priority than the CPU-bound task.
At that point, if the CPU-bound task is executing when the I/O-bound task becomes eligible
to run (for example, when I/O the task is waiting for becomes available), the I/O-bound task
will preempt the CPU-bound task.
ProcessesTypes
The two types of processes are
• Cooperating
• Processes
• Independent
• Processes
Cooperating process
• Can affect or be affected by the execution of another process
• A process that shares data with other processes or sharea logical address space(both
data and code)
Advantages
• Information sharing
• Computation speedup
• Modularity
• Convenience
Independentprocesses
• Cannot affect or be affected by the execution of another process
• Aprocess that does not share data
Atomicoperation
• Atomic operation means an operation that completes in its entirety without
interruption.
• The statements
counter++;
counter--;
must be performed atomically
Race Conditions
The situation where several processes access and manipulate shared data concurrently and
the out come of execution depends on the particular order in which the access takes place are
called race conditions
How can processes coordinate (orsynchronise) in order to guard again strace conditions?
The final value of the shared data depends upon which process finishes last. To prevent
race conditions, concurrent processes must be synchronized.
Types of Solutions
• Software solutions Algorithms whose correctness does not rely on any other assumptions.
• Hardware solutions Rely on some special machine instructions.
• Operating System solutions Provide some functions and data structures to the
programmer through system / library calls.
• Programming Language solutions Linguistic constructs provided as part of a language.
Two-ProcessSolutions
Consider an algorithm that is applicable to only two processes at a time with the following
notation.
Initial notation
• Only 2 processes, P0 and P1
• When usually just presenting process Pi, always denotes other process Pj(i != j)
MUTEX LOCKS
• Mutex is short for Mutual exclusion
• Mutex lockis used to solve the critical-section problem
◦ Used to ensure exclusive access to data shared between threads
◦ Used to protect critical regions and thus prevent race conditions
Illustration
• Aprocess must acquire the lock before entering a critical section using acquire () function
releases the lock when it exits the critical section using release() function
• Definition of acquire()
acquire()
{
while(!available) ; /* busywait*/
available= false;;
}
• Definition of release()
release()
{
available= true;
}
• Structure
SYNCHRONIZATION HARDWARE
SEMOPHORES
Semaphore is asynchronization tool (provided by the OS) that does not require busy
waiting .Semaphore is a variable that has an integer value
• May be initialized to a non negative number
• Wait operation decrements the semaphore value
• Signal operation increments semaphore value
Logical Definition
A semaphore S is an integer variable that ,apart from initialization ,can only be accessed
through 2 atomic and mutually exclusive operations:
• wait(S)-Originallycalled P(), from Dutch proberen―to test ‖
• signal(S) -Originally called V(), from Dutch verhogen ―to increment ‖
Usage of Semaphore
The fundamental principle of semaphore is this: Two or more processes can cooperate by
means of simple signals such that a process can be forced to stop at a specified place until it has
received a specified signal.
wait(mutex);
critical section
signal(mutex);
remaindersection
}while(1);
Usage2:synchronization of 2 processes
Suppose that we have two processes, P1with statement s1 and P2with statement s2. We
require that s2 be executed only after s1 has completed. This can be done with a shared semaphore
variable,mutex, initialized to 0, and by inserting the following statements inP1
s1;signal(mutex);
Advantages
• Applicable to any number of processes on either a single processor or multiple
processors .It is simple and therefore easy to verify
• It can be used to support multiple critical sections
Disadvantages
• Busy-waiting consumes processor time
• Starvation is possible when a process leaves a critical section and more than one
process is waiting.
To avoid busy waiting, when a process has to wait, it will be put in a blocked queue of
processes waiting for the same event. Hence a semaphore is defined as a record
Type def struct
{
int value;
struct process *L;
}semaphore;
Semaphore Operations
Semaphore operations now defined as
When a process must wait for a semaphore S,it is blocked and put on the semaphore’s
S.value—;
if(S.value<0)
{
add this process to
S.L;block();
}
signal(S)
The signal operation removes (assume a fair policy like FIFO) one process from the queue
and puts it in the list of ready processes.
S.value++;
if(S.value <= 0)
{
remove a process P from
S.L;wakeup(P);
}
Implementing S
Wait operation
wait(S1);
C––;
if(C<0)
{
signal(S1);
wait(S2);
}
signal(S1);
signal operation
wait(S1);
C++;
if(C<=0)
signal(S2);
else
signal(S1);
• One bad(or malicious) process can fail the entire collection of processes
CRITICAL REGION
MONITORS
A monitor is high-level synchronization construct that allows the safe sharing of an
abstract data type among concurrent processes.It can be implemented using semaphores.
Syntax
Key Idea:Only one process may be active with in the monitor at a time monitor
monitor-name
{
shared variable declarations
procedure body P1 (…)
{
initialization code
}
}
Monitor is a software module containing : one or more procedures an initialization sequence
local data variables
Characteristics
• Local data variables are accessible only by the monitor process enters monitor by
invoking one of its procedures
• Only one process may be executing in the monitor at a time.
MonitorConditions
To allow a process to wait with in the monitor ,a condition variable must be declared, for
example:
condition x,y;
Condition Variables
Condition variables are local to the monitor (accessible only within the monitor).Condition
variables can only be used with the operations cwait and csignal executed on an associated queue.
• starvation.
• Dead lock can occur if every one tries to get their chopsticks at once. Each gets a left
chopstick, and is stuck, because each right chopstick is someone else’s left chopstick.
SYSTEM MODEL DEADLOCK
Deadlock is a situation that occurs when two processes are waiting for the other to
complete before proceeding. The result is that both processes hang.
Types of Deadlock
• Process Deadlock
◦ A process is deadlocked when it is waiting on an event which will never happen
• System Deadlock
◦ A system is deadlocked when one or more processes are deadlocked
Condition
Deadlock can arise if four conditions hold simultaneously in a system
Protocol
Ensure that the system will never enter a deadlock state
Deadlock Management
• Deadlock prevention prevent deadlocks by constraining how requests resources can be
made for Deadlock avoidance
◦ Do not grant are source request if this allocation have the potential to lead to a
deadlock Deadlock detection
◦ Always grant resource request when possible. Periodically check for deadlocks
◦ If a deadlock exists, recover from it
• Recovery from deadlock
◦ After detection, allow processes to complete and resources to be reused
◦ May involve destroying and restarting processes
DEADLOCK PREVENTION
Deadlock prevention is to prevent the deadlocks occurrence by ensuring that atleast one of
the conditions cannot hold.
Indirect Method
• Prevent the occurrence of the three following conditions
◦ Mutual Exclusion
◦ Hold and Wait
◦ No-Preemption
DEADLOCK AVOIDANCE
A decision is made dynamically whether the current resource allocation request will ,if
granted, potentially lead to a deadlock.It requires knowledge of future process request.
Each process must require that the system has some addition alapriori information
available about its resources requirements:
Simple stand most use ful model requires that each process declare the maximum number
of resources of each type that it may need.
Safe State
When a process requests an available resource, system must decide if immediate
allocation leaves the system in a safe state.
Example
Let’s assume a very simple model: each process declares its maximum needs. In this case,
algorithms exist that will ensure that no unsafe state is reached.
There exists a total of 12 tape drives. The current state looks like this:
Sequence1:
The sequence <P1, P0, P2> is a workable
sequence.At time t0,
Number of free tape drives=Total number of tape drives–Number of tape drives
allocated
12 –(5+2+2)
12 – 9
3.
ProcessP1
P1is allocated with all its tape drives and returned it.
ProcessP0
P0is allocated with all its tape drives and returned it.
ProcessP2
P2is allocated with all its tape drives and returned it.
Sequence2:
Suppose P2 requests and is given one more tape drive. What happens then?
Sequence1:
Is the sequence <P1, P0, P2> a safe sequence or not?
At time t0,
Number of free tape drives =Total number of tape drives–Number of tape drives
allocated
12 –(5+2+3)
12 – 10 =2.
ProcessP1
P1 is allocated with all its tape drives and return edit.
ProcessP0
P0is put in wait state since P0 requested resources (5tapes) are unavailable.
UNIT-4
STORAGE MANAGEMENT AND FILE SYSTEMS INTERFACE
STORAGE MANAGEMENT
Storage management is the process of
• Controlling and coordinating computer memory
• Assigningportions called blocks to various running programs too ptimizeoverall
system performance(Allocating Process)
• Freeing it forreuse whenno longerneeded (Deallocating process)
• Memory management residesinhardware,in the OS(operatingsystem) ,and in programs
and applications.
CPU hardware compare severy address generated in user mode with the registers
• A program executing in user mode at tempt to access OS memory or other users
memory results in a trap to the OS,which treats the attempt as a fatal error
• This prevents a user program from accidentally or deliberately modifying the code or
datastructures of either the OS or other users
Address Binding
• Address Binding is the process of converting the (relative or symbolic) address used in
a program to an actual physical (absolute) RAM address.
• Program must be brought into memory and placed within a process for it to be
executed.
Binding Steps
The stages involved in the binding of instructions and data to memory addresses is listed
below
Compile
Time
◦ If memory location is known a priori, absolute code can be generated; must recompile code
if starting location changes.
• Load time
◦ If memory location is not known at compile time then the relocatable code must be
generated.
• Execution time
Binding delayed until runtime if the process can be moved during its execution Logical Vs
Physical Address Space
The concept of a logical address space that is bound to a separate physical address space
is central to proper memory management.
Logical address
Logical address is the address generated by the CPU. It is also referred to as virtual
address.Physical address
Physical address is the address seen by the memory unit.
Virtual Address
Virtual Address is the logical address results in the execution-time address-binding
scheme.
Note:
• Logical and physical addresses are the same incompile-time and load-time address-
binding schemes;
• Logical (virtual) and physical addresses differ in execution-time address-binding
scheme.
Advantage
• Better memory-space utilization
• Used routine is never loaded
• Useful when large amounts of code are needed to handle in frequently occurring cases
• No special support from the operating system is required implemented through
program design.
Program vs.Memorysizes
What to do when program size is larger than the amount of memory/partition (that exists
or can be) allocated to it?
Advantages
• Changing libraries does not affect previously compiled programs.
• No address resolution necessary at run time
Disadvantages
• The executable size will be larger.
• Multiple copies of code might be in memory
◦ Wastes both disk space and main memory
• New version of library =>must recompile
Stub
With dynamic linking ,a stub is included in the image for each library-routine reference.A
Stub is a small piece of code that indicates how to locate the appropriate memory-resident library
routine,or how to load the library if the routine is not already present.
How it works
• Use a stub to replace the whole library routine
• Enough information to locate the library routine
• When the stub is executed, it replace sit self with the address of the routine and
executes the routine.
◦ Only one copy of library code is needed for all programs.
◦ Such library is also known as a shared library.
SWAPPING
Swapping refers to moving entire processes in and out of main memory.
Purpose
A process to be executed should be available in the main memory for execution. All the
process are stored in the backing store temporarily. The purpose of swapping is to move the
process between main memory and backing store.
Memory Allocation
Memory Allocation is the process of assigning blocks of memory on request for the
program execution.
• Single-partition allocation
• Multiple-partition allocation
Single-partition allocation
In this type of allocation, relocation-register scheme is used to protect user processes
fromeach other, and from changing operating-systemcode and data.
Memory Protection
• Memory protection is to prevent a process from accessing memory it does not
own.Memory protection can be achieved by combining relocation register with the
limit register. Relocation-register scheme provides an effective way to allow the
operating-systemsize to change dynamically.
• Relocation-register scheme used to provide the memory protection by
• Relocation register contains value of smallest physical address
• Limit register contains range of logical addresses
• The dispatcher loads the relocation and limit registers with the correct values as part
of the context switch
• Each logical address must be less than the limit register.
• Then ,MMU maps the logical address dynamically by adding the value in there
location register which is sent to memory.
Advantages
• Simple,easy to understand and use
Disadvantages
• Lead stop oorutilization of processor and memory
• Users job is limited to the size of available memory
Memory Allocation
Fixed Partitions Scheme
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 in to the free partition. When the process terminates ,the
partition becomes available for another process.
Advantages
• Any process whose size is less than or equal to the partition size can be loaded into
any available partition.
• Supports multi programming.
Disadvantages
• If a program is too big to fit into a partition use overlayer technique.
• Memory use is in efficient ,i.e., block of data loaded into memory may be smaller than
the partition . It is known as internal fragmentation.
First fit
• Allocate the first hole that is big enough;
• Process is placed in the first hole it can fit in.
Best fit
• Allocate the smallest hole that is big enough;must search entire list,unless ordered by
size.
• Produces the smallest left over hole
Example
Allocation of 16 MB block using three placement algorithms
Performance Issues
• Best Fit usually performs the worst,since it leaves behind the smallest possible hole
• First Fit is simplest to implementand usually fastest and best
• Next fit is usually a little worse than First Fit
Fragmentation
Fragmentation occurs when a system contains total free memory to satisfy any request, but
that can’t be utilized.
Internal f ragmentation
Internal fragmentation is that the allocated memory may be slightly larger than requested
memory ;this size difference is memory internal to a partition,but not being used.
Solution
• Blocks are usually split to prevent internal fragmentation.
External fragmentation
External fragmentation is done when the total memory space exists to satisfy a request,
but it is not contiguous.
Solution
• Compaction
• Noncontiguous logical-address space
◦ Paging
◦ Segmentation
Compaction
The goal is to shuffle memory content stop place all free memory to ge ther in one large
block.
Compactionis
• Possible only if relocation is dynamic,and is done at execution time.
• Determine its cost
• Impossible only if relocation is static,and is done at assembly or load tim.
Simple Compaction algorithm
• Move all processes toward one end of memory.
• All holes move in the other direction which produces large hole of memory.
• Drawback
◦ Very expensive scheme.
I/O problem
1) Latch job in memory while it is in I/O
2) Do I/O only in to OS buffers.
PAGING
Paging is a memory-management scheme that permits the logical address space of a
process can be non contiguous;process is allocated physical memory whenever the latter is
available.
Basic Method
Frames
Physical memory is divided into fixed-sized block scalled frames (size is power of 2,
between 512 bytes and 8192 bytes).
Illustration of Paging
The hard ware support for paging is illustrated below.
then
Paging Example
Consider the memory of
Page Size =4 bytes
Physical Memory= 32 Bytes (32/4=8 Pages)
Protection
• Memory protection is implemented by associating protection bit with each frame.
Valid-invalid bit attached to each entry in the page table:
• ―Valid‖ indicates that the associated page is in the process’ logical address space, and
is thus a legal page
• ―Invalid ‖indicates that the page is not in the process’logical address space
• Simple partition management due to discontinuous loading and fixed partition size
• No compaction is necessary
Disadvantages of paging
• Job size <=memory size
• Internal fragmentation
SEGMENTATION
Segmentation is the Memory Management Scheme that supports user view of memory.
Logical address space is the collection of segments
Basic Method
Program
A program is a collection of segments.
Segment
A segment is a logical unit of a program such as
• Main program, procedure,function
• stack,symbol table,arrays
Each segment has a name and a length.The addresses specify both the segment name and
the offset within the segment.
Features
• Protect each entity independently
• Allow each segment to grow independently
User’s View of a
Program
User’sView of a Program
Segmentation Architecture
• Segments are numbered and referred to by a segment number ,thus a logical address
consists of a tuple:
Logical Address
◦ base–contains the starting physical address where the segments reside in memory
Example
Suppose a program is divided into 5 segments:
• Segment 0:Subroutine
• Segment1: sqrt function
• Segment2:main program
• Segment3:stack
• Segment 4: symbol table
The segment table is as follows:
Segment Limit Base
0 1000 1400
1 400 6300
2 400 4300
3 1100 3200
4 1000 4700
In Segmented Paging, the main memory is divided into variable size segments which are further
divided into fixed size pages.
3. The logical address is represented as Segment Number (base address), Page number and
page offset.
Characteristics
1) A disk can be rewritten in place; it is possible to read a block from the disk, modify
the block, and write it back into the same place.
2) A disk can access directly any given block of information it contains.
Thus it is simple to access any file either sequentially or randomly, and switching from
one file to another.
Design problems
A file system poses two quite different design problems.
1) Defining how the file systems should look to the user.
2) Creating algorithms and data structures to map the logical file system onto the
physical secondary storage devices.
I/O control
• The lowest level, the I/O control, consists of device drivers and interrupts handlers to
transfer information between the main memory and the disk system.
Basic file system
• The basic file system needs only to issues generic commands to the appropriate device
driver to read and write physical blocks on the disk.
File organization module
• The file organization module knows about files and their logical blocks, as well as
physical blocks. By knowing the type of file allocation used and the location of the file,
the file organization module can translate logical block addresses to physicalblock
addresses for the basic file system to transfer.
Logical file system
• Finally the logical file system manages metadata information. Metadata includes all of
the file-system structure except the actual data. The logical file system manages the
directory structure to provide the file organization module with the information the latter
needs, given a symbolic file name.
Structure Overview
On-disk
On-disk is the file system may contain information about
• How to boot an operating system stored there
• The total number of blocks
• The number and location of free blocks
• The directory structure
• Individual files
On-disk structure
In-memory
In-memory information is used for both file-system management and performance
improvement via caching.
In-memory Structure
The structures may include the ones described below
• In-memory mount table
◦ Contains information about each mounted volume
• In-memory directory-structure
◦ Holds the directory information of recently accessed directories
• System-wide open-file table
◦ Contains a copy of the FCB of each open file as well as other information
• Per-process open file table
◦ Contains a pointer to the appropriate entry in the system-wide open file table as
well as other information
File Creation
To create a new file,
• An application program calls the logical file system.
◦ The logical file system knows the format of the directory structures. Allocates a
new FCB.
• Reads the appropriate directory into the memory
• Updates it with the new file name and FCB and
• Writes it back to the disk.
◦ Once the file is found, the FCB is copied into a system-wide open-file table in
memory.
◦ Entry is made in the per-process open-file table pointing to the entry in the
existing system –wide open-file table
other fields can include a pointer to the
current location in the file and
access mode (read or write) in which the file is open.
◦ open() call returns a pointer to the appropriate entry in the per-process file-system
table. This pointer is named as
File descriptor – UNIX
File Handle – Windows 200
• Raw disk is used where no file system is appropriate. It holds information needed bydisk
Boot information
• Boot information can be stored in a separate partition. It has its own format because at
boot time the system does not have file system device drivers loaded and therefore
cannot interpret the file system format.
Boot Loader
A boot loader contains multiple file systems and multiple operating systems occupy the boot space.
Once loaded, it can boot only one operating systems available on the disk. The disks can have
multiple partitions, each containing a different type of file system and a different operating system.
Root partition
• The root partition contains the operating system kernel and other system files. It is
mounted at boot time. Other partitions are automatically mounted at boot or manually
mounted later, depending on the operating system.
• The operating system verifies that the device contains a valid file system in the
successful mount operation.
◦ Validity checked by asking the device driver to read the device directory and
verifying that the directory has the expected format.
◦ Invalid Format – partition have consistency checked and corrected with or without
user intervention.
• Finally the operating system notes in its in-memory mount table structure that a file -
system is mounted along with the type of a file system.
DIRECTORY IMPLEMENTATION
The directory can be implemented in two ways
1) Linear List
2) Hash Table
Linear List
Linear list of file names with pointer to the data blocks is the simplest method of
implementing a directory which requires linear search to find a particular entry.
• Simple to program.
• Time-consuming to execute.
• File Creation
◦ Search the directory to be sure that no existing file has the same name
• File Deletion
• File Reuse
◦ Mark the entry as unused by assigning a special name or a blank name
◦ Attach to the list of free directory entries.
◦ Copy the last entry in the directory into the freed location and to decrease the
length of the directory.
Advantages
• A linked list is used to decrease the time required to delete a file.
Disadvantages
• Finding the files in linear search
• Searching is slow so some operating system uses software cache to store more recently
used directory information.
Hash Table
A hash table is another data structure which is used for file directory. Hash Table is a
linear list with hash data structure.
The hash table takes a value computed from the file name and returns a pointer to the file
name in the linear list.
Collisions
A collision is a situation where two file names hash to the same location. Alternatively, a
chained-overflow hash table can be used to avoid collisions
Disadvantages
• Greatly decrease the directory search time.
• Hash table are generally fixed size and
• Dependence of the hash function on that size.
ALLOCATION METHODS
Allocation methods is a methods used to allocate space to the files for
• Utilizing the disk space effectively
• Accessing the files quickly
Methods
The three major methods for allocating disk space are
1) Contiguous allocation
2) Linked allocation
3) Indexed allocation
Contiguous Allocation
• Each file occupies a set of contiguous blocks on the disk.
• File location is defined by the disk address of the first block and its length
◦ Example:
If the file is n blocks long and starts at location b, then it occupies blocks b,
b+1, b+2….b+n-1
• Both sequential access and direct access are supported by the contiguous allocation
Disadvantage
Linked Allocation
• Each file is a linked list of disk blocks, the disk blocks may be scattered on the disk.
• Directory contains
◦ A pointer to the first and (optionally the last) block of the file
◦ Pointer is initialized to null to signify an empty file
• Each block contains
◦ a pointer to the next block and the last block contains a NIL pointer
File Operations
• To create a new file,
◦ Create a new entry in the directory in the directory
• To write to a file,
◦ Removes the first free block and writes to that block
◦ New block is then linked to the end of the file
• To read a file,
◦ the pointers are just followed from block to block
Advantages
• No external fragmentation
Disadvantages
Indexed Allocation
• Solution to the problem of both contiguous and linked allocation
◦ By bringing all the pointers together into one location called the index block
• Each file has its own index block which is an array of disk block addresses. The ith
entry in the index block points to the ith block of the file.
Logical View
◦ a block from the free space manager and its address is put in the ith index block
entry
1) Linked Scheme
◦ An index block is normally one disk block
◦ To allow large files, several index blocks are linked together Next
address is nil ora pointer to another index block
2) Multi Level index
◦ A variant of the linked representation use a first level index block to
point to a setof second-level index blocks
◦ This approach could be continued to a third or fourth level,
depending on thedesired maximum file size
3) Combined Scheme
◦ Another alternative, used in the UFS is to keep the first say 15
pointers of the index block in the file’s inode
◦ The first 12 of these pointers point to direct blocks that are they contain
addressesof blocks that contain data of the file.
◦ The next three pointers is an indirect blocks
The first points to a single indirect block which is an index block
containing notdata but the addresses of blocks that do contain data.
The second points to a double indirect block which contain the
address of a block that contains the address of blocks that contain
pointers to the actual datablocks.
The last pointer contains the addresses of a triple indirect block.
The Unix inode
Bit Vector
• The free space list is maintained as bit map vector or bit vector
• Each block is represented by 1 bit. If the block is free the bit is 1 if
the block isallocated the bit is 0.
• Disadvantage is efficiency in finding the first free block or consecutive free
blocks onthe disk
• Indeed many computers supply bit-manipulation instructions that can
be usedeffectively for that purpose.
• The calculation of the block number
(number of bits per word) (number of 0-value words)+offset of first 1 bit
Bit vectors are inefficient unless the entire vector is kept in maim memory.
Keeping it inmain memory is possible for smaller disk but not necessarily for larger
ones.
Linked List
• Free space management is to link together all the free disk blocks, keeping a
pointer tothe first free block in a special location on the disk and caching it
in memory.
• The first block contains a pointer to the next free disk block and so on.
• We could keep a pointer to block 2 as the first free block. Block 2 would
contain a pointer to block 3, which would point to block 4, which would
point to block 5 and so on.
Grouping
• A modification of the free list approach is to store the addresses of n free
blocks in the first free block.
• The first n-1 of these blocks is actually free.
• The last block contains the addresses of another n free blocks, and so on.
• The addresses of a large number of free blocks can now be found quickly.
Counting
• Another approach is to take advantage of the fact that generally several
contiguous blocks may be allocated or freed simultaneously particularly
when space is allocated with the contiguous allocation algorithm or through
clustering.
• Thus rather than keeping a list of n free disk addresses, we can keep the
address of the first free block and the number n of free contiguous blocks
that follow the first block.
• Each entry in the free space list than consists of a disk address and count.
DISK STRUCTURE
The traditional head-sector-cylinder, HSC numbers are mapped to linear block
addresses by numbering the first sector on the first head on the outermost track as sector
0. Numbering proceedswith the rest of the sectors on that same track, and then the rest of
the tracks on the same cylinder before proceeding through the rest of the cylinders to the
center of the disk. In modern practice these linear block addresses are used in place
of the HSC numbers for a variety ofreasons:
The linear length of tracks near the outer edge of the disk is much longer than
for those tracks located near the center, and therefore it is possible to squeeze
many more sectors onto outer tracks than onto inner ones.
All disks have some bad sectors, and therefore disks maintain a few spare
sectors that can be used in place of the bad ones. The mapping of spare
sectors to bad sectors in managed internally to the disk controller.
Modern hard drives can have thousands of cylinders, and hundreds of sectors per
track on their outermost tracks. These numbers exceed the range of HSC numbers for
many ( older ) operating systems, and therefore disks can be configured for any
convenient combination of HSC values that falls within the total number of sectors
physically on the drive.
• There is a limit to how closely packed individual bits can be placed on a
physical media, but that limit is growing increasingly more packed as
technological advances are made.
• Modern disks pack many more sectors into outer cylinders than inner ones,
using one of two approaches:
◦ With Constant Linear Velocity, CLV, the density of bits is uniform from
cylinder to cylinder. Because there are more sectors in outer cylinders,
the disk spins slower when reading those cylinders, causing the rate of
bits passing under the read-write head to remain constant. This is the
approach used by modern CDs and DVDs.
◦ With Constant Angular Velocity, CAV, the disk rotates at a constant
angularspeed, with the bit density decreasing on outer cylinders. (These
disks would have a constant number of sectors per track on all
cylinders.)
DISK SCHEDULING
Disk Scheduling Algorithms is an algorithm used to schedule the servicing
of disk I/Orequests.
Types
FCFS
Scheduling
SSTF
Scheduling
SCAN
Scheduling
C-SCAN
Schedulin
LOOK
Schedulin
The disk scheduling algorithms are illustrated with a request queue (0-199)
• Queue = 98, 183, 37, 122, 14, 124, 65, 67
• Head pointer = 53
FCFS Scheduling
FCFS (First-Come First-Served) Scheduling is the simplest form of disk
scheduling. Itsimply servers the first request in the queue.
Illustration
Initially the disk head is at
cylinder 53.Head Movement
Current Next Total
53 98 45
98 183 85
183 37 146
37 122 85
122 14 108
14 124 110
124 65 59
65 67 2
Total 640
Total head movement = 640 cylinders.
FCFS Scheduling
Advantage
• Simple to write and understand
• Perform operations in order requested
• No reordering of work queue
• No starvation: every request is serviced
Disadvantage
• Large seek time as the disk head may have to jump from one extreme to another.
• Does not provide the fastest service.
SSTF Scheduling
SSTF (shortest-seek time first algorithm) scheduling is an algorithm that
selects therequest with the minimum seek time from the current head position.
Illustration
Initially the disk head is at
cylinder 53.Head Movement
Next
Current Total
(Closet)
53 65 12
65 67 2
67 37 30
37 14 23
14 98 84
98 122 24
122 124 2
124 183 59
Total 236
Total head movement = 236 cylinders.
SSTF Scheduling