Advanced Os
Advanced Os
OPERATING SYSTEMS
Lecture Notes
UNIT -1
COMPUTER SYSTEM AND OPERATING SYSTEM OVERVIEW
OVER VIEW OF OPERATING SYSTEM
What is an Operating System?
A program that acts as an intermediary between a user of a computer and the computer hardware
Operating system goals:
Execute user programs and make solving user problems
easier Make the computer system convenient to use
Use the computer hardware in an efficient manner
Computer System Structure
Computer system can be divided into four components
Hardware – provides basic computing resources
CPU, memory, I/O devices
Operating system
Controls and coordinates use of hardware among various applications and users
Application programs – define the ways in which the system resources are used to solve the computing
problems of the users
Word processors, compilers, web browsers, database systems, video games
Users
People, machines, other computers
Four Components of a Computer System
OS is a resource allocator
Manages all resources
Decides between conflicting requests for efficient and fair resource
use OS is a control program
Controls execution of programs to prevent errors and improper use of the computer
No universally accepted definition
Everything a vendor ships when you order an operating system” is good
approximation But varies wildly
“The one program running at all times on the computer” is the kernel. Everything else is either a
system program (ships with the operating system) or an application program
Computer Startup
bootstrap program is loaded at power-up or reboot
Typically stored in ROM or EPROM, generally known as firmware
Initializes all aspects of system
Loads operating system kernel and starts execution
Computer System Organization
Computer-system operation
One or more CPUs, device controllers connect through common bus providing access to shared memory
Concurrent execution of CPUs and devices competing for memory cycles
Computer-System Operation
I/O devices and the CPU can execute concurrently
Each device controller is in charge of a particular device
type Each device controller has a local buffer
CPU moves data from/to main memory to/from local buffers
I/O is from the device to local buffer of controller
Device controller informs CPU that it has finished its operation by causing An interrupt
Interrupt Timeline
I/O Structure
After I/O starts, control returns to user program only upon I/O completion
Wait instruction idles the CPU until the next interrupt
Wait loop (contention for memory access)
At most one I/O request is outstanding at a time, no simultaneous I/O processing After I/O starts,
control returns to user program without waiting for I/O completion System call – request to the
operating system to allow user to wait for I/O completion Device-status table contains entry for each
I/O device indicating its type, address, and state Operating system indexes into I/O device table to
determine device status and to modify table entry to include interrupt
Caching
Important principle, performed at many levels in a computer (in hardware, operating system,
software) Information in use copied from slower to faster storage temporarily
Faster storage (cache) checked first to determine if information is there
If it is, information used directly from the cache (fast)
If not, data copied to cache and used there
Cache smaller than storage being cached
Cache management important design problem
Cache size and replacement policy
Computer-System Architecture
Most systems use a single general-purpose processor (PDAs through mainframes)
Most systems have special-purpose processors as well
Multiprocessors systems growing in use and importance
Also known as parallel systems, tightly-coupled systems
Advantages include
1.Increased throughput
2.Economy of scale
3.Increased reliability – graceful degradation or fault tolerance
Two types
1.Asymmetric Multiprocessing
2.Symmetric Multiprocessing
A Dual-Core Design
Clustered Systems
Operating-System Operations
Interrupt driven by hardware
Software error or request creates exception or trap
Division by zero, request for operating system service
Other process problems include infinite loop, processes modifying each Other or the operating system
Dual-mode operation allows OS to protect itself and other system components
User mode and kernel mode
Mode bit provided by hardware
Provides ability to distinguish when system is running user code or kernel code
Some instructions designated as privileged, only executable in kernel mode
System call changes mode to kernel, return from call resets it to user
Transition from User to Kernel Mode
Timer to prevent infinite loop / process hogging resources
Set interrupt after specific period
Operating system decrements counter
When counter zero generate an interrupt
Set up before scheduling process to regain control or terminate program that exceeds allotted time
UNIT - 1
Process Management
A process is a program in execution. It is a unit of work within the system. Program is a passive entity,
process is an active entity.
Process needs resources to accomplish its task
CPU, memory, I/O, files
Initialization data
Process termination requires reclaim of any reusable resources
Single-threaded process has one program counter specifying location of next instruction to execute
Process executes instructions sequentially, one at a time, until completion
Multi-threaded process has one program counter per thread
Typically system has many processes, some user, some operating system running concurrently on one or
more CPUs
Concurrency by multiplexing the CPUs among the processes / threads
Memory Management
All data in memory before and after processing
All instructions in memory in order to execute
Memory management determines what is in memory when
Optimizing CPU utilization and computer response to users
Usually disks used to store data that does not fit in main memory or data that must be kept for a
“long” period of time
Proper management is of central importance
Entire speed of computer operation hinges on disk subsystem and its algorithms
MASS STORAGE activities
Free-space management
Storage allocation
Disk scheduling
Some storage need not be fast
Tertiary storage includes optical storage, magnetic tape
Still must be managed
Varies between WORM (write-once, read-many-times) and RW (read-write)
Performance of Various Levels of Storage
Multiprocessor environment must provide cache coherency in hardware such that all CPUs have
the most recent value in their cache
Distributed environment situation even more complex
Several copies of a datum can exist
I/O Subsystem
One purpose of OS is to hide peculiarities of hardware devices from the user
I/O subsystem responsible for
Memory management of I/O including buffering (storing data temporarily while it is being transferred),
caching (storing parts of data in faster storage for performance), spooling (the overlapping of output of
one job with input of other jobs)
General device-driver interface
Drivers for specific hardware devices
Protection and Security
Protection – any mechanism for controlling access of processes or users to resources defined by the OS
Security – defense of the system against internal and external attacks
Huge range, including denial-of-service, worms, viruses, identity theft, theft of service
Systems generally first distinguish among users, to determine who can do what
User identities (user IDs, security IDs) include name and associated number, one per user
User ID then associated with all files, processes of that user to determine access control
Group identifier (group ID) allows set of users to be defined and controls managed, then also associated
with each process, file
Privilege escalation allows user to change to effective ID with more rights
DISTRIBUTED SYSTEMS
Computing Environments
Traditional computer
Blurring over time
Office environment
PCs connected to a network, terminals attached to mainframe or minicomputers providing batch
and timesharing
Now portals allowing networked and remote systems access to same
resources Home networks
Used to be single system, then modems
Now firewalled, networked
Client-Server Computing
Peer-to-Peer Computing
File-system manipulation - The file system is of particular interest. Obviously, programs need to read
and write files and directories, create and delete them, search them, list file Information, permission
management.
Another set of OS functions exists for ensuring the efficient operation of the system itself via
resource sharing
Resource allocation - When multiple users or multiple jobs running concurrently, resources must be
allocated to each of them
Many types of resources - Some (such as CPU cycles, main memory, and file storage) may have special
allocation code, others (such as I/O devices) may have general request and release code
Accounting - To keep track of which users use how much and what kinds of computer resources
Protection and security - The owners of information stored in a multiuser or networked computer
system may want to control use of that information, concurrent processes should not interfere with each
other
Protection involves ensuring that all access to system resources is controlled
Security of the system from outsiders requires user authentication, extends to defending external
I/O devices from invalid access attempts
If a system is to be protected and secure, precautions must be instituted throughout it. A chain is only as
strong as its weakest link.
User Operating System Interface - CLI
Command Line Interface (CLI) or command interpreter allows direct command entry
System Calls
The system call interface invokes intended system call in OS kernel and returns status of the system call
and any return values
The caller need know nothing about how the system call is implemented
Just needs to obey API and understand what OS will do as a result call
Most details of OS interface hidden from programmer by API
Managed by run-time support library (set of functions built into libraries included with
compiler) API – System Call – OS Relationship
MS-DOS execution
System Programs
System programs provide a convenient environment for program development and execution. The can be
divided into:
File manipulation
Status information
File modification
Programming language support
Program loading and execution
Communications
Application programs
Most users’ view of the operation system is defined by system programs, not the actual system calls
Provide a convenient environment for program development and execution
Some of them are simply user interfaces to system calls; others are considerably more complex
File management - Create, delete, copy, rename, print, dump, list, and generally manipulate files and
directories
Status information
Some ask the system for info - date, time, amount of available memory, disk space, number of users
Others provide detailed performance, logging, and debugging information
Typically, these programs format and print the output to the terminal or other output devices
Some systems implement a registry - used to store and retrieve configuration information
File modification
Text editors to create and modify files
Special commands to search contents of files or perform transformations of the text
Programming-language support - Compilers, assemblers, debuggers and interpreters
sometimes provided
Program loading and execution- Absolute loaders, relocatable loaders, linkage editors, and overlay-
loaders, debugging systems for higher-level and machine language
Communications - Provide the mechanism for creating virtual connections among processes, users,
and computer systems
Allow users to send messages to one another’s screens, browse web pages, send electronic-mail
messages, log in remotely, transfer files from one machine to another
Operating System Design and Implementation
Design and Implementation of OS not “solvable”, but some approaches have proven successful
Internal structure of different Operating Systems can vary widely
Start by defining goals and specifications
Affected by choice of hardware, type of system
User goals and System goals
User goals – operating system should be convenient to use, easy to learn, reliable, safe, and fast
System goals – operating system should be easy to design, implement, and maintain, as well as
flexible, reliable, error-free, and efficient
Important principle to separate
Policy: What will be done?
Mechanism: How to do it?
Mechanisms determine how to do something, policies decide what will be done
The separation of policy from mechanism is a very important principle, it allows maximum flexibility
if policy decisions are to be changed later
Simple Structure
MS-DOS – written to provide the most functionality in the least
space Not divided into modules
Although MS-DOS has some structure, its interfaces and levels of Functionality are not well separated
Layered Approach
The operating system is divided into a number of layers (levels), each built on top of lower layers. The
bottom layer (layer 0), is the hardware; the highest (layer N) is the user interface.
With modularity, layers are selected such that each uses functions (operations) and services of only
lower-level layers
UNIX
UNIX – limited by hardware functionality, the original UNIX operating system had limited structuring.
The UNIX OS consists of two separable parts
Systems programs
The kernel
Consists of everything below the system-call interface and above the physical hardware
Provides the file system, CPU scheduling, memory management, and other operating-system
functions; a large number of functions for one level
Layered Operating System
More secure
Detriments:
Performance overhead of user space to kernel space communication
Mac OS X Structure
Modules
Virtual Machines
A virtual machine takes the layered approach to its logical conclusion. It treats hardware and the
operating system kernel as though they were all hardware
Para-virtualization
Presents guest with system similar but not identical to hardware
Guest must be modified to run on paravirtualized hardwareF
Guest can be an OS, or in the case of Solaris 10 applications running in containers
Solaris 10 with Two Containers
VMware Architecture
Operating-System Debugging
UNIT -2
PROCESS MANAGEMENT
Process Concept
An operating system executes a variety of programs:
Batch system – jobs
Time-shared systems – user programs or tasks
Textbook uses the terms job and process almost interchangeably
Process – a program in execution; process execution must progress in sequential fashion
A process includes:
program counter
stack
data section
Process in Memory
Process State
Schedulers
Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready
queue
Short-term scheduler (or CPU scheduler) – selects which process should be executed next and
allocates CPU
Process Creation
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}
Process Termination
Process executes last statement and asks the operating system to delete it (exit)
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
If parent is exiting Some operating system do not allow child to continue if its parent
terminates All children terminated - cascading termination
Interprocess Communication
Processes within a system may be independent or cooperating
Cooperating process can affect or be affected by other processes, including sharing data
Reasons for cooperating processes:
Information sharing
Computation speedup
Modularity
Convenience
Cooperating processes need interprocess communication (IPC)
Two models of IPC
Shared memory
Message passing
Communications Models
Cooperating Processes
Independent process cannot affect or be affected by the execution of another process
Cooperating process can affect or be affected by the execution of another process
Advantages of process cooperation
Information sharing
Computation speed-up
Modularity
Convenience
Dept. of Computer Science and Engineering Page 31
Producer-Consumer Problem
Paradigm for cooperating processes, producer process produces information that is consumed by a
consumer process
unbounded-buffer places no practical limit on the size of the
buffer bounded-buffer assumes that there is a fixed buffer size
Bounded-Buffer – Shared-Memory Solution
Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Solution is correct, but can only use BUFFER_SIZE-1 elements
Bounded-Buffer – Producer
while (true) {
/* Produce an item */
while (((in = (in + 1) % BUFFER SIZE count) == out)
; /* do nothing -- no free buffers
*/ buffer[in] = item;
in = (in + 1) % BUFFER SIZE;
}
Direct Communication
Processes must name each other explicitly:
send (P, message) – send a message to process P
receive(Q, message) – receive a message from process Q
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
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
Each pair of processes may share several communication links
Link may be unidirectional or bi-directional Operations
Buffering
Queue of messages attached to the link; implemented in one of three ways
1. Zero capacity – 0 messages
Execution of RPC
Marshalling Parameters
Threads
To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of
multithreaded computer systems
To discuss the APIs for the Pthreads, Win32, and Java thread libraries
To examine issues related to multithreaded programming
Single and Multithreaded Processes
Benefits
Responsiveness
Resource Sharing
Economy
Scalability
Multicore Programming
Multicore systems putting pressure on programmers, challenges include
Dividing activities
Balance
Data splitting
Data dependency
Testing and debugging
Multithreaded Server Architecture
User Threads
Thread management done by user-level threads librarynThree primary thread libraries:
POSIX Pthreadsl Win32 threads
Java threads
Kernel Threads
Supported by the Kernel
Examples
Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
Many-to-One
Many user-level threads mapped to single kernel thread
Examples:
Solaris Green Threads
GNU Portable Threads
One-to-One
Each user-level thread maps to kernel thread
Examples
Windows NT/XP/2000
Linux
Many-to-Many Model
Allows many user level threads to be mapped to many kernel threads
Allows the operating system to create a sufficient number of kernel threads
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber package
Two-level Model
Similar to M:M, except that it allows a user thread to be bound to kernel thread
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
Thread Libraries
Thread library provides programmer with API for creating and managing threads
Two primary ways of implementing
Library entirely in user space
Kernel-level library supported by the OS
Pthreads
May be provided either as user-level or kernel-level
A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
API specifies behavior of the thread library, implementation is up to development of the library
Common in UNIX operating systems (Solaris, Linux, Mac OS X)
Java Threads
Java threads are managed by the JVM
Typically implemented using the threads model provided by underlying OS
Java threads may be created by:lExtending Thread class Implementing the
Runnable interface
Threading Issues
Semantics of fork() and exec() system calls
Thread cancellation of target thread
Asynchronous or deferred
Signal handling
Thread pools
Thread-specific data
Scheduler activations
Thread Cancellation
Terminating a thread before it has finished
Two general approaches:
Asynchronous cancellation terminates the target thread immediately
Deferred cancellation allows the target thread to periodically check if it should be cancelled
Signal Handling
Signals are used in UNIX systems to notify a process that a particular event has occurred
A signal handler is used to process signals
1.Signal is generated by particular event
2.Signal is delivered to a process
3.Signal is handled
Options:
Deliver the signal to the thread to which the signal applies
Deliver the signal to every thread in the process
Deliver the signal to certain threads in the process
Assign a specific threa to receive all signals for the process
Thread Pools
Create a number of threads in a pool where they await work
Advantages:
Usually slightly faster to service a request with an existing thread than create a new thread
Allows the number of threads in the application(s) to be bound to the size of the pool
Linux Threads
CPU Scheduler
Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Scheduling under 1 and 4 is nonpreemptive
All other scheduling is preemptive
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the short-term scheduler;
this involves:
switching context
switching to user mode
jumping to the proper location in the user program to restart that program
Dispatch latency – time it takes for the dispatcher to stop one process and start another running
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible
Throughput – # of processes that complete their execution per time unit
Turnaround time – amount of time to execute a particular process
Waiting time – amount of time a process has been waiting in the ready queue
Response time – amount of time it takes from when a request was submitted until the first response is
produced, not output (for time-sharing environment)
Max CPU utilization
Max throughput
Min turnaround time
Min waiting time
Min response time
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
Shortest-Job-First (SJF) Scheduling
Associate with each process the length of its next CPU burst. Use these lengths to schedule the process
with the shortest time
SJF is optimal – gives minimum average waiting time for a given set of processes The difficulty is
knowing
Process Arrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
P P3 P2
4 P1
0 3 9 16 24
Determining Length of Next CPU Burst
th
1. tn actual length of n CPU burst
2. n 1 predicted value for the next CPU burst
3. , 0 1
4. Define :
Can only estimate the length
Can be done by using the length of previous CPU bursts, using exponential averaging
Prediction of the Length of the Next CPU Burst
Each process 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 Þ FIFO
q small Þ q must be large with respect to context switch, otherwise overhead is too high
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
P P P P P P P P
Typically, higher average1 turnaround2 3than SJF,1but better1 response1 1 1
0 4 7 10 14 18 22 26 30
Time Quantum and Context Switch Time
Multilevel Queue
Ready queue is partitioned into separate queues:
foreground (interactive)
background (batch)
Each queue has its own scheduling algorithm
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
Thread Scheduling
Distinction between user-level and kernel-level threads Many-to-one and many-to-many models,
thread library schedules user-level threads to run on LWP Known as process-contention scope
(PCS) since scheduling competition is within the process Kernel thread scheduled onto available
CPU is system-contention scope (SCS) – competition among all threads in system
Pthread Scheduling
API allows specifying either PCS or SCS during thread creation
PTHREAD SCOPE PROCESS schedules threads using PCS scheduling
PTHREAD SCOPE SYSTEM schedules threads using SCS scheduling.
Multiple-Processor Scheduling
CPU scheduling more complex when multiple CPUs are
available Homogeneous processors within a multiprocessor
Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the
need for data sharing
Symmetric multiprocessing (SMP) – each processor is self-scheduling, all processes in common ready
queue, or each has its own private queue of ready processes
Processor affinity – process has affinity for processor on which it is currently running
soft affinity
hard affinity
Multicore Processors
Recent trend to place multiple processor cores on same physical chip
Faster and consume less power
Multiple threads per core also growing
Takes advantage of memory stall to make progress on another thread while memory retrieve happens
Solaris Scheduling
Windows XP Priorities
Linux Scheduling
Constant order O(1) scheduling time
Two priority ranges: time-sharing and real-time
Real-time range from 0 to 99 and nice value from 100 to 140
Algorithm Evaluation
Deterministic modeling – takes a particular predetermined workload and defines the performance
of each algorithm for that workload
Queuing models
Implementation
Evaluation of CPU schedulers by Simulation
UNIT -3
CONCURRENCY
Process Synchronization
To introduce the critical-section problem, whose solutions can be used to ensure the consistency of
shared data
To present both software and hardware solutions of the critical-section problem
To introduce the concept of an atomic transaction and describe mechanisms to ensure atomicity
Concurrent access to shared data may result in data inconsistency
Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating
processes
Suppose that we wanted to provide a solution to the consumer-producer problem that fills all the buffers.
We can do so by having an integer count that keeps track of the number of full buffers. Initially, count is
set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the
consumer after it consumes a buffer
Producer
while (true) {
register1 = count
register1 = register1 + 1
count = register1
count-- could be implemented as
register2 = count
register2 = register2 - 1
count = register2
Consider this execution interleaving with “count = 5” initially:
Peterson’s Solution
Two process solution
Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted.
The two processes share two variables:
int turn;
Boolean flag[2]
The variable turn indicates whose turn it is to enter the critical section.
The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true
implies that process Pi is ready!
Algorithm for Process Pi
do {
flag[i] = TRUE;
turn = j;
while (flag[j] && turn == j);
critical section
flag[i] = FALSE;
remainder section
} while (TRUE);
Synchronization Hardware
Many systems provide hardware support for critical section
code Uniprocessors – could disable interrupts
Currently running code would execute without preemption
Generally too inefficient on multiprocessor systems
Operating systems using this not broadly scalable
Modern machines provide special atomic hardware instructions
Atomic = non-interruptable
Either test memory word and set value Or swap contents of two memory words
Swap Instruction
Definition:
void Swap (boolean *a, boolean *b)
{
boolean temp = *a;
*a = *b;
*b = temp:
}
/ critical section
lock = FALSE;
Semaphore
Synchronization tool that does not require busy waiting nSemaphore S – integer
variable Two standard operations modify S: wait() and signal() Originally called P() and
V()
Less complicated
Can only be accessed via two indivisible (atomic) operations
wait (S) {
while S <= 0
; // no-op
S--;
}
signal (S) {
S++;
}
Semaphore Implementation
Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the
same time
Thus, implementation becomes the critical section problem where the wait and signal code are placed in
the crtical section.
Could now have busy waiting in critical section implementation
But implementation code is short
Little busy waiting if critical section rarely occupied
Note that applications may spend lots of time in critical sections and therefore this is not a
good solution.
Semaphore Implementation with no Busy waiting
With each semaphore there is an associated waiting queue. Each entry in a waiting queue has two data
items:
value (of type integer)
pointer to next record in the list
Two operations:
block – place the process invoking the operation on the appropriate waiting queue.
wakeup – remove one of processes in the waiting queue and place it in the ready queue.
Implementation of wait:
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
Implementation of signal:
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
P0 P1
wait (S); wait (Q);
wait (Q); wait (S);
.
. .
. .
signal (S); signal (Q);
signal (Q); signal (S);
Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which
it is suspended
Priority Inversion - Scheduling problem when lower-priority process holds a lock needed by higher-
priority process
Classical Problems of Synchronization
Bounded-Buffer Problem
Readers and Writers Problem
Dining-Philosophers Problem
Bounded-Buffer Problem
N buffers, each can hold one item
Semaphore mutex initialized to the value 1
Semaphore full initialized to the value 0
Semaphore empty initialized to the value N.
The structure of the producer process
do { // produce an item in nextp
wait (empty);
wait (mutex);
/ add the item to the buffer
signal (mutex);
signal (full);
} while (TRUE);
The structure of the consumer process
do { wait (full);
wait (mutex);
/ remove an item from buffer to nextc
signal (mutex);
signal (empty);
Data set
Semaphore mutex initialized to 1
Semaphore wrt initialized to 1
Integer readcount initialized to 0
The structure of a writer process
do { wait (wrt) ;
/ writing is
performed signal (wrt) ;
} while (TRUE);
/ reading is performed
wait (mutex) ; readcount -
-;
if (readcount == 0) signal
(wrt) ;
signal (mutex) ;
} while (TRUE);
Dining-Philosophers Problem
Shared data
Bowl of rice (data set)
Semaphore chopstick [5] initialized to 1
The structure of Philosopher i:
do {
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) % 5] );
// eat
signal ( chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
/ think
} while (TRUE);
wait (mutex) …
wait (mutex)
Omitting of wait (mutex) or signal (mutex) (or both)
Monitors
A high-level abstraction that provides a convenient and effective mechanism for process synchronization
Only one process may be active within the monitor at a time monitor monitor-name
{
/ shared variable declarations
procedure P1 (…) { …. }
…
procedure Pn (…) {……}
Initialization code ( ….) { … }
…
}
}
Schematic view of a Monitor
Condition Variables
condition x, y;
Two operations on a condition variable:
x.wait () – a process that invokes the operation is
suspended.
x.signal () – resumes one of processes (if any) that
invoked x.wait ()
monitor DP
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
void pickup (int i) {
state[i] = HUNGRY;
test(i);
if (state[i] != EATING) self [i].wait;
}
Variables
semaphore mutex; // (initially = 1)
semaphore next; // (initially = 0)
int next-count = 0;nEach procedure F will be replaced by
wait(mutex);
…
body of F;
…
if (next_count > 0)
signal(next)
else
signal(mutex);nMutual exclusion within a monitor is ensured.
Monitor Implementation
For each condition variable x, we have:
semaphore x_sem; // (initially = 0)
int x-count = 0;nThe operation x.wait can be implemented as:
x-count++;
if (next_count > 0)
signal(next);
else
signal(mutex);
wait(x_sem);
x-count--;
Synchronization Examples
Solaris
Windows XP
Linux
Pthreads
Solaris Synchronization
Implements a variety of locks to support multitasking, multithreading (including real-time threads),
and multiprocessing
Uses adaptive mutexes for efficiency when protecting data from short code segments
Uses condition variables and readers-writers locks when longer sections of code need access to data
Uses turnstiles to order the list of threads waiting to acquire either an adaptive mutex or reader-writer
lock
Windows XP Synchronization
Uses interrupt masks to protect access to global resources on uniprocessor systems
Uses spinlocks on multiprocessor systems
Also provides dispatcher objects which may act as either mutexes and semaphores
Dispatcher objects may also provide events
An event acts much like a condition variable
Linux Synchronization
Linux:lPrior to kernel Version 2.6, disables interrupts to implement short critical sections
Version 2.6 and later, fully preemptive
Linux provides:
semaphores
spin locks
Pthreads Synchronization
Pthreads API is OS-independent
It provides:
mutex locks
condition variablesnNon-portable extensions include:
read-write locks
Dept. of Computer Science and Engineering Page 63
spin locks
Atomic Transactions
System Model
Log-based Recovery
Checkpoints
Concurrent Atomic Transactions
System Model
Assures that operations happen as a single logical unit of work, in its entirety, or not at all
Related to field of database systems
Challenge is assuring atomicity despite computer system failures
Transaction - collection of instructions or operations that performs single logical function
Here we are concerned with changes to stable storage – disk
Transaction is series of read and write operations
Terminated by commit (transaction successful) or abort (transaction failed) operation Aborted
transaction must be rolled back to undo any changes it performed
Types of Storage Media
Volatile storage – information stored here does not survive system crashes
Example: main memory, cache
Nonvolatile storage – Information usually survives crashes
Example: disk and tape
Stable storage – Information never lost
Not actually possible, so approximated via replication or RAID to devices with independent failure
modes
Goal is to assure transaction atomicity where failures cause loss of information on volatile storage
Log-Based Recovery
Record to stable storage information about all modifications by a transaction
Most common is write-ahead logging
Log on stable storage, each log record describes single transaction write operation, including
Transaction name
Data item name
Old value
New value
<Ti starts> written to log when transaction Ti starts
<Ti commits> written when Ti commits
Log entry must reach stable storage before operation on data occurs
Checkpoints
Log could become long, and recovery could take long
Checkpoints shorten log and recovery time.
Checkpoint scheme:
1.Output all log records currently in volatile storage to stable storage
2.Output all modified data from volatile to stable storage
3.Output a log record <checkpoint> to the log on stable storage
Now recovery only includes Ti, such that Ti started executing before the most recent checkpoint, and all
transactions after Ti All other transactions already on stable storage
Concurrent Transactions
Must be equivalent to serial execution – serializability
Could perform all transactions in critical section
Inefficient, too restrictive
Concurrency-control algorithms provide serializability
Serializability
Nonserial Schedule
Nonserial schedule allows overlapped execute
Resulting execution not necessarily incorrect
Consider schedule S, operations Oi, Oj
Conflict if access same data item, with at least one write
If Oi, Oj consecutive and operations of different transactions & Oi and Oj don’t conflict
Then S’ with swapped order Oj Oi equivalent to S
If S can become S’ via swapping nonconflicting operations
S is conflict serializable
Locking Protocol
Shared – Ti has shared-mode lock (S) on item Q, Ti can read Q but not write Q
Exclusive – Ti has exclusive-mode lock (X) on Q, Ti can read and write Q
Require every transaction on item Q acquire appropriate lock
If lock already held, new request may have to wait
Similar to readers-writers algorithm
If TS(Ti) < W-timestamp(Q), Ti needs to read value of Q that was already overwritten read operation rejected
and Ti rolled back
If TS(Ti) ≥ W-timestamp(Q) read executed, R-timestamp(Q) set to max(R-timestamp(Q), TS(Ti))
Timestamp-ordering Protocol
UNIT IV
Memory Management
A pair of base and limit registers define the logical address space
Address binding of instructions and data to memory addresses can happen at three different stages
Compile time: If memory location known a priori, absolute code can be generated; must recompile
code if starting location changes
Load time: Must generate relocatable code if memory location is not known at compile time
Execution time: Binding delayed until run time if the process can be moved during its execution from
one memory segment to another. Need hardware support for address maps (e.g., base and limit
registers)
Dynamic Loading
Routine is not loaded until it is called
Better memory-space utilization; unused routine is never loaded
Useful when large amounts of code are needed to handle infrequently occurring cases
No special support from the operating system is required implemented through program design
Dynamic Linking
Linking postponed until execution time
Small piece of code, stub, used to locate the appropriate memory-resident library routine
Stub replaces itself with the address of the routine, and executes the routine
Operating system needed to check if routine is in processes’ memory address
Dynamic linking is particularly useful for libraries
System also known as shared libraries
Swapping
A process can be swapped temporarily out of memory to a backing store, and then brought back into memory
for continued executionnBacking store – fast disk large enough to accommodate copies of all memory
images for all users; must provide direct access to these memory imagesnRoll out, roll in – swapping variant
used for priority-based scheduling algorithms; lower-priority process is swapped out so higher-priority
process can be loaded and executednMajor part of swap time is transfer time; total transfer time is directly
proportional to the amount of memory swappednModified versions of swapping are found on many systems
(i.e., UNIX, Linux, and Windows)
System maintains a ready queue of ready-to-run processes which have memory images on disk
Contiguous Allocation
Multiple-partition allocation
Hole – block of available memory; holes of various size are scattered throughout memory
When a process arrives, it is allocated memory from a hole large enough to accommodate it
Dept. of Computer Science and Engineering Page 71
OS OS OS OS
Paging
Logical address space of a process can be noncontiguous; process is allocated physical memory
whenever the latter is available
Divide physical memory into fixed-sized blocks called frames (size is power of 2, between 512 bytes
and 8,192 bytes)
Divide logical memory into blocks of same size called pagesnKeep track of all free frames
To run a program of size n pages, need to find n free frames and load program
Set up a page table to translate logical to physical addresses
Internal fragmentation
Paging Hardware
p d
m-n n
Paging Example
Free Frames
Associative Memory
Associative memory – parallel search
Address translation (p, d)
If p is in associative register, get frame # out
Otherwise get frame # from page table in memory
Page # Frame #
Shared Pages
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
Private code and data
Each process keeps a separate copy of the code and data
The pages for the private code and data can appear anywhere in the logical address space
Hierarchical Paging
Hashed Page Tables
Inverted Page Tables
where pi is an index into the outer page table, and p2 is the displacement within the page of the
outer page table
12 10 10
Address-Translation Scheme
Segmentation
Memory-management scheme that supports user view of memory
A program is a collection of segments
A segment is a logical unit such as:
main program
procedure function
method
object
local variables, global variables
common block
stack
symbol table
arrays
1 4
4 2
user space
Segmentation Architecture
Logical address consists of a two tuple:
o <segment-number, offset>,
Segment table – maps two-dimensional physical addressephysical;each tablememoryentryhas: space
base – contains the starting physical address where the segments reside in memory
limit – specifies the length of the segment
Segment-table base register (STBR) points to the segment table’s location in memory
Segment-table length register (STLR) indicates number of segments used by a program;
segment number s is legal if s < STLR
Protection
With each entry in segment table associate:
validation bit = 0 Þ illegal segment
read/write/execute privileges
Protection bits associated with segments; code sharing occurs at segment level
Since segments vary in length, memory allocation is a dynamic storage-allocation problem
A segmentation example is shown in the following diagram
Segmentation Hardware
Example of Segmentation
UNIT – 5
VIRTUAL MEMORY
Objective
To describe the benefits of a virtual memory system.
To explain the concepts of demand paging, page-replacement algorithms, and allocation of page
frames.
Virtual Memory
Virtual memory is a technique that allows the execution of process that may not be completely in
memory. The main visible advantage of this scheme is that programs can be larger than physical
memory.
Virtual memory is the separation of user logical memory from physical memory this separation allows an
extremely large virtual memory to be provided for programmers when only a smaller physical memory
is available ( Fig ).
Following are the situations, when entire program is not required to load fully.
1. User written error handling routines are used only when an error occurs in the data or computation.
2. Certain options and features of a program may be used rarely.
3. Many tables are assigned a fixed amount of address space even though only a small amount of the table
is actually used.
The ability to execute a program that is only partially in memory would counter many benefits.
1. Less number of I/O would be needed to load or swap each user program into memory.
2. A program would no longer be constrained by the amount of physical memory that is available.
3. Each user program could take less physical memory, more programs could be run the same time, with a
corresponding increase in CPU utilization and throughput.
Fig. Diagram showing virtual memory that is larger than physical memory.
Virtual memory is commonly implemented by demand paging. It can also be implemented in a
segmentation system. Demand segmentation can also be used to provide virtual memory.
Demand Paging
A demand paging is similar to a paging system with swapping(Fig 5.2). When we want to execute a
process, we swap it into memory. Rather than swapping the entire process into memory.
When a process is to be swapped in, the pager guesses which pages will be used before the process is
swapped out again Instead of swapping in a whole process, the pager brings only those necessary pages into
memory. Thus, it avoids reading into memory pages that will not be used in anyway, decreasing the swap time
and the amount of physical memory needed.
Hardware support is required to distinguish between those pages that are in memory and those pages that
are on the disk using the valid-invalid bit scheme. Where valid and invalid pages can be checked checking the
bit and marking a page will have no effect if the process never attempts to access the pages. While the process
executes and accesses pages that are memory resident, execution proceeds normally.
Access to a page marked invalid causes a page-fault trap. This trap is the result of the operating system's
failure to bring the desired page into memory. But page fault can be handled as following (Fig 5.3):
4. We schedule a disk operation to read the desired page into the newly allocated frame.
5. When the disk read is complete, we modify the internal table kept with the process and the page table to
indicate that the page is now in memory.
6. We restart the instruction that was interrupted by the illegal address trap. The process can now access the
page as though it had always been memory.
Therefore, the operating system reads the desired page into memory and restarts the process as though
the page had always been in memory.
The page replacement is used to make the frame free if they are not in used. If no frame is free then other
process is called in.
1. For a given page size we need to consider only the page number, not the entire address.
2. if we have a reference to a page p, then any immediately following references to page p will never cause
a page fault. Page p will be in memory after the
first reference; the immediately following references will not fault.
To determine the number of page faults for a particular reference string and page replacement algorithm,
we also need to know the number of page frames available. As the number of frames available increase, the
number of page faults will decrease.
FIFO Algorithm
The simplest page-replacement algorithm is a FIFO algorithm. A FIFO replacement algorithm associates
with each page the time when that page was brought into memory. When a page must be replaced, the oldest
page is chosen. We can create a FIFO queue to hold all pages in memory.
The first three references (7, 0, 1) cause page faults, and are brought into these empty eg. 7, 0, 1, 2, 0, 3,
0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1 and consider 3 frames. This replacement means that the next reference to 0 will fault.
Page 1 is then replaced by page 0.
Optimal Algorithm
An optimal page-replacement algorithm has the lowest page-fault rate of all algorithms. An optimal
page-replacement algorithm exists, and has been called OPT or MIN. It is simply
Replace the page that will not be used for the longest
period of time.
The optimal page-replacement algorithm is difficult to implement, because it requires future knowledge
of the reference string.
LRU Algorithm
The FIFO algorithm uses the time when a page was brought into memory; the OPT algorithm uses the
time when a page is to be used. In LRU replace the page that has not been used for the longest period of time.
LRU replacement associates with each page the time of that page's last use. When a page must be
replaced, LRU chooses that page that has not been used for the longest period of time.
R
Let S be the reverse of a reference string S, then the page-fault rate for the OPT algorithm on S is the
R
same as the page-fault rate for the OPT algorithm on S .
Some systems provide no hardware support, and other page-replacement algorithm. Many systems
provide some help, however, in the form of a reference bit. The reference bit for a page is set, by the hardware,
whenever that page is referenced. Reference bits are associated with each entry in the page table Initially, all
bits are cleared (to 0) by the operating system. As a user process executes, the bit associated with each page
referenced is set (to 1) by the hardware.
Additional-Reference-Bits Algorithm
The operating system shifts the reference bit for each page into the high-order or of its 5-bit byte,
shifting the other bits right 1 bit, discarding the low-order bit.
These 5-bit shift registers contain the history of page use for the last eight time periods. If the shift
register contains 00000000, then the page has not been
used for eight time periods; a page that is used at least once each period would have a shift register value of
11111111.
Second-Chance Algorithm
The basic algorithm of second-chance replacement is a FIFO replacement algorithm. When a page gets a
second chance, its reference bit is cleared and its arrival e is reset to the current time.
The second-chance algorithm described above can be enhanced by considering troth the reference bit
and the modify bit as an ordered pair.
Counting Algorithms
There are many other algorithms that can be used for page replacement.
• LFU Algorithm: The least frequently used (LFU) page-replacement algorithm requires that the page with the
smallest count be replaced. This algorithm suffers from the situation in which a page is used heavily during the
initial phase of a process, but then is never used again.
UNIT VI
Principles of deadlock
To develop a description of deadlocks, which prevent sets of concurrent processes from completing their
tasks.To present a number of different methods for preventing or avoiding deadlocks in a computer system
The Deadlock Problem
A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in
the set
Example
System has 2 disk drives
P1 and P2 each hold one disk drive and each needs another
one Example
semaphores A and B, initialized to 1
P0 P1
System Model
Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
Each resource type Ri has Wi instances.
Each process utilizes a resource as follows:
request
use
release
Deadlock Characterization
Process
Pi is holding an instance of Rj
Pi
Rj
Example of a Resource Allocation Graph
Basic Facts
If graph contains no cycles Þ no deadlocknIf graph contains a cycle Þlif only one instance per resource type,
then deadlock
if several instances per resource type, possibility of deadlock
Methods for Handling Deadlocks
Ensure that the system will never enter a deadlock statenAllow the system to enter a deadlock state and then
recovernIgnore the problem and pretend that deadlocks never occur in the system; used by most operating
systems, including UNIX
Deadlock Prevention
Restrain the ways request can be made
Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources
Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other
resources
Require process to request and be allocated all its resources before it begins execution, or allow process to
request resources only when the process has none
Low resource utilization; starvation possible
No Preemption –
If a process that is holding some resources requests another resource that cannot be immediately allocated to
it, then all resources currently being held are released
Preempted resources are added to the list of resources for which the process is waiting
Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting
Circular Wait – impose a total ordering of all resource types, and require that each process requests resources
in an increasing order of enumeration
Deadlock Avoidance
Requires that the system has some additional a priori information
available
Simplest and most useful model requires that each process declare the maximum number of resources of
each type that it may need
The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can
never be a circular-wait condition
Resource-allocation state is defined by the number of available and allocated resources, and the maximum
demands of the processes
Safe State
When a process requests an available resource, system must decide if immediate allocation leaves the system
in a safe state
System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the processes is the systems such that 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 < inThat is:
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
Basic Facts
If a system is in safe state Þ no deadlocksnIf a system is in unsafe state Þ possibility of deadlocknAvoidance Þ
ensure that a system will never enter an unsafe state.
Safe, Unsafe , Deadlock State
Avoidance algorithms
Single instance of a resource type
Use a resource-allocation graph
Multiple instances of a resource type
Use the banker’s algorithm
Resource-Allocation Graph
Banker’s Algorithm
Multiple instancesnEach process must a priori claim maximum usenWhen a process requests a resource it may
have to wait nWhen a process gets all its resources it must return them in a finite amount of time
Request = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of resource type
Rj1. If Requesti £ Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum
claim
2. If Requesti £ Available, go to step 3. Otherwise Pi must wait, since resources are not available
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Request;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
lIf safe Þ the resources are allocated to Pi
lIf unsafe Þ Pi must wait, and the old resource-allocation state is restored
The content of the matrix Need is defined to be Max – Allocation Need ABC
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria
Example: P1 Request (1,0,2)
Check that Request £ Available (that is, (1,0,2) £ (3,3,2) Þ true Allocation Need
Available ABC ABC ABC
P0 010 743 230
P1 30 2 020
P2 301 600
P3 211 011
P4 002 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?
Can request for (0,2,0) by P0 be granted?
Deadlock Detection
Allow system to enter deadlock state Detection algorithmRecovery scheme
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively Initialize:
(a) Work = Available(b)For i = 1,2, …, n, if Allocationi ¹ 0, then
Algorithm requires an order of O(m x n2) operations to detect whether the system is in deadlocked state
Detection-Algorithm Usage
When, and how often, to invoke depends on:
How often a deadlock is likely to occur?
How many processes will need to be rolled back?
one for each disjoint cyclenIf detection algorithm is invoked arbitrarily, there may be many cycles in the
resource graph and so we would not be able to tell which of the many deadlocked processes “caused” the
deadlock
Recovery from Deadlock: Process Termination
Abort all deadlocked processesnAbort one process at a time until the deadlock cycle is eliminatednIn which
order should we choose to abort?
Priority of the process
lHow long process has computed, and how much longer to
completion lResources the process has used
lResources process needs to complete
lHow many processes will need to be terminated
lIs process interactive or batch?
UNIT VII
7.2 : ACCESSMETHODS:-
There are several ways that the information in the file can be accessed.
1)sequential method 2) direct access method 3) other access methods.
1)sequential access method:-
the simplest access method is S.A. information in the file is processed in order, one
after the other. the bulk of the operations on a file are reads & writes. It is based on
a tape model of a file. Fig 10.3
2)Direct access:- or relative access:-
a file is made up of fixed length records, that allow programs to read and write
record rapidly in no particular order. For direct access, file is viewed as a numbered
sequence of blocks or records. A direct access file allows, blocks to be read & write.
So we may read block15, block 54 or write block10. there is no restrictions on the
order of reading or writing for a direct access file. It is great useful for immediate
access to large amount of information.
The file operations must be modified to include the block number as a parameter.
We have read n, where n is the block number.
3)other access methods:-
the other access methods are based on the index for the file. The indexed contain
pointers to the various blocks. To find an entry in the file , we first search the index
and then use the pointer to access the file directly and to find the desired entry. With
large files. The index file itself, may become too large to be kept in memory. One
solution is to create an index for the index file. The primary index file would contain
pointers to secondary index files which would point to the actual data iteams
A relative pathname defines a path from the current directory ex:- prt/first is relative
path name.
A cyclic- graph directory:-
Consider two programmers who are working on a joint project. The files associated
with that project can be stored in a sub directory , separating them from other projects
and files of the two programmers. The common subdirectory is shared by both
programmers. A shared directory or file will exist in the file system in two places at
once. Notice that a shared file is not the same as two copies of the file with two
copies, each programmer can view the copy rather than the original but if one
programmer changes the file the changes will not appear in the others copy with a
shared file there is only one actual file, so any changes made by one person would
be immediately visible to the other.
A tree structure prohibits the sharing of files or directories. An acyclic graph
allows directories to have shared subdirectories and files
FIG 10.10 . it is more complex and more flexiable. Also several problems may
occurs at the traverse and deleting the file contents. .
(b)
Mount Point
7.5:File Sharing
Sharing of files on multi-user systems is desirable nSharing may be done through
a protection schemenOn distributed systems, files may be shared across a
networknNetwork File System (NFS) is a common distributed filesharing method
Protection:-]
When the information is kept in the system the major worry is its protection
from the both physical damage (Reliability) and improper access(Protection).
The reliability is generally provided by duplicate copies of files.
The protection can be provided in many ways . for some single system user, we
might provide protection by physically removing the floppy disks . in a multi-user
systems, other mechanism are needed.
1) types of access:-
if the system do not permit access to the files of other users, protection is not
needed. Protection mechanism provided by controlling accessing. This can be
provided by types of file access. Access is permitted or denied depending on several
factors. Suppose we mentioned read that file allows only for read . Read:- read from
the file.
Write:- write or rewrite the file.
Execute:- load the file in to memory and execute it.
Append:- write new information at the end of the file.
Delete:- delete the file and free its space for possible reuse.
1) bit vector:-
The free space list is implemented as a bit map or bit vector. Each block is
represented by 1 bit. If the block is free the bit is 1 if the block is allocated the bit
is 0.
Ex:- consider a disk where blocks 2,3,4,5,8,9,10,11,12,13,17,18,25, are free and
rest of blocks are allocated the free space bit map would be
001111001111110001100000010000……..
the main advantage of this approach is that it is relatively simple and efficient to
find the first free block or ‘n’ consecutive free blocks on the disk
2) Linked list:-
Another approach is to link together all the free disk blocks, keeping a pointer
to the first free block in a special location on the disk and caching it in memory. This
first block contain a pointer to the next free disk block, and so on.
How ever this scheme is not efficient to traverse the list, we must read each
block, which requires I/O time.
Disk space is also wasted to maintain the pointer to next free space.
3) Grouping:-
Another method is store the addresses of ‘n’ free blocks in the first free block.
The first (n-1) of these blocks are actually free. The last block contains the
addresses of another ‘n’ free blocks and so on. Fig 11.8
Advantages:- the main advantage of this approach is that the addresses of a
large no.of blocks can be found quickly.
4) Counting:-
Another approach is counting. Generally several contiguous blocks may be
allocated or freed simultaneously. Particularly when space is allocated with the
contiguous allocation algorithm rather than keeping a list of ‘n’ free disk address. We
can keep the address of first free block and the number ‘n’ of free contiguous blocks
that follow the first block. Each entry in the free space list then consists of a disk
address and a count.
7.9:Directory Implementation:-
1) Linear list:-
1) the simple method of implement ting a directory is to use a linear list of
file names with pointers to the data blocks.
2) A linear list of directory entries requires a linear search to find a particular
entry.
3) This method is simple to program but is time consuming to execute.
4) To create a new file, we must first search the directory to be sure that no
existing file has the same name. Then, we add a new entry at the end of
the directory.
UNIT VIII
MASS-STORAGE STRUCTURE
Mass-Storage Systems
nDescribe the physical structure of secondary and tertiary storage devices and
the resulting effects on the uses of the devices nExplain the performance
characteristics of mass-storage devices nDiscuss operating-system services
provided for mass storage, including RAID and HSM
8.1:Overview of Mass Storage Structure
Magnetic disks provide bulk of secondary storage of modern
computers Drives rotate at 60 to 200 times per second
Transfer rate is rate at which data flow between drive and computer
Positioning time (random-access time) is time to move disk arm to desired cylinder
(seek time) and time for desired sector to rotate under the disk head (rotational
latency) Head crash results from disk head making contact with the disk surface
That’s bad
Disks can be removable
Drive attached to computer via I/O bus
Busses vary, including EIDE, ATA, SATA, USB, Fibre Channel, SCSI
Host controller in computer uses bus to talk to disk controller built into drive or storage
array
Moving-head Disk Mechanism
8.4:Disk Scheduling
The operating system is responsible for using hardware efficiently — for the disk
drives, this means having a fast access time and disk bandwidth Access time
has two major components
Seek time is the time for the disk are to move the heads to the
cylinder containing the desired sector
Rotational latency is the additional time waiting for the disk to rotate the desired
sector to the disk head
Minimize seek time
Seek time » seek distance
Disk bandwidth is the total number of bytes transferred, divided by the total
time between the first request for service and the completion of the last transfer
Several algorithms exist to schedule the servicing of disk I/O requests nWe
illustrate them with a request queue (0-199) 98, 183, 37, 122, 14, 124, 65, 67
Head pointer 53
FCFS
Illustration shows total head movement of 640 cylinders
SSTF
Selects the request with the minimum seek time from the current head
position SSTF scheduling is a form of SJF scheduling; may cause starvation
of some requests
nIllustration shows total head movement of 236 cylinders
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.nSCAN algorithm Sometimes
called the elevator algorithm
8.5:Swap-Space Management
Swap-space — Virtual memory uses disk space as an extension of main
memory
Swap-space can be carved out of the normal file system, or, more commonly, it
can be in a separate disk partition
Swap-space management
4.3BSD allocates swap space when process starts; holds text segment
(the program) and data segment
Kernel uses swap maps to track swap-space use
Solaris 2 allocates swap space only when a page is forced out of physical