os notes
os notes
Introduction to OS
A program that acts as an intermediary between a user of a
computer and the computer hardware
o Operating system
o Users
OS is a resource allocator
o Decides between conflicting requests for efficient and fair resource use
OS is a control program
Computer Startup
Device controller informs CPU that it has finished its operation by causing an
interrupt
polling
I/O Structure
After I/O starts, control returns to user program only upon I/O completion
After I/O starts, control returns to user program without waiting for I/O
completion
Storage Structure
Main memory – only large storage media that the CPU can access directly
Only one interrupt is generated per block, rather than the one
interrupt per byte
Storage Hierarchy
o Speed
o Cost
o Volatility
Caching
o Disk surface is logically divided into tracks, which are subdivided into
sectors
o Advantages include
Increased throughput
Economy of scale
o Two types
Asymmetric Multiprocessing
Symmetric Multiprocessing
o Single user cannot keep CPU and I/O devices busy at all times
o When it has to wait (for I/O for example), OS switches to another job
OS Services
One set of operating-system services provides functions that are helpful to the
user:
o User interface - Almost all operating systems have a user interface (UI)
o Accounting - To keep track of which users use how much and what
kinds of computer resources
Three most common APIs are Win32 API for Windows, POSIX API for
POSIX- based systems (including virtually all versions of UNIX, Linux,
and Mac OS X), and Java API for the Java virtual machine (JVM)
Example
System call sequence to copy the contents of one file to another file
Typically, a number associated with each system call
The caller need know nothing about how the system call is implemented
o Just needs to obey API and understand what OS will do as a result call
Process control
File management
Device management
Information maintenance
Communications
Protection
OS Structure
Layered Approach
With modularity, layers are selected such that each uses functions
(operations) and services of only lower-level layers
Benefits:
o More secure
Detriments:
Virtual Machne
Process Management
An operating system executes a variety of programs:
A process includes:
o program counter
o stack
o data section
Process State
Process state
Program counter
CPU registers
Memory-management information
Accounting information
Context Switching
When CPU switches to another process, the system must save the
state of the old process and load the saved state for the new process
via a context switch
Schedulers
Process Creation
Resource sharing
Execution
Address space
UNIX examples
Process Termination
Process executes last statement and asks the operating system to delete it
(exit)
o If parent is exiting
o Information sharing
o Computation speedup
o Modularity
o Convenience
o Shared memory
o Message passing
Cooperating Process
o Information sharing
o Computation speed-up
o Modularity
o Convenience
while (true) {
/* Produce an item */
buffer[in] = item;
while (true)
item = buffer[out];
IPC-Message Passing
o receive(message)
Direct Communication
Indirect Communication
o Operations
destroy a mailbox
Synchronisation
o Blocking send has the sender block until the message is received
o Non-blocking send has the sender send the message and continue
Buffering
Thread
A thread is a flow of execution through the process code, with its
own program counter, system registers and stack.
Benefits
Responsiveness
Resource Sharing
Economy
Scalability
User Threads
o POSIX Pthreads
o Win32 threads
o Java threads
Kernel Thread
Examples
o Windows XP/2000
o Solaris
o Linux
o Tru64 UNIX
o Mac OS X
Multithreading Models
Many-to-One
o Examples:
o Examples
o Windows NT/XP/2000
o Linux
Many-to-Many
Thread library provides programmer with API for creating and managing
threads
Pthreads
A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
Java Threads
o Asynchronous or deferred
Signal handling
Thread pools
Thread-specific data
Scheduler activations
Thread Cancellation
Thread Pools
Advantages:
Thread Scheduling
Process switching needs interaction with Thread switching does not need
operating system. to interact with operating system.
If one process is blocked then no other While one thread is blocked and
process can execute until the first waiting, second thread in the
process is unblocked. same task can run.
Process Scheduling
Maximum CPU utilization obtained with multiprogramming
CPU Scheduler
4. Terminates
preemptive Dispatcher
Dispatcher module gives control of the CPU to the process selected by
the short- term scheduler; this involves:
o switching context
Max throughput
Associate with each process the length of its next CPU burst.
Use these lengths to schedule the process with the shortest
time
Priority Scheduling
o Preemptive
o nonpreemptive
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
o q large FIFO
o foreground – RR
o background – FCFS
Scheduling must be done between the queues
o number of queues
Process Synchronization
Concurrent access to shared data may result in data inconsistency
implemented as register1
= count
register1 = register1
+ 1 count = register1
register2 = count
register2 = register2
- 1 count = register2
Assume that the LOAD and STORE instructions are atomic; that is,
cannot be interrupted.
o int turn;
o Boolean flag[2]
The variable turn indicates whose turn it is to enter the critical section.
Hardware Synchronization
Atomic = non-interruptable
o Either test memory word and set value
do {
acquire lock
critical
section release
lock
remainder section
} while (TRUE);
TestAndndSet Instruction
boolean rv = *target;
*target =
TRUE; return
rv:
Solution:
do {
; // do nothing
// critical
section lock =
FALSE;
// remainder section
} while (TRUE);
Sawp Instruction
*a = *b;
*b = temp:
Solution
: do {
key = TRUE;
Swap (&lock,
&key );
// critical
// remainder section
} while (TRUE);
do {
waiting[i] = TRUE;
key = TRUE;
waiting[i] = FALSE;
// critical
section j = (i + 1) %
n;
waiting[j]) j = (j
+ 1) % n;
if (j == i)
lock = FALSE;
else
waiting[j] = FALSE;
// remainder section
} while (TRUE);
Semaphore
Less complicated
; // no-op }
S--;
to 1 do {
wait (mutex);
// Critical Section
signal (mutex);
// remainder section
} while (TRUE);
Semaphore Implementation
Two operations:
o block – place the process invoking the operation on the
Implementation of
wait:
wait(semaphore
*S) {
S->value--;
if (S->value < 0) {
>list; block();
Implementation of signal:
signal(semaphore
*S) { S-
>value++;
if (S->value <= 0) {
wakeup(P);
Bounded-Buffer Problem
Dining-Philosophers Problem
Bounded-Buffer Problem
The pool consists of n buffers, each capable of holding one item. The mutex semaphore
provides mutual exclusion for accesses to the buffer pool and is initialized to the value
1. The empty and full semaphores count the number of empty and full buffers. The
semaphore empty is initialized to the value n; the semaphore full is initialized to the
value 0.
N buffers, each can hold one item
process do {
// produce an item in
wait (mutex);
signal (full);
} while (TRUE);
process do {
wait (full);
wait
(mutex);
signal (empty);
// consume the item in nextc
} while (TRUE);
Readers-Writers Problem
o Readers – only read the data set; they do not perform any updates
Shared Data
o Data set
process do {
wait (wrt) ;
// writing is
performed signal
(wrt) ;
} while (TRUE);
process do {
wait (mutex) ;
readcount ++ ;
if (readcount == 1)
wait (wrt) ;
signal (mutex)
// reading is performed
wait (mutex) ;
readcount - - ;
if (readcount == 0)
signal (wrt) ;
signal (mutex) ;
} while (TRUE);
Dining-Philosophers Problem
Consider five philosophers who spend their lives thinking and eating. The philosophers
share a circular table surrounded by five chairs, each belonging to one philosopher. In
the center of the table is a bowl of rice, and the table is laid with five single chopsticks).
When a philosopher thinks, she does not interact with her colleagues. From time to
time, a philosopher gets hungry and tries to pick up the two chopsticks that are closest
to her (the chopsticks that are between her and her left and right neighbors). A
philosopher may pick up only one chopstick at a time. Obviously, she cannot pick up a
chopstick that is already in the hand of a neighbor. When a hungry philosopher has both
her chopsticks at the same time, she eats without releasing the chopsticks. When she is
finished eating, she puts down both chopsticks and starts thinking again.
Shared data
o Bowl of rice (data set)
o 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);
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:
o x.wait () – a process that invokes the operation is suspended.
x.signal () – resumes one of processes (if any) that invoked
x.wait ()
Monitor with Condition Variables
Monitor Implementation Using Semaphores
Variables
semaphore mutex; // (initially
= 1) semaphore next; //
(initially = 0) int next-count =
0;
Each procedure F will be replaced by
wait(mutex);
…
body of F;
…
if (next_count > 0)
signal(next)
else
signal(mutex);
Mutual exclusion within a monitor is ensured.
Monitor Implementation
For each condition variable x, we have:
semaphore x_sem; // (initially
= 0) int x-count = 0;
The operation x.wait can be implemented as:
x-count++;
if (next_count > 0)
signal(next);
else
signal(mutex);
wait(x_sem
); x-
count--;
The operation x.signal can be
Deadlock
A set of blocked processes each holding a resource and waiting to
acquire a resource held by another process in the set
Example
o P1 and P2 each hold one disk drive and each needs another one
Example
P0 P1
System Model
Resource types R1, R2, . . ., Rm (CPU cycles, memory space, I/O devices)
o request
o use
o release
Deadlock Characterization
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.
o P = {P1, P2, …, Pn}, the set consisting of all the processes in the
system
o R = {R1, R2, …, Rm}, the set consisting of all resource types in the
system
Process
Pi requests instance of Rj
Pi is holding an instance of Rj
Ignore the problem and pretend that deadlocks never occur in the
system; used by most operating systems, including UNIX
Deadlock Prevention
No Preemption –
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
Safe state
That is:
Facts
Banker’s Algorithm
Assumptions
Multiple instances
Each process must a priori claim maximum use
When a process requests a resource it may have to wait
When a process gets all its resources it must return them in a finite
amount of time
Data Structure for Bankers’ Algorithm
Let n = number of processes, and m = number of resources types.
Available: Vector of length m. If available [j] = k, there are k
instances of resource type Rj available
Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k
instances of resource type Rj
Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated
k
instances of Rj
Memory Management
Program must be brought (from disk) into memory and placed within
a process for it to be run
Main memory and registers are only storage CPU can access directly
A pair of base and limit registers define the logical address space
Logical vs Physical Address Space
Address Binding
o 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)
The user program deals with logical addresses; it never sees the real
physical addresses
Dynamic Loading
Dynamic Linking
Stub replaces itself with the address of the routine, and executes the routine
libraries Swapping
Major part of swap time is transfer time; total transfer time is directly
proportional to the amount of memory swapped
Multiple-partition allocation
Best-fit: Allocate the smallest hole that is big enough; must search
entire list, unless ordered by size
Worst-fit: Allocate the largest hole; must also search entire list
Fragmentation
o I/O problem
Paging
Internal fragmentation
Memory Protection
Shared Pages
Shared code
o One copy of read-only (reentrant) code shared among processes
(i.e., text editors, compilers, window systems).
o The pages for the private code and data can appear
anywhere in the logical address space
Hierarchical Paging
Virtual page numbers are compared in this chain searching for a match
o Demand paging
o Demand segmentation
Demand Paging
o Faster response
o More users
Lazy swapper – never swaps a page into memory unless page will be needed
Page Fault
4. Reset tables
Page Replacement
Prevent over-allocation of memory by modifying page-fault service
routine to include page replacement
Bring the desired page into the (newly) free frame; update the page
and frame tables
Restart the process
(First-in-First-Out)
Allocation of Frames
Each process needs minimum number of pages
Two major allocation schemes
o fixed allocation
o priority allocation
Equal allocation – For example, if there are 100 frames and 5
processes, give each process 20 frames.
Proportional allocation – Allocate according to the size of process
si size of process pi S
si
m totalnumber of frames
s
ai allocation for pi i
mS
Global vs Local Allocation
Global replacement – process selects a replacement frame from
the set of all frames; one process can take a frame from another
Local replacement – each process selects from only its own set of
allocated frames
Thrashing
If a process does not have “enough” pages, the page-fault rate is very
high. This leads to:
o low CPU utilization
o operating system thinks that it needs to increase the
degree of multiprogramming
o another process added to the system
Thrashing a process is busy swapping pages in and out
MODULE-III
File System
File
Types:
o Data
numeric
character
binary
o Program
File Structure
o Lines
o Fixed length
o Variable length
Complex Structures
o Formatted document
Who decides:
o Operating system
l Program
File Attribute
Time, date, and user identification – data for protection, security, and
usage monitoring
File Types
File Operations
Open(Fi) – search the directory structure on disk for entry Fi, and
move the content of entry to memory
read read n
next write n
write position to n
next read
reset next
write next
(rewrite)
Directory Structure
Naming problem
Grouping problem
Path name
Efficient searching
No grouping capability
C. Tree Structure Directory
Efficient searching
Grouping Capability
File Sharing
Client-server model allows clients to mount remote file systems from servers
o Standard operating system file calls are translated into remote calls
Remote file systems add new failure modes, due to network failure, server
failure
File structure
An allocation method refers to how disk blocks are allocated for files:
A. Contiguous Allocation
n Random access
B. Linked Allocation
n No random access
n Mapping
C. Indexed Allocation
n Random access
n Dynamic access without external fragmentation, but have
overhead of index block.
Disk Structure
o Sector 0 is the first sector of the first track on the outermost cylinder.
Disk Scheduling
o Seek time is the time for the disk are to move the heads to the
cylinder containing the desired sector.
o Rotational latency is the additional time waiting for the disk to
rotate the desired sector to the disk head.
n Selects the request with the minimum seek time from the current head position.
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.
The head moves from one end of the disk to the other. servicing
requests as it goes. When it reaches the other end, however, it
immediately returns to the beginning of the disk, without servicing
any requests on the return trip.
Treats the cylinders as a circular list that wraps around from the last
cylinder to the first one
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.
Disk Management
Low-level formatting, or physical formatting — Dividing a disk into
sectors that the disk controller can read and write.
To use a disk to hold files, the operating system still needs to record
its own data structures on the disk.
The controller can be told to replace each bad sector logically with
one of the spare sectors. This scheme is known as sector sparing or
forwarding.
A swap space can reside in one of two places: it can be carved out of
the normal file system, or it can be in a separate disk partition.
If the swap space is simply a large file within the file system, normal
file-system routines can be used to create it, name it, and allocate its
space.
I/O Systems
I/O Hardware
A bus is a set of wires and a rigidly defined protocol that specifies a set
of messages that can be sent on the wires.
When device A has a cable that plugs into device B, and device B has a
cable that plugs into device C, and device C plugs into a port on the
computer, this arrangement is called a daisy chain. A daisy chain
usually operates as a bus.
But the SCSI protocol is complex, the SCSI bus controller is often
implemented as a separate circuit board (or a host adapter) that plugs
into the computer. It typically contains a processor, microcode, and
some private memory to enable it to process the SCSI protocol
messages.
Polling
Determines state of device
o command-ready
o busy
o Error
Most CPUs have two interrupt request lines. One is the nonmaskable
interrupt, which is reserved for events such as unrecoverable memory
errors.
The second interrupt line is maskable: it can be turned off by the CPU
before the execution of critical instruction sequences that must not be
interrupted.
o Character-stream or block
o Sequential or random-access
o Sharable or dedicated
o Speed of operation
o read-write, read only, or write
Network Devices
o Difficult to use
Scheduling
o Key to performance
o i.e., Printing
Reference
Abraham Silberschatz, Greg Gagne, and Peter Baer Galvin,
"Operating System Concepts, Ninth Edition ",