REedo JGXEr WB2 DC HQ2 U Yfr 5 H 2 Jad RVQM
REedo JGXEr WB2 DC HQ2 U Yfr 5 H 2 Jad RVQM
OS Notes
What is an Operating System?
Basically OS is a
Resource manager with objective of efficient resource use and user
convenience
Control program that controls the execution of user programs to
prevent errors and improper use of the computer
OS Notes
Computer System Components
OS Notes
Abstract View of System Components
OS Notes
Operating System Definitions
OS Notes
Computer startup
OS Notes
Mainframe Systems
In old days H/W used to be card readers, tape drives, line
printers etc (ie basically slow devices as lot of mechanical
parts are involved)
But CPU was relatively quite fast being an electronic
device
So at this time interactive systems were not possible as
they will slow down the execution dramatically because of
slow I/O devices and so CPU will mostly be idle. And
that’s why we needed Batch Systems
OS Notes
Memory Layout for a Simple Batch System
OS Notes
Multiprogrammed Batch Systems
Several jobs are kept in main memory at the same time, and the
CPU is multiplexed among them.
OS Notes
OS Features Needed for Multiprogramming
OS Notes
Time-Sharing Systems–Interactive Computing
OS Notes
Multiprogramming/multitasking
OS Notes
Desktop Systems
OS Notes
Parallel Systems
OS Notes
Parallel Systems (Cont.)
OS Notes
Multiprocessing Architecture
OS Notes
Distributed Systems
OS Notes
Distributed Systems (cont)
OS Notes
General Structure of Client-Server
OS Notes
Real-Time Systems
OS Notes
Real-Time Systems (Cont.)
Hard real-time:
Secondary storage limited or absent, data stored in short
term memory, or read-only memory (ROM)
Conflicts with time-sharing systems as no virtual memory.
Soft real-time
Limited utility in industrial control of robotics
Useful in applications (multimedia, virtual reality, undersea
exploration, planetory rovers) requiring advanced operating-
system features.
Ex. RT Linux
OS Notes
Chapter 2: Computer-System Structures
OS Notes
Computer-System Architecture
Bootstrap:(1) Initialize h/w - like cpu registers, device controllers, memory controllers etc
( 2 ) Loads o.s kernel into the primary memory and then transfers control to O.S
OS Notes
Computer-System Operation
OS Notes
Common Functions of Interrupts
OS Notes
Interrupt Handling
OS Notes
I/O Structure
OS Notes
Two I/O Methods
Synchronous Asynchronous
OS Notes
Device-Status Table
The O.S also need to be able to keep track of many i/o requests at
the same time. For this purpose the O.S uses a table containing an
entry for each i/o device. Such table entry indicates the device type,
address & status( not functioning, idle, busy)
OS Notes
Direct Memory Access Structure
OS Notes
Storage Structure
OS Notes
Moving-Head Disk Mechanism
OS Notes
Magentic Tape
Slow
Good for sequential access and not good for random
access
Generally used as Backup media
OS Notes
Storage Hierarchy
OS Notes
Storage-Device Hierarchy
OS Notes
Migration of A From Disk to Register
OS Notes
Hardware Protection
Dual-Mode Operation
I/O Protection
Memory Protection
CPU Protection
OS Notes
Dual-Mode Operation
OS Notes
Dual-Mode Operation (Cont.)
monitor user
set user mode
OS Notes
I/O Protection
OS Notes
Use of A System Call to Perform I/O
OS Notes
Memory Protection
OS Notes
Contd ..
OS Notes
Use of A Base and Limit Register
OS Notes
Hardware Address Protection
300040 300040 +120900 = 420940
OS Notes
Hardware Protection
OS Notes
CPU Protection
OS Notes
Chapter 3: Operating-System Structures
System Components
Operating System Services
System Calls
System Programs
System Structure
Virtual Machines
System Design and Implementation
System Generation
OS Notes
Common System Components
Process Management
Main Memory Management
File Management
I/O System Management
Secondary Storage Management
Networking
Protection System
Command-Interpreter System
OS Notes
Process Management
OS Notes
Main-Memory Management
OS Notes
File Management
OS Notes
I/O System Management
OS Notes
Secondary-Storage Management
OS Notes
Networking (Distributed Systems)
A distributed system is a collection processors that do not
share memory or a clock. Each processor has its own
local memory.
The processors in the system are connected through a
communication network.
Communication takes place using some protocol.
A distributed system provides user access to various
system resources.
Access to a shared resource allows:
Computation speed-up
Increased data availability
Enhanced reliability
NOTE : O.S usually generalize network access as a form of a
file access, with the details of networking being contained
in the network interfaces device driver
So the objective is to hide the details of getting information
from other system and to make such access as efficient as
possible.
OS Notes
Protection System
OS Notes
Command-Interpreter System
command-line interpreter
shell (in UNIX)
OS Notes
Command-Interpreter System
OS Notes
Operating System Services
OS Notes
Additional Operating System Functions
Additional functions exist not for helping the user, but rather
for ensuring efficient system operations.
• Resource allocation – allocating resources to multiple users
or multiple jobs running at the same time.
• Accounting – keep track of and record which users use how
much and what kinds of computer resources for account
billing or for accumulating usage statistics.
• Protection – ensuring that all access to system resources is
controlled.
OS Notes
System Calls
OS Notes
API – System Call – OS Relationship
OS Notes
Standard C Library Example
C program invoking printf() library call, which calls write()
system call
OS Notes
OS Notes
OS Notes
OS Notes
OS Notes
Passing of Parameters As A Table
Systemc
all 13 (X)
OS Notes
Types of System Calls
OS Notes
MS-DOS Execution
( is a single tasking O.S)
OS Notes
UNIX Running Multiple Programs
Note: UNIX process related system calls fork, exec, exit (return
with error code 0 (no error), any positive no (error no)
OS Notes
Communication Models
Communication may take place using either message
passing or shared memory.
OS Notes
System Programs
OS Notes
MS-DOS Layer Structure
OS Notes
MS-DOS System Structure
OS Notes
UNIX System Structure
OS Notes
UNIX System Structure
OS Notes
Layered Approach
OS Notes
An Operating System Layer
OS Notes
Virtual Machines
OS Notes
Virtual Machines (Cont.)
OS Notes
System Models
OS Notes
Advantages/Disadvantages of Virtual Machines
OS Notes
Java Virtual Machine
OS Notes
Java Virtual Machine
OS Notes
System Design Goals
OS Notes
System Implementation
OS Notes
System Generation (SYSGEN)
OS Notes
Chapter 4: Processes
Process Concept
Process Scheduling
Operations on Processes
Cooperating Processes
Interprocess Communication
OS Notes
Process Concept
OS Notes
Use of A Base and Limit Register
OS Notes
Hardware Address Protection
300040 300040 +120900 = 420940
OS Notes
Process State
OS Notes
Diagram of Process State
OS Notes
Process Control Block (PCB)
Each process is represented in the O.S by a PCB (process/task
control block)
Information associated with each process.
Process state (new, ready, running, waiting, halted)
Program counter
CPU registers (accumulator, index registers, stack pointer,
general purpose register )
CPU scheduling information (process priority, pointer to next
PCB in queue )
Memory-management information (base register, limit
register, page table/segmet table pointer)
Accounting information (amount of CPU or real time used,
time limits, jobs or process no. )
I/O status information (list of i/o devices allocated to this
process, a list of open files)
OS Notes
Process Control Block (PCB)
OS Notes
CPU Switch From Process to Process
OS Notes
Process Scheduling Queues
The objective is to try to keep the CPU as busy as possible for maximum CPU
utilization & throughput – on a single CPU m/c only one process at a time can
be allocated CPU
OS Notes
Ready Queue And Various I/O Device Queues
OS Notes
Representation of Process Scheduling
Note : The process could be removed forcibly from the CPU, as a result of an
interrupt and put back in the ready queue.
Resources that
QUEUES
OS Notes
serve the queue
Schedulers
Job queue
Ready CPU
queue
OS Notes
Schedulers (Cont.)
Short-term scheduler is invoked very frequently (milliseconds) so
(must be fast). { as the process will run for a short time (interactive )
then i/o will occur or time slice will expire }
L.T.S should select a good mix of i/o bound and CPU bound process so
as to make efficient and optimal use of all the i/o devices and CPU
OS Notes
Addition of Medium Term Scheduling
(In time sharing system generally long term scheduler is absent and in place a
medium term scheduler is used)
The key idea behind medium term scheduler is that some times it can be
advantageous to remove process from memory (and from active contention of
CPU) and thus to decrease the degree of multiprogramming. At some later time
the process can be reintroduced into memory and its execution can be continued
where it left off. This scheme is called swapping. Swapping (medium term
scheduler) may be necessary to improve the process mix or because a change in
memory requirement has overcommitted available memory, requiring memory to
be freed.
OS Notes
Context Switch
OS Notes
CPU Switch From Process to Process
OS Notes
Process Creation
OS Notes
Process Creation (Cont.)
OS Notes
Process Termination
When a processes terminates, all the resources of the process,
including physical or virtual memory, open files and i/o buffers are de
allocated by O.S
When Process executes last statement and asks the operating system
to delete it by implictly/explict call to the exit system call
Output data from child to parent (via wait).
Process’ resources are deallocated by operating system.
Parent may terminate execution of children processes (abort).
Child has exceeded allocated resources.
Task assigned to child is no longer required.
Parent is exiting.
Operating system does not allow child to continue if its parent
terminates.
Cascading termination.
User could kill another process using the kill system call (kill
generates a signal to abort a process but the process can ignore it
(in most of the cases )
The wait system call returns the process identifier of a terminated
child, so that the parent can tell which of it’s possibly many children
has terminated
If the parent terminates, however all the children are terminated by
the O.S. Without a parent, Unix does not know to whom to report
the activities of the child
OS Notes
Cooperating Processes
Independent process cannot affect or be affected by the
execution of another process. ( any process that does not
share any data (temporary or persistent with any other
processes is independent )
Cooperating process can affect or be affected by the
execution of another process ( any process that share
data with other processes is co – operating )
Advantages of process cooperation
Information sharing
Computation speed-up (big program can be divided in many
small program and they can run concurrently and can share
data whenever required
Modularity (logically breaking down a program in smaller
ones make the processes/program more modular
Convenience
OS Notes
Producer-Consumer Problem
To be co-operative (share data ) the process need to
communicate IPC (inter process communication )
OS Notes
Bounded-Buffer – Shared-Memory Solution
Shared data
#define BUFFER_SIZE 10
Typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0; //rear
int out = 0; //front
// Solution is correct, but can only use BUFFER_SIZE-1
elements
OS Notes
Bounded-Buffer – Producer Process
item nextProduced;
while (1) {
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
}
OS Notes
Bounded-Buffer – Consumer Process
item nextConsumed;
while (1) {
while (in == out)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
}
OS Notes
Interprocess Communication (IPC)
1) shared memory (as in producer consumer problem)
2) Message system as discussed below
OS Notes
Direct Communication
Processes must name each other explicitly (symmetric addressing ):
send (P, message) – send a message to process P
receive(Q, message) – receive a message from process Q
Asymmetric addressing : only sender need to name the
receiver, the receiver is not required to name the sender
{ send (p, messages), receive (id, message ), id will be
automatically set the identity of the message sender }
Properties of communication link
Links are established automatically.
A link is associated with exactly one pair of communicating
processes.
Between each pair there exists exactly one link.
The link may be unidirectional, but is usually bi-directional.
OS Notes
Indirect Communication
Messages are directed and received from mailboxes (also
referred to as ports).
Each mailbox has a unique id.
Processes can communicate only if they share a mailbox.
Properties of communication link
Link established only if processes share a common mailbox
A link may be associated with many processes. (many
members may be members of mailbox)
Each pair of processes may share several communication
links. (some process may be sharing more than one
mailbox)
Link may be unidirectional or bi-directional.
OS Notes
Indirect Communication
Operations
create a new mailbox
send and receive messages through mailbox
destroy a mailbox
Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
OS Notes
Synchronization
OS Notes
Buffering
OS Notes
Other IPC Issues
OS Notes
OS Notes
Chapter 5: Threads
OS Notes
Single and Multithreaded Processes
Open files Open files
PC PC
Program
Counter
OS Notes
Benefits
OS Notes
User Threads
Thread implementation & management done by user-level
threads library (thread _create, thread_exit, thread_wait, etc.)
rather than via system calls.
Disadvantages:
If the kernel is single threaded, then any user level thread
executing a system call will cause the entire task to wait, until
the system call returns because kernel schedule only processes
& processes waiting for I/O (system call ) are put in wait queue
& can not be allotted CPU.
OS Notes
Kernel Threads
The threads are implemented & managed with the help of O.S. kernel. The
smallest unit of processing the kernel will recognize & thus schedule is a thread.
So each thread may be schedule independently.
Process B
Process A …..
t1b t100b
t1a
So process B could receive 100 times the CPU time than process A receives.
Now if a thread make an I/O and a system call, the whole process need not be
blocked (only that thread is blocked) & thus another thread in the same process
run during this time.
Examples
- Windows 95/98/NT/2000
- Solaris
- Tru64 UNIX
- BeOS
- Linux
OS Notes
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
OS Notes
Many-to-One
Example :- Unix
OS Notes
Many-to-One Model
OS Notes
One-to-One
Examples
- Windows 95/98/NT/2000
- OS/2
OS Notes
One-to-one Model
OS Notes
Many-to-Many Model
OS Notes
Many-to-Many Model
OS Notes
Pthreads
OS Notes
Solaris 2 Threads
Supports kernel & user level threads
/or simply
process
OS Notes
Thread in Solaris 2
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Multiple-Processor Scheduling
Real-Time Scheduling
Algorithm Evaluation
OS Notes
Basic Concepts
CPU scheduling is the basis of multi programmed O.S
Our Goal Maximum CPU utilization
Several processes are kept in memory at one time. When one
processes has to wait (for say i/o ), the O.S’s CPU scheduler
takes the CPU away from that process and gives the CPU to
another process.
The process in memory and ready to run are kept in the ready
queue.
The O.S scheduler CPU scheduler (or short term scheduler)
selects a job from the ready queue to run.
Ready queue is not necessarily in first-in-first-out (FIFO) order.
A ready queue may be implemented as a FIFO queue, a priority
queue, a tree or simply an unordered link list.
The records in the ready queue are the PCBs
Process execution generally consists of alternative cycles of
CPU execution and I/O wait. CPU burst cycle–I/O Burst Cycle
OS Notes
Alternating Sequence of CPU And I/O Bursts
OS Notes
Histogram of CPU-burst Times
Note: fig shows there are large no. of short CPU burst, and there is a small
no. of long I/O bursts. An i/o bound program would typically have many
very short CPU burst and long i/o bursts. An CPU bound program may
have a few very long CPU bursts. This distribution can be important in the
selection of an appropriate CPU scheduling
OS Notes
CPU Scheduler
OS Notes
Dispatcher
OS Notes
Scheduling Criteria
OS Notes
Optimization Criteria
OS Notes
First-Come, First-Served (FCFS) Scheduling
P1 P2 P3
0 24 27 30
OS Notes
FCFS Scheduling (Cont.)
First to enter the ready queue will be first to be allocated to CPU
When a process enters the ready queue, its PCB is linked in the
tail of the ready queue in this scheme.
FCFS is nonpreemptive ( cond. 1 & 4)
Suppose that the processes arrive in the order
P2 , P3 , P1 .
The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case.
Convoy effect as all the other processes wait for one big
process to get off the CPU resulting in lower CPU and device
utilization ( as this will not decrease the waiting time of the big
process as it will increases the waiting time of all the other
processes
Soln short process first and then schedule long process to
avoid convoy effect
OS Notes
Shortest-Job-First (SJR) Scheduling
Associate with each process the length of its next CPU
burst (in case of a tie we can use FCFS ). Use these
lengths to schedule the process with the shortest time.
Two schemes:
nonpreemptive – once CPU given to the process it cannot
be preempted until completes its CPU burst.
preemptive – if a new process arrives with CPU burst length
less than remaining time of current executing process,
preempt the running process. This scheme is known as the
Shortest-Remaining-Time-First (SRTF).
SJF is optimal – gives minimum average waiting time for
a given set of processes. { as by moving a short process
before a long one, the waiting time of the short processes
decreases more than it increases the waiting time of the
long process. Consequently the average waiting time
decreases
OS Notes
Example of Non-Preemptive SJF
P1 P3 P2 P4
0 3 7 8 12 16
OS Notes
Example of Preemptive SJF
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
OS Notes
Determining Length of Next CPU Burst
n1 tn 1 n .
OS Notes
Examples of Exponential Averaging
=0
n+1 = n
Recent history has no effect. Last prediction has all the
weightage
=1
n+1 = tn
Only the actual last CPU burst counts.
If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 + …
+(1 - )j tn -1 + …
+(1 - )n=1 tn 0
OS Notes
Priority Scheduling
A priority number (integer) is associated with each process
The CPU is allocated to the process with the highest priority
(smallest integer highest priority). { if equal priority use FCFS
to break the tie }
Preemptive (a preemptive priority scheduling algo. Will
preempt the CPU if the priority of the newly arrived process
is higher than the priority of the currently running process )
nonpreemptive
SJF is a priority scheduling where priority is the predicted next
CPU burst time.
Internal priority (set by OS) generally assigned based on the
factors like time limits, memory requirements, the no. of open
files and ratio of average i/o burst to average CPU burst.
External Priorities (set due to the criteria's external to the O.S)
are generally assigned based on the factors like importance of
the process, the type and amount of funds being paid for
computer use, the department sponsoring the work and other
often political factors
Problem Starvation – low priority processes may never
execute.
Solution Aging – as time progresses increase the priority of
the process. (for example say a waiting process’s priority is
increased by 1 every 15 minutes)
OS Notes
Round Robin (RR)
Each process (can pick process from the ready queue in
FIFO order) gets a small unit of CPU time (time
quantum), usually 10-100 milliseconds. After this time
has elapsed, the process is preempted and added to the
end of the ready queue.
If there are n processes in the ready queue and the time
quantum is q, then each process gets 1/n of the CPU time
in chunks of at most q time units at once. No process
waits more than (n-1)q time units.
Performance
q large it will be like FIFO
q small q must be large with respect to context switch,
otherwise overhead is too high. most of the time will be
wasted in context switches so if context switch time is
added in, the average turnaround time increases for a
smaller time quantum, since more context switch will be
required
OS Notes
Example of RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
OS Notes
Time Quantum and Context Switch Time
OS Notes
Multilevel Queue
Ready queue may be partitioned into separate queues:
foreground processes
background processes
Each queue has its own scheduling algorithm, for
example
foreground – RR
background – FCFS
Scheduling must be done between the queues.
Fixed priority scheduling; (i.e., serve all from foreground
then from background). Possibility of starvation.
Time slice – each queue gets a certain amount of CPU time
which it can schedule amongst its processes; i.e., 80% to
foreground in RR
20% to background in FCFS
Scheduling among the queues is preemptive if a job in
low priority queue is running & a job in high priority process
arrives then job in low priority process is preempted.
OS Notes
Multilevel Queue Scheduling
OS Notes
Multilevel Feedback Queue
This scheme allow the processes to move among the queues :
the idea is to separate processes with different CPU – burst
characteristics. If a process uses too much time it will be moved
to the lower priority queue. This scheme leaves i/o bound and
interactive processes in the high priority queues. Similarly a
process that waits too long in a lower priority queue may be
moved to a higher priority queue. This form of aging prevents
starvation.
Scheduling among queue is fixed priority preemptive
scheduling.
Multilevel-feedback-queue scheduler defined by the following
parameters:
number of queues
scheduling algorithms for each queue
method used to determine when to upgrade a process
method used to determine when to demote a process
method used to determine which queue a process will enter
when that process needs service (generally a
process enters in queue 0 (highest priority queue ) and if it
is taking too much time than it is gradually demoted to the
lower priority queues.)
OS Notes
Example of Multilevel Feedback Queue
Three queues:
Q0 – time quantum 8 milliseconds
Q1 – time quantum 16 milliseconds
Q2 – FCFS
Scheduling
A new job enters queue Q0 which is served in FCFS order.
When it gains CPU, job receives 8 milliseconds. If it does
not finish in 8 milliseconds, job is moved to queue Q1.
At Q1 job is again served (may be in FCFS ) and receives
16 additional milliseconds. If it still does not complete, it is
preempted and moved to queue Q2.
OS Notes
Multilevel Feedback Queues
OS Notes
Multiple-Processor Scheduling
OS Notes
Algorithm Evaluation
OS Notes
Chapter 7: Process Synchronization
Background
The Critical-Section Problem
Synchronization Hardware
Semaphores
Classical Problems of Synchronization
Critical Regions
Monitors
Synchronization in Solaris 2 & Windows 2000
OS Notes
Background
OS Notes
Bounded-Buffer
Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int counter = 0;
OS Notes
Bounded-Buffer
Producer process
item nextProduced;
while (1) {
while (counter == BUFFER_SIZE)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) ;
counter++;
}
OS Notes
Bounded-Buffer
Consumer process
item nextConsumed;
while (1) {
while (counter == 0)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) ;
counter--;
}
OS Notes
Bounded Buffer
The statements
counter++;
counter- -;
OS Notes
Bounded Buffer
register1 = counter
register1 = register1 + 1
counter = register1
register2 = counter
register2 = register2 – 1
counter = register2
OS Notes
Bounded Buffer
OS Notes
Bounded Buffer
OS Notes
Race Condition
OS Notes
The Critical-Section Problem
OS Notes
Solution to Critical-Section Problem
1. Mutual Exclusion. If process Pi is executing in its critical
section, then no other processes can be executing in their
critical sections.
2. Progress. If no process is executing in its critical section
and there exist some processes that wish to enter their
critical section, then the selection of the processes that
will enter the critical section next cannot be postponed
indefinitely.
3. Bounded Waiting. A bound must exist on the number of
times that other processes are allowed to enter their
critical sections after a process has made a request to
enter its critical section and before that request is
granted.
Assume that each process executes at a nonzero speed
No assumption concerning relative speed of the n
processes.
OS Notes
Initial Attempts to Solve Problem
( general structure of a typical process – which is sharing some
critical data with other processes )
OS Notes
Synchronization Hardware
Some simple h/w instructions (like testandset & Swap )
are available on many systems to solve the critical
section problem
Testandset modify the content of a word atomically
.
boolean TestAndSet(boolean &target) {
Should
boolean rv = target; be
target = true; execut
ed
atomic
return rv; ally
}
Note : in a uni-processor system solving critical
section problem is easy just disallow interrupts to
occur while a shared variable is being modified but
that is not so in case of multiprocessing environment
OS Notes
Mutual Exclusion with Test-and-Set
Shared data:
boolean lock = false;
Process Pi
do {
while (TestAndSet(lock)) do no-operation;
critical section
lock = false;
remainder section
}
OS Notes
Synchronization Hardware
OS Notes
Mutual Exclusion with Swap
Process Pi
do {
key = true;
while (key == true)
Swap(lock,key);
critical section
lock = false;
remainder section
}
***
OS Notes
Semaphores
OS Notes
Critical Section of n Processes
Shared data:
semaphore mutex; //initially mutex = 1
Process Pi:
do {
wait(mutex);
critical section
signal(mutex);
remainder section
} while (1);
OS Notes
Semaphore Implementation
OS Notes
Implementation
Semaphore operations now defined as
wait(S):
S.value--;
if (S.value < 0) {
add this process to S.L;
/* 1)list of waiting processes for semaphore S
2) waiting : state of the process is switched to waiting & then control is
transferred to the CPU scheduler, which selects another process to execute
3) list : list of PCBs of waiting processes */
block;
}
signal(S):
S.value++;
if (S.value <= 0) {
remove a process P from S.L;
wakeup(P);
/* resume the execution of a blocked process that is remove a process from writing list
and put it in the ready queue */
}
Note : In this case unlike busy waiting semaphore, the semaphore value can be (-)ve. If
the semaphore value is (-)ve, its magnitude is the no. of processes waiting on that
semaphore
OS Notes
Semaphore as a General Synchronization Tool
OS Notes
Deadlock and Starvation
OS Notes
Two Types of Semaphores
OS Notes
Implementing S as a Binary Semaphore
Data structures:
binary-semaphore S1, S2;
int C:
Initialization:
S1 = 1
S2 = 0
C = initial value of semaphore S
OS Notes
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); }
signal(S1);
OS Notes
Classical Problems of Synchronization
Bounded-Buffer Problem
Dining-Philosophers Problem
OS Notes
Bounded-Buffer Problem
Initially:
OS Notes
Bounded-Buffer Problem Producer Process
do {
…
produce an item in nextp
…
wait(empty);
wait(mutex);
…
add nextp to buffer
…
signal(mutex);
signal(full);
} while (1);
OS Notes
Bounded-Buffer Problem Consumer Process
do {
wait(full)
wait(mutex);
…
remove an item from buffer to nextc
…
signal(mutex);
signal(empty);
…
consume the item in nextc
…
} while (1);
OS Notes
Readers-Writers Problem
First reader writer problem :: no reader will be kept waiting unless a writer has
already obtained permission to use the shared object. In other words, no reader
should wait for other readers to finish simply because a writer is waiting (problem
writer can starve )
Second reader writer problem :: once a writer is ready, that writer performs its write
as soon as possible. In other words if a writer is waiting to access the object, no
new reader may start reading (problem reader can starve )
The algorithm below is for first reader writer problem
Shared data
Initially
OS Notes
Readers-Writers Problem Writer Process
wait(wrt);
…
writing is performed
…
signal(wrt);
OS Notes
Readers-Writers Problem Reader Process
wait(mutex);
readcount++;
if (readcount == 1)
wait(wrt);
signal(mutex);
…
reading is performed
…
wait(mutex);
readcount--;
if (readcount == 0)
signal(wrt);
signal(mutex):
mutex :: to ensure mutual exclusion when the variable readcount is updated
Wrt :: used by the writer & also by first and last reader that enters or exit the
C.S (it is not used by readers who enter or exit while other readers are in
their C.S)
Note that , if a writer is in CS & n readers are waiting, then one reader is
queued on wrt and n-1 readers are queued on mutex. Also observe that,
when a writer executes signal(wrt), we may resume the execution of either
the waiting reader or a single waiting writer. The selection is made by the
scheduler.
OS Notes
Dining-Philosophers Problem
OS Notes
Dining-Philosophers Problem
OS Notes
Deadlock Free Dining-Philosophers Problem
OS Notes
Critical Regions
OS Notes
Critical Regions
OS Notes
Monitors
High-level synchronization construct that allows the safe sharing
of an abstract data type among concurrent processes.
monitor monitor-name
{
shared variable declarations
procedure body P1 (…) {
...
}
procedure body P2 (…) {
...
}
procedure body Pn (…) {
...
}
{
initialization code
}
}
OS Notes
Monitors …..contd
OS Notes
Monitors
OS Notes
Schematic View of a Monitor
OS Notes
Monitor With Condition Variables
OS Notes
Monitor Implementation
Let there are many processes waiting on a condition variable
(say x) then which will be resumed when signal(x) is executed
one soln is the process to resume is taken in FCFS order
from the the condition variable’s (x’s) waiting queue of
processes.
other soln. is to wait with some priority as discussed below.
Conditional-wait construct: x.wait(c);
c – integer expression evaluated when the wait operation is
executed.
value of c (a priority number) stored with the name of the process
that is suspended.
when x.signal is executed, process with smallest associated
priority number is resumed next.
OS Notes
Correctness of Monitor Depends on its Use
OS Notes
Chapter 8: Deadlocks
System Model
Deadlock Characterization
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
Recovery from Deadlock
Combined Approach to Deadlock Handling
OS Notes
The Deadlock Problem
P0 P1
wait (A);(1) wait(B) (2)
wait (B); (3) wait(A) (4)
OS Notes
Bridge Crossing Example
OS Notes
System Model
OS Notes
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Mutual exclusion: only one process at a time can use a
resource. (ie resource is not sharable some resources have
to be non sharable like printer ( while printing you can not mix
the o/p of 2 processes)
Hold and wait: a process holding at least one resource is
waiting to acquire additional resources held by other processes.
No preemption: a resource can be released only voluntarily by
the process holding it, after that process has completed its task.
Circular wait: there exists a set {P0, P1, …, P0} of waiting
processes such that P0 is waiting for a resource that is held by
P1, P1 is waiting for a resource that is held by
P2, …, Pn–1 is waiting for a resource that is held by
Pn, and P0 is waiting for a resource that is held by P0.
Note : these condition are not totally independent for example
circular wait implies hold and wait.
OS Notes
Resource-Allocation Graph
OS Notes
Resource-Allocation Graph (Cont.)
Process
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
OS Notes
Example of a Resource Allocation Graph
OS Notes
Resource Allocation Graph With A Deadlock
OS Notes
Resource Allocation Graph With A Cycle But No Deadlock
OS Notes
Basic Facts
OS Notes
Methods for Handling Deadlocks
Ensure that the system will never enter a deadlock state.
(deadlock prevention, deadlock avoidance )
OS Notes
Deadlock Prevention
Ensure that at least one of the necessary condition for
deadlock cannot hold
Restrain the ways request can be made.
Mutual Exclusion –
not required for sharable resources; must hold for nonsharable
resources. (read only file no problem many processes can
read the file simultaneously, printer is intrinsically non sharable
& so had to be accessed in a mutually exclusive way )
OS Notes
Deadlock Prevention (Cont.)
No Preemption –
If a process (say p1) that is holding some resources
requests another resource that cannot be immediately
allocated to it, then all resources currently being held by p1
are released.
Preempted resources are added to the list of resources for
which the p1 process is waiting.
Process (p1) will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting.
Note : The protocol is often applied to resources whose
state can be easily saved and restarted later, such as
CPU registers & memory space. It cannot generally be
applied to such resources as printers and tape drives.
OS Notes
Deadlock Prevention (Cont.)
Circular Wait – impose a total ordering of all resource
types, and require that each process requests resources
in an increasing order of enumeration.
{P0, P1, …. Pn} , {R1, R2,….Rn}
P0 is waiting for resource held by P1 :: P0 P1
let Pi is holding resource Ri while requesting resource
Ri+1. so we must have F(Ri) < F(Ri+1) (1: F is a
enumeration function 2: the equation signify that the
process can request only in increasing order of
enumeration. )
But in case of circular wait it should be
F(R0) < F(R1) < ………… < F(Rn) < F(R0) F(R0) < F(R0)
which is not possible so circular wait can not occur.
OS Notes
Deadlock Avoidance
OS Notes
Safe State
When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe state.
Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources
that Pi can still request can be satisfied by currently available
resources + resources held by all the Pj, with j<I.
If Pi resource needs are not immediately available, then Pi can wait
until all Pj have finished.
When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate.
When Pi terminates, Pi+1 can obtain its needed resources, and so
on.
OS Notes
Basic Facts
OS Notes
Safe, Unsafe , Deadlock State
OS Notes
Resource-Allocation Graph Algorithm
Claim edge Pi Rj indicated that process Pi may request
resource Rj in the future ; represented by a dashed line.
OS Notes
Banker’s Algorithm
Multiple instances.
OS Notes
Data Structures for the Banker’s Algorithm
OS Notes
Safety Algorithm
OS Notes
Resource-Request Algorithm for Process Pi
OS Notes
Example of Banker’s Algorithm
OS Notes
Example (Cont.)
OS Notes
Example P1 Request (1,0,2) (Cont.)
Check that Request Available (that is, (1,0,2) (3,3,2)
true.
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 1 600
P3 2 1 1 011
P4 0 0 2 431
Executing safety algorithm shows that sequence <P1, P3, P4,
P0, P2> satisfies safety requirement.
Can request for (3,3,0) by P4 be granted? (no, since resources
are not available )
Can request for (0,2,0) by P0 be granted? ( no, since although
resources are available but resulting state is unsafe )
OS Notes
Deadlock Detection
Detection algorithm
Recovery scheme
OS Notes
Single Instance of Each Resource Type
OS Notes
Resource-Allocation Graph and Wait-for Graph
OS Notes
Several Instances of a Resource Type
OS Notes
Detection Algorithm
OS Notes
Detection Algorithm (Cont.)
OS Notes
Example of Detection Algorithm
OS Notes
Example (Cont.)
OS Notes
Detection-Algorithm Usage
OS Notes
Recovery from Deadlock:
Process Termination
Resource Preemption
Process Termination ::
OS Notes
Recovery from Deadlock: Resource Preemption
OS Notes
Combined Approach to Deadlock Handling
OS Notes
Chapter 9: Memory Management
Background
Swapping
Contiguous Allocation
Paging
Segmentation
Segmentation with Paging
Questions: a cpu with say 16 address line can access 216 memory locations i.e. 64 Kbytes
-q1: what can be the maximim size of a single program
-q2: can we have 8 MB of memory (i.e. Memory less than 16 KB)
O.S. Notes prepared by Dr. Naveen Choudhary
Dynamic Loading
Assembler Assembler
7. A job being swapped out should not have any pending i/o. otherwise pending i/o will
ultimately write into the wrong (swapped in ) process area.
soln. never to swap a process with pending i/o
To execute i/o operation only into operating system buffer
O.S. Notes prepared by Dr. Naveen Choudhary
Schematic View of Swapping
Single-partition allocation
Relocation-register & limit register are used to protect user
processes from each other, and from changing operating-system
code and data.
Relocation register contains value of smallest physical address;
limit register contains range of logical addresses – each logical
address must be less than the limit register.
When the CPU scheduler selects a process for execution, the
dispatcher loads the relocation & limit registers with the correct
values as part of the context switch.
Free
O.S. Notes prepared by Dr. Naveen Choudhary
Hardware Support for Relocation and Limit Registers
OS OS OS OS
process 8 process 10
OS OS
400 k 400 k Selecting an
P1 P1 optimal
900 k 900 k
100 k compaction
1000 k P2 strategy is quite
1600 k
P2 P3 difficult (optimal
1700 k – minimum
P3 1900 k
2000 k 400 movement of
300 k program & data)
2300 k
O.S. Notes prepared by Dr. Naveen Choudhary NEXT…
Fragmentation……..
Page offset (d) – combined with base address to define the physical
memory address that is sent to the memory unit.
Internal Fragmentation in paging
No external fragmentation. But there will be internal fragmentation if
program size is more than say n pages but less than n+1 pages ( if
process size is n pages & 1 bytes then almost 1 whole page is internal
fragmentation)
On average one half page per process is internal fragmentation so
small page size is better.
But for big pages there will be less entries in the page table so less
over head for the OS. Large pages is also good for disk I\O as it will be
more efficient then doing may small I\O (I\O efficient when data being
transferred is large)
Trend is towards large page size (2 or 4 kilo bytes)
4
Address 5 x 4 + 0 = 20
5 x 4 + 2 = 22 5
frame No. x NO. of
6 x 4 + 2 = 26 6
bytes per page +
offset
7
Hierarchical Paging
p1 p2 d
10 10 12
where pi is an index into the outer page table, and p2 is the displacement
within the page of the outer page table.
For 3 – level paging we need to have p1,p2 & p3
As the no. of levels increases the access time for memory increase but
caching (Associate members) with high hit ratio can help greatly.
Actual page/frame
Frame No.
Shared code
One copy of read-only (reentrant) code shared among
processes (i.e., text editors, compilers, window systems).
Shared code must appear in same location in the logical
address space of all processes.
4
1
3 2
4
Relocation.
dynamic
by segment table
Sharing.
shared segments
same segment number
Allocation.
first fit/best fit
external fragmentation
•There can be jump inst within the shared segment. What this address will be
(segment No., offset) so how the segment will refer itself. Because there is only on
physical copy of sqrt, it must refer to itself in same way for other users.---i.e. it must have
a unique segment No.
•So if large no. of processes are sharing a segment then having a unique segment no.
for
O.S. shared segment
Notes prepared by Dr. Naveenno. in all the processes will be a problem.
Choudhary
Segmentation with Paging – MULTICS
To extend the scheme further (let s = 18 bit then we can have 262, 144
segments, requiring excessively large segment table. we can page the
segment table itself. So now our logical address will look like)
Segment no. offset
s1 s2 d1 d2
8
O.S. Notes prepared by Dr. Naveen Choudhary 10 6 10
Chapter 10: Virtual Memory
Background
Demand Paging
Process Creation
Page Replacement
Allocation of Frames
Thrashing
Operating System Examples
0
0
page table
During address translation, if valid–invalid bit in page
table entry is 0 page fault.
O. S. Notes Prepared by Dr. Naveen Choudhary
Page Table When Some Pages Are Not in Main Memory
Cont…
O. S. Notes Prepared by Dr. Naveen Choudhary ….
Page Fault
50% of the time the page that is being replaced has been modified and
therefore needs to be swapped out.
Swap Page Time (page fault service time ) = 10 msec = 10,000 microsec
EAT = (1 – p) x 1 + p (10000)
1 – P + 10000P
= 1 + 9999P
To improve page fault service time :- use swap space
swap space is generally faster then access from file
system. It is faster b/s swap space is allocated in much larger blocks,
and file look ups & indirect allocation method are not used
1 1 1 4 4 4 5 5 5
2 2 2 1 1 1 3 3
3 3 3 2 2 2 4
1 5 5 5 5 4 4
2 2 1 1 1 1 5
10 page faults
3 3 3 2 2 2 2
4 4 4 4 3 3 3
= 15 page faults
1 4
6 page faults
2
( 4 (initial faults)+2 page faults )
3
4 5
1 5
2
3 5 4
4 3
1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0
7 0 1 2 2 3 0 4 2 2 0 3 3 1 2 0 1 7
= 12 page fault
Head Tail
Reference bit Rb
Second chance ( give pages with reference bit set, a second chance
Goal :- is to keep regularly referenced pages in the memory. data
structure used Circular queue, reference bit
Need reference bit.
Clock replacement.
If page to be replaced (in clock order) has reference bit = 1.
then:
set reference bit 0.
leave page in memory.
replace next page (in clock order), subject to same rules.
The working set strategy prevents Thrashing while keeping the degree of
multi programming as high as possible thus optimizing the CPU utilization.
The difficulty with the working set model is keeping track of the working
set. The working set window is a moving window. At each memory
references, a new reference appears at one end & the oldest reference
drops off the other end.
Consider I/O. Pages that are used for copying a file from a I/O device
must be locked from being selected for eviction by a page replacement
algorithm.
I/O is being done from a I/O device to a memory page now if this page is
replaced with another page of some other process then information from
I/O device will be written wrongly to the new page (as I/O will be
implement by different I/O process & it will be unaware of the page
change)
Solution 1: Write/ read I/O from Kernel/ system buffer so a process
wishing to do I/O will first write/ read the information in kernel buffer &
from there I/O processor will read data ( but this extra copying will
increase the overhead
Solution 2: Associate a lock bit to every frame in memory. while doing I/O
set lock bit for frame. The frame with the lock bit set can not be replaced.
File Concept
Access Methods
Directory Structure
File Sharing
Protection
OS Notes
File Concept
OS Notes
File Structure
None - sequence of words, bytes
Simple record structure
Lines
Fixed length
Variable length
Can simulate last two with first method by inserting
appropriate control characters.
The structure of the file is decided by
Operating system
Program
note : UNIX OS define all files to be simply a stream of
bytes and each byte is individually addressable by its
offset from the beginning (or end ) of the file
OS Notes
File Attributes
OS Notes
File Operations
Create
Write
Read
Reposition within file – file seek
Delete
Truncate (contents of a file is deleted ie file size
becomes 0 but other attributes of file are not changed.
Open(Fi) – search the directory structure on disk for entry
Fi, and move the content of entry to memory. so that we
need not waste time in reading the directory for the
secondary storage every time we access the file
Close (Fi) – move the content of entry Fi in memory to
directory structure on disk.
OS Notes
File Types – Name, Extension
OS in some cases like .exe, .com etc or application program in other
cases (.java, .c etc) uses the file extension to recognize (in some cases
to open the appropriate application for the given file windows,
macintosh )
Unix uses a crude magic number stored at the beginning of some file
to indicate roughly the type of file executable, batch, postscipt etc.
OS Notes {not all files have magic numbers }
Access Methods
Sequential Access
read next
write next
reset
no read after last write
(rewrite)
Direct Access
read n
write n
position to n
read next
write next
rewrite n
n = relative block number
OS Notes
Sequential-access File
(Offset = 0 )
(reset)
OS Notes
Simulation of Sequential Access on a Direct-access File
OS Notes
Example of Index and Relative Files
(other access methods )
OS Notes
Directory Structure
Directory
Files
F1 F2 F4
F3
Fn
OS Notes
Information in a Device Directory
Name
Type
Address
Current length
Maximum length
Date last accessed (for archival)
Date last updated (for dump)
Owner ID (who pays)
Protection information (discuss later)
OS Notes
Operations Performed on Directory
OS Notes
Organize the Directory (Logically) to Obtain
OS Notes
Single-Level Directory
Naming problem
(only single root dir so we can’t have two files with the
same name )
Grouping problem
OS Notes
Two-Level Directory
OS Notes
Tree-Structured Directories
OS Notes
Tree-Structured Directories (Cont.)
Efficient searching
Grouping Capability
OS Notes
Tree-Structured Directories (Cont.)
Absolute or relative path name
Absolute path start from root
Relative path start from current working directory
Creating a new file is done in current directory.
Delete a file
rm <file-name>
Creating a new subdirectory is done in current directory.
mkdir <dir-name>
Example: if in current directory /mail
mkdir count
mail
OS Notes
Acyclic-Graph Directories (Cont.)
Two or more different names (aliasing) of a file but
actually single physical file is possible
problem ::
Traversing dir structure (we do not want to traverse a single
file more than once )
Links should have special way of representation in dir
entries and such entries should be ignored while
traversing the dir.
Deletion :: deleting a directory entry may not mean
deleting the file (if actual file is deleted we may have
dangling pointers) as more than one entry may be
pointing to the same file
soln of the above (deletion ) keep reference count
with each dir. Entry (about the file ) and delete the file
only when reference count becomes 0
OS Notes
General Graph Directory
OS Notes
General Graph Directory (Cont.)
OS Notes
Protection
OS Notes
Access Lists and Groups
Mode of access: read, write, execute
Three classes of users
RWX
a) owner access 7 111
RWX
b) group access 6 110
RWX
c) public access 1 001
Ask manager to create a group (unique name), say G,
and add some users to the group.
For a particular file (say game) or subdirectory, define an
appropriate access.
OS Notes
Consistency Semantics ….contd…
OS Notes
Chapter 12: File System Implementation
OS Notes
File-System Structure
File structure
Logical storage unit
Collection of related information
File system resides on secondary storage (disks).
File system organized into layers.
File control block – storage structure consisting of
information about a file.
OS Notes
Layered File System
Converts say read xyz(next) to detailed info.
about file like logical block no. etc
OS Notes
A Typical File Control Block
OS Notes
In-Memory File System Structures
OS Notes
In-Memory File System Structures
OS Notes
Allocation Methods
Contiguous allocation
Linked allocation
Indexed allocation
OS Notes
Contiguous Allocation
Each file occupies a set of contiguous blocks on the
disk.
Use best fit or first fit but this can lead to external
fragmentation
Files cannot grow.( as we need to give the Max.
anticipated file size at the time of creation of the file and
this can lead to internal fragmentation if the Max.
anticipated file size is not fully used
OS Notes
Contiguous Allocation of Disk Space
OS Notes
Linked Allocation
block = pointer
data
OS Notes
Linked Allocation (Cont.)
Simple – need only starting address in directory entry
No external fragmentation
Free-space management system (like FAT) – but this will
require some additional space
No random access possible
OS Notes
Linked Allocation
OS Notes
File-Allocation Table
Free
block
214 0
FAT can reduce the random access time; Generally FAT will
be available at the start of the partition
OS Notes
Indexed Allocation
index table
Data blocks
OS Notes
Example of Indexed Allocation
OS Notes
Indexed Allocation (Cont.)
Q R
Q = displacement into index table 2nd block as 600 >1*512
R = displacement into block 88
OS Notes
Indexed Allocation – Mapping (Cont.)
Index
blk1
Index
blk2
OS Notes
Indexed Allocation – Mapping (Cont.)
Can address 512 * 3 = 1536 words
512
words
1
2
512
outer-index
inode
OS Notes
Free-Space Management
Contiguous free
blocks
Bit vector (n blocks)
0 1 2 n-1
1 1 1 …
0 block[i] occupied
bit[i] =
1 block[i] free
OS Notes
Free-Space Management (Cont.)
OS Notes
Free-Space Management (Cont.)
Grouping
A modification of the free list approach is to store the
address of n free blocks in the first free block. The first n-1
of these blocks are actually free. The last free block
contains the address of another free blocks & so on.
Advantage address of large no. of free blocks can be
found quickly unlike linked list method
Counting
Generally several contiguous blocks may be allocated or
freed simultaneously
So each entry in the free space list ( some blocks may be
reserved to keep this list ) can be made to contain the
address of the first free block and the number n of free
contiguous blocks that follow the first free block
OS Notes
Linked Free Space List on Disk
OS Notes
Efficiency and Performance
Efficiency dependent on:
disk allocation and directory algorithms
types of data kept in file’s directory entry ( file last modified, file last
accessed )
Performance
disk cache – separate section of main memory for frequently used
blocks
free-behind and read-ahead – techniques to optimize sequential
access from disk
Free behind removes a page from the buffer as soon as the
next page is requested as previous pages is not likely to be
used in sequential access
Synchronous write :: write immediately to disk & the program
issuing command for synchronous write should wait till the write to
disk is completed. ( it is not so in asynchronous write just write to
cache & return control to the program. The page/block will be
written to disk at some later free time )
improve PC performance by dedicating section of memory as virtual
disk, or RAM disk.
OS Notes
Various Disk-Caching Locations
Ram disk (virtual disk ) :: A section of primary memory is set aside &
treated as a virtual disk.
Ramdisk device driver accepts all the standard disk operation but
perform those operation on the memory section, instead of on disk
The difference b/w a Ram disk and a disk cache is that the contents
of the Ramdisk are totally user controlled, whereas those of the disk
cache are under the control of OS. For instance, a Ram disk will stay
empty until the user creates file there
OS Notes
Page Cache
Routine I/O through the file system uses the buffer (disk)
cache.( i/o read(file), i/o write(file)
OS Notes
I/O Without a Unified Buffer Cache
Get block
of disl
OS Notes
Unified Buffer Cache
OS Notes
I/O Using a Unified Buffer Cache
OS Notes
Recovery
OS Notes
Log Structured File Systems
OS Notes
Chapter 13: I/O Systems
I/O Hardware
Application I/O Interface
Kernel I/O Subsystem
Transforming I/O Requests to Hardware Operations
Performance
OS Notes
I/O Hardware
OS Notes
A Typical PC Bus Structure
OS Notes
Device I/O Port Locations on PCs (partial)
OS Notes
Polling
Determines state of device
Wait till busy bit is cleared
Sets write bit in command reg. and write a byte in data out reg.
Set command-ready bit in command reg. and then when controller
notices that command ready bit is set then it sets the busy bit in the
status reg.
The controller reads the command register & sees the write
command. It reads the data-out register to get the byte and does the
i/o to the device
The controller clears the command ready bit, clears the error bit in
the status register to indicate that the device i/o succeeded & clears
the buy bit to indicate that it is finished.
Disadvantage : Busy-wait cycle to wait for I/O from device
1contro/c
ommand
reg.
2status
reg.
1 2 3 4 3 data in
processor
Control
reg.
command/data
4 data out
reg.
OS Notes
Interrupts
CPU Interrupt request line triggered by I/O device
Interrupt handler receives interrupts
cpu
IRQ (interrupt request
line )
OS Notes
Interrupt-Driven I/O Cycle
OS Notes
Intel Pentium Processor Event-Vector Table
How to efficiently dispatch the interrupt handler for the device (without
first polling all the devices to see which one raised the interrupts )
device interrupt with a no. which is the offset in the interrupt vector table
containing the memory address of specialized interrupt handler
OS Notes
Direct Memory Access
OS Notes
Six Step Process to Perform DMA Transfer
OS Notes
Application I/O Interface
There are variety of i/o devices & new devices are launched
every now & then. Each device has its own set of capabilities,
control bit definition & protocol for interfacing with host
(processor ) – and they all are different.
The issue is how we can design an OS such that new devices
can be attached to the computer without the OS being rewritten
I/O system calls encapsulate device behaviors in generic
classes
Device-driver layer hides differences among I/O controllers from
kernel
Devices vary in many dimensions
Character-stream or block
Sequential or random-access
Sharable or dedicated
Speed of operation
read-write, read only, or write only
OS Notes
A Kernel I/O Structure
OS Notes
Characteristics of I/O Devices
OS Notes
Block and Character Devices
OS Notes
Network Devices
host1 host2
s1 s1
host3
s2
s2
OS Notes
Clocks and Timers
In most computers, the h/w clock is constructed from high
frequency counter
Provide current time, elapsed time, timer
OS Notes
Blocking and Nonblocking I/O
OS Notes
Kernel I/O Subsystem
Scheduling
To schedule a set of i/o request means to determine a good order
in which to execute them with the objective to improve overall
system performance. The order in which application issues system
call rarely is the best choice
os maintains a queue of request for each device. When an
application issues a blocking i/o system call, the request is placed
on the queue for that device. The i/o scheduler rearranges the
order of the queue to improve the overall system efficiency and the
average response time experienced by the application
OS Notes
Kernel I/O Subsystem
Caching - fast memory (generally static RAM ) holding copy of
data
Always just a copy
Key to performance generally performance improves as process
shows locality of reference
Spooling - hold output for a device
Useful for devices, such as printer, that cannot accept interleaved
data stream
Although a printer can serve only one job at a time, several
application may wish to print their output concurrently without
having their output mixed together. The OS solves this problem by
intercepting all output to the printer. Each application’s output is
spooled to a separate file on disk. The spooling system copies the
queued spool files to the printer one at a time
Spooling is one way that OS can coordinate concurrent output
Device reservation - provides exclusive access to a device by a
processes
System calls for allocation and deallocation
Watch out for deadlock
OS Notes
Error Handling
OS Notes
Kernel Data Structures
OS Notes
UNIX I/O Kernel Structure
OS Notes
I/O Requests to Hardware Operations
OS Notes
Life Cycle of An I/O Request
OS Notes
Chapter 14: Mass-Storage Systems
Disk Structure
Disk Scheduling
Disk Management
Swap-Space Management
RAID Structure
Disk Attachment
Stable-Storage Implementation
Tertiary Storage Devices
Operating System Issues
Performance Issues
OS Notes
Disk Structure
OS Notes
OS Notes
Disk Scheduling
OS Notes
Disk Scheduling (Cont.)
Head pointer 53
OS Notes
FCFS
OS Notes
SSTF
Selects the request with the minimum seek time from the
current head position.
This algo. is not fair but efficient
This algo. is not optimal. A generic optimal algo for disk
scheduling does not exist
SSTF scheduling is a form of SJF scheduling; may cause
starvation of some requests.
Illustration shows total head movement of 236 cylinders.
OS Notes
SSTF (Cont.)
OS Notes
SCAN
The disk arm starts at one end of the disk, and moves
toward the other end, servicing requests until it gets to the
other end of the disk, where the head movement is
reversed and servicing continues.
Sometimes called the elevator algorithm.
Illustration shows total head movement of 208 cylinders.
OS Notes
SCAN (Cont.)
OS Notes
C-SCAN
OS Notes
C-SCAN (Cont.)
OS Notes
C-LOOK
Version of C-SCAN
Arm only goes as far as the last request in each direction,
then reverses direction immediately, without first going all
the way to the end of the disk.
OS Notes
C-LOOK (Cont.)
OS Notes
Selecting a Disk-Scheduling Algorithm
SSTF is common and has a natural appeal
SCAN and C-SCAN perform better for systems that place a
heavy load on the disk.
Performance depends on the number and types of requests. For
a particular list of request, it is possible to define an optimal
order of retrieval, but the computation needed to find an optimal
schedule may not justify the saving over SSTF or SCAN
Requests for disk service can be influenced by the file-allocation
method.
Contiguous allocated file generate several request that are close
together on disk so less head movement
Linked & Indexed file file block can be scattered anywhere
resulting in greater head movement.
The disk-scheduling algorithm should be written as a separate
module of the operating system, allowing it to be replaced with a
different algorithm if necessary.
Either SSTF or LOOK is a reasonable choice for the default
algorithm.
OS Notes
Disk Management
Low-level formatting, or physical formatting — Dividing a disk into
sectors that the disk controller can read and write.( sector
header(sector no.), data (256, 512 or 1024 ), trailer (Error Correcting
Code)
To use a disk to hold files, the operating system still needs to
record its own data structures on the disk.
Partition the disk into one or more groups of cylinders.
Logical formatting or “making a file system”. the data structure may
include maps of free and allocated space (a FAT or inodes) and an initial
empty directory )
Boot block initializes system.
The bootstrap is stored in ROM & also some part on disk & loaded into
memory by the ROM part of bootstrap program
Bootstrap loader program.
Methods such as sector sparing & sector slipping used to handle
bad blocks in advanced disks like SCSI ( Small Computer Systems
Interface )
Sector sparing : map the bad sector say 87 to good spare sector 298
when the next time the system boots with the help of some special
command
Spare sectors are available on each cylinder & there is also a spare
cylinder. So as not to affect disk scheduling too much. When a sector in
a cylinder goes bad, it is replaced by a spare sector on the same
cylinder
Sector slipping : let sector 17 gone bad & there is a spare sector no.
202. shift each sector starting from sector 17 to 202 by one position
In DOS just mark bad blocks in FAT while doing logical formatting
or when chkdsk command is run
OS Notes
Booting from disk in Windows 2000.
OS Notes
MS-DOS Disk Layout
OS Notes
Swap-Space Management
OS Notes
4.3 BSD Text-Segment Swap Map
OS Notes
4.3 BSD Data-Segment Swap Map
The data segment swap map is more complicated, because the data segment can grow
over time. The map is of fixed size, but contains swap addresses for blocks of varying
size. Given index i, a block pointed to by swap map entry is of size 2i *16K to a maximum
of 2 megabytes. So using this scheme the blocks of large processes can be found quickly,
and the swap map remains small. Moreover for small processes only small blocks will be
needed to be used minimizing fragmentation.
OS Notes
RAID Structure
OS Notes
RAID: Improvement in Performance via
Parallelism
Several improvements in disk-use techniques involve the use of multiple
disks working cooperatively.
OS Notes
• Raid Level 0 - This level includes striping only, with no mirroring.
RAID Levels
• Raid Level 1 - This level includes mirroring only, no striping.
• Raid Level 3 - This level is similar to level 2, except that it takes advantage of
the fact that each disk is still doing its own error-detection, so that when an
error occurs, there is no question about which disk in the array has the bad
data. As a result a single parity bit is all that is needed to recover the lost data
from an array of disks. Level 3 also includes striping, which improves
performance.
• Raid Level 5 - This level is similar to level 4, except the parity blocks are
distributed over all disks, thereby more evenly balancing the load on the
system. For any given block on the disk(s), one of the disks will hold the parity
information for that block and the other N-1 disks will hold the data. Note that
the same disk cannot hold both data and parity for the same block, as both
would be lost in the event of a disk crash.
• Raid Level 6 - This level extends raid level 5 by storing multiple bits of error-
recovery codes, ( such as the Reed-Solomon codes ), for each bit position of
data, rather than a single parity bit. In the example shown below 2 bits of ECC
are stored for every 4 bits of data, allowing data recovery in the face of up to
two simultaneous disk failures. Note that this still involves only 50% increase in
storage needs, as opposed to 100% for simple mirroring which could only
tolerate a single disk failure.
OS Notes
Stable-Storage Implementation
Type of disk failure & recovery methods
A disk write results in one of the three outcomes
Successful completion
Partial failure : partially data were written on the disk
block & then write failed
Total failure: could not write the block atall so old data
on the disk block are in tact
A gen soln.
Keep two physical blocks for each logical block. A o/p
operation will be executed as follow in such case
Write the info. In first physical block
When the first write completes successfully, write the
same info. Onto the second block
Operation is declared completed only after the second
write completes successfully
OS Notes