Você está na página 1de 67

Text Book

CM0144 Spring 2004/5

Operating Systems Operating Systems


Deitel Deitel Choffnes, 2004 (Third Edition), Pearson Prentice
Hall, ISBN 0-13-124696-8

Dr. Coral Yan Huang


Email: Yan.Huang@cs.cf.ac.uk
Office: T/2.09

Spring2005 1 Spring2005 2

Contents (1) Contents (2)

1. Introduction to operating systems 9. Memory management


2. Hardware and software concepts 10. Virtual memory management
3. Process concepts 11. Disk performance optimization
4. Thread concepts 12. File and Database systems
5. Asynchronous concurrent execution 13. Performance and processor design
6. Concurrent programming 14. Case study: UNIX
7. Deadlock 15. Case study: Windows XP
8. Processor scheduling
Spring2005 3 Spring2005 4

What is an Operating System?

Chapter 1 User User User User

Compiler E-Mail Editor IRC Chat


System and application programs

Operating System

Introduction to Operating Systems Hardware

An operating system is software that enables applications


to interact with a computer’s hardware.

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

History of Operating Systems (3) History of Operating Systems (4)


„ 1980s
‰ 1970s ‰ The decade of the PC and the workstation—
„ Multimodel multiprogramming systems that support „ Individuals and small businesses can have their own
batch processing, timesharing and real-time powerful computer.
applications.
‰ Graphical User Interface (GUI) with mouse
‰ Multimodel systems support both batch-processing and
real-time applications provides easy-to-use interface
‰ Real-time applications require the operating system to ‰ Distributed computing (under Client/Server model)
supply a response within a certain bounded time period. is widespread
„ Commercial products of Operating Systems.
„ TCP/IP communications standard became widely used.
„ Security was encompassed

Spring2005 9 Spring2005 10

History of Operating Systems (5) History of Operating Systems (6)


„ 1990s
„ 1990s ‰ Microsoft Corporation became dominant.
‰ Hardware performance continued to improve exponentially. Year Operating Systems
„ Speed — several hundred MIPS (normal PC) , over one TIPS DOS
1981
(supercomputer).
1985 Win 1.0
„ Storage — one GB a hard disk
1990 Windows 3.0
‰ With the creation of World Wide Web (WWW) and fast
internet connections, distributed computing is commonly 1993 Windows 3.1 Windows NT 3.1
used between PCs. 1994 Windows NT 3.5
„ OS support for networking tasks became standard. 1995 Windows 95 Windows NT 3.51
„ Security threats. 1996 Windows NT 4.0
To be continued
1997 Windows NT Enterprise Edition

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

1999 ‰ Object Technology


„ The appearance of object-oriented languages such as
2000 Windows ME Windows 2000 Family
C++ and Java
2001 Windows XP (Home, Professional) „ An Object includes both data and functions. In addition,
2002 (Windows XP Service Pack 1) programmers can create relationships between one
object and another such as inheritance.
2003 Windows Server 2003
„ Object-Oriented modular operating systems are easier
2004+ (Windows XP Service Pack 2) to maintain and extend.
Windows XP Media Center Edition 2005
Windows XP Professional x64Edition

To be Continued
Spring2005 13 Spring2005 14

History of Operating Systems (9) Operating System Components

„ 2000 and Beyond „ Shell, or command interpreter — allows user


‰ Middleware is software that links web applications to enter a command.
over a network. „ Kernel — the software that contains the core
„ CORBA, RMI, JXTA, Globus, Web service technologies
components of the system.
„ GRID
‰ Process scheduler
‰ Next Operating systems
„ Improved security,
‰ Memory manager
„ High-level standard support for parallelism ‰ I/O manager
„ Enhanced 3D user interface ‰ Interprocess communication (IPC) manager
„ More compatible with other operating systems. ‰ File system manager

Spring2005 15 Spring2005 16

Operating System Goals Operating System Architectures (1)

„ Efficiency „ Monolithic Architecture


„ Robustness ‰ The earliest and most common
„ Scalability ‰ Every component is contained in the kernel and
can directly communicate with other.
„ Extensibility Applications
„ Portability
System calls
„ Security
„ Interactivity File system Process Scheduler IPC Manager
Kernel
„ Usability Memory Manager Network …….

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

Memory management Process Synchronization


Spring2005 19 Spring2005 20

Summary

„ What is an Operating System? Chapter 2


History of Operating Systems.
„
Hardware and Software
„ Core operating system components
„ Operation system goals Concepts
„ Operating system architecture

Spring2005 21 Spring2005 22

Hardware Components Mainboards

„ The central PCB (printed circuit board)


Processor(s)
‰ Slots – for processors, memory and other devices
memory
‰ Bus – provides high speed communication.
‰ Chips, such as
I/O Devices „ BIOS (basic I/O system) chip
„ Controllers

Spring2005 23 Spring2005 24

4
Processor Processor Components (1)

„ A processor is a hardware component that


executes a stream of machine language. ALU

„ Processors can be Fetch/decode


Execution unit
unit
Registers
‰ CPU (Central Processing Unit)
‰ Graphics coprocessor L1 Instruction L1 data
cache cache
‰ DSP (Digital Signal Processor)
„ Instruction set defines the set of instructions L2 cache
a processor can execute.
Bus

Spring2005 25 Spring2005 26

Processor Components (2) Clocks

„ 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

Memory Hierarchy Memory


Latency (in processor cycles)
0 Register „ RAM – Random Access Memory
SRAM L1 Cache
2~3
SRAM L2 Cache
„ DRAM – Dynamic RAM. Requires that a
~10
DRAM refresh circuit periodically (a few times every
DDR millisecond) reads the contents or the data
Main memory
~30 Rambus
SDRAM will be lost.
„ SRAM – Static RAM. It doesn’t need to be
Hard Disk
Tape Secondary refreshed to maintain the data.
~106 CDs And tertiary
storage ‰ Much faster but very expensive.
DVDs

Spring2005 29 Spring2005 30

5
Secondary Storage Buses

„ Hard Disk: 1TB „ Data bus


„ Compact Disk (CD). 700MB per side „ Address bus
„ Digital versatile disk (DVD). Store data in „ I/O channel.
thinner tracks on up to two layers per side. „ Peripheral component interconnect (PCI) bus
Each layer can store up to 5.6 GB. „ Accelerated Graphics Port (AGP)

Spring2005 31 Spring2005 32

Peripheral Devices Software Overview

„ 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

Machine Language and Assembly High-level Languages


Language
„ A computer can only understand its own „ Fortran is the first high-level language (mid-
machine language 1950s).
‰ It consists a stream of numbers
„ Then COBOL (late 1950s)
‰ Incomprehensible to humans
„ C (1970s)
„ Assembly language – use English-like
abbreviations to represent the computer’s „ C++ (early 1980s) is object-oriented
basic operations. language.
‰ A translator (called an assembler) converts „ Java (1995)
assembly-language program to machine-language
program. „ C# and .NET

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

Java compiler Interpreter Execution


Java
Source
bytecode
Code JVM

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

„ Hardware components Chapter 3:


‰ Processor, memory, bus, secondary storage,
clocks, peripheral devices Process Concepts
„ Software overview
‰ Machine, assembly and high-level languages
‰ Interpreters and Compilers
‰ Application Programming Interfaces
‰ Compiling, linking and loading

Spring2005 43 Spring2005 44

What is a process? Process States


‰ Running State: the process is executing on a processor
„ A process is an entity with its own address ‰ Ready State: the process could execute on a processor
space. The address space typically consists when one is available
of ‰ Blocked State: the process is waiting for some event to
happen before it can proceed.
‰ text region: code
‰ data region: variables and dynamically allocated admitted Interrupt/timeout
exit
memory New Terminated

‰ stack region: instructions and local variables Ready Running

„ A process is a program in execution


Scheduler dispatch

I/O or event I/O or event wait


completion Blocked
Spring2005 45 Spring2005 46

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

a particular I/O device. Each device has its


own device queue

Spring2005 49 Spring2005 50

Process Operations (1)


Process queues (3)
‰ Create and destroy a process
Scheduling
Ready Queue CPU A

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

Process Operations (2) Context Switching


‰ Suspend and resume a process
„ A suspended process is indefinitely removed without „ Switching the CPU to another process
being destroyed requires saving the execution context of the
„ Normally for detecting security threats and debugging
purposes
old process into its PCB and loading the
Interrupt/timeout
execution context of the new process.
admitted dispatch exit „ Switch is pure overhead Context Switch
New Ready Running Terminated
resume suspend Save EC load EC
suspend Executing idle Executing
I/O or into PCB0 from PCB0
Suspended- event wait
ready I/O or event Suspended- suspend
load EC save EC
completion blocked idle Executing idle
from PCB1 into PCB1
I/O or event
Blocked
completion resume
53
EC—Execution context 54
Spring2005 Spring2005

9
Interrupts (1) Interrupts (2)

„ Interrupt is an event generated by the „ An interrupt handler is a set of instructions to


hardware that alters the sequence in which a be executed in response to each type of
processor executes instructions interrupt.
‰ Synchronous interrupts are caused by an event „ Benefits of Interrupts
related to executing a current process’s ‰ Interrupt provides a low-overhead means of
instructions. gaining the attention of the CPU. This eliminates
‰ Asynchronous interrupts are caused are caused the need for the CPU to remain busy polling to
by an event unrelated with the execution of a see if devices require its attention
current process’s instructions ‰ Polling vs. interrupt

Spring2005 55 Spring2005 56

Interrupt Processing Interrupt Classes

‰ An interrupt occurs „ Hardware-generated interrupts


‰ OS gains control. It saves the state of the ‰ I/O
interrupted process to its PCB and puts the
process into the ready queue ‰ Timer
‰ OS passes control to the appropriate interrupt ‰ Interprocessor interrupts
handler to handle the interrupt „ Software-generated interrupts – Exceptions
‰ The interrupt handler performs appropriate actions ‰ Fault
based on the type of interrupt.
‰ The state of the interrupted process (or some ‰ Trap
“next” process) is restored, and this next process ‰ Abort
executes

Spring2005 57 Spring2005 58

Interprocess Communication (IPC) Signals

„ 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.

‰ Mask – used to block a particular signal.

Spring2005 59 Spring2005 60

10
Message passing Summery

Send (receiverProcess, message) „ What is a process?


Receive (senderProcess, message)
„ Process states
„ Synchronous communication – blocking send „ Process Control Block (PCB)
„ Asynchronous communication – non-blocking „ Process queues and scheduler
send „ Process operations
„ Broadcast „ Context switching
„ Interrupts
„ Interprocess communication (IPC)
Spring2005 61 Spring2005 62

What is a thread?

Chapter 4 „ A thread, also called a lightweight process, is


a basic unit of CPU utilization that executes
Thread Concepts using the program and other resources of its
associated process
‰ Threads within a process share many of the
process’s resources such as its address space
and open files
‰ Several threads can be associated with one
process
‰ Threads allow a process to do more than one task
at a time
Spring2005 63 Spring2005 64

Motivation for Threads


Thread relationship to process
„ A thread has its own
Process of one thread „ Software design – Separating independent
„ Thread ID
Address space files
code segments into individual threads can
„ registers improve performance
„ Stack „ Performance – Multiple threads can share a
„ Signal masks
processor (or a set of processors) so that
„ TSD(Thread-Specific Data) Process of multiple threads
tasks are performed in parallel.
„ It shares with other threads Address space files
associated with the same „ Cooperation – Threads can communicate
process using their shared address space.
„ Address space
„ Open files
Spring2005 65 Spring2005 66

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

Threading Models User-level Threads (1)

„ 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

User-level Threads (2) User-level Threads (3)

„ 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)

„ This is also called one-to-one thread mapping One process

„ The operating system provides each user User space

thread with a kernel thread that the system


Thread
can dispatch
‰ Each kernel thread has its own execution context
‰ Kernel thread is managed by the system Execution
context

Kernel space

Spring2005 73 Spring2005 74

Kernel-level Threads (3) Combining User- and Kernel-level


Threads (1)
„ Benefits „ This is also called many-to-many thread
‰ Better performance mapping.
‰ Each thread can be managed individually. ‰ It maps many user-level threads to a set of kernel
„ Other threads can be dispatched when one thread is blocked. threads.
„ Better interactivity ‰ The number of user threads and the number of
‰ Systemwide priority is supported the kernel threads need not be equal.
„ Drawbacks „ Implementation
‰ More overhead ‰ Thread pooling
‰ Less portability „ One-to-one mapping requires that one data structure is
‰ Uses more resources allocated to represent each kernel thread – Too much
overhead

Spring2005 75 Spring2005 76

Combining User- and Kernel-level Combining User- and Kernel-level


Threads (2) Threads (3)
„ Thread pooling allows an application to specify the
number of kernel threads it requires. Many-to-one User space
One
mapping can be used for threads that exhibit a low process
degree of parallelism.
„ It allow the kernel threads to remain in the system after Thread
a user thread dies. The kernel thread can then be
allocated to a new user thread.
„ Worker threads – the persistent kernel threads that
Execution
typically performs several different functions.
context

Kernel space

Spring2005 77 Spring2005 78

13
Thread Implementation Issues (1) Thread Implementation Issues (2)

„ Thread Signal Delivery Signal handlers


‰ Signal types
Threads
„ A synchronous signal occurs as the direct result of an
instruction executed by the current process or thread.
„ An asynchronous signal occurs due to an event T1 T2 T3
unrelated to the current instruction.
‰ Each signal must specify a process ID to indicate
the signal recipient.
‰ A signal mask allows a thread to disable signals of
particular types. Marked
unmarked
signal
Spring2005 79 Spring2005 80

Thread Implementation Issues (2) Java Threads (1)

„ Thread termination „ There are two ways to create a new thread of


‰ Normal termination: execution.
„ the operating system can immediately remove the 1. Declare a class to be a subclass of Thread.
thread from the system „ This subclass should override the run method of class
Thread.
‰ Premature termination „ An instance of the subclass can then be allocated and started.
„ Threading libraries determine how and when to remove 2. Declare a class that implements the Runnable interface.
the thread from the system. „ This class then implements the run method.
„ An instance of the class can then be allocated, passed as an
argument when creating Thread, and started.

Note: See http://java.sun.com/j2se/1.4.2/docs/api/index.html for more details.

Spring2005 81 Spring2005 82

Java Class Thread Java Threads --Example 1


class PrimeThread1 extends Thread {
„ java.lang.Thread long minprime;
‰ Has several constructors, such as PrimeThread1(long num) {
„ public Thread(); this.minprime=num;
}
„ public Thread(String threadName);
public void run() {
„ Public Thread(Runnable target); //calculate the next prime larger than minprime
„ Public Thread(Runnable target, String threadName); }
(Automatically generated names are of the form "Thread-"+n, where n is }
an integer. )
‰ Methods
„ run() – The code placed in the run method does the real work of the
thread. PrimeThread1 p = new PrimeThread1(143);
„ start() – Causes this thread to begin execution; the Java Virtual p.start();
Machine calls the run method of this thread.
„ sleep(long millis) – Causes the currently executing thread to sleep for
the specified number of milliseconds.

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.

PrimeThread p = new PrimeThread(143);


new Thread(p).start();

Spring2005 85 Spring2005 86

Summery

„ What are threads? Chapter 5:


„ Motivation
„ Thread states Asynchronous Concurrent
„ Thread operations Execution
„ Thread Models
‰ Kernel-level threads, user-level threads and
combination of user- and kernel-level threads
„ Thread implementation issues
„ Java Threads (two methods)

Spring2005 87 Spring2005 88

Bounded-Buffer Producer and


Introduction
Consumer Problem
„ Concurrent: threads that co-exist in a
„ Assume we have a bounded-buffer with fixed
system at the same time
size of N. Producer adds items to the buffer
„ Asynchronous: threads that operate and consumer takes items from the buffer
independently of one another but must
occasionally communicate and synchronize out in
to perform cooperative tasks.
0 1 2 N-2 N-1
count

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

Race Conditions Critical-Section


„ Race condition – this situation occurs when „ Declare a section of code to be critical, in
several threads access and manipulate the which a set of threads may be changing
same data concurrently and where the common variables, updating a table, writing a
outcome of the execution depends on the file.
particular order in which the access takes „ Regulate access to the critical section to
place. guarantee that the execution of critical
„ Solution: Only one thread at a time can section by the threads is mutually exclusive in
manipulate the shared modifiable variables. time.
Thread synchronization and coordination
are needed.
Spring2005 93 Spring2005 94

Solution for Critical Section Problems Mutual Exclusion Primitives


‰ Mutual exclusion: Only one process is allowed to be While(true){
executing in the critical section at one time
‰ Progress: If no process is executing in its CS, then Enter critical section;
one of the processes that wish to enter the critical
section, and is not executing in the remainder Critical section;
section, should be selected to enter the critical Exit critical section;
section. This selection cannot be indefinitely
postponed. Non-critical section;
‰ Bounded waiting: A process should not wait }
indefinitely to enter its CS

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

Dekker’s Algorithm : Version 2 (1) Dekker’s Algorithm : Version 2 (2)


„ Eliminate the lockstep synchronization Thread 1 Thread 2
„ Replace the variable turn with the two While(true){ While(true){
while(t2Inside) {wait;} while(t1Inside) {wait;}
variables: t1Inside=true; t2Inside=true;
boolean t1Inside = false; critical section; critical section;
boolean t2Inside = false; t1Inside=false; t2Inside=false;
t1Inside = true indicates that thread 1 is inside the non-critical section; non-critical section;
critical section. t2Inside = true indicates that thread 2 } }
is inside the critical section.

Spring2005 99 Spring2005 100

Dekker’s Algorithm : Version 2 (3) Dekker’s Algorithm : Version 3 (1)

„ 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.

Spring2005 101 Spring2005 102

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;
} }

Spring2005 103 Spring2005 104

Dekker’s Algorithm : Version 4 (1) Dekker’s Algorithm : Version 4 (1)

Thread 1 Thread 2 „ Satisfies mutual exclusion requirement and


while(true){ prevents deadlock, but it allows indefinite
while(true){
t1WantToEnter=true; t2WantToEnter=true; postponement.
while(t2WantToEnter) { while(t1WantToEnter) {
t1WantToEnter=false; t2WantToEnter=false; It is possible for the following situation to happen:
wait for a certain time; wait for a certain time; thread 1: set t1WantToEnter = true;
t1WantToEnter=true; t2WantToEnter=true; thread 2: set t2WantToEnter = true;
} } Thread 1 and 2 will loop forever in the while statement.
critical section; critical section;
t1WantToEnter=false; t2WantToEnter=false;
non-critical section; non-critical section;
} }
Spring2005 105 Spring2005 106

Dekker’s Algorithm : Version 5 (1) Dekker’s Algorithm : Version 5 (2)


– A proper solution Thread 1
– A proper solution
Thread 2
„ A boolean variable for each thread to indicate while(true){ while(true){
its desire to enter the critical section: t1WantToEnter=true; t2WantToEnter=true;
boolean t1WantToEnter = false; while(t2WantToEnter) { while(t1WantToEnter) {
boolean t2WantToEnter = false; if(favouredThread==2){ if(favouredThread==1)
t1WantToEnter=false; t2WantToEnter=false;
„ An int variable to indicate the favoured thread while(favouredThread==2){wait;} while(favouredThread==1){wait;}
that will enter the critical section. t1WantToEnter=true; t2WantToEnter=true;
int favouredThread = 1; }} }}
critical section; critical section;
favouredThread=2; favouredThread=1;
t1WantToEnter=false; t2WantToEnter=false;
non-critical section; non-critical section;
}
}
Spring2005 107 Spring2005 108

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?

Spring2005 109 Spring2005 110

Peterson’s Algorithm (2) Peterson’s Algorithm (3)


Thread 1 Thread 2
while(true){ while(true){
„ Satisfies mutual exclusion requirement and
t1WantToEnter=true; t2WantToEnter=true; prevents deadlock and indefinite
favouredThread=2; favouredThread=1; postponement.
while(t2WantToEnter && while(t1WantToEnter &&
favouredThread==2 ) { favouredThread==1 ) { „ Busy wait
wait; wait;
} }
critical section; critical section;
t1WantToEnter=false; t2WantToEnter=false;
non-critical section; non-critical section;
} }

Spring2005 111 Spring2005 112

Hardware solutions Hardware solutions


– Disable Interrupts – Test-and-Set Instruction
„ The reason mutual exclusion is needed is „ A special hardware instruction – Test-and-Set
largely because pre-emption allows multiple instruction enables a thread to perform some
threads to work on shared data operations atomically.
asynchronously. „ To allow a thread to enter a critical section,
„ Threads are typically pre-empted by the Test-and-Set instruction will read a flag to
interrupts. determine that no other thread is executing in
„ Not practical. (what if a thread enters an the critical section. Then it sets a lock to
indefinite loop in its critical section?) indicate that the thread is executing in the
critical section.

Spring2005 113 Spring2005 114

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

Thread Synchronization with Semaphores Thread Synchronization with Semaphores

„ 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.

Spring2005 117 Spring2005 118

Producer/Consumer relationship Producer/Consumer relationship


implemented with semaphores (1) implemented with semaphores (2)
While(true){
Global variables: int nextValueProduced
While(true){
‰Semaphore valueProduced for the “value has nextValueProduced=create a new value
been produced” event. It is initialised to 0. P(valueConsumed)
‰Semaphore valueConsumed for the “value has sharedValue=nextValueProduced; Consumer thread
V(valueProduced)
been consumed” event. It is initialised to 1. } while(true){
‰Int sharedValue is a variable shared by both } int nextValueConsumed
While(true){
producer and consumer threads. Producer thread nextValueProduced=create a new value
P(valueProduced)
nextValueConsumed=sharedValue
V(valueConsumed);
consume the value
}}
Spring2005 119 Spring2005 120

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

„ Race conditions, Critical Section and Mutual


exclusion Chapter 6
„ Solutions for Critical Section problems
‰ Dekker’s algorithm (version1-5)
Concurrent Programming
‰ Peterson’s algorithm
‰ Hardware solutions
‰ Semaphores
„ Thread synchronization with semaphores
‰ Consumer and producer
‰ Bounded-buffer problem
‰ The readers and writers problem

Spring2005 125 Spring2005 126

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

The Dining Philosophers Problem (2) The Dining-Philosopher Problem (3)


Semaphore chopsticks[] = new Semaphore[5];
„ A simple solution is to represent each
//process i
chopstick by a semaphore. while(true) {
‰ A philosopher tries to grab a chopstick by P(chopsticks[i]);
P(chopsticks[(i+1)mod5]);
executing a P operation on that semaphore …
‰ a philosopher releases his chopstick by executing eat;

a V operation on the semaphore
V(chopsticks[i]);
V (chopsticks[(i+1)mod5]);

think;
…..
}
Spring2005 129 Spring2005 130

The Dining Philosophers Problem (4) Semaphore – problems (1)


„ What happens if a process interchanges the
„ Possibility of deadlock order of P and V operations? – not mutually
‰ What happens if all the philosophers are exclusive any more.
hungry at the same time, and they all grab the Process P1 Process P2 Process P3
P(s) V(s) P(s)
chopstick on their left, and then wait for the CS; CS; CS;
chopstick on their right? V(s); P(s); V(s);

„ What happen if a process replaces V operation


with P operation? – leads to deadlock.
Process P1 Process P2 Process P3
P(s) P(s) P(s)
CS; CS; CS;
P(s); V(s); V(s);
Spring2005 131 Spring2005 132

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);

Spring2005 133 Spring2005 134

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.

Spring2005 135 Spring2005 136

Bounded-Buffer Problem (1)


Monitors
„ Variable declaration and initialization:
Shared Data //Shared Data
Entry Queue
Condition variables and int size = 100;
X Conditional Data their associated queues byte[] buffer = new byte[size];
Y int in=0, out=0, count=0;

Operations on shared data //Conditional variables


ConditionalVariable notFull, notEmpty;

Initialization

Spring2005 137 Spring2005 138

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;
}

Spring2005 139 Spring2005 140

Bounded-Buffer Problem (4) Dining Philosophers Problem (1)


„ Assuming the instance of the monitor is
called BFMonitor. „ Variable declaration and initialization:
„ static final int THINKING=0, HUNGRY=1, EATING=2;
„ The producer process: „ //Shared data
…produce a new item … „ int[] state = new int[5];
BFMonitor.deposit(item); „ //Conditional variable
„ ConditionalVariable[] self = new ConditionalVariable[5];
„ The consumer process:
item = BFMonitor.fetch();
…consume the item…

Spring2005 141 Spring2005 142

Dining Philosophers Problem (2) Dining Philosophers Problem (3)

„ Operation pickup (int i) is used by „ Operation test (int i) is a private function. It


philosopher i to pick up the chopsticks on allows philosopher i to eat only when both
both sides so that he can eat. his neighbors are not eating.
„ public void pickup( int i ) { void test(int i){
„ state[i] = HUNGRY; if( state[left(i)] != EATING && state[i] == HUNGRY
„ test(i); && state[right(i)] != EATING){
„ if( state[i] != eating ) state[i] = EATING;
„ wait( self[i] ); notify(self[i]);
„ } }
}

Spring2005 143 Spring2005 144

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);
}

Spring2005 145 Spring2005 146

Summary

„ Deadlock and Starvation Chapter 7


„ Semaphores (continued) Deadlock
‰ Dining-philosopher problem.
‰ Problems
„ Monitors
‰ Definition and structure.
‰ Bounded-buffer problem.
‰ Dining philosophers problem.

Spring2005 147 Spring2005 148

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

Spring2005 149 Spring2005 150

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.

Spring2005 151 Spring2005 152

Related Problem: Starvation Resource Concepts

„ Starvation (or indefinite postponement) is a „ Resources can be


situation where processes wait indefinitely for ‰ Preemptible – Can be removed from a process.
a resource. Such as processors and main memory
‰ Nonpreemptible – Cannot be removed from a
process until the process voluntarily releases
them. Such as printers, tape drives and scanners.
‰ Shared – Can be shared by multiple processors.

Spring2005 153 Spring2005 154

Necessary Conditions Resource Allocation Graph


„ Vertices V = {P, R}
„ Necessary conditions for deadlock
‰ P={P1, P2, … Pn}: active processes in the system
‰ Mutual Exclusion
‰ P={R1, R2, … Rm}: resource types in the system
‰ Hold and wait
‰ No pre-emption „ Directed edges
‰ Circular wait ‰ Request edges: Pi ÆRj signifies process Pi requested
an instance of resource type Rj and is currently waiting
„ All four conditions must hold for a deadlock for the resource
to occur ‰ Assignment edges: Rj Æ Pi signifies an instance of Rj
has been allocated to process Pi

Spring2005 155 Spring2005 156

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

Spring2005 157 Spring2005 158

Detecting Deadlock in a Resource Reduction of resource allocation graph


allocation Graph (2) – An Example (1)
R1 R1
„ Reduction of resource allocation graph
‰ If a process’s resource requests may be granted,
we reduce the graph by the process by removing
all the arrows to and from that process . P1 P2 P1 P2
Reducing by P3 Reducing by P1
‰ If a graph can be reduced by all its processes,
then there is no deadlock, otherwise, the
irreducible processes constitute the set of R2 R2
deadlock processes in the graph.
P3 P3

Spring2005 159 Spring2005 160

Reduction of resource allocation graph


– An Example (2) Another Example
R1
R1 R3

P1 P2 P1 P2
Reducing by P2 P1 P2 P3

R2 R2
R4
No Deadlock R2

P3 P3

Spring2005 161 Spring2005 162

27
Another Example again And again
R1 R3 R1 R3

P1 P2 P3 P1 P2 P3

R4 R4
R2 R2

Spring2005 163 Spring2005 164

…again Handling Deadlock (1)


R1 R3 „ Deadlock prevention
‰ Prevent deadlocks by restraining how requests
can be made.

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

Spring2005 165 Spring2005 166

Handling Deadlocks (2)


Deadlock Prevention (1)
„ Deadlock detection „ Mutual exclusion: No mutually exclusive access to the
‰ Identify the processes and resources that are sharable resources – Not reasonable.
involved if a deadlock has occurred. „ Hold and wait (two protocols):
„ Deadlock Recovery ‰ Each process requests and is allocated all its resources
‰ Clear deadlocks from a system so that the before the start of its execution.
deadlocked processes may complete their ‰ A process is allowed to request a resource only when it
execution and free their resources. has none.
‰ Disadvantages:
„ Resource utilization is low
„ Starvation

Spring2005 167 Spring2005 168

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.

Spring2005 169 Spring2005 170

Safe sequence Safe Sequence and Safe State


„ A safe sequence is a sequence of processes <P1, P2, „ A system is in a safe state only if there exists a safe
…, Pn>, such that for each Pi, the resources that Pi can sequence.
request can be satisfied by the current available „ If no safe sequence exists, the system state is unsafe.
resources plus the resources held by all the Pj, with j<i . An unsafe state may lead to a deadlock.
R1 R3 „ It is possible to go from a safe state to a unsafe state.

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

Dijkstra’s Banker’s Algorithm (1)


Deadlock-avoidance Algorithms
„ A system groups all the resources it manages
„ Deadlock-avoidance algorithms: ensure that into resource types. The resources of the
the system is always in a safe state. same type provide identical functionality.
„ Dijkstra’s banker’s algorithm for a system
with only one type of resource. (It is easy to
be extended to systems with multiple
resource types.)

Spring2005 173 Spring2005 174

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

Spring2005 175 Spring2005 176

Example of an Unsafe State Summary

„ Suppose a system contains 12 equivalent „ What is deadlock?


resources and three processes sharing the „ Deadlock’s four necessary conditions
resources. The resources are allocated as „ Resource Allocation Graph
following „ Handling deadlock
Process Maximum Current Current ‰ Deadlock detection – Reduce RAG
need loan claim ‰ Deadlock prevention and avoidance
P1 10 8 2 ‰ Deadlock recovery
P2 5 2 3 „ Safe State
P3 3 1 2 „ Safe sequence
„ Dijkstra’s banker’s algorithm

Spring2005 177 Spring2005 178

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.

Spring2005 179 Spring2005 180

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...

Spring2005 181 Spring2005 182

When do scheduling decisions take Preemptive vs. Non-preemptive


place? Scheduling
„ Non-preemptive Scheduling: Once the
„ When a process switches from
system has assigned a processor to a
running to waiting.
process, the system cannot remove that
„ When a process switches from processor from that process
running to ready.
„ Preemptive Scheduling: The system can
„ When a process switches from remove the processor from the process it is
waiting to ready. running.
„ When a process terminates.

Spring2005 183 Spring2005 184

Priorities Scheduling objectives

„ Priorities are often used to determine how to „ Maximize throughput


schedule and dispatch processes. „ Minimize the time each process waits before
‰ Static priorities executing
‰ Dynamic priorities „ Maximize resource utilization
„ Avoid indefinite postponement
„ Minimize overhead
„ Ensure predictability
„ Enforce priorities

Spring2005 185 Spring2005 186

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?

Spring2005 187 Spring2005 188

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

Spring2005 189 Spring2005 190

Priority Scheduling Round-Robin(RR) Scheduling


„ A priority is associated with each process, and the
processor is allocated to the process with the „ Similar to FCFS but with preemption.
highest priority. Normally low numbers represent „ Have a time quantum (time slice). Let the
high priority. first process in the queue run until it exceeds
„ May be preemptive or non-preemptive. the time quantum, then run the next process.
„ May leave some low-priority processes waiting
indefinitely for processor – Indefinite Blocking.
„ Aging – a technique of gradually increasing the
priority of processes that wait for a long time.

Spring2005 191 Spring2005 192

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.

Spring2005 193 Spring2005 194

Multilevel Queue scheduling Multilevel Feedback Queue Scheduling


Highest Priority „ Similar to multilevel queue scheduling except
System processes
processes can move between queues as
Interactive Processes their priority changes.
„ Separate processes with different processor
Interactive Editing Processes
burst characteristics. A process with too
Batch Processes much processor time will be moved into a
lower-priority queue.
Student Processes
Lowest Priority

Spring2005 195 Spring2005 196

Multilevel Feedback Queue Scheduling


– Example Algorithm Evaluation – Example
„ Five processes arrive at time 0, in the order given, with
QUEUE 0 (Quantum = 8) the length of the CPU burst time given in milliseconds:

QUEUE 1 (Quantum = 16)


Process Burst Time
P1 10
QUEUE 3 (FCFS) P2 29
P3 3
P4 7
•A process entering the ready queue is put in queue 0. P5 12
•Only when queue i-1 is empty, will it execute processes in queue i.
Consider the FCFS, SJF and RR (quantum = 10
•A process that arrives for queue i will preempt a process in queue i+1.
milliseconds) scheduling algorithms for this set of
•If a process in queue i does not finish in a time quantum, it is moved
processes. Which algorithm would give the minimum
to the tail of queue i+1.
average waiting time?
Spring2005 197 Spring2005 198

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

Spring2005 199 Spring2005 200

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.

Spring2005 201 Spring2005 202

Memory Hierarchy Memory Management Strategies


Cache
memory „ Fetch strategies – determines when to move the
A processor may access next piece of a program or data to main memory
Storage access programs and data from secondary storage
Time and speed directly
decrease Primary ‰ Demand fetch strategies
memory ‰ Anticipatory fetch strategies
Storage cost The system must
Increase first move „ Placement strategies – determines where in main
programs and memory should place incoming program or data
storage capacity Secondary and tertiary data into main pieces.
decrease storage memory before a
processor may ‰ First-fit, best-fit, worst-fit
reference them „ Replacement strategies – determines which piece to
remove from the main memory

Spring2005 203 Spring2005 204

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.

Spring2005 205 Spring2005 206

Logical and Physical Addresses


Contiguous Single-Partition Allocation
Logical
address •A base register contains the memory base value of a process.
space •A limit register contains the range of the logical addresses.
Dynamical address translation Real address
Space Limit base
Logical address Register Register Memory
MMU Physical address
1000 25000 Operating
Memory System
Management
Unit Yes 25000
CPU < + 25486
486 25486
Logical
No Physical
Address Address

Spring2005 207 Spring2005 208

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

Spring2005 209 Spring2005 210

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

Spring2005 211 Spring2005 212

Contiguous Variable-Partition Allocation First-Fit Strategy


Memory Placement Strategies
Operating
„ First-fit: allocate the first hole large enough. System
Request for 31MB a
Hole (40)
„ Best-fit: allocate hole with the smallest
In use
leftover. b
Hole (32)
„ Worst-fit: allocate the largest hole. Start length
In use
address
c
a 70MB Hole(70)
b 32MB In use
d
c 40MB
Hole (100)
d 100MB
Free Memory List In use

Spring2005 213 Spring2005 214

Best-Fit Strategy Worst-Fit Strategy


Operating Operating
System System
Request for 31MB a Request for 31MB a
Hole (40) Hole (40)
In use In use
b b
Hole (32) Hole (32)
Start length Start length
In use In use
address address
c c
a 70MB Hole(70) a 70MB Hole(70)
b 32MB In use b 32MB In use
d d
c 40MB c 40MB
Hole (100) Hole (100)
d 100MB d 100MB
Free Memory List In use Free Memory List In use

Spring2005 215 Spring2005 216

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

Contiguous Variable-Partition Allocation Contiguous Variable-Partition Allocation


External Fragmentation External Fragmentation – Example
„ As processes are loaded and removed from Operating Operating Operating
System
memory, the free memory space is broken System System
P1 40MB Hole Hole
into little pieces, creating an external P1 finishes

fragmentation problem. P2 80MB P2 80MB P2 80MB

P3 100MB P3 100MB P3 100MB

P4 40MB P4 40MB Hole


P4 finishes
P5 55MB P5 55MB P5 55MB

Spring2005 219 Spring2005 220

Contiguous Variable-Partition Allocation Block Mapping


Reducing External Fragmentation
„ Coalescing – merge adjacent holes to form a „ Group information (program and data of
single, larger hole. processes) into blocks – the system only
„ Memory compaction (also referred to as needs to keep track of where in main memory
garbage collection) – relocate all occupied each virtual memory block has been placed.
areas of memory to one end of main memory. ‰ Pages – blocks that are a fixed size (paging)
This leaves a single large free memory hole. ‰ Segments – blocks that are of different size.
(segmentation)
„ Non-contiguous memory allocation:
‰ Paging „ An address is represented as a ordered pair
Block number Displacement
‰ Segmentation b d
Virtual address: (b, d)

Spring2005 221 Spring2005 222

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

Representation of Memory Addresses Paging Address Translation by Direct


Mapping (1)
„ Consider a system using n bits to represent The system can locate directly any entry in the page table
with a single access to the table
both physical and logical addresses.
‰ The most-significant n-m bits is used for the page Page table origin
or page frame number Register (stores
base address of page number Displacement
‰ The remaining m bits are used for the page table) p d
displacement. b

Consider a 32-bit system using 4k pages. (n=32, m=12). b


+

Each page or frame number is represented using 20 bits.


A logical address (15, 200) is represented as the binary string e e d
b+p
00000000000000001111000011001000
Physical address
Page table
Spring2005 225 Spring2005 226

Paging Address Translation by Direct Paging Address Translation by Associative


Mapping (2) Mapping (1)
Physical
Page size = Frame size =32 bytes address
Frame The page table is placed into a content-addressed
number 0 0
32 Associative memory. Every entry in the associative memory
1 64 is searched simultaneously for a page.
2 96 page number Displacement
Logical Address Physical Address
p d
8 15 24 15 736
23
24 778
132
Physical
25 address
Page table 793
p e e d
origin Register =24*32+15
132 + 140 24 Physical address

Page table Physical Memory


associative table
Spring2005 227 Spring2005 228

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.

Spring2005 229 Spring2005 230

Paging Address Translation by Sharing in a Paging System


Direct/Associative Mapping (2) Process 1’s
Main memory
Page table page table
origin Register page number Displacement
p d a
b b

TLB( most active a


pages only)
+ p e
Process 2’s a b
b page table
d

Process 3’s d
page table
b+p e e d
d
Physical address
b
Page table

Spring2005 231 Spring2005 232

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.

Spring2005 233 Spring2005 234

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
+

‰ Protection bits indicates whether some operations on


the segment are allowed. For example, set a segment
read-only. Segment number Displacement e+d
> +
s d

Segment-overflow
exception
Spring2005 235 Spring2005 236

Sharing in a Paging System


Segmentation: An Example Main memory

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

Spring2005 237 Spring2005 238

Fragmentation in Segmentation Segmentation with Paging (1)

„ Segmentation can cause external „ Segments are arranged across multiple


fragmentation. pages.
„ Solution: „ A Logical memory address is presented as a
‰ Page the segments – Segmentation with paging ordered triple (s, p, d), where s is the
segment number, p is the page number, and
d is the displacement within the page

Spring2005 239 Spring2005 240

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

Why Virtual Memory?


Chapter 10 „ Previous memory allocation strategies
require the entire process to be in memory
Virtual Memory before its execution.
„ Benefits of the ability to execute a program
that is only partially in memory:
‰ A program is no longer constrained by the
amount of physical memory that is available.
‰ More programs can be run at the same time.
‰ Less I/O would be needed.
Spring2005 243 Spring2005 244

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.

Spring2005 245 Spring2005 246

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

Spring2005 247 Spring2005 248

Implementation: Demand paging


scheme Implementation

„ 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.

Spring2005 249 Spring2005 250

Handling Page Faults Performance of Demand Paging (1)

„ Find a free frame. „ Assume


„ Read the desired page into the frame. „ m : memory access time.
„ Reset the page table to indicate that the „ p : page-fault rate (0≤ p ≤ 1).
page is in memory. „ f : page-fault service time.
„ Restart the instruction that was interrupted „ The effective access time for a demand-paged
by the page-fault trap. The process now can memory:
access the page as though it had always ‰ effective access time = (1-p) × m + p × f
been in memory.

Spring2005 251 Spring2005 252

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.

Spring2005 253 Spring2005 254

Page-Replacement Algorithms (1)


Page Replacement (2)
„ Page replacement may double the page-fault „ Random Page Replacement
‰ Easy-to-implement and low-overhead
service time and increase the effective access
‰ It may accidentally select the next page to replace the page
time accordingly. that will be referenced next.
„ Reduce the overhead by using a dirty bit: „ First-In-First-Out Page Replacement
‰ A dirty bit is associated with each page. ‰ Replace the one that has been in memory the longest
‰ The dirty bit is set whenever there is any ‰ It can replace heavily used pages
modification in the page. „ Least-Recently-Used (LRU) Page Replacement
‰ When a page is selected for replacement, the page ‰ Replace the one that has spent the longest time in memory
without being referenced.
is written to the disk only if its dirty bit is set.
‰ Better performance at the cost of system overhead

Spring2005 255 Spring2005 256

Page Replacement Algorithms (2) Page-Replacement Algorithms:


Evaluation method
„ Least-Frequently-Used Page Replacement
‰ Replace the one that is least frequently used. „ The lower the page-fault rate the better.
‰ Substantial overhead „ Evaluation: Run on a string of memory references
„ Not-Used-Recently Page Replacement and count the number of page faults. For example
‰ Replace the one that has not been used recently. ‰ 7, 8, 0, 6, 5, 4, 6, 9, 2, 3, 3
‰ Each number is a reference to a page.
„ Optimal Algorithm ‰ The more frames available, the lower the number of page
‰ Replace the page that will not be used for the longest faults.
period of time.

Spring2005 257 Spring2005 258

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

and insert a new page at the tail of the queue. 0 0 0 0 3 3 3 2 2 2 2 2 1 1 1 1 1 0 0

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.

Spring2005 259 Spring2005 260

Page-Replacement Algorithms: Page-Replacement Algorithms:


Optimal Algorithm Optimal Algorithm
„ Replace the page that will not be used for the longest Reference strings
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
period of time.
7 7 7 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 7 7
„ It has the lowest page-fault rate. 0 0 0 0 0 0 4 4 4 0 0 0 0 0 0 0 0 0 0
„ Assume there are 3 frames in memory. If run on a Page frames 1 1 1 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1
reference string
7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 number of page faults = 9
using optimal algorithm, how many page faults?
Optimal algorithm is difficult to implement, because it
requires future knowledge of the reference string.

Spring2005 261 Spring2005 262

Page-Replacement Algorithms: Page-Replacement Algorithms:


Least Recently Used (LRU) Algorithm Least Recently Used(LRU) Algorithm
Reference strings
„ Replace the page that has not been used for the 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
longest period of time. 7 7 7 2 2 2 2 4 4 4 0 0 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0
„ An approximation to the optimal algorithm.
Page frames 1 1 1 3 3 3 2 2 2 2 2 2 2 2 2 7 7 7
„ Assume there are 3 frames in memory. If run on a
reference string number of page faults = 12
7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
using LRU algorithm, how many page faults?

Spring2005 263 Spring2005 264

44
Summary

„ Why is virtual memory needed? Chapter 11


„ Overlay and swapping. Disk Performance Optimization
„ Implementation of Virtual Memory
‰ Demand paging scheme.
‰ Page-fault handling.
„ Performance evaluation
‰ effective access time and page-fault rate.
„ Page-replacement algorithms:
‰ FIFO, Optimal, and LRU.

Spring2005 265 Spring2005 266

Evolution of Secondary Storage Characteristics of Moving-Head Disk


Storage
Hard Disk
Magnetic tape
from a few hundred MB
„ Data is recorded on a series of magnetic disks, or
Storage
to a few GB platters, connected to a spindle that rotates at high
A few pennies per MB
Hard Disk Hard Disk speed. Each platter has two surfaces.
$999.99 1TB
(IBM) „ The data on each disk surface is accessed by a
Punched cards 5MB Hard Disk
And punched Cost $50,00 Over 200GB
read-write head
Paper tape <1$/GB „ As the platters spin, each head sketches out a
circular track of data on a disk surface. A vertical
1951 1957 1990s 2003 2004
set of circular tracks is called cylinder. The track is
divided into sectors, often 512bytes.

Spring2005 267 Spring2005 268

Picture of a Platter Disk Access Time and bandwidth


platter „ The Disk access time has two major components
‰ The seek time is the time for the disk arm to move the
Spindle heads to the cylinder containing the desired sector.
‰ The rotational latency is the additional time waiting for the
disk to rotate the desired sector to the disk head.
„ The disk bandwidth is the total number of bytes
transferred, divided by the total time between the
Track
first request for service and the completion of the
Sector last transfer.
„ Both the access time and bandwidth can be
improved by scheduling the servicing of disk I/O
requests.

Spring2005 269 Spring2005 270

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.

0 14 37 53 65 98 122 183 199 0 14 37 53 65 98 122 183 199

The total movement is 640 cylinders The total movement is 208 cylinders

Spring2005 271 Spring2005 272

Disk Scheduling: Disk Scheduling:


Scan Scheduling C-Scan Scheduling
The disk arm starts at one end of the disk and It is designed to provide a more uniform wait time.
moves toward the other end. The disk arm moves the head from one end to the
other end, serving requests along the way.
For example, a disk queue with requests for I/O to sectors on cylinders
98,183,37,122,14,124,65,67 For example, a disk queue with requests for I/O to sectors on cylinders
The head is initially at cylinder 53. 98,183,37,122,14,124,65,67
The head is initially at cylinder 53.
0 14 37 53 65 98 122 183 199 0 14 37 53 65 98 122 183 199

The total movement is 236 cylinders The total movement is 331 cylinders
Spring2005 273 Spring2005 274

Disk Scheduling: Disk Scheduling:


Look Scheduling C-Look Scheduling
Similar with Scan scheduling, but the disk arm Similar with C-Scan scheduling, but the disk arm
only goes as far as the final request in each only goes as far as the final request in each
direction. direction.
For example, a disk queue with requests for I/O to sectors on cylinders
98,183,37,122,14,124,65,67 For example, a disk queue with requests for I/O to sectors on cylinders
The head is initially at cylinder 53. 98,183,37,122,14,124,65,67
The head is initially at cylinder 53.
0 14 37 53 65 98 122 183 199 0 14 37 53 65 98 122 183 199

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).

Spring2005 277 Spring2005 278

Disk Formatting (2) Boot Block (1)

„ 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.

Spring2005 279 Spring2005 280

Boot Block (2) Redundant Arrays of Independent Disks


„ The full bootstrap program is stored in a set of data (RAID) (1)
blocks called the boot blocks, at a fixed location on „ RAID is a family of techniques that use
the disk.
multiple disks that are organized to provide
„ A disk that has the boot blocks is called boot disk or high performance and reliability.
system disk.
Boot blocks „ The storage of each disk is divided into fixed-
size blocks called strips. Contiguous strips of
FAT
a file are typically placed on separate disks
Root directory so that a request for file data can be serviced
using multiple disks at once.
Data Blocks
„ There are over five different organizations, or
Disk layout levels, of disk arrays.
Spring2005 281 Spring2005 282

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

Level 1 (Mirroring) (1) Level 1 (Mirroring) (2)


„ It uses disk mirroring to provide redundancy. So Mirrored pair Mirrored pair
each disk in the array is duplicated. D1 D2 D3 D4

„ Strips are not provided in this level. A B C D A B C D I J K L I J K L


„ High fault tolerance – It provides the highest degree E F G H E F G H M N O P M N O P

of fault tolerance among the most popular RAID


levels.
Data retrieved
Data written
„ High cost – Only half of the array’s disk capacity can From the array Controller to the array
be used to store unique data. A, B I

Read Write
Request Request

Spring2005 285 Spring2005 286

Level 2 (Bit-level Hamming ECC Parity) Level 3, 4 and 5


„ Its arrays are striped at the bit level.
„ Hamming Error-Correcting Codes (ECCs) is used to „ Level 3 (Bit-level XOR ECC parity)
check errors in data transmission and correct them.
„ Level 4 (Block-level XOR ECC parity)
Data Disks Parity disk ‰ It is striped using fixed-size blocks.
D1 D2 D3 D4 P1
A1 B1 A2 B2 A3 B3 A4 B4 Ax Bx
„ Level 5 (Block-level Distributed XOR ECC
parity)
‰ The parity blocks are distributed throughout the
Control must read from array of disks.
All data disks in the array Controller
and write to parity disk
To perform the write A1-B2

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

Spring2005 289 Spring2005 290

Typical File System


Chapter 12 Directory

File and Database Systems Partition A Files

Directory
Partition B

Files

Spring2005 291 Spring2005 292

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.

Spring2005 293 Spring2005 294

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

Spring2005 295 Spring2005 296

Directories – Links (2) Metadata


Directory

name location „ File systems need to store data (Metadata) other


foo 467 than user data and directories, such as
bar 843
‰ A file system identifier
‰ The number of blocks in the file system
... …
‰ The locations of the storage device’s free blocks
Hard link ‰ The location of the root directory
foo_hard 467
Soft link
‰ The date and time at which the file system was last
foo-soft ./foo
modified
other 1021
„ Metadata is used to protect the integrity of the file
system and cannot be modified directly by users.

Spring2005 297 Spring2005 298

File Descriptor (1) File Descriptor (2)

„ A open-file table that keeps track of open files „ Type


„ Disposition (permanent or temporary)
is maintained by the OS to avoid repeated
„ Creation data and time
traversals over the directory. „ Date and time last modified
‰ It contains a file control block for each open file, „ Access activity counts
which includes
„ File name
„ A file descriptor is a non-negative integer that
„ Location in secondary storage indexes into the open-file table. It is returned
„ Data access method by a file open operation.
„ Device type
„ Access control data

Spring2005 299 Spring2005 300

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

File Access Methods: File Access Methods:


Sequential Access Direct Access

„ Information in the file is processed in order. „ A file is made up of fixed-length logical


records that allow programs to read and write
records rapidly in no particular order.
beginning end
Read or write
Current position

0 n t

Read n write t

Spring2005 303 Spring2005 304

File Allocation Methods:


File Organization
Contiguous Allocation
„ Refers to the manner in which the records of a file „ Each file occupies a set of contiguous blocks of
are arranged on secondary storage. disk.
‰ Sequential – records are placed in physical order. „ Benefits:
‰ Direct – records are directly (randomly) accessed by their
physical addresses on a direct access storage device.
‰ Quick and easy calculation of block holding data.
‰ Indexed sequential – Records on disk are arranged in ‰ Supports both sequential and direct access.
logical sequence according to a key contained in each „ Disadvantages:
record.
‰ Partitioned – This is a file of sequential subfiles. Each ‰ Where is the best place to put a new file?
sequential subfile is called a member. The starting address ‰ Problems when the file gets bigger.
of each member is stored in the file’s directory.
‰ Fragmentation problem.

Spring2005 305 Spring2005 306

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

Spring2005 307 Spring2005 308

File Allocation Methods File Allocation Methods


– FAT Allocation (1) – FAT Allocation (2)
„ Use a file allocation table (FAT) – A variation on
Linked Allocation. „ Benefit: direct access time is improved.
Directory
0 „ Disadvantage: Results in a significant
2
File start block EOF number of disk head seeks.
Cat 12 6 2 0 1 2 -1 3 4 „ What happens if FAT is cached?
10 5 6 7 8 9
6
10 11 12 13 14
12
12 15 16 17 18 19

16 Disk
10

FAT
Spring2005 309 Spring2005 310

File Allocation Methods File Allocation Methods


– Indexed Allocation (1) – Indexed Allocation (2)
„ Each file has its own index block, which is an array
of disk-block addresses. The ith entry in the index „ Benefits
block points to the ith block of the file.
‰ Solves fragmentation and size-declaration
problems of contiguous allocation.
‰ Support direct access, but not as efficient as FAT.
16
Directory
File index block
10
6 „ Disadvantages
0 1 2 3 4 12 2
Cat 12 8 ‰ Wasted space due to overhead of index block
5 6 7 8 9 -1
-1
10 11 12 13 14
Index block
15 16 17 18 19

Disk
Spring2005 311 Spring2005 312

52
Free Space Management File Access Control (1)

„ A file system needs to maintain a record of „ Use access control matrix.


the location of blocks that are available to ‰ The entry aij is 1 if user i is allowed access to file j
store new data. File
1 2 3 4 5 6 7 8 9 10
‰ Use a linked-list (free list) of free blocks. User
1 1 1 0 0 0 0 0 0 1 0
‰ Use a bit map. A bit map contains one bit for each 2 1 0 0 0 1 0 0 0 0 1
block in the file system, where the ith bit 3 0 0 0 1 0 1 0 0 1 0
corresponds to the ith block in the file system. 4 0 1 1 0 0 1 0 0 1 0
5 0 0 0 0 0 0 1 0 0 0

Spring2005 313 Spring2005 314

File Access Control (2) Data Integrity Protection – Backup

„ Access control by user classes. „ A physical backup duplicates a storage device’s


data at the bit level.
‰ Owner ‰ It stores no information about the logical structure of the file
system.
‰ Specified user
‰ It is difficult to be restored on computers with different
‰ Group architecture.
‰ The whole file system has to be recorded and restored.
‰ Public
„ A logical backup stores data in a common format
The access control data is usually stored as part of using a directory structure, allowing systems with
the file control block different native file formats to read and restore the
backup data.
‰ An incremental backup is a logical backup that only stores
file system data that has changed since the previous
backup.

Spring2005 315 Spring2005 316

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.

Data Log(free space)--Æ


Spring2005 317 Spring2005 318

53
Database Systems Advantages of Database Systems

„ A database is a centrally controlled, „ Database system organizes data according


integrated collection of data to its content.
„ A database system involves „ It incorporates a querying mechanism that
‰ the data allows applications to specify which
‰ the hardware on which the data resides information to retrieve.
‰ the software that controls access to data (called a
database management system or DBMS) „ It uses standardized data organization
„ Databases are commonly implemented on techniques and its structure cannot be altered
web servers and shared by multiple by applications.
processes.
Spring2005 319 Spring2005 320

Data Access in Database Systems Relational Database Model (1)

„ 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

Relational Database Model (2) Relational Database Model (3)

„ Project operation – Creates a subset of a „ Advantages


database which contains only certain subsets ‰ It is easy to be implemented in a physical
of the table’s rows and columns. database system.
‰ It is relatively easy to convert any other database
„ Join operation – Combines small database structure into the relational model.
tables into a larger one. ‰ The project and join operations are easy to
implement and make the creation of new relations
required for particular applications easy to
perform.
‰ Access control to sensitive data is straightforward
to implement.

Spring2005 323 Spring2005 324

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

Spring2005 325 Spring2005 326

I/O Hardware (1) I/O Hardware (2)


„ Computers operate many kinds of devices:
‰ Storage devices: disks, tape. „ An I/O port typically consists of four registers:
‰ Transmission devices: network cards, modems. ‰ Status register is read by the host to get the status
‰ Human-interface devices: screen, keyboard, mouse. of a device.
„ A device communicates with a computer system by ‰ Control register is written by the host to start a
sending signals over a cable or bus via an I/O port. command.
„ A controller is a collection of electronics that can ‰ Data-in register is read by the host to get input.
operate a port, a bus, or a device. ‰ Data-out register is written by the host to send
output.

Spring2005 327 Spring2005 328

Polling Interrupts (1)


„ The CPU repeatedly reads the busy bit in the status
„ The CPU senses the interrupt request line
register until that bit becomes clear.
after executing every instruction.
„ It is efficient if the controller and device are fast and
always ready for service. „ The I/O interrupt cycle:
„ It is inefficient if they are not. ‰ The device controller raises an interrupt by
„ It may be more efficient to have the device asserting a signal on the interrupt request line.
controller to notify the CPU that a device is ready for ‰ the CPU detects the interrupt, saves its state, and
service – Interrupt. then jumps to the interrupt-handler routine at a
fixed memory address.

Spring2005 329 Spring2005 330

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.

Spring2005 331 Spring2005 332

DMA Data transfer Kernel I/O Subsystem


„ CPU writes a DMA command block into
„ The services provided by the kernel’s I/O
memory. The block contains:
subsystem:
‰ A pointer to the source of the transfer.
‰ I/O scheduling.
‰ A pointer to the destination of the transfer.
‰ Buffering.
‰ The number of bytes to be transferred.
‰ Catching.
„ CPU writes the address of the DMA ‰ Spooling.
command block to the DMA controller. ‰ Device reservation.
‰ Error handling.

Spring2005 333 Spring2005 334

I/O Scheduling Buffering

„ 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

Error Handling Summary


„ The basic I/O hardware elements: buses,
„ Generally, an I/O system call will return a bit controllers, devices.
of information about the status of the call,
„ Mechanisms for interaction between device
signifying either success or failure
controller and CPU: polling, interrupts, and
„ A failed call results in recovery processing by DMA.
the operating system
„ I/O subsystem provides services such as I/O
scheduling, buffering, caching, spooling, and
error handling.

Spring2005 339 Spring2005 340

Introduce

Chapter 14 „ It is important to determine how effectively a


particular system manages its resources.
Performance and Processor „ The performance of a system depends
Design heavily on its hardware, operating system
and the interaction between the two.
„ Evaluation techniques required to measure
the performance of the system.

Spring2005 341 Spring2005 342

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.

Spring2005 343 Spring2005 344

Performance Measures (2) Performance Evaluation Techniques (1)

‰ Throughput – work-per-unit-time „ Tracing and profiling


‰ Traces are records of system activity executing in user
‰ Workload – the amount of work that has been mode.
submitted to the system ‰ Profiles are records of system activity executing in kernel
mode.
‰ Capacity – the maximum throughput a system can
„ Timing and micro-benchmarks
attain ‰ Timing is used to evaluate the performance of computer
‰ Utilization – The fraction of time that a resource is hardware.
in use. „ MIPS – Millions of instructions per second
„ BIPS – Billions of instructions per second
„ TIPS – Trillions of instructions per second
‰ Micro-benchmarks measure the time required to perform
an operating system operation.

Spring2005 345 Spring2005 346

Performance Evaluation Techniques (2) Performance Evaluation Techniques (3)

„ 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

Spring2005 347 Spring2005 348

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

Spring2005 349 Spring2005 350

Reduced Instruction Set Computing Post-RISC Processors


(RISC)
„ Provides fewer instructions, most of which „ Superscalar architecture
can be performed quickly. „ Out-of-Order Execution (OOO)
„ The programming complexity is moved from
hardware to software. „ Branch prediction
„ RISC control units are implemented in „ On-chip floating and vector processing
hardware. support
„ Supports only fixed-length instructions, and „ Additional infrequently-used instructions
makes better use of pipelines. „ CISC convergence to RISC
„ Examples: MIPS, SPARC, G5

Spring2005 351 Spring2005 352

Summary

„ Common purposes for performance Chapter 15


evaluation
„ Performance measures UNIX System
„ Performance Evaluation Techniques
„ Performance Techniques in processor design

Spring2005 353 Spring2005 354

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

Terminal controllers device controllers memory controllers


terminals disks and tapes physical memory
Spring2005 355 Spring2005 356

File Manipulation File manipulation


„ Files in UNIX are sequences of bytes
‰ Kernel doesn’t impose a structure on files. „ System calls
„ Tree-structured directory ‰ creat, open, read, write, close, link, unlink,
The binary boot image of the operating system
vmunix symlink, trunc, fcntl
dev Device special files ‰ mkdir, rmdir, opendir, readdir, closedir
bin binary files of essential UNIX programs ‰ stat, rename, chmod, chown, ioctl
lib libraries
/
usr binary files of applications
user Files of users

etc Administrative files and programs

tmp Spring2005 357 Spring2005 358

Process Control Signals


Shell process
shell
Shell process Parent process shell
parent
fork wait „ Signals are a facility for handling exceptional
child process
child zombie
zombie process conditions.
execve exit „ There are 20 signals, each responding to a
Program
executes
distinct condition. This may be
‰ a keyboard interrupt such as ^C.
„ fork: create a child process ‰ an bad memory reference.
„ execve: load a new program into memory ‰ ..
„ exit: complete execution
„ wait: suspend execution until child calls exit
Spring2005 359 Spring2005 360

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 ...

Spring2005 361 Spring2005 362

Images and Processes (2) Process Management


„ A process table contains an entry for each
„ An image consists of three segments: text process. Each entry contains
segment, data segment, and stack segment. ‰ process ID
Program text segment ‰ process state
Data segment ‰ process priority
‰ text table address
Stack segment ‰ segment addresses ...

„ The execution of an image is a process. „ The UNIX dispatcher uses this table to
schedule processes.

Spring2005 363 Spring2005 364

CPU Scheduling Memory Management (1)


„ Swapping
„ Each process has a scheduling priority. A ‰ Some processes are swapped out if memory is
larger number indicates lower priority. not enough.
„ The more CPU time a process accumulates, ‰ A swapping process is in charge of swapping.
Memory
the more priority it gets, and vice versa.
Kernel
„ Round-Robin Scheduling.
Swapping process

Second Storage
Process table
Process A

User processes Process B


Spring2005 365 Spring2005 366

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

File system (2) Summary


„ Layer structure.
Configuration table „ Programmer and User interface.
00 „ Process management
01
‰ Process image
02
‰ CPU scheduling
„ Memory management
Device drivers
‰ swapping and segmentation
00 01 02
„ File system
Application process ‰ files
Read 01 ‰ block and character devices

Spring2005 369 Spring2005 370

System Architecture

Chapter 16 User Model


User process DLL User process

Windows XP Environment subsystem

Kernel Model
Native API Interface

Executive services

Device drivers Microkernel

Hardware abstraction layer(HAL)

Spring2005 371 Spring2005 372

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 ...

Spring2005 373 Spring2005 374

System Management Mechanisms Registry

„ 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.

Spring2005 375 Spring2005 376

Object Manager Interrupt Request Levels (IRQLs) (1)

„ 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

Spring2005 377 Spring2005 378

63
Interrupt Request Levels (IRQLs) (2) Asynchronous Procedure Calls (APCs)

High level „ APCs are procedure calls that threads or the


Power level
Request level
Critical system can queue for execution by a specific
System
Clock level Hardware interrupts thread.
Profile level interrupts
‰ This is used to complete various functions that
Interrupt
priority Device IRQLs must be done in a specific thread context.
Thread
DPC/Dispatch level Software
APC level interrupts
Passive level No interrupts I/O Request
Executing
I/O Completion
APC

Spring2005 379 Spring2005 380

Deferred Procedure Calls (DPCs) System Threads

„ 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.

Spring2005 381 Spring2005 382

Process Organization Thread Organization


Kernel Space Process address Space

Executive Process Block Executive Thread Block


(EPROCESS block) Process Environment Block (EThread block) Thread Environment Block
(PEB) Kernel Thread (TEB)
Kernel Process Block
Block
ID ID
Priority, state
ID DLLs, ID Critical sections
Priority, quantum
Pointer Information about heaps Starting address Information about its stack
Working set information Pending I/O requests

Kernel Space Process address Space

PEB

Spring2005 383 Spring2005 384

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 Thread Pools

„ 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

Thread States Thread Scheduling Algorithm


creation Stack paged transition
Initialized Into memory
„ This is based on priority
Ready Wait completion Wait completion
‰ Multi-level queue scheduling
But Stack paged ‰ Windows XP has 32 priority levels: 0-31.
pending Out of memory
pre-emption
pre-emption ‰ A thread is placed into the ready queue
or timeout Standby Waiting corresponding to its priority.
execution

Running

termination

Terminated

Spring2005 389 Spring2005 390

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

„ 4 GB of virtual address space is available to d b


each process. Top 2GB for kernel mode
threads and the bottom 2 GB for user mode a+d
b + t

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

device device device device device


Spring2005 393 Spring2005 394

Processing an I/O Request


User space 1. I/O Request
process NTFS
9. Data sent to calling
Environment subsystems
2. I/O Request
process address space
„ NTFS works with disk volumes. Volumes are
I/O Manager
3. IRP
driver
allocated in clusters. Logical cluster number
4. IRP is used as logical disk address.
Driver
Kernel space
„ Each volume has 4 sections
5. IRP
6. IRP pending ‰ Boot section
driver
7. Process I/O ‰ Master file table (MFT) section
8. Device access ‰ System files
HAL
‰ File area
Physical
Spring2005 device 395 Spring2005 396

66
More about Windows XP Summary

„ Interprocess communication „ System management mechanisms


„ Component Object Model (COM) ‰ Registry
‰ Object manager
„ Networking ‰ Interrupt request levels
„ .Net ‰ Asynchronous and deferred procedure calls
„ Process and thread management
„ Memory management
„ File system management
„ Input/output management

Spring2005 397 Spring2005 398

67

Você também pode gostar