Escolar Documentos
Profissional Documentos
Cultura Documentos
Spring2005 1 Spring2005 2
Operating System
Spring2005 5 Spring2005 6
1
History of Operating Systems (1) History of Operating Systems (2)
1960s
1940s: no operating systems.
Multiprogramming Systems
Assembly language developed More peripheral devices, such as card readers, disk
1950s: drives, tape drives, printers.
Process-bound jobs and IO-bound jobs.
First OS for IBM 701 computer by General Motors Running multiple jobs at a time — optimize resource
Research Labs utilization.
One job a time—Single-stream batch-processing Timesharing Systems
systems. Interactive users communicate with their jobs during
execution. (via “dumb terminals”)
processor job1 job2 job3 job4 …
The term process is used to describe a program in
execution.
Virtual memory
Spring2005 7 Spring2005 8
Spring2005 9 Spring2005 10
To be Continued
Spring2005 11 Spring2005 12
2
History of Operating Systems (7) History of Operating Systems (8)
Year Operating Systems
1998 Windows 98 Windows NT 4.0 1990s
Terminal Server Edition
To be Continued
Spring2005 13 Spring2005 14
Spring2005 15 Spring2005 16
Spring2005 17 Spring2005 18
3
Operating System Architectures (2) Operating System Architectures (3)
Microkernel Architecture
Layered Architecture Only a small number of services (Typically IPC,
Group components that perform similar functions Memory management and process
into layers. Each layer communicates only with synchronization) are provided to keep the kernel
neighbour layers small and scalable. Most OS components such as
the file system, process scheduler and device
Layer 4 User applications User Space manager execute outside the kernel.
User applications User Space
Layer 3 I/O Management
Layer 2 Message interpreter System calls
Kernel Space
Layer 1 Memory management File System Process scheduling Device Management
Layer 0 Process management
Kernel Space
IPC
Summary
Spring2005 21 Spring2005 22
Spring2005 23 Spring2005 24
4
Processor Processor Components (1)
Spring2005 25 Spring2005 26
ALU – Arithmetic and logic unit Computer time is measured in cycles. A cycle
Registers are high-speed memories located refers to one complete oscillation of an
on a processor that hold data for immediate electrical signal provided by the system clock
use by the processor. generator.
System clock generator decides the
frequency at which buses transfer data,
measured in cycles per second or Hz
Spring2005 27 Spring2005 28
Spring2005 29 Spring2005 30
5
Secondary Storage Buses
Spring2005 31 Spring2005 32
Serial ports – transfer data one bit a time. Programming languages can be
Mice, keyboards Machine and assembly languages
Parallel ports – transfer data several bits a High-level languages
time. Java, C++, VB++..
Printers
USB ports – Transfer data at a fast speed.
480Mbit per second (USB 2.0)
External disk drives, digital cameral, printers
Spring2005 33 Spring2005 34
Spring2005 35 Spring2005 36
6
Interpreters and Compilers Application Programming Interfaces
(APIs)
Compiler – covert high-level language APIs provide a set of routines that
program into machine language or low-level programmers can use to request services
language. from the system.
Interpreter – Directly execute source code or UNIX POSIX (Portable Operating System
code that has been reduced to a low-level Interface)
language. Win32 API
Spring2005 37 Spring2005 38
Compiling, Linking and Loading (1) Compiling, Linking and Loading (2)
Compiling Linking
Integrate the various modules referenced by a
Source Tokens Abstract
Intermediate program into a single executable routine.
code Syntax tree
lexer Parser code Shared libraries – collection of functions that can
generator
be shared between different processes.
Dynamic linking – The linking is postponed until
execution time.
Code
optimizer Object module
Low-level Low-level generator
language language (Machine
Instructions)
Spring2005 39 Spring2005 40
Compiling, linking and loading (3) Compiling, linking and loading (4)
Loading
Load program load modules into memory.
Compiler Linker Loader
Absolute loading
Program Object Load Executable
Relocatable loading
Code Module Module Program
Other
Absolute Absolute loading Absolute System
Object
Compile Library
Symbolic Address Address Modules
Address Relocatable
Relocatable loading Absolute Dynamically
Address Loaded
Address
Library
Dynamic loading – a program load module is not loaded
until it is called.
Spring2005 41 Spring2005 42
7
Summary
Spring2005 43 Spring2005 44
Process Control Block (PCB) (1) Process Control Block (PCB) (2)
Maintains information about the Process counter A process table is used to allow PCB to be
process. registers accessed quickly.
Process counter – a value that state
priority
determines which instruction the PID PCB
Address space
process should execute next. 1
parent 2
Execution Context of a processor– children …
the register content when the process Open files n
was last running. It enables a process Process table PCB
execution context to be restored when …………... PCB
PCB
the process returns to the running
state.
PID – process identification number
Spring2005 47 Spring2005 48
8
Process Queues (1) Process queues (2)
PCB7 PCB3 PCB5
Job queue: a list of all the processes in the head
Ready
system Queue tail
Ready queue: a list of processes that are
residing in memory and are ready and PCB5 PCB2
waiting to execute head
Print
Device queue: a list of processes waiting for Queue tail
Spring2005 49 Spring2005 50
B C D
Scheduling
I/O I/O queue I/O request
E F
Time-out
Block and unblock a process
Fork a child Dispatch a process
Interrupt
Change priority
Operations for interprocess communication.
Spring2005 51 Spring2005 52
9
Interrupts (1) Interrupts (2)
Spring2005 55 Spring2005 56
Spring2005 57 Spring2005 58
Essential for processes that must coordinate Signals are software interrupts that notify a process
that an event has occurred.
activities to achieve a common goal.
It does not allow processes to exchange data with other
Implementation processes.
Signals A process may catch, ignore or mask a signal.
Catch – A routine specified by the process is called by the
Message passing system when it delivers the signal.
Ignore – the process relies on the system’s default action to
handle the signal.
Abort
Memory dump.
Spring2005 59 Spring2005 60
10
Message passing Summery
What is a thread?
11
Thread States Threads Operations
born
Interval expires
start Common operations
ready
Create
notify I/O completion
Exit
preemption dispatch
Suspend and resume
running
Wait for event I/O request Sleep and wake
sleep complete Some OS also has
waiting sleeping dead blocked Cancel
Join
Spring2005 67 Spring2005 68
Thread implementations vary among This is also called many-to-one thread mapping.
operating systems. The user-level threads perform threading operations
in user space.
Three primary threading models
They are created by runtime libraries that cannot execute
User-level threads privileged instructions or access kernel primitives directly.
Kernel-level threads Each multithreaded process is responsible for maintaining
A combination of user- and kernel-level threads its threads.
The system treats each multithreaded process as a single
execution unit.
Spring2005 69 Spring2005 70
Benefits
One User space Better portability.
process It is easier for a developer to control the scheduling to meet
a specific requirements of an application
Thread Low overhead
Drawbacks
Performance is not guaranteed.
Execution The multithreaded process is viewed as a single thread of
context control by the system
The entire multithreaded process blocks when any of its
threads request a blocking I/O operation.
Thread scheduling priority is not supported systemwide.
Kernel space
Spring2005 71 Spring2005 72
12
Kernel-level Threads (1) Kernel-level Threads (2)
Kernel space
Spring2005 73 Spring2005 74
Spring2005 75 Spring2005 76
Kernel space
Spring2005 77 Spring2005 78
13
Thread Implementation Issues (1) Thread Implementation Issues (2)
Spring2005 81 Spring2005 82
Spring2005 83 Spring2005 84
14
Java Interface Runnable Java Threads --Example 2
class PrimeThread2 implements Runnable {
long minprime;
Java.lang.Runnable PrimeThread2(long num) {
this.minprime=num;
should be implemented by any class whose }
instances are intended to be executed by a public void run() {
//calculate the next prime larger than minprime
thread. }
}
The class must define a run method.
Spring2005 85 Spring2005 86
Summery
Spring2005 87 Spring2005 88
item
Spring2005 89 Spring2005 90
15
Bounded-Buffer Producer and Bounded-Buffer Producer and
Consumer Problem – A solution Consumer Problem – A solution
Low-level Language:
Producer process Consumer process Count++; Count --;
While (count >0){ R1 = count; R2 = count;
While (count < N){
…. count--; R1 = R1 + 1; R2 = R1 - 1;
item = produce an item; item =buffer[out]; count = R1; count = R2;
.. out = (out+1)%N;
… Assume count=5. Here is one possible interleaving:
count++;
buffer[in]=item; consume item s1 producer R1 = count; { R1=5 }
in = (in+1)%N; …
s2 producer R1 = R1 + 1; { R1=6 }
} }
s3 consumer R2 = count; { R2=5 }
Note that both threads shares variable count. What s4 consumer R2 = R1 - 1; { R2=4 }
Incorrect!
happens if both routines are executed concurrently? s5 producer count = R1; { count=6 }
s6 consumer count = R2; { count=4 }
Spring2005 91 Spring2005 92
Spring2005 95 Spring2005 96
16
Dekker’s Algorithm : Version 1 (1) Dekker’s Algorithm : Version 1 (2)
Set a shared variable turn (initialized to 1). If
turn=i then thread i is allowed to enter the Busy wait – waste CPU utilization.
critical section More drawbacks:
What happens if thread 2 needs to enter the critical section
Thread 1 Thread 2 first?
While(true){ While(true){ After thread 1 exits critical section, it sets turn=2, which
assumes that thread 2 is ready to enter the critical section. But
while(turn!=1) {wait;} while(turn!=2) {wait;} thread 2 may still be busy doing something else.
critical section; critical section; What happens if one thread needs to enter the critical section
turn = 2; turn = 1; more frequently than the other? (lockstep synchronization)
non-critical section; non-critical section;
} }
Spring2005 97 Spring2005 98
Drawbacks
Doesn’t guarantee mutual exclusion Replace the variables t1Inside and t2Inside with
What happens if both t1Inside and t2Inside are false and the two variables:
both threads attempt to enter their critical section at the boolean t1WantToEnter = false;
same time? boolean t2WantToEnter = false;
Busy wait t1WantToEnter = true indicates that thread 1 desires to
enter the critical section. t2WantToEnter = true
indicates that thread 2 desires to enter the critical
section.
17
Dekker’s Algorithm : Version 3 (2) Dekker’s Algorithm : Version 3 (2)
It is possible for the following situation to happen:
Thread 1 Thread 2 thread 1: set t1WantToEnter = true;
While(true){ While(true){ thread 2: set t2WantToEnter = true;
t1WantToEnter=true; t2WantToEnter=true; Thread 1 and 2 will loop forever in the while statement.
while(t2WantToEnter) {wait;} while(t1WantToEnter) {wait;}
critical section; critical section;
Satisfies the mutual-exclusive requirement, but
t1WantToEnter=false; t2WantToEnter=false; introduces deadlock.
non-critical section; non-critical section;
} }
18
Dekker’s Algorithm : Version 5 (2) Peterson’s Algorithm (1)
– A proper solution
Satisfies mutual exclusion requirement and A simper algorithm for enforcing two-process
prevents deadlock and indefinite mutual exclusion with busy wait.
postponement. Has the same global variables as Dekker’s
But, what happens if the system pre-empts algorithm.
thread 1 when thread 1 has just exited the boolean t1WantToEnter = false;
boolean t2WantToEnter = false;
inner wait loop and before it sets
int favouredThread = 1;
t1WantToEnter to true?
19
Mutual Exclusion with Semaphores (1) Mutual Exclusion with Semaphores (2)
A semaphore is an integer variable that, apart from
initialization, is accessed only through two standard Usage: Semaphore S;
operations: P and V P(S);
Definitions of P(S) and V (S): Critical section;
P(S){
V(S)
if (S>0) S=S-1 Mutual exclusion on a semaphore is
else put the calling thread into the semaphore S’s waiting queue enforced within P and V
}
P and V are atomic operations which means
V(S){
they must be executed indivisibly
if any threads are waiting in the semaphore S’s waiting queue
Resume the “next” waiting thread from the queue
else S=S+1
}
Spring2005 115 Spring2005 116
Semaphores can be used to synchronize two Use semaphore to notify the occurrence of an
or more concurrent threads. event:
Producer/Consumer problems Thread T1 wants to be notified about the occurrence of an
event.
producer consumer
Thread T2 is capable of detecting the occurrence of the
event and then notify its occurrence to the threads who
Wait for the old value to be consumed Wait for value to be created are interested in the event.
Use a semaphore S, initialized to 0.
Produce a new value Consume the value
T1: P(S), this causes T1 to block
Notify about the new value Notify about the consumption of the value T2: V(S), this signals the occurrence of the event and
allows T1 to proceed.
20
Bounded-Buffer Problem The Readers and Writers Problem
mutex semaphore provides mutual exclusion for A file is to be shared among several concurrent
accesses to the buffer pool. It is initialized to 0. threads. Some of those threads (readers) may
empty and full semaphores count the number of want only to read the file, whereas others
empty and full buffers respectively. Initially, (writers) may want to update the file.
empty=N, full=0. (N is the buffer size.)
Two variations
Producer thread Consumer thread
While (true) { While (true) {
No reader will be kept waiting unless a writer has
.. Produce an item … P(full); P(mutex); already obtained permission to use the file.
P(empty); P(mutex); .. remove an item from buffer; A writer can always perform its write as soon as
.. Add the item to buffer … V(mutex); V(empty); possible.
V(mutex); V(full); …Consume the item ..
} }
Spring2005 121 Spring2005 122
The Readers and Writers Problem – The Readers and Writers Problem –
Data Structure Reader and Writer Processes
P(mutex);
Semaphore mutex=1, wrt=1; if(readcount<=0){
P(wrt);
int readcount = 0; P(wrt);
}
…
readcount ++;
mutex semaphore is used to ensure mutual exclusion Reader Thread writing is performed;
V(mutex);
when the variable readcount is updated; wrt semaphore ….
…
functions as a mutually exclusive semaphore for the V(wrt);
reading is performed;
writers. It is also used by the first and last reader that ….
enters or exits the critical section. The readcount variable P(mutex);
keeps track of how many readers currently are reading the readcount--; Writer Thread
if(readcount<=0) V(wrt);
file.
V(mutex);
Spring2005 123 Spring2005 124
Summary
21
Deadlock and Starvation The Dining Philosophers Problem (1)
Deadlock is the situation where two or more Consider five philosophers sitting around a
processes are waiting indefinitely for an circular table. There is a bowl of rice in the
event that can be caused by only one of the center of the table and 5 single chopsticks
waiting processes. lay on the table. When a philosopher gets
Starvation is a situation where processes hungry, he tries to pick up the pair of
wait indefinitely within the semaphore. chopsticks closest to him, one chopstick a
time. When he gets two chopsticks in his
hands, he eats. He puts back the chopsticks
after he has finished eating.
Spring2005 127 Spring2005 128
22
Semaphore – problems (2) Monitors
What happens if a process omits the P
operation? – not mutually exclusive any more. A high-level synchronization construct in which
Process P1 Process P2 Process P3
mutual exclusion is rigidly enforced.
P(s) CS; P(s)
CS; V(s); CS;
The representation of a monitor type consists
V(s); V(s); of
Declarations of variables
What happens if a process omits the V
Declarations of functions that implement operations
operation? – leads to deadlock.
Process P1 Process P2 Process P3
on the variables
P(s) P(S); P(s)
CS; CS; CS;
V(s); V(s);
Monitors
Monitors – Conditional Variables
Mutual exclusion is rigidly enforced at the monitor
boundary: Each condition variable has a queue.
A thread must call a monitor entry routine to enter the monitor.
If no other threads are executing inside the monitor, the If the condition is not right, the process calls
monitor entry routine acquires a lock on the monitor for the wait() to exit th monitor and put itself into the
calling thread and lets it enter the monitor.
If another thread is executing inside the monitor, the monitor is
queue associated with the condition.
locked and the calling thread has to wait outside the monitor. Processes calling signal() cause a waiting
A thread inside a monitor uses a conditional variable process to be removed from the queue and
to wait on a condition outside the monitor.
be ready to enter the monitor again.
Initialization
23
Bounded-Buffer Problem (2)
Bounded-Buffer Problem (3)
Operation deposit (byte item) is used to put Operation fetch () is used to fetch an item
an item into the buffer. from the buffer.
void deposit(byte item){ byte fetch() {
if(count == size) wait (notFull); if(count==0) wait(noEmpty);
buffer[in] = item; byte item = buffer[out];
in = (in+1) mod size; out = (out+1)% size;
count++; count--;
if(count == 1) notify(notEmpty); if(count == size-1) notify(notFull);
}
return item;
}
24
Dining Philosophers Problem (4) Dining Philosophers Problem (5)
When a philosopher finishes eating, Assuming instance of the monitor is called
putdown(int i) is called to change his state
DPMonitor.
and notify his two neighbors.
public void putdown(i){ The processes for philosopher i:
state[i]=THINKING; DPMonitor.pickup(i);
test(left(i)); …Eating is performed …
test(right(i)); DPMonitor.putdown(i);
}
Summary
Deadlock Example
What is Deadlock? Resource R1 is allocated
Process P2 is requesting
To process P1
resource R1
R1
When several processes compete for a finite
number of resources, a deadlock situation P1 P2
may arise where a process requests a
resource and the resource is held by one of R2
Process P1 is requesting Resource R2 is allocated
the waiting processes. resource R2 To process P2
A process is said to be deadlocked if it
requests a resource that will not become Each process holds a resource being requested by
available. the other process and neither process is willing to
release the resource it holds
25
Deadlock in Spooling System
Another example
A spooling system improves system
Assume there are resources R1 and R2 and throughout by dissociating a program from a
processes P1 and P2. P1 and P2 execute slow device. It can be prone to deadlock.
concurrently.
For example, a spooling system sends pages to a
P(1){ P(2){ faster device, such as a hard disk, before they are
acquire R1; acquire R2; printed on a printer.
acquire R2; acquire R1; If the spooling system requires that the complete
operate on R1 and R2; operate on R1 and R2; output of a program be available before printing
release R1; release R2; can begin, several partially completed jobs can
release R2; release R1; become deadlocked if the pages they generated
} } have filled the available space for spooling.
26
Detecting Deadlock in a Resource
An example allocation Graph (1)
R1 R3
In a resource-allocation graph in which each
resource type only has one instance, a cycle in
the graph is both a necessary and a sufficient
P1 P2 P3 condition for the existence of deadlock.
If the graph contains no cycles, then no process in the
system is deadlocked.
If the graph contains a cycle, then deadlock exists.
R2 R4
P1 P2 P1 P2
Reducing by P2 P1 P2 P3
R2 R2
R4
No Deadlock R2
P3 P3
27
Another Example again And again
R1 R3 R1 R3
P1 P2 P3 P1 P2 P3
R4 R4
R2 R2
P1
Deadlock Avoidance
P2 P3
Avoid deadlock by deciding whether a request
for a resource can be satisfied or must wait to
avoid a possible future deadlock. This method
R4
requires additional information about how
R2
resources are to be requested.
P4
28
Safe state
Deadlock Prevention (2)
No Pre-emption The system is said to be in a safe state if the
If a process that is holding some resources operating system can guarantee that all
requests another resource that cannot current processes can complete their work
immediately be allocated to it, then all resources
within a finite time. If not, then the system is
currently being held are pre-empted.
said to be in an unsafe state.
Circular Wait
Impose a total ordering of all resource types, and
require that each process requests resources in
an increasing order of enumeration.
R1 R1
P1 P2 P3 P2 P2
P1 P1
R2 R2
Safe Sate Unsafe Sate
A safe sequence:
R2 R4 <P1, P2, P3>
Spring2005 171 Spring2005 172
29
Dijkstra’s Banker’s Algorithm (1) Example of a Safe State
The system shares a fixed number of resources, t, Suppose a system contains 12 equivalent
and a fixed number of processes, n resources and three processes sharing the
Each process specifiesin advance the maximum resources. The resources are allocated as
number of resources that it requires to complete following
its work Process Maximum Current Current
need loan claim
The system accepts a process’s request if that
P1 4 1 3
process’s maximum need does not exceed the
P2 6 4 2
total number of resources available in the system,
t. P3 8 5 3
Processor Scheduling
Chapter 8 Processor scheduling is the task of selecting
Processor Scheduling a waiting process from the ready queue and
allocating the processor to it.
30
Scheduling Levels
CPU-I/O Burst Cycle
Long-term scheduler: selects processes from
the process pool (usually on disk) and loads Process execution consists of a cycle of
them into memory processor execution and I/O wait.
Short-term scheduler: selects processes When one process does I/O, a scheduler will
from the ready queue typically switch the processor to another
process.
Disk Ready Queue CPU
Long-term Short-term
scheduling scheduling
I/O, Interrupt,
Timeout...
31
Scheduling Criteria First-Come, First-Served (FCFS)
Use FIFO queue.
Processor Utilization – as high as possible.
Non-preemptive – A process doesn’t give up
Throughput – number of processes completed per processor until it either terminates or
time unit. performs I/O.
Turnaround time – interval from submission to Question: What if we have 3 processes: P1
completion of process. (takes 24s), P2 (3), P3 (6). If arrive in order
Waiting time – time spent ready to run but not P1->P2->P3, What is waiting time?
Turnaround time? Throughout? What if arrive
running. in order P2->P3->P1?
Answer Shortest-Job-First(SJF)
Gantt Chart
p1 p2 p3 Assign processor to the process that has the
24 27 33
Average waiting Time = (0+24+27)/3 = 17 smallest next processor burst
Turnaround Time = (24+27+33)/3 = 28 May be either preemptive or non-preemptive
Gantt Chart Optimal with respect to average waiting time
p2 p3 p1
How does scheduler know the length of the next
3 9 33 processor burst?
Average waiting Time = (0+3+9)/3 = 4
Turnaround Time = (3+9+33)/3 = 15
32
Round-Robin(RR) Scheduling Multilevel Queue Scheduling
Very small quantum – Context switch overhead.
Very big quantum – turns into FCFS. Like Round-Robin except have multiple
queues.
Question: time quantum=4;
Process burst time Partitions the ready queue into several
P1 8 queues. Processes are permanently
P2 3
assigned to one queue. Each queue has its
P3 10
own scheduling algorithm. Fixed-priority
What happens if time quantum = 1 and 20? preemptive scheduling between queues.
33
Algorithm Evaluation – Example Summary
FCFS P1 P2 P3 P4 P5
0 10 39 42 49 61 What is CPU scheduling?
Average waiting time = (0+10+39+42+49)/5=28 Scheduling criteria: CPU utilization, waiting
P3 P4 P1 P5 P2
time, turnaround time…
SJF
0 3 10 20 32 61 Scheduling algorithms: FCFS, SJF, RR,
Average waiting time = (10+32+0+3+20)/5=13 Multilevel queue ...
RR P1 P2 P3 P4 P5 P2 P5 P2
0 10 20 23 30 40 50 52 61
Average waiting time = (0+32+20+23+40)/5=23
Background
Chapter 9 Memory is a large array of bytes, each with
Memory Management its own address.
The program must be loaded into memory to
be executed.
CPU fetches instructions from memory
according to the program counter. The
instruction may cause loading from and
storing to memory.
34
Contiguous vs. Non-contiguous Memory
Allocation Memory Allocation
Contiguous memory allocation. The memory is usually
0
To run a program, the system has to find enough divided into two partitions, Operating System
contiguous memory to accommodate the entire one for the resident
program. operating system, and one
Non-contiguous memory allocation. for the user processes. It is User processes
A program is divided into blocks or segments that common for the operating
the system may place in nonadjacent slots in main system to occupy low
memory. memory.
Fragmentation
Contiguous Fixed-Partition Allocation:
Fragmentation – the system cannot use
Divide the memory into certain areas of available memory.
Operating
fixed-length partitions. System
External fragmentation – the available memory
The degree of Partition A space is broken into chunks, and the largest
multiprogramming is contiguous chunk is insufficient for a request.
constrained. Partition B
Internal fragment – memory that is internal to a
The size of each process is Partition C partition, but cannot be used.
bounded.
Suffers internal Partition D
fragmentation
35
Contiguous Variable-Partition Allocation Contiguous Variable-Partition Allocation
(1) (2)
P5 needs 10MB
Job queue
P4 needs 100MB
P3 needs 60MB
Initially, all the memory is considered as one P2 needs 80MB
large block of available memory, a hole. P1 needs 40MB
Main memory
When a process needs memory, a hole large Operating Operating Operating
enough for the process is allocated for it. System System System
P1 40MB P1 40MB P1 40MB
A free memory list is used to track available
memory. P2 80MB P2 80MB
P3 60MB
36
An Example (1) An Example (2)
0 0 0 0 0
Question: OS OS OS OS OS
400k 400k 400k 400k 400k
Process Memory Burst Time
Assume that we have 2560k of P1 P1 P1
P1 600k 10
memory available and a resident P2 1000k 5 1000k 1000k 1000k 1000k
OS of 400k. Given the input queue P3 300k 40 P4 P4
in the table, using FCFS, how P4 500k 35 P2 1500k 1500k
would the First-Fit algorithm place
the processes in the input queue
2000k 2000k 2000k 2000k
(The newly freed memory hole is P3 P3 P3 P3
appended to the end of the free
memory list)? 2560k 2560k 2560k 2560k
2560k
Time:0 Time:0 Time:5 Time:5 Time:10
Spring2005 217 Spring2005 218
37
Logical Address Translation
Paging
Block number Displacement
b d Break physical memory into fixed-sized blocks
called frames.
Break logical memory into fixed-sized blocks
called pages.
b e + Real address=d+e
Logical address is an ordered pair (p, d), where
p is the page number in logical memory and d is
the page offset.
Block map table
A page table is indexed by the page number,
and each item contains the physical frame
number that contains the page.
Spring2005 223 Spring2005 224
38
Paging Address Translation by Associative Paging Address Translation by
Mapping (2) Direct/Associative Mapping (1)
Associative memory has a cycle time much An associate memory, called translation
greater than main memory. lookaside buffer (TLB) is used to hold a small
Pure associative is fast but not often used. part of page table (most active pages only).
High speed cache and associate memory are far When a process references a logical
too expensive to hold the complete page mapping address, the system first tries to find the page
table. entry in TLB. If the page entry cannot be
found, the system then locates the page entry
in the conventional page table.
Process 3’s d
page table
b+p e e d
d
Physical address
b
Page table
Segmentation Segmentation
Paging separates user’s view of memory A program’s data and instructions are divided
from the actual physical memory. into blocks called segments. Each segment
The user views memory as a collection of consists of contiguous locations. The
variable-sized segments, with no necessary segments can be of different size.
ordering among segments. Logical address is an ordered pair (s, d), where
Segmentation supports this user view of s is the segment number in logical memory and
d is the segment offset.
memory.
39
Segment Map Table Logical address translation
Segment table
segment table b
The segment map table entry origin Register
(stores base
l s
address of
segment table) b+s len e Main memory
segment Protection Base address
bits of segment in b
length
Main memory
+
Segment-overflow
exception
Spring2005 235 Spring2005 236
Main 400
Segment 1
Program 700
Process 1’s
Segment 0 1000 segment table
length base Segment 3 Segment shared by
1500
0 800 2000 Processes 1 and 2
sub1
sub2 1 300 400 2000
Segment 1 1200 2800 Segment 0
2
Segment 2 3 500 1000 2800
sub3 Process 3’s
Segment table Segment 2 page table
Segment 3 4000
Logical address space
Physical memory
40
Segmentation with Paging (2) Summary
Logical address
s p d
Over-flow Exception
Contiguous vs. non-contiguous memory
allocation
> No
Contiguous memory allocation:
+
Segment Page table Single partition.
length Base b
Page table Multiple partition: Fixed and variable partition.
b
Segment table Blocking memory systems
a
b+p Paging
segment table + f f d
origin Register Segmentation
Physical address
Segmentation with paging
Spring2005 241 Spring2005 242
Overlays
Swapping
Overlays allow the system to execute a
program that is larger than main memory. A process can be swapped temporarily
out of memory to a backing store, and
The program is divided into logical sections. When
the memory for a section is not needed, the then brought back into memory to
system can replace it with the memory for a continue execution.
needed section. The backing store is commonly a fast
disk.
The context switch time is very high.
41
Virtual Memory Diagram of Virtual Memory
Page
frame Valid-invalid bit
Virtual memory is a technique that allows the A B
0 0 i 0
execution of processes that may not be 1
Swap in
1 B 0 v 1
completely in memory. C 2 i D
2 2
3 2 v
3 D 3
4 i A C E
4 E v 4 F
5 4 G
F 6 i
5 Physical
G Page table Memory Disk
6
Virtual
Memory
A process is viewed as a set of sequential Add valid-invalid bit into page table.
pages. It resides on secondary memory “valid” indicates that the associated page is both
(disk). legal and in memory.
A page is never brought into memory unless “invalid” indicates that the associate page is not in
it will be needed. memory.
Access to a page marked invalid causes a
page-fault trap to the OS.
42
Performance of Demand Paging (2) Page Replacement (1)
If we take an average page-fault service time of 25
milliseconds and a memory access time of 100 The page-fault rate is not a problem if each
nanoseconds, then
page is faulted at most once.
effective access time =(1-p) × 100 + p × 25,000,000
=100 + 24,999,900 × p (nanoseconds) What happens if we are over-allocating
If one access out of 1000 causes a page fault: memory?
effective access time=25,099.900 (nanoseconds) Page replacement: if no frame is free, find one
Conclusion: The effective access time is directly that is not currently being used and free it. The
proportional to the page-fault rate. It is important to keep page is freed by writing its contents to swap
the page-fault rate low. space, and changing the page table.
43
Page-Replacement Algorithms: Page-Replacement Algorithms:
First-In First-Out (FIFO) Algorithm First-In First-Out (FIFO) Algorithm
Reference strings
Create a FIFO queue to hold all pages in memory. 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Always replace the page at the head of the queue, 7 7 7 2 2 2 2 4 4 4 0 0 0 0 0 0 0 7 7 7
Page frames 1 1 1 1 0 0 0 3 3 3 3 3 2 2 2 2 2 1
Assume there are 3 frames in memory. If run on a
reference string
number of page faults = 15
7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
using FIFO algorithm, how many page faults? Bad replacement choices increase the page-fault rate:
The page replaced may be an active page in constant use.
After paging out this page to bring a new one, a page fault
may occur immediately.
44
Summary
45
Disk Scheduling: Disk Scheduling:
First-Come First-Serve Scheduling Shortest-Seek-Time-First Scheduling
For example, a disk queue with requests for I/O to sectors on cylinders For example, a disk queue with requests for I/O to sectors on cylinders
98,183,37,122,14,124,65,67 98,183,37,122,14,124,65,67
The head is initially at cylinder 53. The head is initially at cylinder 53.
The total movement is 640 cylinders The total movement is 208 cylinders
The total movement is 236 cylinders The total movement is 331 cylinders
Spring2005 273 Spring2005 274
The total movement is 208 cylinders The total movement is 299 cylinders
Spring2005 275 Spring2005 276
46
Caching and buffering Disk Formatting (1)
A disk cache buffer is a region of main memory Low-level formatting: before a disk can store data, it
reserved for disk data. It allows the system to must be divided into sectors that the disk controller
quickly access data that would otherwise need to be can read and write.
fetched from disk. The data structure of a sector
Write-back caching: the system writes the modified data to
disk periodically, enabling the system to batch multiple I/O header
requests.
Data area
Write-through caching: the system writes the modified data
to disk immediately. trailer
Challenges: The header and trailer store information used by the disk
The size of the buffer. controller, such as a section number and an error-
Replacement strategies correcting code (ECC).
To use a disk to hold files, the system needs When a computer is powered up, it needs to
to record its own data structures on the disk. have an initial program to run, which is the
It does this in two steps. bootstrap program.
Partition the disk into one or more groups of The bootstrap program is usually stored in
cylinders. The disk treats each partition as a ROM (Read-Only Memory)
separate disk.
It is read only
Do a logical formatting on each partition. This
It is difficult to update.
allows the system to store the initial file-system
Solution: Store a small bootstrap loader program in the
data structures onto the disk.
boot ROM, whose job is to bring in a full bootstrap
program from disk.
47
Redundant Arrays of Independent Disks Level 0 (Striping)
(RAID) (2)
It uses a striped disk array with no redundancy.
file With n disks, it is n times faster than a single disk.
A B C D
E F G H
I J K L
Data retrieved
Data written
From the array Controller to the array
A, B K
strips
RAID interface
Read Write
Spring2005 283 Request Request
Spring2005 284
Read Write
Request Request
WriteSpring2005
request 287 Spring2005 288
48
Level 5 (Block-level Distributed XOR Summary
ECC parity)
Mirrored pair Mirrored pair
D1 D2 D3 D4 Evolution of secondary storage
A B P1 D G P3 I P4
Disk structure
C A1 E P2 F H J K Disk scheduling
Caching and buffering
Data written Disk management
Controller to the array
Disk formatting
B I
Boot block
Write Write RAID organizations: levels 0-5
Request Request
Directory
Partition B
Files
File systems
File Concepts
A file system organizes files and manages
File attributes: name, type, location, size, access to data. It is responsible for:
protection, time, date, and user identification. File management – provides mechanisms for files
The information about all files is kept in the to be stored, referenced, shared and secured.
directory structure. Auxiliary storage management – allocates space
for files on secondary and tertiary storage
File operations: creating, writing, reading, devices.
reposition, deleting, truncating, appending, File integrity mechanisms – ensures that the
and renaming. information stored in a file is not corrupted.
Access methods – how the stored data can be
accessed.
49
Directory Structure Directories – Links (1)
Tree-structured Directories
A link is a directory entry that references a
The tree has a root directory. data file that is typically located in a different
Each file in the system has a unique path directory. It is often used to simplify file
name. system navigation and to share files.
A directory (or subdirectory) contains a set of A soft link( “shortcut”, “alias”) is a directory entry
containing the pathname for another file.
files or subdirectories. A directory is simply
A hard link is a directory entry that specifies the
another file, but it is treated in a special way.
location of the file on the storage device.
Relative and absolute paths
50
Mounting (1) Mounting (2)
File system A (Local) File system B
/ /
Users often require access to information that usr home mnt usr home mnt
is not part of the local file system.
xfs newfs bin
Mounting combines multiple file systems into
foo bar dir
one namespace, so that the files in the Before
multiple file systems can be identified by a
After /
Mount point
single file system.
usr home mnt
It assigns a directory, called the mount point, in
the local file system to the root of the mounted file File system B mounted at xfs newfs
directory /mnt/xfs in file
system. system A usr home mnt foo bar dir
bin
Spring2005 301 Spring2005 302
0 n t
Read n write t
51
File Allocation Methods: File Allocation Methods:
Linked Allocation (1) Linked Allocation (2)
Each file is a linked list of disk blocks which may
Benefits: solves all the problems of
be scattered anywhere in the disk.
contiguous allocation.
Disadvantages:
Directory Potentially terrible performance for direct access.
0 1 2 -1 3 4
File start end
5 6 7 8 9
Each file needs more space because each block
Cat 12 2 contains a pointer to the next block.
Dog 3 5 10 11 12 13 14
15 16 17 18 19
Reliability problem – what if one pointer is lost?
Disk
16 Disk
10
FAT
Spring2005 309 Spring2005 310
Disk
Spring2005 311 Spring2005 312
52
Free Space Management File Access Control (1)
Data Integrity and Log-Structured File Data Integrity and Log-Structured File
System (LSF) (1) System (LSF) (2)
If a system failure occurs during a write operation, With transaction logging, the file system may
the file system data may be left in an inconsistent
state.
still get in a inconsistent state.
Transaction-based logging reduces the risk of data An LSF performs file system operations as
loss by using atomic transaction. logged transactions.
A group of operations are performed in their entirety, or not
at all. If an error occurs before completion of the
The entire disk serves as a log file to record
transaction, the system returns back to the state before the transactions.
transaction began. Modified directories and metadata are always
It is usually implemented by recording the result of each
operation in a log file. Once the transaction is completed,
written to the end of the log.
the log is transferred to permanent storage.
53
Database Systems Advantages of Database Systems
Database systems are data-independent systems A relational database is composed of relations (tables). A row
enabling multiple applications to access the same of the relation (table) is called a tuple. One of the attributes
(column) in each tuple is used as the primary key for
data with different logical views. referencing data in the relation. The tuples of the relation are
Database languages facilitate data independence by uniquely identifiable by the primary key.
providing a standard way to access information. A
database language consists of Number Name Departme Salary Location
A data definition language (DDL) nt
1234 Jones, A 712 1100 London
A data manipulation language (DML)
tuple 2347 Kerwin, T 003 1456 Cardiff
A query language.
1009 Myers, B 323 3200 London
789 Stevens, L 198 2001 Cardiff
Primary attribute
key
Spring2005 321 Spring2005 322
54
Summary
File concepts. Chapter 13
File systems: directories, metadata and mounting.
I/O Systems
File access methods – sequential and direct access.
File organization
File allocation methods: contiguous, linked, linked
with a FAT, and indexed.
Free space management
File access control
Data integrity protection
Database systems
55
Interrupts (2) Direct Memory Access (DMA)
The interrupt handler determines the cause of the PIO – A process is used to transfer data
interrupt, performs the necessary processing. between the CPU and a device by watching
The interrupt handler returns the CPU to the status bits and feeding data into a data
execution state prior to the interrupt. register 1 byte at a time.
Enables the CPU to respond to an It is expensive to use a general-purpose
asynchronous event. processor for PIO.
More sophisticated interrupt features needed Use a special-purpose processor called
for a modern operating system. direct-memory-access controller for PIO.
Scheduling can improve the overall system A buffer is a memory area that stores data
performance, can share a device fairly while they are transferred between two
among processes, and can reduce the devices or between a device and an
average waiting time. application.
A queue of requests is maintained for each Why buffer?
device. The I/O system can rearrange the To cope with speed mismatch between the
producer and consumer of a data stream.
order of the queue to improve the overall
To adapt between devices that have different data-
system efficiency.
transfer size.
To support copy semantics for application I/O.
Spring2005 335 Spring2005 336
56
Caching Spooling
A cache is region of fast memory that holds A spool is a buffer that holds output for a
copies of data. device.
Access to the cached copy is more efficient An example: several applications wish to print
than access to the original. their output concurrently.
The difference between a buffer and a Each output is spooled to a separate disk file, and
cache: added into a queue.
A cache only holds a copy of a data item on The spooling system copies the queued spooling file
faster storage, whereas a buffer may hold the to the printer one at a time.
only existing copy of the data item.
Spring2005 337 Spring2005 338
Introduce
57
Common reasons for performance Performance Measures (1)
evaluation
Selection evaluation – determines whether a Turnaround time – interval from submission to
system from a vendor is appropriate completion of a job.
Response time – interval from a user’s pressing
Performance projection – estimates a new
Enter key or clicking a mouse until the system
system that doesn’t exist. displays its response.
Performance monitoring – accumulates Reaction time – interval from a user’s pressing an
performance data on an existing system to enter key or clicking a mouse until the first time
ensure that it is meeting its performance slice of service is given to that user’s request.
goals.
Benchmarks Simulation
A benchmark is a program that is executed to A computerized model of the system is used to
evaluate a machine. evaluate the system.
Such as SPECmarks, SYSmark benchmark, WebMark, Performance Monitoring
MobileMark ..
Collect and analyse the information regarding the
Synthetic programmes performance of an existing system.
They are similar to benchmarks, except that they
focus on a specific component of the system.
Such as WinBen 99, IOStone, STREAM
58
Performance Techniques in Processor Complex Instruction Set Computing
Design (CISC)
Instruction set architecture (ISA) is an Incorporate frequently used sections of code
interface that describes the processor, into single machine-language instructions
including its instruction set, number of (CISC instructions) to get better performance.
registers and memory size. The complex instructions are implemented
It is hardware equivalent to an OS API. via microprogramming.
The ISA design decisions directly affect a It moves most of the complexity from the
processor’s performance. software to the hardware.
It also reduces the size of programs.
Examples: Pentium, Athlon
Summary
59
UNIX Layer Structure Programmer Interface
users
Shells and commands System calls define the programmer interface
compilers and interpreters to UNIX.
system libraries Three main types of system call:
System-call interface to the kernel
File manipulation
CPU scheduling file system memory management Process control
virtual memory I/O systems …..
Information manipulation
Kernel interface to the hardware
60
User Interface Images and Processes (1)
A user routine can be viewed as an image
Shell is a customized command line interpreter. which contains:
When UNIX is booted, a process named init is program
active. init creates a logon process and opens stdin, process ID
stdout and stderr files to accept user input from the
terminal and display output and error messages. scheduling information such as priority
In responding to a command, the shell forks a child data storage
process to carry out the command. the contents of registers
open files, current directory ...
The execution of an image is a process. The UNIX dispatcher uses this table to
schedule processes.
Second Storage
Process table
Process A
61
Memory Management (2) File System (1)
Because several processes can physically All data are treated as a string of bytes and no
share a text segment, the text segment physical structure is imposed by the system.
cannot be released until all the processes This gives compatibility of file and I/O devices.
using it have died. Ordinary files.
Special files.
A text table is used to keep track of active Block or structured devices (normally a disk) hold files.
text segments. Character devices include printers, terminals and other
nonblock peripherals.
Each device has a device driver. All devices are
listed in a configuration table and identified by a
device number.
Spring2005 367 Spring2005 368
System Architecture
Kernel Model
Native API Interface
Executive services
62
User Model
Kernel Mode
First layer Environment subsystems are user-mode
The Hardware Abstraction Layer hides the underlying processes interposed between executive
hardware and provide a virtual machine interface to other
processes. services and the rest of user space. It exports
Second layer an API for a specific environment.
Device drivers transfer logical I/O calls to specific physical I/O Win32 subsystem
hardware primitives.
Microkernel coordinates dispatching and multi processor
POSIX subsystem
synchronization and handles asynchronous procedure calls. 16-bit MS DOS
Top layer Win64 subsystem
Executive services such as I/O manager, security manager,
power manager, process manager, object manager ...
How components and user processes store The registry is a database storing
and access configuration data in the system configuration information specific to users,
How objects are implemented, managed and applications and hardware.
manipulated The registry is organized as a tree.
How interrupts are used Each node in the tree represents a registry key.
A key stores subkeys and values
A value contains a value name and a value data.
Windows XP represents physical resources and IRQLs are a measure of interrupt priority.
logical resources with objects. An interrupt that executes at an IRQL higher than the
current one can interrupt the current execution and obtain
The objects are constructs that Windows XP uses to the processor.
represent resources( not the objects in OO programming). Windows XP defines several IRQLs
Object types include files, devices, processes, threads, Passive IRQL: threads
pipes, semaphores and many others. APC IRQL
DPC/dispatch IRQL
The objects can be accessed through object handles. Device IRQLs
Object manager is responsible for creating, deleting Profile IRQL
Clock IRQL
objects and maintaining information about each Request IRQL
object type. Power IRQL
High IRQL
63
Interrupt Request Levels (IRQLs) (2) Asynchronous Procedure Calls (APCs)
DPCs are software interrupts which run in the There are two kinds of system threads:
context of the currently executing thread. Kernel thread – a thread which is created by a
kernel component, executes in kernel mode and
Hardware interrupts are processed by calling the
typically belongs to a system process.
associated interrupt service routine, which is
System worker thread – a thread which is created
responsible for quickly acknowledging the
at system initialization time or dynamically in
interrupt and queuing a DPC for later execution. response to a high volume of requests and sleeps
until a work item is received. It also belongs to a
system process.
Windows XP includes three types of worker threads:
delayed, critical and hypercritical.
PEB
64
Creating and Terminating Processes Jobs
The parent and child processes are Several processes can be grouped together
completely independent. (The child process into a unit, called a job.
receives a new address space when it is It allows developer to define rules and set limits
created) on a number of processes.
A primary thread is created when a process It can be easily terminated
is initiated by the system.
A process terminates when Process A
All of its threads terminate Process B
Any of its thread explicitly terminate the process Process C
Its user logs off Process D
Job
Spring2005 385 Spring2005 386
Fibers are used to port code to and from Each process has a thread pool that consists
other operating systems. of a number of worker threads which execute
A fiber is created by a thread. It is similar to a functions queued by user threads.
thread, except that it is scheduled for The worker thread sleeps until a request is
execution by the thread that created it, rather queued to the pool.
than the microkernel. The thread that queues the request must specify
Windows XP threads are implemented with a the function to execute and must provide context
one-to-one mapping. Fibers allows Windows information.
XP applications to write code executed using
the many-to-many mapping.
Spring2005 387 Spring2005 388
Running
termination
Terminated
65
Virtual Address Translation
Memory Management
Page virtual address
directory d t o
registry
Use 32-bit memory model. The biggest a +
possible address is 4 GB. a
threads. . c c o
. b+t
A Virtual Memory Manager (VMM) manages . physical address
.
memory. It handles paging. Page directory .
table .
Page table
Spring2005 391 Spring2005 392
I/O Management
File Management User mode API System Calls
Plug and
Play
Environment subsystems Manager
Supports
FAT, used in MS-DOS file system. I/O Manager
FAT32, allows long file names and larger disk drive. Power
Device driver
NTFS, with more features such as file, folder-level manager
Device driver
security, encryption, compression and so on.
Device driver
Device driver
Kernel mode
HAL
66
More about Windows XP Summary
67