Operating System Concepts

1

Syllabus
ƒ 上課時間: Friday 19:35-22:00 ƒ 教室:M 501 ƒ 教科書: Silberschatz, Galvin, and Gagne, “Operating System Concept,” Seventh Edition, John Wiley & Sons, Inc., 2006. ƒ 成績評量:(subject to changes.): 期中考(30%), 期末考(30%), 課堂參與(40%)

2
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Contents
1. Introduction 2. Computer-System Structures 3. Operating-System Structures 4. Processes 5. Threads 6. CPU Scheduling 7. Process Synchronization 8. Deadlocks 9. Memory Management 10. Virtual Memory 11. File Systems
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

3

Chapter 1. Introduction

4

Introduction
ƒ What is an Operating System?
ƒ A basis for application programs ƒ An intermediary between users and hardware

ƒ Amazing variety
ƒ Mainframe, personal computer (PC), handheld computer, embedded computer without any user view Convenient vs Efficient
5
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Computer System Components
User User ................. User compilers, word processors, spreadsheets, browsers, etc. CPU, I/O devices, memory, etc.

Application Programs Operating System Hardware

ƒ OS – a government/environment provider
6
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

User View
ƒ The user view of the computer varies by the interface being used! ƒ Examples:
ƒ Personal Computer Æ Ease of use ƒ Mainframe or minicomputer Æ maximization of resource utilization
ƒ Efficiency and fair share

ƒ Workstations Æ compromise between individual usability & resource utilization ƒ Handheld computer Æ individual usability ƒ Embedded computer without user view Æ run without user intervention 7
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

System View
ƒ A Resource Allocator
ƒ CPU time, Memory Space, File Storage, I/O Devices, Shared Code, Data Structures, and more

ƒ A Control Program
ƒ Control execution of user programs ƒ Prevent errors and misuse

ƒ OS definitions – US Dept.of Justice against Microsoft in 1998
ƒ The stuff shipped by vendors as an OS ƒ Run at all time
8
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

System Goals
ƒ Two Conflicting Goals:
ƒ Convenient for the user! ƒ Efficient operation of the computer system!

ƒ We should
ƒ recognize the influences of operating systems and computer architecture on each other ƒ and learn why and how OS’s are by tracing their evolution and predicting what they will become! 9
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

application programs. X. 2004. etc. paging. signal handling. disk drivers. etc. CPU scheduling. file system. virtual memory. device controller. swapping. caching/buffering. terminals. . UNIX 10 * All rights reserved. physical memory. memory. Kernel interface to the hardware terminal controller.UNIX Architecture User interface System call interface useruser user user user user user Shells. National Taiwan University. etc. compilers. Tei-Wei Kuo. devices such as disks.

. 2004. Tei-Wei Kuo. National Taiwan University.Mainframe Systems ƒ The first used to tackle many commercial and scientific applications! ƒ 0th Generation – 1940?s ƒ A significant amount of set-up time in the running of a job ƒ Programmer = operator ƒ Programmed in binary Æ assembler Æ (1950) high level languages 11 * All rights reserved.

• loader • job sequencing • control card interpreter User Program Area monitor 12 . 2004. Tei-Wei Kuo. National Taiwan University.Mainframe – Batch Systems ƒ Batches sorted and submitted by the operator ƒ Simple batch systems ƒ Off-line processing ~ Replace slow input devices with faster units Æ replace card readers with disks ƒ Resident monitor ~ Automatically transfer control from one job to the next * All rights reserved.

Mainframe – Batch Systems ƒ Spooling (Simultaneous Peripheral Operation OnLine) ~ Replace sequential-access devices with random-access device => Overlap the I/O of one job with the computation of others e. Tei-Wei Kuo. disk Æ printer ƒ Job Scheduling disks card reader * All rights reserved. CPU services. card Æ disk. National Taiwan University. disks CPU printer 13 . 2004.g.

Mainframe – Multiprogrammed Systems ƒ Multiprogramming increases CPU utilization by organizing jobs so that the CPU always has one to execute – Early 1960 ƒ Multiporgrammed batched systems ƒ Job scheduling and CPU scheduling ƒ Goal : efficient use of scare resources * All rights reserved. National Taiwan University. Tei-Wei Kuo. monitor CPU scheduling job1 job2 job3 disk 14 . 2004.

. 2004.. ƒ An interactive (or handon) computer system ƒ Multics.. National Taiwan University.. Tei-Wei Kuo. and so on disk 15 . on-line file system virtual memory sophisticated CPU scheduling job synchronization protection & security . IBM OS/360 * All rights reserved.Mainframe – Time-Sharing Systems ƒ Time sharing (or multitasking) is a logical extension of multiprogramming! ƒ Started in 1960s and become common in 1970s..

Desktop Systems ƒ Personal Computers (PC’s) ƒ Appeared in the 1970s. 2004. Tei-Wei Kuo. ƒ Goals of operating systems keep changing ƒ Less-Powerful Hardware & Isolated EnvironmentÆ Poor Features ƒ Benefited from the development of mainframe OS’s and the dropping of hardware cost ƒ Advanced protection features ƒ User Convenience & Responsiveness 16 * All rights reserved. National Taiwan University. .

correction) • A Tandem fault-tolerance solution * All rights reserved. and sometimes memory and peripheral devices ƒ Loosely coupled: otherwise ƒ Advantages ƒ Speedup – Throughput ƒ Lower cost – Economy of Scale ƒ More reliable – Graceful Degradation Æ Fail Soft (detection. 17 . diagnosis. 2004. National Taiwan University. Tei-Wei Kuo.Parallel Systems ƒ Tightly coupled: have more than one processor in close communication sharing computer bus. clock.

2004. National Taiwan University. .Parallel Systems ƒ Symmetric multiprocessing model: each processor runs an identical copy of the OS ƒ Asymmetric multiprocessing model: a masterslave relationship ~ Dynamically allocate or pre-allocate tasks ~ Commonly seen in extremely large systems ~ Hardware and software make a difference? ƒ Trend: the dropping of microporcessor cost Î OS functions are offloaded to slave processors (back-ends) 18 * All rights reserved. Tei-Wei Kuo.

peripheral devices. 19 . Tei-Wei Kuo.Distributed Systems ƒ Definition: Loosely-Coupled Systems – processors do not share memory or a clock ƒ Heterogeneous vs Homogeneous ƒ Advantages or Reasons ƒ Resource sharing: computation power. email * All rights reserved. National Taiwan University. specialized hardware ƒ Computation speedup: distribute the computation among various sites – load sharing ƒ Reliability: redundancy Æ reliability ƒ Communication: X-window. 2004.

Tei-Wei Kuo. satellite wireless transmission. ƒ Networks vary by the protocols used. etc.etc. ƒ TCP/IP. National Taiwan University. 2004. ƒ Types – distance ƒ Local-area network (LAN) ƒ Wide-area network (WAN) ƒ Metropolitan-area network (MAN) ƒ Small-area network – distance of few feet ƒ Media – copper wires. 20 . ATM.Distributed Systems ƒ Distributed systems depend on networking for their functionality. * All rights reserved. infrared communication. fiber strands.

.Distributed Systems ƒ Client-Server Systems ƒ Compute-server systems ƒ File-server systems ƒ Peer-to-Peer Systems ƒ Network connectivity is an essential component. National Taiwan University. ƒ Network Operating Systems ƒ Autonomous computers ƒ A distributed operating system – a single OS controlling the network. Tei-Wei Kuo. 21 * All rights reserved. 2004.

Tei-Wei Kuo. ƒ Types ƒ Asymmetric/symmetric clustering ƒ Parallel clustering – multiple hosts that access the same data on the shared storage. 2004. 22 .Clustered Systems ƒ Definition: Clustered computers which share storage and are closely linked via LAN networking. performance improvement. ƒ Global clusters ƒ Distributed Lock Manager (DLM) * All rights reserved. ƒ Advantages: high availability. etc. National Taiwan University.

is not met. Tei-Wei Kuo. ƒ Many advanced features are absent. e. . 23 * All rights reserved. National Taiwan University. deadline.g.Real-Time Systems ƒ Definition: A real-time system is a computer system where a timely response by the computer to external stimuli is vital! ƒ Hard real-time system: The system has failed if a timing constraint. ƒ All delays in the system must be bounded. 2004.

Real-Time Systems ƒ Soft real-time system: Missing a timing constraint is serious. 2004. ƒ Real-time means on-time instead of fast 24 * All rights reserved. National Taiwan University. but does not necessarily result in a failure unless it is excessive ƒ A critical task has a higher priority. Tei-Wei Kuo. ƒ Supported in most commercial OS. .

Applications for Real-Time Systems! 25 * All rights reserved. 2004. National Taiwan University. . Tei-Wei Kuo.

.Real-Time Systems ƒ Applications ƒ Virtual reality ƒ Air traffic control ƒ Games ƒ Space shuttle ƒ User interface ƒ Navigation ƒ Vision and speech ƒ Multimedia systems ƒ Industrial control systems recognition (approx. 100 ~ 200ms) ƒ Home appliance ƒ PDA. Tei-Wei Kuo. National Taiwan University. 2004. telephone controller system ƒ Nuclear power plant ƒ And more 26 * All rights reserved.

National Taiwan University..g. Tei-Wei Kuo. 2004. Personal Digital Assistant (PDA) ƒ New Challenges – convenience vs portability ƒ Limited Size and Weight ƒ Small Memory Size ƒ No Virtual Memory ƒ Slow Processor ƒ Battery Power ƒ Small display screen ƒ Web-clipping * All rights reserved. 27 .Handheld Systems ƒ Handheld Systems ƒ E.

2004. . National Taiwan University.Feature Migration ƒ MULTIplexed Information and Computing Services (MULTICS) ƒ 1965-1970 at MIT as a utility ƒ UNIX ƒ Since 1970 on PDP-11 ƒ Recent OS’s ƒ MS Windows. IBM OS/2. MacOS X ƒ OS features being scaled down to fit PC’s ƒ Personal Workstations – large PC’s 28 * All rights reserved. Tei-Wei Kuo.

g. ƒ Network connectivity ƒ New categories of devices ƒ Load balancers ƒ Embedded Computing ƒ Car engines.. VCR’s. . Tei-Wei Kuo.Computing Environments ƒ Traditional Computing ƒ E. 29 * All rights reserved. network computers. etc. typical office environment ƒ Web-Based Computing ƒ Web Technology ƒ Portals. 2004. home automation ƒ Embedded OS’s often have limited features. National Taiwan University. robots.

30 . Processes 5. Deadlocks 9. CPU Scheduling 7. Threads 6. Virtual Memory 11. Memory Management 10. Operating-System Structures 4. 2004. Computer-System Structures 3. Tei-Wei Kuo. Introduction 2. File Systems * All rights reserved. Process Synchronization 8. National Taiwan University.Contents 1.

Chapter 2 Computer-System Structure 31 .

printer CPU printer controller tape-drive controller disk controller tape drivers memory controller memory disks 32 ƒ Device controllers: synchronize and manage access to devices. * All rights reserved. Tei-Wei Kuo. 2004. National Taiwan University. .Computer-System Structure ƒ Objective: General knowledge of the structure of a computer system.

etc.g. e. . (/etc/rc* & init or /sbin/rc* & init) 33 * All rights reserved. login processes. various daemons.. Tei-Wei Kuo. after the kernel has been bootstrapped. e. device controllers. CPU registers.Booting ƒ Bootstrap program: ƒ Initialize all aspects of the system.g. 2004. memory.. National Taiwan University. ƒ Load and run the OS ƒ Operating system: run init to initialize system processes.

etc – (trap) process execution interrupt handler return ƒ Procedures: generic handler or interrupt vector (MS-DOS. e. . e.g.Interrupt ƒ Hardware interrupt. National Taiwan University. system calls.UNIX) 34 * All rights reserved. signals. 2004. invalid memory access. Tei-Wei Kuo.g. division by zero. services requests of I/O devices ƒ Software interrupt.

registers .Interrupt Handling Procedure interrupted process fixed address per interrupt type system stack interrupted address.. 2004. 35 ..... Tei-Wei Kuo. National Taiwan University. handler ƒ Saving of the address of the interrupted instruction: fixed locations or stacks ƒ Interrupt disabling or enabling issues: lost interrupt?! prioritized interrupts Æ masking * All rights reserved.

saved return addr.g. Æ program counter) Interrupt Vector indexed by a unique device number 0 1 ------------------- Interrupt Handlers (Interrupt Service Routines) n * All rights reserved.. 36 .Interrupt Handling Procedure ƒ Interrupt Handling Î Save interrupt information Î OS determine the interrupt type (by polling) Î Call the corresponding handlers Î Return to the interrupted job by the restoring important information (e. 2004. National Taiwan University. Tei-Wei Kuo.

disk 37 . National Taiwan University. 2004. Tei-Wei Kuo.I/O Structure ƒ Device controllers are responsible of moving data between the peripheral devices and their local buffer storages. printer CPU printer controller registers buffers tape drivers tape-drive controller DMA registers buffers memory controller memory * All rights reserved.

2004.I/O Structure ƒ I/O operation a. b. Tei-Wei Kuo. . National Taiwan University. CPU sets up specific controller registers within the controller. Notify the completion of the operation by triggering an interrupt 38 * All rights reserved. Read: devices Æ controller buffers Æ memory Write: memory Æ controller buffers Æ devices c.

I/O Types a. . National Taiwan University. etc. 2004. Synchronous I/O ƒ Issues: overlapping of computations and IO activities. I/O system call wait till the or • wait instruction (idle till interrupted) completion • looping or • polling • wait for an interrupt Loop: jmp Loop 39 * All rights reserved. Tei-Wei Kuo. concurrent I/O activities.

Time Asynchronous I/O 40 . Tei-Wei Kuo. 2004. National Taiwan University.I/O Types user Requesting process wait Device driver user Requesting process Device driver Kernel Interrupt handler Interrupt handler Kernel Hardware data transfer Hardware data transfer Time Synchronous I/O * All rights reserved.

I/O types b. 2004. Asynchronous I/O wait till the completion sync *efficiency 41 * All rights reserved. wait mechanisms!! . National Taiwan University. Tei-Wei Kuo.

... 38596 len?1372 Request file:xx Record Addr.... 2004.. Request addr. len Request file:yy Record Addr.. len process 1 •Tracking of many I/O requests •type-ahead service process 2 42 * All rights reserved. National Taiwan University. Tei-Wei Kuo.I/O Types ƒ A Device-Status Table Approach card reader 1 status: idle line printer 3 status: busy disk unit 3 status: idle .

DMA moves blocks of data between the memory and its own buffers. 2004. 9600-baud terminal 2-microsecond service / 1000 microseconds High-speed device: 2-microsecond service / 4 microseconds ƒ Procedure ƒ ƒ ƒ ƒ Execute the device driver to set up the registers of the DMA controller. Tei-Wei Kuo.g. Interrupt the CPU when the job is done.DMA ƒ Goal: Release CPU from handling excessive interrupts! ƒ E. Transfer from its buffers to its devices. . 43 * All rights reserved. National Taiwan University.

Volatility 44 * All rights reserved. National Taiwan University. Tei-Wei Kuo. 2004. Cost.Storage Structure CPU HW-Managed registers cache memory Magnetic Disks Primary Storage • volatile storage Secondary Storage • nonvolatile storage Tertiary Storage • removable media SW-Managed ƒ Access time: a cycle ƒ Access time: several cycles ƒ Access time: many cycles CD-ROMs/DVDs Jukeboxes * Differences: Size. Speed. .

such as video controller. National Taiwan University. . or special I/O instructions is used to move data between memory & device controller registers Device Controller Memory ƒ Programmed I/O – polling ƒ or interrupt-driven handling 45 * All rights reserved. Tei-Wei Kuo. 2004. ƒ Processor can have direct access! Intermediate storage for data in the registers of device controllers Memory-Mapped I/O (PC & Mac) (1) Frequently used devices (2) Devices must be fast.Memory ƒ ƒ R1 R2 R3 . . .

2004. Tei-Wei Kuo.Magnetic disks sector track spindle r/w head ƒ Transfer Rate ƒ RandomAccess Time ƒ Seek time in x ms ƒ Rotational latency in y ms ƒ 60~200 times/sec 46 cylinder arm assembly platter * All rights reserved. National Taiwan University. disk arm .

Tei-Wei Kuo. 2004. low capacity. low density.Magnetic Disks ƒ Disks ƒ Fixed-head disks: ƒ More r/w heads v. but less expensive ƒ Tapes: backup or data transfer bet machines 47 * All rights reserved.s. Speed ƒ Computer Æ host controller Æ disk controller Æ disk drives (cache ÅÆ disks) ƒ Floppy disk ƒ slow rotation. fast track switching ƒ Moving-head disks (hard disk) ƒ Primary concerns: ƒ Cost. National Taiwan University. Size. .

High hitting rate • instruction & data cache • combined cache Speed Electronic Disk Magnetic Disk Optical Disk Magnetic Tape Faster than magnetic disk – nonvolatile?! Alias: RAM Disks Sequential Access XX GB/350F 48 . 2004.Storage Hierarchy register Cache Main Memory Volatile Storage Cost * All rights reserved. National Taiwan University. Tei-Wei Kuo.

Storage Hierarchy ƒ Caching ƒ Information is copied to a faster storage system on a temporary basis ƒ Assumption: Data will be used again soon. Tei-Wei Kuo. ƒ Programmable registers. etc. National Taiwan University. instr. . ƒ Cache Management ƒ Cache Size and the Replacement Policy ƒ Movement of Information Between Hierarchy ƒ Hardware Design & Controlling Operating Systems 49 * All rights reserved. 2004. cache.

e. 2004. cache coherency ƒ Multiprocessor or distributed systems CPU Cache Memory CPU cache Memory 50 * All rights reserved. Tei-Wei Kuo.Storage Hierarchy ƒ Coherency and Consistency ƒ Among several storage levels (vertical) ƒ Multitasking vs unitasking ƒ Among units of the same storage level . National Taiwan University.g. . (horizontal).

Tei-Wei Kuo.g.Hardware Protection ƒ Goal: ƒ Prevent errors and misuse! ƒ E. input errors of a program in a simple batch operating system ƒ E. the modifications of data and code segments of another process or OS ƒ Dual-Mode Operations – a mode bit ƒ User-mode executions except those after a trap or an interrupt occurs. 2004. National Taiwan University. supervisor mode) ƒ Privileged instruction:machine instructions that may cause harm * All rights reserved. ƒ Monitor-mode (system mode.. 51 .g. privileged mode..

Hardware Protection ƒ System Calls – trap to OS for executing privileged instructions. ƒ User programs must issue I/O through OS ƒ User programs can never gain control over the computer in the system mode. . Memory. ƒ Resources to protect ƒ I/O devices. Tei-Wei Kuo. 2004. CPU ƒ I/O Protection (I/O devices are scare resources!) ƒ I/O instructions are privileged. 52 * All rights reserved. National Taiwan University.

Hardware Protection ƒ Memory Protection ƒ Goal: Prevent a user program from modifying the code or data structures of either the OS or other users! ƒ Instructions to modify the memory space for a process are privileged. National Taiwan University. Tei-Wei Kuo. 2004. Base register Limit register Ù Check for every memory address by hardware 53 * All rights reserved. kernel job1 job2 …… …… .

context switch 54 * All rights reserved. Tei-Wei Kuo.Hardware Protection ƒ CPU Protection ƒ Goal ƒ Prevent user programs from sucking up CPU power! ƒ Use a timer to implement time-sharing or to compute the current time. . 2004. ƒ Computer control is turned over to OS for every time-slice of time! ƒ Terms: time-sharing. ƒ Instructions that modify timers are privileged. National Taiwan University.

National Taiwan University.. star networks (with gateways) 55 * All rights reserved.g.g. ring.. .Network Structure ƒ Local-Area Network (LAN) ƒ Characteristics: ƒ Geographically distributed in a small area. an office with different computers and peripheral devices. Tei-Wei Kuo. twisted pair cables for 10BaseT Ethernet or fiber optic cables for 100BaseT Ethernet ƒ Started in 1970s ƒ Configurations: multiaccess bus. 2004. e. ƒ More reliable and better speed ƒ High-quality cables. e.

a company or a school. • Having one (or several) gateway with the outside world. or connected LAN’s. 56 * All rights reserved.Network Structure ƒ Wide-Area Network (WAN) ƒ Emerged in late 1960s (Arpanet in 1968) ƒ World Wide Web (WWW) ƒ Utilize TCP/IP over ARPANET/Internet. Tei-Wei Kuo. . • Definition of “Intranet”: roughly speaking for any network under one authorization. National Taiwan University.. e. 2004. • In general. it has a higher bandwidth because of a LAN.g. • Often in a Local Area Network (LAN).

Network Structure – WAN HINET HINET TARNET TARNET gateway Intranet Intranet Intranet Intranet Intranet A Intranet A Intranet A AIntranet gateway router Intranet Intranet 57 * All rights reserved. 2004. . National Taiwan University. Tei-Wei Kuo.

g. ƒ Connecting several subnets (of different or the same networks. to maintain network topology by broadcasting.g. . Bitnet and Internet)for forwarding packets to proper subnets. 2004. e. Tei-Wei Kuo. ƒ Gateway ƒ Functionality containing that of routers. National Taiwan University.Network Structure – WAN ƒ Router ƒ With a Routing table ƒ Use some routing protocol.. 58 * All rights reserved.. ƒ Connecting several subnets (of the same IP-orhigher-layer protocols) for forwarding packets to proper subnets. e.

. Tei-Wei Kuo. T3: 45mbps (28T1) ƒ Telephone-system services over T1 ƒ Modems ƒ Conversion of the analog signal and digital signal 59 * All rights reserved. 2004.Network Structure – WAN ƒ Connections between networks ƒ T1: 1. National Taiwan University.544 mbps.

Tei-Wei Kuo. 2004. . National Taiwan University.Network Layers in Linux applications BSD sockets INET sockets TCP Network Layer Applications Kernel UDP ARP Internet Protocol (IP) PPP SLIP Ethernet 60 * All rights reserved.

101.1 ƒ 256*256*256*256 combinations ƒ 140. 140.edu. National Taiwan University.123. 2004. 61 .ccu. 101.123.101 and 140.102 ƒ Mapping of IP addresses and host names ƒ Static assignments: /etc/hosts ƒ Dynamic acquisition: DNS (Domain Name Server) ƒ /etc/resolv.103 * All rights reserved. re-check it up with DNS! ƒ Domain name: cs. Tei-Wei Kuo.1 -> Host Address ƒ Subnet: ƒ 140.123.TCP/IP ƒ IP Address: ƒ 140.tw as a domain name for 140. and 140.123.123 -> Network Address ƒ 101.confg ƒ If /etc/hosts is out-of-date.123.123.100.

TCP/IP ƒ Transmission Control Protocol (TCP) ƒ Reliable point-to-point packet transmissions. 2004. ƒ User Datagram Protocol (UDP) ƒ Unreliable point-to-point services. ƒ Applications which communicate over TCP/IP with each another must provide IP addresses and port numbers. 62 * All rights reserved. ƒ Both are over IP. . National Taiwan University. ƒ /etc/services ƒ Port# 80 for a web server. Tei-Wei Kuo.

. ƒ Ethernet cards only recognize frames with their physical addresses. e. 08-01-2b-00-50-A6. ƒ Machines with the indicated IP addresses reply with their Ethernet physical addresses. 63 * All rights reserved. National Taiwan University. ƒ Broadcast requests over Ethernet for IP address resolution over ARP.TCP/IP ƒ Mapping of Ethernet physical addresses and IP addresses ƒ Each Ethernet card has a built-in Ethernet physical address.. Tei-Wei Kuo. ƒ Linux uses ARP (Address Resolution Protocol) to know and maintain the mapping.g. 2004.

National Taiwan University.g.. e. . Tei-Wei Kuo.TCP/IP A TCP packet TCP header + Data An IP packet IP header Data An Ethernet Ethernet header frame Data • Each IP packet has an indicator of which protocol used. 2004. TCP or UDP 64 * All rights reserved.

Operating-System Structures 4. 65 . Memory Management 10. Process Synchronization 8. National Taiwan University. 2004. File Systems * All rights reserved. CPU Scheduling 7. Computer-System Structures 3. Virtual Memory 11. Threads 6. Tei-Wei Kuo. Introduction 2. Deadlocks 9. Processes 5.Contents 1.

Chapter 3 Operating-System Structures 66 .

. 2004. National Taiwan University. Tei-Wei Kuo.Operating-System Structures ƒ Goals: Provide a way to understand an operating systems ƒ Services ƒ Interface ƒ System Components ƒ The type of system desired is the basis for choices among various algorithms and strategies! 67 * All rights reserved.

2004.System Components – Process Management ƒ Process Management ƒ Process: An Active Entity ƒ Physical and Logical Resources ƒ Memory. Tei-Wei Kuo. etc. And More 68 * All rights reserved. I/O buffers. . Program (code) ƒ Data Structures Representing Current Activities: + Program Counter Stack Data Section CPU Registers …. data. National Taiwan University.

System Components – Process Management ƒ Services ƒ ƒ ƒ ƒ ƒ Process creation and deletion Process suspension and resumption Process synchronization Process communication Deadlock handling 69 * All rights reserved. . 2004. Tei-Wei Kuo. National Taiwan University.

National Taiwan University. 2004. where each has its own address ƒ OS must keep several programs in memory to improve CPU utilization and user response time ƒ Management algorithms depend on the hardware support ƒ Services ƒ Memory usage and availability ƒ Decision of memory assignment ƒ Memory allocation and deallocation 70 * All rights reserved. Tei-Wei Kuo. .System Components – MainMemory Management ƒ Memory: a large array of words or bytes.

records * All rights reserved. 71 . optical disks. Tei-Wei Kuo. bytes. ƒ General Views: ƒ A sequence of bits. National Taiwan University. 2004. lines.System Components – File Management ƒ Goal: ƒ A uniform logical view of information storage ƒ Each medium controlled by a device ƒ Magnetic tapes. etc. magnetic disks. ƒ OS provides a logical storage unit: File ƒ Formats: ƒ Free form or being formatted rigidly.

2004.System Components – File Management ƒ Services ƒ File creation and deletion ƒ Directory creation and deletion ƒ Primitives for file and directory manipulation ƒ Mapping of files onto secondary storage ƒ File Backup * Privileges for file access control * All rights reserved. National Taiwan University. Tei-Wei Kuo. 72 .

2004. National Taiwan University. and spooling system ƒ A general device-driver interface ƒ Drivers 73 * All rights reserved. Tei-Wei Kuo.System Components – I/O System Management ƒ Goal: ƒ Hide the peculiarities of specific hardware devices from users ƒ Components of an I/O System ƒ A buffering. caching. .

g. e. 2004.. . continuous allocation ƒ Disk scheduling.. National Taiwan University. FCFS 74 * All rights reserved. e. Tei-Wei Kuo.g.System Components – Secondary-Storage Management ƒ Goal: ƒ On-line storage medium for programs & data ƒ Backup of main memory ƒ Services for Disk Management ƒ Free-space management ƒ Storage allocation.

network file-system (NFS). 2004. and hypertext transfer protocol (http) 75 * All rights reserved. .System Components – Networking ƒ Issues ƒ Resources sharing ƒ Routing & connection strategies ƒ Contention and security ƒ Network access is usually generalized as a form of file access ƒ World-Wide-Web over file-transfer protocol (ftp). National Taiwan University. Tei-Wei Kuo.

ƒ Services ƒ Detection & controlling mechanisms ƒ Specification mechanisms ƒ Remark: Reliability! 76 * All rights reserved. memory space. etc. National Taiwan University. CPU.System Components – Protection System ƒ Goal ƒ Resources are only allowed to accessed by authorized processes. Tei-Wei Kuo. ƒ Protected Resources ƒ Files. 2004. .

Tei-Wei Kuo.g. Get the next command Execute the command 77 . 2004. National Taiwan University..com in MS-DOS User-friendly? * All rights reserved. UNIX shell and command.System Components – Command-Interpreter system ƒ Command Interpreter ƒ Interface between the user and the operating system ƒ Friendly interfaces ƒ Command-line-based interfaces or mused-based window-and-menu interface ƒ e.

delete. running. 78 . 2004. etc ƒ I/O Operations ƒ General/special operations for devices: ƒ Efficiency & protection ƒ File-System Manipulation ƒ Read. terminating. write. create. National Taiwan University. etc ƒ Communications ƒ Intra-processor or inter-processor communication – shared memory or message passing * All rights reserved.Operation-System Services ƒ Program Execution ƒ Loading. Tei-Wei Kuo.

. memory. devices. National Taiwan University. user programs Æ Ensure correct & consistent computing ƒ Resource Allocation ƒ Utilization & efficiency ƒ Accounting ƒ Protection & Security • user convenience or system efficiency! 79 * All rights reserved. Tei-Wei Kuo.Operation-System Services ƒ Error Detection ƒ Possible errors from CPU. 2004.

National Taiwan University. ƒ Example: read and copy of a file! ƒ Library Calls vs System Calls 80 * All rights reserved. 2004.Operation-System Services ƒ System calls ƒ Interface between processes & OS ƒ How to make system calls? ƒ Assemble-language instructions or subroutine/functions calls in high-level language such as C or Perl? ƒ Generation of in-line instructions or a call to a special run-time routine. Tei-Wei Kuo. .

National Taiwan University.Operation-System Services x register x: parameters for call load address x system call 13 use parameters from table x Code for System Call 13 ƒ How a system call occurs? ƒ Types and information ƒ Parameter Passing ƒ Registers ƒ Registers pointing to blocks ƒ Linux ƒ Stacks 81 * All rights reserved. 2004. . Tei-Wei Kuo.

. Tei-Wei Kuo.Operation-System Services ƒ System Calls ƒ ƒ ƒ ƒ ƒ Process Control File Management Device Management Information Maintenance Communications 82 * All rights reserved. 2004. National Taiwan University.

Operation-System Services ƒ Process & Job Control ƒ End (normal exit) or abort (abnormal) ƒ Error level or no ƒ Load and execute ƒ How to return control? ƒ e. shell load & execute commands ƒ Creation and/or termination of processes ƒ Multiprogramming? 83 * All rights reserved.g.. . Tei-Wei Kuo. National Taiwan University. 2004.

2004. National Taiwan University. memory allocation & de-allocation 84 * All rights reserved.Operation-System Services ƒ Process & Job Control (continued) ƒ Process Control ƒ Get or set attributes of processes ƒ Wait for a specified amount of time or an event ƒ Signal event ƒ Memory dumping. Tei-Wei Kuo. profiling. tracing. .

National Taiwan University. . Tei-Wei Kuo. 2004.Operation-System Services ƒ Examples: MS-DOS & UNIX free memory process command interpreter kernel process A interpreter free memory process B kernel 85 * All rights reserved.

rewinding) ƒ lseek ƒ Get or set attributes of files ƒ Operations for directories 86 * All rights reserved.g.Operation-System Services ƒ File Management ƒ Create and delete ƒ Open and close ƒ Read. 2004. Tei-Wei Kuo. . and reposition (e.. National Taiwan University. write.

and reposition (e. 2004. . Tei-Wei Kuo. rewinding) ƒ Get or set file attributes ƒ Logically attach or detach devices 87 * All rights reserved..g. write. ƒ Read. National Taiwan University.Operation-System Services ƒ Device management ƒ Request or release ƒ Open and close of special files ƒ Files are abstract or virtual devices.

Operation-System Services ƒ Information maintenance ƒ Get or set date or time ƒ Get or set system data. close. 2004. such as the amount of free memory ƒ Communication ƒ Message Passing ƒ Open. accept connections ƒ Host ID or process ID ƒ Send and receive messages ƒ Transfer status information ƒ Shared Memory ƒ Memory mapping & process synchronization88 * All rights reserved. National Taiwan University. . Tei-Wei Kuo.

Operation-System Services ƒ Shared Memory ƒ Max Speed & Comm Convenience ƒ Message Passing ƒ No Access Conflict & Easy Implementation Process A M Process B M Process A Shared Memory Process B kernel M kernel 89 * All rights reserved. Tei-Wei Kuo. 2004. . National Taiwan University.

g. compilers. e. ƒ Programming Language Supports. Tei-Wei Kuo. e. e. Status information. ƒ Communications.g. e. telnet. File Modifications.. rm. editors.System Programs ƒ Goal: ƒ Provide a convenient environment for program development and execution ƒ Types ƒ ƒ ƒ ƒ File Management.g. 90 . loader.g. date. e.. e.g... * All rights reserved. National Taiwan University.g. Program Loading and Executions. 2004...

. 2004. Tei-Wei Kuo. National Taiwan University.System Programs – Command Interpreter ƒ Two approaches: ƒ Contain codes to execute commands ƒ Fast but the interpreter tends to be big! ƒ Painful in revision! del cd 91 * All rights reserved.

. Parameter Passing ƒ Potential Hazard: virtual memory b. National Taiwan University. Inconsistent Interpretation of Parameters 92 * All rights reserved. Tei-Wei Kuo. 2004.System Programs – Command Interpreter ƒ Implement commands as system programs Æ Search exec files which corresponds to commands (UNIX) ƒ Issues a. Being Slow c.

Tei-Wei Kuo.System Structure – MS-DOS ƒ MS-DOS Layer Structure Application program Resident system program MS-DOS device drivers ROM BIOS device drivers 93 * All rights reserved. National Taiwan University. 2004. .

. etc. Kernel interface to the hardware terminal controller. virtual memory. swapping. paging. Tei-Wei Kuo. compilers. terminals. CPU scheduling. X. National Taiwan University. physical memory. 2004. signal handling. disk drivers. file system. devices such as disks. device controller. caching/buffering. UNIX 94 * All rights reserved. application programs. etc. memory. etc.System Structure – UNIX User interface System call interface useruser user user user user user Shells.

Tei-Wei Kuo. less efficiency due to overheads! * All rights reserved.System Structure ƒ A Layered Approach – A Myth new ops existing ops Layer M hidden ops Layer M-1 Advantage: Modularity ~ Debugging & Verification Difficulty: Appropriate layer definitions. National Taiwan University. 2004. 95 .

2004.System Structure ƒ A Layer Definition Example: L5 L4 L3 L2 L1 L0 User programs I/O buffering Operator-console device driver Memory management CPU scheduling Hardware 96 * All rights reserved. National Taiwan University. . Tei-Wei Kuo.

97 .System Structure – OS/2 ƒ OS/2 Layer Structure Application Application Application Application-program Interface Subsystem Subsystem Subsystem System kernel ‧memory management ‧task scheduling ‧device management Device driver Device driver Device driver * Some layers of NT were from user space to kernel space in NT4.0 * All rights reserved. National Taiwan University. 2004. Tei-Wei Kuo.

National Taiwan University. security 98 * All rights reserved. Tei-Wei Kuo. ƒ Moving all nonessential components from the kernel to the user or system programs! ƒ No consensus on services in kernel ƒ Mostly on process and memory management and communication ƒ Benefits: ƒ Ease of OS service extensions Æ portability.System Structure – Microkernels ƒ The concept of microkernels was proposed in CMU in mid 1980s (Mach). 2004. reliability. .

99 . HW interrupts. MacOS X (Mach kernel). Tei-Wei Kuo. QNX (msg passing. proc scheduling. National Taiwan University.System Structure – Microkernels ƒ Examples ƒ Microkernels: True64UNIX (Mach kernel). low-level networking) ƒ Hybrid structures: Windows NT Win32 Applications Win32 Server OS/2 Applications OS/2 Server POSIX Applications POSIX Server kernel * All rights reserved. 2004.

virtual machine implementation hardware 100 . 2004. National Taiwan University.Virtual Machine ƒ Virtual Machines: provide an interface that is identical to the underlying bare hardware processes processes processes processes interface kernel kernel VM1 kernel VM2 kernel VM3 hardware * All rights reserved. Tei-Wei Kuo.

National Taiwan University. Disk Systems ƒ An IBM minidisk approach ƒ User/Monitor Modes ƒ (Physical) Monitor Mode ƒ Virtual machine software ƒ (Physical) User Mode ƒ Virtual monitor mode & Virtual user mode 101 * All rights reserved.Virtual Machine ƒ Implementation Issues: ƒ Emulation of Physical Devices ƒ E. ..g. Tei-Wei Kuo. 2004.

National Taiwan University.Virtual Machine virtual user mode P1/VM1 system call processes processes processes Trap Service for the system call virtual monitor kernel 1 kernel 2 kernel 3 mode monitor mode virtual machine software hardware Finish service Restart VM1 Set program counter & register contents. Tei-Wei Kuo. 2004. & then restart VM1 Simulate the effect of the I/O instruction time 102 * All rights reserved. .

.Virtual Machine ƒ Disadvantages: ƒ Slow! ƒ Execute most instructions directly on the hardware ƒ No direct sharing of resources ƒ Physical devices and communications * I/O could be slow (interpreted) or fast (spooling) 103 * All rights reserved. Tei-Wei Kuo. 2004. National Taiwan University.

. such as Mach (software emulation) ƒ Emulations of Machines and OS’s.g. National Taiwan University. e..Virtual Machine ƒ Advantages: ƒ Complete Protection – Complete Isolation! ƒ OS Research & Development ƒ System Development Time ƒ Extensions to Multiple Personalities. Windows over Linux 104 * All rights reserved. Tei-Wei Kuo. 2004.

Virtual Machine – Java java .class files) ƒ Class verifier ƒ Java interpreter ƒ An interpreter.class files ƒ Sun Microsystems in late 1995 ƒ Java Language and API Library ƒ Java Virtual Machine (JVM) class loader verifier java interpreter ƒ Class loader (for bytecode . Tei-Wei Kuo. National Taiwan University. a just-in-time (JIT) compiler. 2004. hardware 105 host system * All rights reserved. .

National Taiwan University. 2004.class files ƒ JVM ƒ Garbage collection ƒ Reclaim unused objects class loader verifier java interpreter ƒ Implementation being specific for different systems ƒ Programs are architecture neutral and portable host system 106 * All rights reserved.Virtual Machine – Java java . Tei-Wei Kuo. .

e.System Design & Implementation ƒ Design Goals & Specifications: ƒ User Goals. Macintosh OS 107 . 2004. e.g. National Taiwan University.. Tei-Wei Kuo.. ease of use ƒ System Goals. reliable ƒ Rule 1: Separation of Policy & Mechanism ƒ Policy:What will be done? ƒ Mechanism:How to do things? ƒ Example: timer construct and time slice ƒ Two extreme cases: Microkernel-based OS * All rights reserved.g.

etc. National Taiwan University.System Design & Implementation ƒ OS Implementation in High-Level Languages ƒ E. .g. 2004. UNIX.. more compact. and portable ƒ Disadvantages: ƒ Less efficient but more storage for code * All rights reserved. Tei-Wei Kuo. OS/2. ƒ Advantages: ƒ Being easy to understand & debug ƒ Being written fast. exploring of excellent algorithms. MS NT. 108 etc. * Tracing for bottleneck identification.

device. etc. Generality. National Taiwan University. . 2004. memory. No recompilation & completely Linking of table-driven modules for selected OS Recompilation of a modified source code ƒ Issues ƒ Size. Tei-Wei Kuo.System Generation ƒ SYSGEN (System Generation) ƒ Ask and probe for information concerning the specific configuration of a hardware system ƒ CPU. Ease of modification 109 * All rights reserved. OS options.

Threads 6. 110 . File Systems * All rights reserved. CPU Scheduling 7. Operating-System Structures 4. Tei-Wei Kuo.Contents 1. Deadlocks 9. Memory Management 10. Computer-System Structures 3. National Taiwan University. Process Synchronization 8. Virtual Memory 11. Introduction 2. 2004. Processes 5.

Chapter 4 Processes 111 .

Processes ƒ Objective: ƒ Process Concept & Definitions ƒ Process Classification: ƒ Operating system processes executing system code ƒ User processes executing system code ƒ User processes executing user code 112 * All rights reserved. 2004. National Taiwan University. . Tei-Wei Kuo.

. Tei-Wei Kuo.pagedaemon responsible for paging ƒ Kernel process * All rights reserved. 113 . National Taiwan University. (/etc/rc* & init or /sbin/rc* & init) ƒ User process with superuser privileges ƒ PID 2 . the scheduler) ƒ Kernel process ƒ No program on disks correspond to this process ƒ PID 1 – init responsible for bringing up a Unix system after the kernel has been bootstrapped. 2004.Processes ƒ Example: Special Processes in Unix ƒ PID 0 – Swapper (i.e.

National Taiwan University.Processes ƒ Process ƒ A Basic Unit of Work from the Viewpoint of OS ƒ Types: ƒ Sequential processes: an activity resulted from the execution of a program by a processor ƒ Multi-thread processes ƒ An Active Entity ƒ Program Code – A Passive Entity ƒ Stack and Data Segments ƒ The Current Activity ƒ PC. Contents in the Stack and Data Segments 114 * All rights reserved. Registers . 2004. . Tei-Wei Kuo.

National Taiwan University. Tei-Wei Kuo. 2004. I/O or event wait 115 .Processes ƒ Process State new terminated admitted interrupt scheduled exit ready I/O or event completion running waiting * All rights reserved.

Tei-Wei Kuo. National Taiwan University. . 2004.Processes ƒ Process Control Block (PCB) ƒ ƒ ƒ ƒ ƒ ƒ ƒ Process State Program Counter CPU Registers CPU Scheduling Information Memory Management Information Accounting Information I/O Status Information 116 * All rights reserved.

Processes ƒ PCB: The repository for any information that may vary from process to process PCB[] 0 1 2 pointer process state pc register NPROC-1 117 * All rights reserved. 2004. National Taiwan University. . Tei-Wei Kuo.

statistics accounting. 118 * All rights reserved. 2004. ƒ pid. . state.u ƒ Things the system should know when process is running ƒ signal disposition. etc. National Taiwan University. timer counters.Processes ƒ Process Control Block (PCB) – An Unix Example ƒ proc[i] ƒ Everything the system must know when the process is swapped out. priority. ƒ . Tei-Wei Kuo. etc. files[].

u argv.… user stack heap Data Segment page table u_proc Red Zone sp proc[i] entry p_p0br Code Segment PC 119 * All rights reserved. argc. 2004.Processes ƒ Example: 4. . National Taiwan University. Tei-Wei Kuo.3BSD text structure x_caddr p_textp p_addr per-process kernel stack .

4BSD process grp file descriptors per-process kernel stack .… user stack region lists heap Data Segment Code Segment u_proc 120 … proc[i] entry VM space p_addr p_p0br page table * All rights reserved. argc.u argv. Tei-Wei Kuo. 2004. National Taiwan University. .Processes ƒ Example: 4.

Tei-Wei Kuo. 2004.Process Scheduling ƒ The goal of multiprogramming ƒ Maximize CPU/resource utilization! ƒ The goal of time sharing ƒ Allow each user to interact with his/her program! PCB1 ready queue disk unit 0 tape unit 1 PCB2 head tail head tail head tail PCB3 121 * All rights reserved. . National Taiwan University.

fork a child wait for an interrupt 122 . 2004.Process Scheduling – A Queueing Diagram ready queue I/O I/O queue dispatch CPU I/O request time slice expired child terminate child executes interrupt occurs * All rights reserved. National Taiwan University. Tei-Wei Kuo.

May not exist physically 123 * All rights reserved. Can take more time in selecting processes because of a longer interval between executions 3. Control the degree of multiprogramming 2.Process Scheduling – Schedulers ƒ Long-Term (/Job) Scheduler CPU Job pool Memory ƒ Goal: Select a good mix of I/O-bound and CPU-bound process ƒ Remarks: 1. National Taiwan University. . Tei-Wei Kuo. 2004.

2004. . National Taiwan University.Process Scheduling – Schedulers ƒ Short-Term (/CPU) Scheduler ƒ Goal:Efficiently allocate the CPU to one of the ready processes according to some criteria. ƒ Mid-Term Scheduler ƒ Swap processes in and out memory to control the degree of multiprogramming 124 * All rights reserved. Tei-Wei Kuo.

g. ƒ Issues: ƒ The cost depends on hardware support ƒ e. are introduced to break this bottleneck! * All rights reserved.. ƒ The context of a process is usually reflected in PCB and others. i. 125 . Tei-Wei Kuo.Process Scheduling – Context Switches ƒ Context Switch ~ Pure Overheads ƒ Save the state of the old process and load the state of the newly scheduled process.u in Unix. ƒ Threads. .g. light-weight process (LWP). National Taiwan University.. e.e. processes with multiple register sets or computers with advanced memory management. 2004.

2004. Tei-Wei Kuo.Operations on Processes ƒ Process Creation & Termination ƒ Restrictions on resource usage ƒ Passing of Information ƒ Concurrent execution root pagedaemon swapper user1 * All rights reserved. National Taiwan University. init user2 user3 126 .

except the returned value from fork(): ƒ Child returns with a value 0 ƒ Parent returns with process id of child ƒ No shared data structures between parent and child – Communicate via shared files. Tei-Wei Kuo. pipes.Operations on Processes ƒ Process Duplication ƒ A copy of parent address space + context is made for child. . etc. National Taiwan University. 2004. ƒ Use execve() to load a new program ƒ fork() vs vfork() (Unix) 127 * All rights reserved.

“Fork Failed”). “ls”. NULL). } else { /* parent process */ wait(NULL). } * All rights reserved. 128 . National Taiwan University. Tei-Wei Kuo.Operations on Processes ƒ Example: … if ( pid = fork() ) == 0) { /* child process */ execlp(“/bin/ls”. } else if (pid < 0) { fprintf(stderr. exit(-1). 2004.

Tei-Wei Kuo. ƒ Cascading Termination 129 * All rights reserved. wait. . signal.Operations on Processes ƒ Termination of Child Processes ƒ Reasons: ƒ Resource usages. abort. ƒ Kill. 2004. needs. National Taiwan University. etc. exit. etc.

National Taiwan University. e. multiple work 130 * All rights reserved. 2004.g.g. e.g. Tei-Wei Kuo. functionality dividing ƒ Convenience. ... parallelism.Cooperating Processes ƒ Cooperating processes can affect or be affected by the other processes ƒ Independent Processes ƒ Reasons: ƒ Information Sharing.. ƒ Modularity..g. files ƒ Computation Speedup. e. e.

2004. Tei-Wei Kuo.Cooperating Processes ƒ A Consumer-Producer Example: ƒ Bounded buffer or unbounded buffer ƒ Supported by inter-process communication (IPC) or by hand coding 2 1 buffer[0…n-1] 0 Initially. . National Taiwan University. n-1 in z out in=out=0; n-2 131 * All rights reserved.

} 132 * All rights reserved. /* do nothing */ buffer[ in ] = nextp. 2004. . Tei-Wei Kuo. in = (in+1) % BUFFER_SIZE.Cooperating Processes Producer: while (1) { /* produce an item nextp */ while (((in+1) % BUFFER_SIZE) == out) . National Taiwan University.

/* do nothing */ nextc = buffer[ out ]. Tei-Wei Kuo.Cooperating Processes Consumer: while (1) { while (in == out) . . National Taiwan University. 2004. out = (out+1) % BUFFER_SIZE . /* consume the item in nextc */ } 133 * All rights reserved.

.Interprocess Communication ƒ Why Inter-Process Communication (IPC)? ƒ Exchanging of Data and Control Information! ƒ Why Process Synchronization? ƒ Protect critical sections! ƒ Ensure the order of executions! 134 * All rights reserved. Tei-Wei Kuo. 2004. National Taiwan University.

. automatic/explicit buffering. National Taiwan University. direct/indirect communication. 2004. etc. symmetric/asymmetric communication. 135 * All rights reserved. Tei-Wei Kuo. send by copy or reference.Interprocess Communication ƒ IPC ƒ Shared Memory ƒ Message Passing ƒ Logical Implementation of Message Passing ƒ Fixed/variable msg size.

2004.Interprocess Communication ƒ Classification of Communication by Naming ƒ Processes must have a way to refer to each other! ƒ Types ƒ Direct Communication ƒ Indirect Communication 136 * All rights reserved. Tei-Wei Kuo. National Taiwan University. .

Tei-Wei Kuo. National Taiwan University.Interprocess Communication – Direct Communication ƒ Process must explicitly name the recipient or sender of a communication ƒ Send(P. Two processes per a link c. One link per pair of processes d. . 2004. Bidirectional or unidirectional 137 * All rights reserved. msg) ƒ Properties of a Link: a. b. Communication links are established automatically. Receive(Q. msg).

Interprocess Communication – Direct Communication ƒ Issue in Addressing: ƒ Symmetric or asymmetric addressing Send(P. Receive(id. msg). 2004. National Taiwan University. msg) ƒ Difficulty: ƒ Process naming vs modularity 138 * All rights reserved. . Tei-Wei Kuo.

National Taiwan University. Bidirectional or unidirectional * All rights reserved. msg)=> A A =>receive(A. n processes per link for n >= 1. Tei-Wei Kuo. 2004. n links can exist for a pair of processes for n >=1. 2. 3.Interprocess Communication – Indirect Communication ƒ Two processes can communicate only if the process share a mailbox (or ports) send(A. msg) ƒ Properties: 1. . A link is established between a pair of processes only if they share a mailbox. 139 4.

Owners vs Users ƒ Process Æ owner as the sole recipient? ƒ OS Æ Let the creator be the owner? Privileges can be passed? Garbage collection is needed? 140 * All rights reserved. National Taiwan University. . Who is the recipient of a message? P1 P2 msgs ? P3 b. Tei-Wei Kuo. 2004.Interprocess Communication – Indirect Communication ƒ Issues: a.

Interprocess Communication – Synchronization ƒ Blocking or Nonblocking (Synchronous versus Asynchronous) ƒ ƒ ƒ ƒ Blocking send Nonblocking send Blocking receive Nonblocking receive ƒ Rendezvous – blocking send & receive 141 * All rights reserved. 2004. National Taiwan University. Tei-Wei Kuo. .

ƒ ƒ ƒ ƒ Zero capacity(no buffering) ƒ ƒ Msg transfer must be synchronized – rendezvous! Sender can continue execution without waiting till the link is full Sender is never delayed! Bounded capacity Unbounded capacity ƒ The last two items are for asynchronous communication and may need acknowledgement 142 * All rights reserved. .Interprocess Communication – Buffering ƒ The Capacity of a Link = the # of messages could be held in the link. National Taiwan University. 2004. Tei-Wei Kuo.

Interprocess Communication – Buffering ƒ Special cases: a. . Tei-Wei Kuo. Senders are blocked until the receivers have received msgs and replied by reply msgs Æ A Remote Procedure Call (RPC) framework 143 * All rights reserved. Msgs may be lost if the receiver can not catch up with msg sending Æ synchronization b. 2004. National Taiwan University.

2004. 144 * All rights reserved. Tei-Wei Kuo. No capacity Æ sender is blocked.Interprocess Communication – Exception Conditions ƒ Process termination a. BufferingÆ messages are accumulated. . b. National Taiwan University. Receiver Termination a. Sender TerminationÆ Notify or terminate the receiver! b.

such as CRC. . Sender detects & resends messages. National Taiwan University. inside messages & resend them as necessary! 145 * All rights reserved. Tei-Wei Kuo. ƒ Issues: a. OS detects & notify the sender to handle it. such as timeout! b. Distinguish multiple copies if retransmitting is possible ƒ Scrambled Messages: ƒ Usually OS adds checksums. Detecting methods. 2004.Interprocess Communication – Exception Conditions ƒ Ways to Recover Lost Messages (due to hardware or network failure): ƒ ƒ ƒ OS detects & resends messages.

National Taiwan University. ƒ The Kernel mailbox is used by the kernel to communication with the tasks ƒ The Notify mailbox is used by the kernel sends notification of event occurrences. 146 * All rights reserved. .Mach ƒ Mach – A message-based OS from the Carnegie Mellon University ƒ When a task is created. are also created. 2004.Example . called ports. two special mailboxes. Tei-Wei Kuo.

c. a. b.Example . 2004. Tei-Wei Kuo.Mach ƒ Three system calls for message transfer: ƒ msg_send: ƒ a. 147 . d. A cached message per sending thread for a mailbox * One task can either own or receive from a mailbox. * All rights reserved. Options when mailbox is full: Wait indefinitely Return immediately Wait at most for n ms Temporarily cache a message. National Taiwan University.

2004. Wait at most for n ms ƒ msg_rpc ƒ Remote Procedure Calls 148 * All rights reserved. Return immediately c. Tei-Wei Kuo. National Taiwan University. Only one task can own & have a receiving privilege of it * options when mailbox is empty: a. .Mach ƒ msg_receive ƒ To receive from a mailbox or a set of mailboxes. Wait indefinitely b.Example .

e. # of msgs in a link ƒ All messages have the same priority and are served in a FIFO fashion.Mach ƒ port_allocate ƒ create a mailbox (owner) ƒ port_status ~ .Example . National Taiwan University.g. 149 * All rights reserved. Tei-Wei Kuo. . ƒ Message Size ƒ A fixed-length head + a variable-length data + two mailbox names ƒ Message copying: message copying Æ remapping of addressing space ƒ System calls are carried out by messages. 2004.

and returns the handle to one of them to the client. . 2. The client and server use the corresponding port handle to send messages or callbacks and to listen for replies. The client sends a connection request. The client opens a handle to a subsystem’s connection port object. Tei-Wei Kuo. 3. 4. 2004. 150 * All rights reserved.Example – Windows 2000 ƒ Local Procedure Call (LPC) – Message Passing on the Same Processor 1. The server creates two private communication ports. National Taiwan University.

2004. National Taiwan University. Tei-Wei Kuo. . 151 * All rights reserved.Example – Windows 2000 ƒ Three Types of Message Passing Techniques ƒ Small messages ƒ Message copying ƒ Large messages – section object ƒ To avoid memory copy ƒ Sending and receiving of the pointer and size information of the object ƒ A callback mechanism ƒ When a response could not be made immediately.

National Taiwan University. Tei-Wei Kuo. 21-ftp.19.86. etc.25.8:80 152 * /etc/services: Port # under 1024 ~ 23-telnet. 2004.2:1652 161.Communication in ClientServer Systems ƒ Socket ƒ An endpoint for communication identified by an IP address concatenated with a port number Host X Socket Socket ƒ A client-server architecture Web server Socket Socket 146.86. 80-web server.19.5. .2:1652 146.8:80 161. * All rights reserved.5.25.

National Taiwan University. bin = new BufferReader(new InputStreamReader(in)). 153 Server * All rights reserved. sock = new ServerSocket(5155).Communication in ClientServer Systems ƒ Three types of sockets in Java ƒ Connection-oriented (TCP) – Socket class ƒ Connectionless (UDP) – DatagramSocket class ƒ MulticastSocket class – DatagramSocket subclass Client sock = new Socket(“127. client.Date().close(). . pout. pout = new PrintWriter(client. true).5155).0. 2004. … sock.getOutputStream().close().getInputStream(). … client = sock.close(). … in = sock.accept().util.0.toString()).1”. Tei-Wei Kuo. … Pout.println(new java.

parameters to pack. 154 * All rights reserved. identifiers of functions to call. Tei-Wei Kuo. etc. . ƒ Needs: ƒ Ports to listen from the RPC daemon site and to return results. National Taiwan University. 2004.Communication in ClientServer Systems ƒ Remote Procedure Call (RPC) ƒ A way to abstract the procedure-call mechanism for use between systems with network connection. ƒ Stubs at the client site ƒ One for each RPC ƒ Locate the proper port and marshall parameters.

National Taiwan University. 155 .Communication in ClientServer Systems ƒ Needs (continued) ƒ Stubs at the server site ƒ Receive the message ƒ Invoke the procedure and return the results. Tei-Wei Kuo. ƒ Issues for RPC ƒ Data representation ƒ External Data Representation (XDR) ƒ Parameter marshalling ƒ Semantics of a call ƒ History of all messages processed ƒ Binding of the client and server port ƒ Matchmaker – a rendezvous mechanism * All rights reserved. 2004.

Port: kernel Re: port P to X Matchmaker replies to client with port P Daemon listens to port P and receives msg Daemon processes request and sends output Port: P <contents> Port: kernel <output> 156 .Communication in ClientServer Systems Client Call kernel to send RPC msg to Procedure X Kernel sends msg to matchmaker Messages Port: matchaker Re: addr. National Taiwan University. to X Server Matchmaker receives msg Kernel places port P in usr RPC msg Kernel sends RPC Kernel receives reply and passes to user * All rights reserved. Tei-Wei Kuo. 2004.

status. . delete. 2004.Communication in ClientServer Systems ƒ An Example for RPC ƒ A Distributed File System (DFS) ƒ A set of RPC daemons and clients ƒ DFS port on a server on which a file operation is to take place: ƒ Disk operations: read. National Taiwan University. etc – corresponding to usual system calls 157 * All rights reserved. Tei-Wei Kuo. write.

National Taiwan University. . Tei-Wei Kuo. etc. ƒ Skeleton – for the unmarshalling of parameters and invocation of the method and the sending of a parcel back 158 * All rights reserved. ƒ boolean val = Server.Communication in ClientServer Systems ƒ Remote Method Invocation (RMI) ƒ Allow a thread to invoke a method on a remote object. 2004.someMethod(A.B) ƒ Implementation ƒ Stub – a proxy for the remote object ƒ Parcel – a method name and its marshalled parameters.

Serializable 159 * All rights reserved. National Taiwan University. Tei-Wei Kuo. . 2004.io.Communication in ClientServer Systems ƒ Parameter Passing ƒ Local (or Nonremote) Objects ƒ Pass-by-copy – an object serialization ƒ Remote Objects – Reside on a different Java virtual machine (JVM) ƒ Pass-by-reference ƒ Implementation of the interface – java.

160 . CPU Scheduling 7. National Taiwan University. Introduction 2. Threads 6. Virtual Memory 11. File Systems * All rights reserved.Contents 1. Process Synchronization 8. 2004. Tei-Wei Kuo. Computer-System Structures 3. Memory Management 10. Processes 5. Deadlocks 9. Operating-System Structures 4.

Chapter 5 Threads 161 .

Threads ƒ Objectives: ƒ Concepts and issues associated with multithreaded computer systems. program counter. . 2004. a register set. National Taiwan University. Tei-Wei Kuo. ƒ Thread – Lightweight process(LWP) ƒ a basic unit of CPU utilization ƒ A thread ID. and a stack space ƒ Process – heavyweight process ƒ A single thread of control 162 * All rights reserved.

Tei-Wei Kuo. 2004. National Taiwan University. ƒ Displaying ƒ Keystroke reading ƒ Spelling and grammar checking ƒ A web server ƒ Clients’ services ƒ Request listening 163 .Threads code segment ƒ Motivation ƒ A web browser ƒ Data retrieval ƒ Text/image displaying ƒ A word processor stack stack stack registers registers registers data segment files files * All rights reserved.

2004.Threads ƒ Benefits ƒ Responsiveness ƒ Resource Sharing ƒ Economy ƒ Creation and context switching ƒ 30 times slower in process creation in Solaris 2 ƒ 5 times slower in process context switching in Solaris 2 ƒ Utilization of Multiprocessor Architectures 164 * All rights reserved. . Tei-Wei Kuo. National Taiwan University.

Tei-Wei Kuo. ƒ Examples: ƒ Advantages ƒ Disadvantages ƒ Blocking of a thread in executing a system call can block the entire process. ƒ POSIX Pthreads.User-Level Threads ƒ User-level threads are implemented by a thread library at the user level. Solaris 2 UI-threads ƒ Context switching among them is extremely fast . 165 * All rights reserved. 2004. Mach C-threads. National Taiwan University.

2. Tei-Wei Kuo.Kernel-Level Threads ƒ Kernel-level threads are provided a set of system calls similar to those of processes ƒ Examples ƒ Windows 2000. True64UNIX ƒ Blocking of a thread will not block its entire task. 166 * All rights reserved. National Taiwan University. Solaris ƒ Advantage ƒ Disadvantage ƒ Context switching cost is a little bit higher because the kernel must do the switching. 2004. .

ƒ No parallelism for multiple processors ƒ Example: Green threads for Solaris 2 167 * All rights reserved.Multithreading Models ƒ Many-to-One Model ƒ Many user-level threads to one kernel thread ƒ Advantage: k ƒ Efficiency ƒ Disadvantage: ƒ One blocking system call blocks all. . National Taiwan University. 2004. Tei-Wei Kuo.

ƒ Example: Windows NT. Tei-Wei Kuo. ƒ Disadvantage: Overheads in creating a kernel thread. k . National Taiwan University. OS/2 168 * All rights reserved. 2004.Multithreading Models ƒ One-to-One Model ƒ One user-level thread to one kernel thread ƒ Advantage: One system call blocks one thread. Windows 2000.

2004.Tru64 UNIX 169 * All rights reserved. IRIX. . HPUX.Multithreading Models ƒ Many-to-Many Model ƒ Many user-level threads to many kernel threads ƒ Advantage: k k k ƒ A combination of parallelism and efficiency ƒ Example: Solaris 2. National Taiwan University. Tei-Wei Kuo.

National Taiwan University. 2004.Threading Issues ƒ Fork and Exec System Calls ƒ Fork: Duplicate all threads or create a duplicate with one thread? ƒ Exec: Replace the entire process. including all threads and LWPs. Tei-Wei Kuo. . ƒ Fork Æ exec? 170 * All rights reserved.

Threading Issues
ƒ Thread Cancellation
ƒ Target thread ƒ Two scenarios:
ƒ Asynchronous cancellation ƒ Deferred cancellation
ƒ Cancellation points in Pthread.

ƒ Difficulty
ƒ Resources have been allocated to a cancelled thread. ƒ A thread is cancelled while it is updating data. 171
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Threading Issues
ƒ Signal Handling
ƒ Signal
ƒ Synchronous – delivered to the same process that performed the operation causing the signal,
ƒ e.g., illegal memory access or division by zero

ƒ Asynchronous
ƒ e.g., ^C or timer expiration

ƒ Default or user-defined signal handler ƒ Signal masking
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

172

Threading Issues
ƒ Delivery of a Signal
ƒ To the thread to which the signal applies
ƒ e.g., division-by-zero

ƒ To every thread in the process
ƒ e.g., ^C

ƒ To certain threads in the process ƒ Assign a specific thread to receive all threads for the process
ƒ Solaris 2

ƒ Asynchronous Procedure Calls (APCs)
ƒ To a particular thread rather than a process 173
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Threading Issues
ƒ Thread Pools
ƒ Motivations
ƒ Dynamic creation of threads ƒ Limit on the number of active threads

ƒ Awake and pass a request to a thread in the pool ƒ Benefits
ƒ Faster for service delivery and limit on the # of threads

ƒ Dynamic or static thread pools
174 ƒ Thread-specific data – Win32 & Pthreads
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Pthreads
ƒ Pthreads (IEEE 1003.1c)
ƒ API Specification for Thread Creation and Synchronization ƒ UNIX-Based Systems, Such As Solaris 2.

ƒ User-Level Library ƒ Header File: <pthread.h> ƒ pthread_attr_init(), pthread_create(), pthread_exit(), pthread_join(), etc.
175
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Pthreads
#include <pthread.h> main(int argc, char *argv[]) { … pthread_attr_init(&attr); pthread_create(&tid, &attr, runner, argv[1]); pthread_join(tid, NULL); …} void *runner(void *param) { int i, upper = atoi(param), sum = 0; if (upper > 0) for(i=1;i<=upper,i++) sum+=i; pthread_exit(0); }
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

176

Solaris 2

kernel

ƒ Implementation user-level of Pthread API thread in addition to light weight supporting process user-level kernel threads threads with a library for thread creation and CPU management.
177

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Solaris 2
ƒ Many-to-Many Model
ƒ Each process has at least one LWP
ƒ Each LWP has a kernel-level thread

ƒ User-level threads must be connected to LWPs to accomplish work.
ƒ A bound user-level thread ƒ An unbound thread

ƒ Some kernel threads running on the kernel’s behalf have no associated LWPs – system threads
178
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Solaris 2
ƒ Processor Allocation:
ƒ Multiprogramming or Pinned

ƒ Switches of user-level threads among LWPs do not need kernel intervention. ƒ If the kernel thread is blocked, so does the LWP and its user-level threads.
ƒ Dynamic adjustment of the number of LWPs 179
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

SP). National Taiwan University. and priority – in user space ƒ A LWP ƒ A Register set for the running userlevel thread – in kernel space LWP1 LWP2 ƒ A Kernel thread ƒ A copy of the kernel registers. scheduling information 180 Solaris 2 Process * All rights reserved.Solaris 2 Process ID Mem Map Priority Open Files ƒ Data Structures ƒ A User-Level Thread ƒ A Thread ID. priority. a register set (including PC. Tei-Wei Kuo. stack. . 2004. a pointer to its LWP.

. National Taiwan University. Tei-Wei Kuo. A Kernel Stack. and A Private Storage Space 181 * All rights reserved. A User Stack.Windows 2000 ƒ Win32 API ƒ One-to-One Model ƒ Fiber Library for the M:M Model ƒ A Thread Contains ƒ A Thread ID ƒ Context: A Register Set. 2004.

a kernel stack. the address of the starting routine ƒ KTHREAD (kernel thread block) ƒ Scheduling and synchronization information. a ptr to TEB User Space ƒ TEB (thread environment block) ƒ A user stack. Tei-Wei Kuo. . 2004.Windows 2000 ƒ Data Structures ƒ ETHREAD (executive thread block) Kernel Space ƒ A ptr to the process. National Taiwan University. an array for threadspecific data. 182 * All rights reserved.a ptr to KTHREAD.

virtual memory.Linux ƒ Threads introduced in Version 2. . 2004. pointers to the same data structures for open files. ƒ Flag setting in clone() invocation. National Taiwan University. ƒ Pthread implementations 183 * All rights reserved. etc. signal handling.g. e.. Tei-Wei Kuo.2 ƒ clone() versus fork() ƒ Term task for process& thread ƒ Several per-process data structures.

making the thread eligible to be run by the JVM.Java ƒ Thread Support at the Language Level ƒ Mapping of Java Threads to Kernel Threads on the Underlying OS? ƒ Windows 2000: 1:1 Model ƒ Thread Creation ƒ Create a new class derived from the Thread class ƒ Run its start method ƒ Allocate memory and initialize a new thread in the JVM ƒ start() calls the run method. 2004. National Taiwan University. Tei-Wei Kuo. 184 * All rights reserved. .

start(). thrd. Tei-Wei Kuo.Java class Summation extends Thread { public Summation(int n) { upper = n. 185 . National Taiwan University. …} * All rights reserved. 2004. } public void run() { int sum = 0.ParseInt(args[0])). …} …} public class ThreadTester {… Summation thrd = new Summation(Integer.

Operating-System Structures 4. Memory Management 10. Virtual Memory 11. Process Synchronization 8. Deadlocks 9. Introduction 2. CPU Scheduling 7. Processes 5. Threads 6. Computer-System Structures 3. National Taiwan University. File Systems * All rights reserved. Tei-Wei Kuo. 2004. 186 .Contents 1.

Chapter 6 CPU Scheduling 187 .

. 2004. National Taiwan University. Tei-Wei Kuo.CPU Scheduling ƒ Objective: ƒ Basic Scheduling Concepts ƒ CPU Scheduling Algorithms ƒ Why Multiprogramming? ƒ Maximize CPU/Resources Utilization (Based on Some Criteria) 188 * All rights reserved.

I/O-Burst 189 . 2004. Tei-Wei Kuo.CPU Scheduling ƒ Process Execution ƒ CPU-bound programs tend to have a few very long CPU bursts. CPU-Burst New Terminate * All rights reserved. ƒ IO-bound programs tend to have many very short CPU bursts. National Taiwan University.

National Taiwan University.CPU Scheduling ƒ The distribution can help in selecting an appropriate CPU-scheduling algorithms 120 100 60 20 8 * All rights reserved. frequency 24 16 Burst Duration (ms) 190 . Tei-Wei Kuo. 2004.

CPU Scheduling ƒ CPU Scheduler – The Selection of Process for Execution ƒ A short-term scheduler New dispatched Ready Waiting 191 * All rights reserved. 2004. Terminated Running . Tei-Wei Kuo. National Taiwan University.

g. National Taiwan University. 2004. .CPU Scheduling ƒ Nonpreemptive Scheduling ƒ A running process keeps CPU until it volunteers to release CPU ƒ E..g. Windows 3. Tei-Wei Kuo.1 192 * All rights reserved.. I/O or termination ƒ Advantage ƒ Easy to implement (at the cost of service response to other processes) ƒ E.

Tei-Wei Kuo. especially for multiprocessor or real-time systems.g. CPU scheduling occurs whenever some process becomes ready or the running process leaves the running state! ƒ Issues involved: ƒ Protection of Resources..CPU Scheduling ƒ Preemptive Scheduling ƒ Beside the instances for non-preemptive scheduling. ƒ Synchronization ƒ E. Interrupts and System calls 193 * All rights reserved. National Taiwan University. 2004. . such as I/O queues or shared data.

2004. Tei-Wei Kuo. National Taiwan University. Must be fast Start a process 194 .CPU Scheduling ƒ Dispatcher ƒ Functionality: ƒ Switching context ƒ Switching to user mode ƒ Restarting a user program ƒ Dispatch Latency: Stop a process * All rights reserved.

2004.Scheduling Criteria ƒ Why? ƒ Different scheduling algorithms may favor one class of processes over another! ƒ Criteria ƒ ƒ ƒ ƒ ƒ CPU Utilization Throughput Turnaround Time: CompletionT-StartT Waiting Time: Waiting in the ReadyQ Response Time: FirstResponseTime 195 * All rights reserved. Tei-Wei Kuo. National Taiwan University. .

Scheduling Criteria ƒ How to Measure the Performance of CPU Scheduling Algorithms? ƒ Optimization of what? ƒ General Consideration ƒ Average Measure ƒ Minimum or Maximum Values ƒ Variance Æ Predictable Behavior 196 * All rights reserved. National Taiwan University. 2004. . Tei-Wei Kuo.

First-Served Scheduling (FIFO) ƒ Shortest-Job-First Scheduling (SJF) ƒ Priority Scheduling ƒ Round-Robin Scheduling (RR) ƒ Multilevel Queue Scheduling ƒ Multilevel Feedback Queue Scheduling ƒ Multiple-Processor Scheduling 197 * All rights reserved. National Taiwan University. 2004. Tei-Wei Kuo.Scheduling Algorithms ƒ First-Come. .

2004.First-Come. Tei-Wei Kuo. First-Served Scheduling (FCFS) ƒ The process which requests the CPU first is allocated the CPU ƒ Properties: ƒ Non-preemptive scheduling ƒ CPU might be hold for an extended period. CPU request * All rights reserved. A FIFO ready queue dispatched 198 . National Taiwan University.

Tei-Wei Kuo. . First-Served Scheduling (FCFS) ƒ Example Process P1 P2 P3 Gantt Chart P1 0 P2 P3 0 3 6 CPU Burst Time 24 3 3 Average waiting time P2 P3 24 27 30 = (0+24+27)/3 = 17 Average waiting time P1 30 = (6+0+3)/3 = 3 199 *The average waiting time is highly affected by process CPU burst times ! * All rights reserved. 2004. National Taiwan University.First-Come.

2004. 200 . Tei-Wei Kuo. National Taiwan University. First-Served Scheduling (FCFS) ƒ Example: Convoy Effect ƒ One CPU-bound process + many I/O-bound processes CPU ready queue I/O device idle ready queue All other processes wait for it to get off the CPU! * All rights reserved.First-Come.

2004.Shortest-Job-First Scheduling (SJF) ƒ Non-Preemptive SJF ƒ Shortest next CPU burst first process P1 P2 P3 P4 9 CPU burst time 6 8 7 3 P3 16 P2 24 201 * All rights reserved. Average waiting time = (3+16+9+0)/4 = 7 P4 P1 0 3 . Tei-Wei Kuo. National Taiwan University.

2004. Tei-Wei Kuo. .Shortest-Job-First Scheduling (SJF) ƒ Nonpreemptive SJF is optimal when processes are all ready at time 0 ƒ The minimum average waiting time! ƒ Prediction of the next CPU burst time? ƒ Long-Term Scheduler ƒ A specified amount at its submission time ƒ Short-Term Scheduler ƒ Exponential average (0<= α <=1) τn+1 = α tn + (1-α) τn 202 * All rights reserved. National Taiwan University.

2004. National Taiwan University. .Shortest-Job-First Scheduling (SJF) ƒ Preemptive SJF ƒ Shortest-remaining-time-first Process P1 P2 P3 P4 CPU Burst Time Arrival Time 8 0 4 1 9 2 5 3 Average Waiting Time = ((10-1) + (1-1) + (17-2) + (5-3))/4 = 26/4 = 6.5 203 P1 P2 P4 P1 P3 10 17 26 0 1 5 * All rights reserved. Tei-Wei Kuo.

.5 Non-preemptive AWT = (0+(10-1))/2 = 9/2 = 4.5 * Context switching cost ~ modeling & analysis 204 * All rights reserved. National Taiwan University.Shortest-Job-First Scheduling (SJF) ƒ Preemptive or Non-preemptive? ƒ Criteria such as AWT (Average Waiting Time) 0 or 0 1 2 1 10 10 11 11 Preemptive AWT = ((2-1)+0) = 0. Tei-Wei Kuo. 2004.

2004. National Taiwan University.Priority Scheduling ƒ CPU is assigned to the process with the highest priority – A framework for various scheduling algorithms: ƒ FCFS: Equal-Priority with TieBreaking by FCFS ƒ SFJ: Priority = 1 / next CPU burst length 205 * All rights reserved. Tei-Wei Kuo. .

2 P1 6 P3 P4 16 18 19 206 . Tei-Wei Kuo. National Taiwan University. 2004. Average waiting time = (6+0+16+18+1)/5 = 8.Priority Scheduling Process P1 P2 P3 P4 P5 CPU Burst Time 10 1 2 1 5 Priority 3 1 3 4 2 Gantt Graph P2 P5 0 1 * All rights reserved.

Tei-Wei Kuo. . 207 * All rights reserved. Average CPU Burst Average I/O Burst ƒ Externally defined – set by criteria external to the OS. such as the criticality levels of jobs. such as the # of open files. 2004. National Taiwan University.Priority Scheduling ƒ Priority Assignment ƒ Internally defined – use some measurable quantity.

or the running process relinquishes the CPU. National Taiwan University. 2004. ƒ Non-preemptive scheduling – CPU scheduling is invoked only when the running process relinquishes the CPU. . 208 * All rights reserved. Tei-Wei Kuo.Priority Scheduling ƒ Preemptive or Non-Preemptive? ƒ Preemptive scheduling – CPU scheduling is invoked whenever a process arrives at the ready queue.

2004. Tei-Wei Kuo.Priority Scheduling ƒ Major Problem ƒ Indefinite Blocking (/Starvation) ƒ Low-priority processes could starve to death! ƒ A Solution: Aging ƒ A technique that increases the priority of processes waiting in the system for a long time. National Taiwan University. . 209 * All rights reserved.

. National Taiwan University. Tei-Wei Kuo. 2004.Round-Robin Scheduling (RR) ƒ RR is similar to FCFS except that preemption is added to switch between processes. ready running Interrupt at every time quantum (time slice) ƒ Goal: Fairness – Time Sharing CPU FIFO… The quantum is used up! New process 210 * All rights reserved.

Tei-Wei Kuo. 2004. National Taiwan University.66 211 * All rights reserved.Round-Robin Scheduling (RR) Process P1 P2 P3 CPU Burst Time 24 Time slice = 4 3 3 P1 P2 P3 P1 P1 P1 P1 P1 0 4 7 10 14 18 22 26 30 AWT = ((10-4) + (4-0) + (7-0))/3 = 17/3 = 5. .

ƒ IF q = ε.Round-Robin Scheduling (RR) ƒ Service Size and Interval ƒ Time quantum = q Æ Service interval <= (n1)*q if n processes are ready. then RR Æ FCFS. . 2004. Tei-Wei Kuo. National Taiwan University. ƒ IF q = ∞. then RR Æ processor sharing. The # of context switchings increases! process 0 0 0 6 10 10 10 quantum context switch # 12 0 6 1 1 9 => 1/11 of CPU is wasted! 212 If context switch cost = 10% time quantum * All rights reserved.

Round-Robin Scheduling (RR) ƒ Turnaround Time process (10ms) quantum = 10 P1 P2 P3 0 10 10 20 20 0 0 30 0 quantum = 1 10 10 10 20 20 20 30 30 30 Average Turnaround Time = (10+20+30)/3 = 20 => 80% CPU Burst < time slice ATT = (28+29+30)/3 = 29 213 * All rights reserved. National Taiwan University. Tei-Wei Kuo. 2004. .

2004. Tei-Wei Kuo. … . System Processes Interactive Processes Batch Processes 214 * All rights reserved.Multilevel Queue Scheduling ƒ Partition the ready queue into several separate queues => Processes can be classified into different groups and permanently assigned to one queue. National Taiwan University.

g. ƒ Inter-queue scheduling a. b. Time slice between queues a.g. c.. 2004. foreground queues always have absolute priority over the background queues. National Taiwan University. More?? * All rights reserved.Multilevel Queue Scheduling ƒ Intra-queue scheduling ƒ Independent choice of scheduling algorithms. and 20% CPU to background processes. 215 . Fixed-priority preemptive scheduling a. e. 80% CPU is given to foreground processes.. e. Tei-Wei Kuo.

Tei-Wei Kuo. The method to determine which queue a newly ready process will enter. The method to determine when to upgrade a process to a higher priority queue. The scheduling algorithm for each queue c. 216 *Inter-queue scheduling: Fixed-priority preemptive?! * All rights reserved. ƒ Configurable Parameters: a.Multilevel Feedback Queue Scheduling ƒ Different from Multilevel Queue Scheduling by Allowing Processes to Migrate Among Queues. . The method to determine when to demote a process to a lower priority queue. National Taiwan University. e. # of queues b. d. 2004.

Tei-Wei Kuo.Multilevel Feedback Queue Scheduling ƒ Example quantum = 8 quantum = 16 FCFS *Idea: Separate processes with different CPU-burst characteristics! * All rights reserved. National Taiwan University. 217 . 2004.

2004. National Taiwan University. 218 * All rights reserved. Î Can processes run on any processor? ƒ A Heterogeneous System ƒ Programs must be compiled for instructions on proper processors. Tei-Wei Kuo. .Multiple-Processor Scheduling ƒ CPU scheduling in a system with multiple CPUs ƒ A Homogeneous System ƒ Processes are identical in terms of their functionality.

queues.g. . e.. Master-Slave – Asymmetric Multiprocessing ƒ One processor accesses the system structures Æ no need for data sharing 219 ƒ A common ready queue for all processors. ƒ ƒ * All rights reserved.Multiple-Processor Scheduling ƒ Load Sharing – Load Balancing!! ƒ A queue for each processor ƒ Self-Scheduling – Symmetric Multiprocessing Self-Scheduling ƒ Need synchronization to access common data structure. 2004. Tei-Wei Kuo. National Taiwan University.

220 * All rights reserved. National Taiwan University. 2004. instead of fast! ƒ Hard real-time systems: ƒ Failure to meet the timing constraints (such as deadline) of processes may result in a catastrophe! ƒ Soft real-time systems: ƒ Failure to meet the timing constraints may still contribute value to the system. Tei-Wei Kuo. .Real-Time Scheduling ƒ Definition ƒ Real-time means on-time.

Releasing resources needed by the higher priority process 3.Real-Time Scheduling ƒ Dispatch Latency Dispatch latency Conflicts Dispatch 1. Preemption of the running process 2. Tei-Wei Kuo. 2004. National Taiwan University. Context switching to the higher priority process 221 * All rights reserved. .

can only be done after a system call completes or an I/O blocking occurs ƒ Solutions: 1.. 2004. National Taiwan University.Real-Time Scheduling ƒ Minimization of Dispatch Latency? ƒ Context switching in many OS.g. Insert safe preemption points in longduration system calls. . Protect kernel data by some synchronization mechanisms to make the entire kernel preemptible. some UNIX versions. e. Tei-Wei Kuo. 2. 222 * All rights reserved.

. P1 P2 P3 D P1 P2 P3 Request for D D D D V Time Time D Time 223 P3 also waits for P2 to complete? * All rights reserved.Real-Time Scheduling ƒ Priority Inversion: ƒ A higher-priority processes must wait for the execution of a lower-priority processes. 2004. National Taiwan University. Tei-Wei Kuo.

National Taiwan University. Tei-Wei Kuo. P1 P2 P3 D P1 P2 Request for D D D D V Time Time D 224 Time P3 * All rights reserved. 2004. .Real-Time Scheduling ƒ Priority Inheritance ƒ The blocked process inherits the priority of the process that causes the blocking.

2004. Tei-Wei Kuo. . scheduled by EDF if its CPU utilization ∑ ⎛ 225 ⎝ ⎠ i i * All rights reserved.Real-Time Scheduling ƒ Earliest Deadline First Scheduling (EDF) ƒ Processes with closer deadlines have higher priorities. ( priority (τ i ) ∝ (1 / d i )) ƒ An optimal dynamic-priority-driven scheduling algorithm for periodic and aperiodic processes! Liu & Layland [JACM 73] showed that EDF is optimal in the sense that a process set is C ⎞ ⎜ P ⎟ is no larger than 100%. National Taiwan University.

Tei-Wei Kuo. 2004.Real-Time Scheduling – EDF process P1 P2 P3 P1 0 1 0 1 0 1 0 2 6 P3 6 12 18 20 226 * All rights reserved. National Taiwan University. CPU Burst time Deadline Initial Arrival Time 4 0 20 5 1 15 6 2 16 P3 6 12 12 P1 15 15 16 Average waiting time =(11+0+4)/3=5 P2 20 20 .

2004. including devices Process Synchronization 227 * All rights reserved. Tei-Wei Kuo. .A General Architecture of RTOS’s ƒ Objectives in the Design of Many RTOS’s ƒ Efficient Scheduling Mechanisms ƒ Good Resource Management Policies ƒ Predictable Performance ƒ Common Functionality of Many RTOS’s ƒ ƒ ƒ ƒ Task Management Memory Management Resource Control. National Taiwan University.

2004. .A General Architecture User Space OS processes Top Half Bottom Half hardware System calls such as I/O requests which may cause the releasing CPU of a process! Timer expires to • Expire the running process’s time quota • Keep the accounting info for each process Interrupts for Services 228 * All rights reserved. National Taiwan University. Tei-Wei Kuo.

A General Architecture ƒ 2-Step Interrupt Services Interrupt/ISR Latency I ƒ Immediate Interrupt Service ƒ Interrupt priorities > process priorities ƒ Time: Completion of higher priority ISR. starting of the right ISR (urgent/low-level work. National Taiwan University. Tei-Wei Kuo. 2004. disabling of certain interrupts. . set events) ISR IST Latency ƒ Scheduled Interrupt Service ƒ Usually done by preemptible threads Scheduled Service ƒ Remark: Reducing of non-preemptible code. 229 * All rights reserved. etc. context switch. Priority Tracking/Inheritance (LynxOS).

National Taiwan University. 2004. except when voluntary context switches occur – thread quantum? ƒ Timer Resolution ƒ Tick size vs Interrupt Frequency ƒ 10ms? 1ms? 1us? 1ns? ƒ Fine-Grained hardware clock 230 * All rights reserved. . Tei-Wei Kuo.A General Architecture ƒ Scheduler ƒ A central part in the kernel ƒ The scheduler is usually driven by a clock interrupt periodically.

etc. 2004.A General Architecture ƒ Memory Management ƒ No protection for many embedded systems ƒ Memory-locking to avoid paging ƒ Process Synchronization ƒ Sources of Priority Inversion ƒ Nonpreemptible code ƒ Critical sections ƒ A limited number of priority levels. . National Taiwan University. Tei-Wei Kuo. 231 * All rights reserved.

Algorithm Evaluation ƒ A General Procedure ƒ Select criteria that may include several measures. Tei-Wei Kuo. . e. 2004. maximize CPU utilization while confining the maximum response time to 1 second ƒ Evaluate various algorithms ƒ Evaluation Methods: ƒ ƒ ƒ ƒ Deterministic modeling Queuing models Simulation Implementation 232 * All rights reserved.g. National Taiwan University..

treads might be identified ƒ But it needs exact numbers for inputs. National Taiwan University. Tei-Wei Kuo.Deterministic Modeling ƒ A Typical Type of Analytic Evaluation ƒ Take a particular predetermined workload and defines the performance of each algorithm for that workload ƒ Properties ƒ Simple and fast ƒ Through excessive executions of a number of examples. 233 . and its answers only apply to those cases ƒ Being too specific and requires too exact knowledge to be useful! * All rights reserved. 2004.

Deterministic Modeling FCFC P1 0 process CPU Burst time P1 10 P2 29 P3 3 P4 7 P5 12 P2 10 P3 P4 39 42 49 P5 61 Average Waiting Time (AWT)=(0+10+39+42+49)/5=28 Nonpreemptive Shortest Job First P3 P4 P1 P5 0 3 10 20 32 AWT=(10+32+0+3+20)/5=13 Round Robin (quantum =10) P2 61 P1 0 P2 P3 P4 P5 P2 P5 P2 10 2023 30 40 50 52 61 AWT=(0+(10+20+2)+20+23+(30+10))/5=23 .

National Taiwan University. . 2004. Arrival rate: the distribution of times when processes arrive. and there is no static set of processes Workload: a. b.Queueing Models ƒ Motivation: ƒ Workloads vary. Tei-Wei Kuo. The distributions of CPU & I/O bursts ƒ Models (~ Queueing-Network Analysis) ƒ ƒ Service rate 235 * All rights reserved.

. Each server has a queue of waiting processes ƒ Compute average queue length. Tei-Wei Kuo.Queueing Models ƒ Model a computer system as a network of servers. waiting time. and so on. National Taiwan University. ƒ Properties: ƒ Generally useful but with limited application to the classes of algorithms & distributions ƒ Assumptions are made to make problems solvable => inaccurate results 236 * All rights reserved. 2004.

. National Taiwan University. then w = 2 seconds. 2004. Tei-Wei Kuo. 237 * All rights reserved.Queueing Models ƒ Example: Little’s formula n = λ∗w w λ steady state! λ n = # of processes in the queue λ = arrival rate ω = average waiting time in the queue ƒ If n =14 & λ =7 processes/sec.

ƒ Trace tapes: monitor the real system & record the sequence of actual events. 2004. * All rights reserved. 238 . National Taiwan University. ƒ Procedures: ƒ Program a model of the computer system ƒ Drive the simulation with various data sets ƒ Randomly generated according to some probability distributions => inaccuracy occurs because of only the occurrence frequency of events.Simulation ƒ Motivation: ƒ Get a more accurate evaluation. Miss the order & the relationships of events. Tei-Wei Kuo.

239 * All rights reserved.Simulation ƒ Properties: ƒ Accurate results can be gotten. but it could be expensive in terms of computation time and storage space. design. and debugging of a simulator can be a big job. National Taiwan University. Tei-Wei Kuo. 2004. . ƒ The coding.

.Implementation ƒ Motivation: ƒ Get more accurate results than a simulation! ƒ Procedure: ƒ Code scheduling algorithms ƒ Put them in the OS ƒ Evaluate the real behaviors 240 * All rights reserved. Tei-Wei Kuo. National Taiwan University. 2004.

Implementation ƒ Difficulties: ƒ Cost in coding algorithms and modifying the OS ƒ Reaction of users to a constantly changing the OS ƒ The environment in which algorithms are used will change ƒ For example. . Tei-Wei Kuo. users may adjust their behaviors according to the selected algorithms => Separation of the policy and mechanism! 241 * All rights reserved. National Taiwan University. 2004.

.. those for user-level threads ƒ Thread scheduling is done locally to each application. ƒ System Global Scheduling ƒ E.. 242 * All rights reserved. National Taiwan University. Tei-Wei Kuo.g.Process Scheduling Model ƒ Process Local Scheduling ƒ E. 2004. those for Kernel-level threads ƒ The kernel decides which thread to run.g.

2004. National Taiwan University. Tei-Wei Kuo. .Process Scheduling Model – Solaris 2 ƒ Priority-Based Process Scheduling ƒ Real-Time ƒ System ƒ Kernel-service processes low ƒ Time-Sharing ƒ A default class ƒ Interactive ƒ Each LWP inherits its class from its parent process 243 * All rights reserved.

Process Scheduling Model – Solaris 2 ƒ Real-Time ƒ A guaranteed response ƒ System ƒ The priorities of system processes are fixed. ƒ Time-Sharing ƒ Multilevel feedback queue scheduling – priorities inversely proportional to time slices ƒ Interactive ƒ Prefer windowing process * All rights reserved. National Taiwan University. Tei-Wei Kuo. 2004. 244 .

National Taiwan University.Process Scheduling Model – Solaris 2 ƒ The selected thread runs until one of the following occurs: ƒ It blocks. ƒ It is preempted by a higher-priority thread. . Tei-Wei Kuo. 2004. 245 * All rights reserved. ƒ RR is used when several threads have the same priority. ƒ It uses its time slice (if it is not a system thread).

. 2004.Process Scheduling Model – Windows 2000 ƒ Priority-Based Preemptive Scheduling ƒ Priority Class/Relationship: 0. Tei-Wei Kuo. ƒ It terminates ƒ Its time quantum ends ƒ It calls a blocking system call ƒ Idle thread ƒ A queue per priority level 246 * All rights reserved. . National Taiwan University.31 ƒ Dispatcher: A process runs until ƒ It is preempted by a higher-priority process.

ƒ A typical class – Normal_Priority_Class ƒ Time quantum – thread ƒ Increased after some waiting ƒ Different for I/O devices. ƒ Favor foreground processes (more time quantum) * All rights reserved. National Taiwan University. 2004. ƒ Decreased after some computation ƒ The priority is never lowered below the base priority. 247 . Tei-Wei Kuo.Process Scheduling Model – Windows 2000 ƒ Each thread has a base priority that represents a value in the priority range of its class.

2004.Process Scheduling Model – Windows 2000 A Typical Class Realtime Timecritical Highest Above normal High Above Normal normal Below Idle normal priority 31 26 25 24 23 22 16 15 15 14 13 12 11 1 15 12 11 10 9 8 1 15 10 9 8 7 6 1 15 8 7 6 5 4 1 15 6 5 4 3 2 1 248 Base Priority Normal Below normal Lowest Idle Real-Time Class * All rights reserved.15) . Tei-Wei Kuo. National Taiwan University.. Variable Class (1.

1b) ƒ Time-Sharing ƒ Soft Real-Time: FCFS.Process Scheduling Model – Linux ƒ Three Classes (POSIX. National Taiwan University. . and RR ƒ Real-Time Scheduling Algorithms ƒ FCFS & RR always run the highest priority process. ƒ FCFS runs a process until it exits or blocks. 2004. Tei-Wei Kuo. ƒ No scheduling in the kernel space for conventional Linux 249 * All rights reserved.

2004. ƒ nice in UNIX 250 * All rights reserved. ƒ Mixture of a history and its priority ƒ Favor interactive or I/O-bound processes ƒ Background processes could be given lower priorities to receive less credits. National Taiwan University. Tei-Wei Kuo. .Process Scheduling Model – Linux ƒ A Time-Sharing Algorithm for Fairness ƒ Credits = (credits / 2) + priority ƒ Recrediting when no runnable process has any credits.

Computer-System Structures 3.Contents 1. Introduction 2. Tei-Wei Kuo. 251 . Deadlocks 9. 2004. Processes 5. File Systems * All rights reserved. Memory Management 10. National Taiwan University. Threads 6. Process Synchronization 8. Operating-System Structures 4. Virtual Memory 11. CPU Scheduling 7.

Chapter 7 Process Synchronization 252 .

Tei-Wei Kuo. National Taiwan University. 2004.Process Synchronization ƒ Why Synchronization? ƒ To ensure data consistency for concurrent access to shared data! ƒ Contents: ƒ Various mechanisms to ensure the orderly execution of cooperating processes 253 * All rights reserved. .

buffer[in] = nextp. } . counter++. nextc = buffer[out]. in = (in+1) % BUFFER_SIZE. ….Process Synchronization ƒ A Consumer-Producer Example ƒ Producer ƒ Consumer: while (1) { while (counter == 0) . out = (out +1) % BUFFER_SIZE. } while (1) { while (counter == BUFFER_SIZE) . counter--. consume an item in nextc. produce an item in nextp.

Process Synchronization ƒ counter++ vs counter— r1 = counter r1 = r1 + 1 counter = r1 r2 = counter r2 = r2 . 4. 5. 1. let counter = 5. 3. 6. P: r1 = counter P: r1 = r1 + 1 C: r2 = counter C: r2 = r2 – 1 P: counter = r1 C: counter = r2 A Race Condition! . 2.1 counter = r2 ƒ Initially.

ƒ Each process has a segment of code. called a critical section.Process Synchronization ƒ A Race Condition: ƒ A situation where the outcome of the execution depends on the particular order of process scheduling. whose execution must be mutually exclusive. 256 * All rights reserved. ƒ The Critical-Section Problem: ƒ Design a protocol that processes can use to cooperate. . National Taiwan University. Tei-Wei Kuo. 2004.

remainder section. exit notification exit section. 257 . National Taiwan University. * All rights reserved. 2004.Process Synchronization ƒ A General Structure for the CriticalSection Problem do { permission request entry section. Tei-Wei Kuo. } while (1). critical section.

b. . Only one process can be in its critical section. 2004. Only processes not in their remainder section can decide which will enter its critical section.The Critical-Section Problem ƒ Three Requirements a. 258 * All rights reserved. Progress a. Mutual Exclusion 2. National Taiwan University. A waiting process only waits for a bounded number of processes to enter their critical sections. 1. Tei-Wei Kuo. The selection cannot be postponed indefinitely. 3. Bounded Waiting a.

The Critical-Section Problem – A Two-Process Solution
ƒ Notation
ƒ Processes Pi and Pj, where j=1-i; do { while (turn != i) ; critical section turn=j; remainder section } while (1);
259
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

ƒ Assumption
ƒ Every basic machine-language instruction is atomic. ƒ Algorithm 1
ƒ Idea: Remember which process is allowed to enter its critical section, That is, process i can enter its critical section if turn = i.

The Critical-Section Problem – A Two-Process Solution
ƒ Algorithm 1 fails the progress requirement:
suspend or Time quit!

P0
turn=0 exit turn=1

P1
exit turn=0
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Time
blocked on P1’s entry section 260

The Critical-Section Problem – A Two-Process Solution
ƒ Algorithm 2
ƒ Idea: Remember the state of each process. ƒ flag[i]==true Æ Pi is ready to enter its critical section. ƒ Algorithm 2 fails the progress requirement when flag[0]==flag[1]==true;
Initially, flag[0]=flag[1]=false do { flag[i]=true; while (flag[j]) ; critical section flag[i]=false; remainder section } while (1);
261

ƒ the exact timing of the two processes?
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

* The switching of “flag[i]=true” and “while (flag[j]);”.

The Critical-Section Problem – A Two-Process Solution
ƒ Algorithm 3
ƒ Idea: Combine the ideas of Algorithms 1 and 2 ƒ When (flag[i] && turn=i), Pj must wait. ƒ Initially, flag[0]=flag[1]=false, and turn = 0 or 1
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

do { flag[i]=true; turn=j; while (flag[j] && turn==j) ; critical section flag[i]=false; remainder section } while (1);
262

The Critical-Section Problem – A Two-Process Solution
ƒ Properties of Algorithm 3
ƒ Mutual Exclusion
ƒ The eventual value of turn determines which process enters the critical section.

ƒ Progress
ƒ A process can only be stuck in the while loop, and the process which can keep it waiting must be in its critical sections.

ƒ Bounded Waiting
ƒ Each process wait at most one entry by the other process.
263
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

The Critical-Section Problem – A Multiple-Process Solution
ƒ Bakery Algorithm
ƒ Originally designed for distributed systems ƒ Processes which are ready to enter their critical section must take a number and wait till the number becomes the lowest.

ƒ int number[i]: Pi’s number if it is nonzero. ƒ boolean choosing[i]: Pi is taking a number.
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

264

The Critical-Section Problem – A Multiple-Process Solution
do { choosing[i]=true; number[i]=max(number[0], …number[n-1])+1; choosing[i]=false; for (j=0; j < n; j++) while choosing[j] ; while (number[j] != 0 && (number[j],j)<(number[i],i)) ; critical section number[i]=0; remainder section } while (1);

• An observation: If Pi is in its critical section, and Pk (k != i) has already chosen its number[k], then (number[i],i) < (number[k],k).

Synchronization Hardware
ƒ Motivation:
ƒ Hardware features make programming easier and improve system efficiency.

ƒ Approach:
ƒ Disable Interrupt Æ No Preemption
ƒ Infeasible in multiprocessor environment where message passing is used. ƒ Potential impacts on interrupt-driven system clocks.

ƒ Atomic Hardware Instructions
ƒ Test-and-set, Swap, etc.
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

266

Synchronization Hardware
boolean TestAndSet(boolean &target) { boolean rv = target; target=true; return rv; }
do { while (TestAndSet(lock)) ; critical section lock=false; remainder section } while (1);

. critical section lock=false. remainder section } while (1). while (key == true) Swap(lock.Synchronization Hardware void Swap(boolean &a. key). } do { key=true. a=b. b=temp. boolean &b) { boolean temp = a.

j= (i+1) % n.Synchronization Hardware do { waiting[i]=true. If (j=i) lock=false. key=true. else waiting[j]=false. critical section. while(j != i) && (not waiting[j]) j= (j+1) % n. waiting[i]=false. . ƒBounded Waiting ƒ Wait at most n-1 times ƒAtomic TestAndSet is hard to implement in a multiprocessor environment. while (waiting[i] && key) key=TestAndSet(lock). ƒ Mutual Exclusion ƒ Pass if key == F or waiting[i] == F ƒ Progress ƒ Exit process sends a process in. remainder section } while (1).

and “S++” 270 . ƒ Semaphore ƒ A variable S only accessible by two atomic operations: wait(S) { /* P */ while (S <= 0) . “S—”. Tei-Wei Kuo. S—. } * All rights reserved. National Taiwan University. 2004. } /* V */ •Indivisibility for “(S<=0)”. signal(S) { S++.Semaphores ƒ Motivation: ƒ A high-level solution for more complex problems.

signal(synch). . ƒ Precedence Enforcement P1: S1. remainder section } while (1). S2. critical section signal(mutex). P2: wait(synch).Semaphores – Usages ƒ Critical Sections do { wait(mutex).

ƒ Semaphores with Block-Waiting ƒ No busy waiting from the entry to the critical section! 272 * All rights reserved. National Taiwan University. . 2004. spinlocks are useful since no context switching is involved. Tei-Wei Kuo.Semaphores ƒ Implementation ƒ Spinlock – A Busy-Waiting Semaphore ƒ “while (S <= 0)” causes the wasting of CPU cycles! ƒ Advantage: ƒ When locks are held for a short time.

} semaphore . void wait(semaphore S) { S. if (S.value <= 0) { if (S. void signal(semaphore S). . struct process *L. S. add this process to S.value < 0.L. wakeup(P).value--.value < 0) { remove a process P form S. } } } } * |S. block().value++.value| = the # of waiting processes if S.Semaphores ƒ Semaphores with Block Waiting typedef struct { int value.L.

2 ƒ Multiprocessor Environments ƒ Remarks: Busy-waiting is limited to only the critical sections of the wait() & signal()! 274 * All rights reserved. in Section 7. the Bakery Algorithm. ƒ Mutual exclusion in wait() & signal() ƒ Uniprocessor Environments ƒ Interrupt Disabling ƒ TestAndSet. National Taiwan University.. 2004. Tei-Wei Kuo. Swap ƒ Software Methods. e. but there is a restriction for the boundedwaiting requirement.Semaphores ƒ The queueing strategy can be arbitrary. .g.

275 . signal(S).. … signal(Q). wait(S).g. P1: wait(Q). P0: wait(S). a LIFO queue * All rights reserved. ƒ Starvation (or Indefinite Blocking) ƒ E.Deadlocks and Starvation ƒ Deadlock ƒ A set of processes is in a deadlock state when every process in the set is waiting for an event that can be caused only by another process in the set. Tei-Wei Kuo. signal(Q). 2004. wait(Q). National Taiwan University. … signal(S).

Tei-Wei Kuo. * S1 & S2: binary semaphores * All rights reserved. C++. /* protect C */ wait(S1). C--. if (C < 0) { if (C <= 0) signal(S1). /* wakeup */ wait(S2). National Taiwan University. signal (S2). 276 . signal(S1).Binary Semaphore ƒ Binary Semaphores versus Counting Semaphores ƒ The value ranges from 0 to 1Æ easy implementation! wait(S) signal(S) wait(S1). 2004. else } signal (S1).

signal(full). ……. . /* control buffer availability */ wait(mutex). 2004. Tei-Wei Kuo. signal(mutex). /* mutual exclusion */ …… add nextp to buffer. /* increase item counts */ } while (1). 277 Initialized to n Initialized to 1 Initialized to 0 * All rights reserved. National Taiwan University.Classical Synchronization Problems – The Bounded Buffer Producer: do { produce an item in nextp. wait(empty).

National Taiwan University. 278 Initialized to n * All rights reserved. /* control buffer availability */ wait(mutex). } while (1). Tei-Wei Kuo. /* mutual exclusion */ ……. remove an item from buffer to nextp. signal(empty). /* increase item counts */ consume nextp. . 2004. …… signal(mutex).Classical Synchronization Problems – The Bounded Buffer Consumer: do { Initialized to 0 Initialized to 1 wait(full).

2004. National Taiwan University.Classical Synchronization Problems – Readers and Writers ƒ The Basic Assumption: ƒ Readers: shared locks ƒ Writers: exclusive locks ƒ The first reader-writers problem ƒ No readers will be kept waiting unless a writer has already obtained permission to use the shared object Æ potential hazard to writers! ƒ The second reader-writers problem: ƒ Once a writer is ready. Tei-Wei Kuo. . it performs its write asap! Æ potential hazard to readers! 279 * All rights reserved.

if (readcount == 1) Writer: wait(wrt). Queueing wait(wrt). mechanism …… …… reading …… wait(mutex). mutex. …… if (readcount== 0) signal(wrt) signal(wrt). wait(mutex). int readcount=0. Solution readcount++. . (initialized to 1). signal(mutex). Which is awaken? signal(mutex).Classical Synchronization Problems – Readers and Writers Reader: First R/W semaphore wrt. writing is performed readcount--.

dead 281 . National Taiwan University. 2004.Classical Synchronization Problems – Dining-Philosophers ƒ Each philosopher must pick up one chopstick beside him/her at a time ƒ When two chopsticks are picked up. thinking hungry eating * All rights reserved. Tei-Wei Kuo. the philosopher can eat.

Classical Synchronization Problems – Dining-Philosophers semaphore chopstick[5]. wait(chopstick[(i + 1) % 5 ]). …think … } while (1). do { wait(chopstick[i]). … eat … signal(chopstick[i]). signal(chopstick[(i+1) % 5]). .

odds pick up their right one first. and evens pick up their left one first. National Taiwan University. ƒ Solutions to Starvation: ƒ No philosopher will starve to death. Tei-Wei Kuo.Classical Synchronization Problems – Dining-Philosophers ƒ Deadlock or Starvation?! ƒ Solutions to Deadlocks: ƒ At most four philosophers appear. ƒ A deadlock could happen?? 283 * All rights reserved. ƒ Pick up two chopsticks “simultaneously”.. 2004. . ƒ Order their behaviors.g. e.

g. 284 .e. Tei-Wei Kuo. 2004. National Taiwan University.Critical Regions ƒ Motivation: ƒ Various programming errors in using low-level constructs.. semaphores ƒ Interchange the order of wait and signal operations ƒ Miss some waits or signals ƒ Replace waits with signals ƒ etc ƒ The needs of high-level language constructs to reduce the possibility of errors! * All rights reserved.

National Taiwan University. in. out. ƒ Variable v – shared among processes and only accessible in the region struct buffer { item pool[n]. Tei-Wei Kuo. ƒ B – condition ƒ count < 0 ƒ S – statements * All rights reserved. Example: Mutual Exclusion region v when (true) S1. 2004.Critical Regions ƒ Region v when B do S. }. 285 . region v when (true) S2. int count.

out. pool[in] = nextp. Tei-Wei Kuo. National Taiwan University. Producer: Consumer: region buffer when region buffer when (count > 0) { (count < n) { nextc = pool[out]. 2004. count--. }. .Critical Regions – ConsumerProducer struct buffer { item pool[n]. in. in = (in + 1) % n. int count. } } 286 * All rights reserved. out = (out + 1) % n. count++.

else signal(mutex). int first-count=0. if (second-count > 0) /* to retest B */ /* try other processes waiting semaphore second-delay. Tei-Wei Kuo. * All rights reserved. first-count++. . 2004. while (!B) { /* to (re-)test B */ /* fail B */ semaphore first-delay. National Taiwan University. on second-delay */ int second-count=0. /* block itself on first-delay */ 287 wait(first-delay). signal(second-delay).Critical Regions – Implementation by Semaphores Region x when B do S. /* to protect the region */ wait(mutex). semaphore mutex.

National Taiwan University. 288 . /* block itself on first-delay */ wait(second-delay). Tei-Wei Kuo. } S. else if (second-count > 0) signal(second-delay). 2004. if (first-count > 0) signal(first-delay). else signal(mutex). * All rights reserved. second-count++. second-count--. if (first-count > 0) signal(first-delay).Critical Regions – Implementation by Semaphores first-count--. else signal(second-delay).

signal … … … procedures entry queue initialization code * All rights reserved.wait() or x. 2004.Monitor ƒ Components ƒ Variables – monitor state ƒ Procedures shared data ƒ Only access local variables or formal parameters queue for x x ƒ Condition variables ƒ Tailor-made sync ƒ x. National Taiwan University. Tei-Wei Kuo. monitor name { variable declaration void proc1(…) { } … void procn(…) { } 289 } .

2004. no effect is imposed. or waits for another condition. National Taiwan University.Monitor ƒ Semantics of signal & wait ƒ x.signal() a suspended process Q ƒ P either waits until Q leaves the monitor or waits for another condition ƒ Q either waits until P leaves the monitor. Tei-Wei Kuo. If there is none. 290 * All rights reserved. .signal() resumes one suspended process. ƒ P x.

test((i + 1) % 5). eating} state[5]. 291 } * All rights reserved. } void putdown(int i) { stat[i] = thinking.putdown(i). 2004. National Taiwan University. test(i). test((i+4) % 5). condition self[5]. hungry. Tei-Wei Kuo. void pickup(int i) { stat[i]=hungry. if (stat[i] != eating) self[i].pickup(i). … eat … dp. .Monitor – Dining-Philosophers Pi: dp.wait. monitor dp { enum {thinking.

.signal(). Tei-Wei Kuo. i++) state[i] = thinking. 2004.Monitor – Dining-Philosophers void test(int i) { if (stat[(i+4) % 5]) != eating && stat[i] == hungry && state[(i+1) % 5] != eating) { No deadlock! stat[i] = eating. } } void init() { for (int i=0. But starvation could occur! self[i]. i < 5. National Taiwan University. } 292 * All rights reserved.

National Taiwan University. Tei-Wei Kuo. on which processes may suspend themselves ƒ nextcount ƒ For each external function F wait(mutex). 2004. … if (next-count > 0) signal(next). 293 . else signal(mutex).Monitor – Implementation by Semaphores ƒ Semaphores ƒ mutex – to protect the monitor ƒ next – being initialized to zero. * All rights reserved. … body of F.

wait() x-count++. if (next-count > 0) signal(next). signal(x-sem). National Taiwan University.Monitor – Implementation by Semaphores ƒ For every condition x ƒ A semaphore x-sem ƒ An integer variable x-count ƒ Implementation of x. } 294 * x. Tei-Wei Kuo. next-count--. ƒ x.signal() are invoked within a monitor. .signal if (x-count > 0) { next-count++. else signal(mutex).wait() and x. 2004.wait() and x. x-count--. * All rights reserved. wait(next). wait(x-sem).signal : ƒ x.

ƒ where the expression c is evaluated to determine its process’s resumption order.Monitor ƒ Process-Resumption Order monitor ResAllc { boolean busy. busy=true. R. void acquire(int time) { if (busy) x.wait(time). 295 * All rights reserved. Tei-Wei Kuo.wait(c). ƒ A solution: x. } … } ƒ Queuing mechanisms for a monitor and its condition variables. 2004.acquire(t). R. National Taiwan University.release. . condition x. … access the resource.

ƒ Processes may never release resources. 2004. 296 * All rights reserved.Monitor ƒ Concerns: ƒ Processes may access resources without consulting the monitor. ƒ Processes may release resources which they never requested. National Taiwan University. Tei-Wei Kuo. . ƒ Process may even request resources twice.

ƒ Note: Scheduling behavior should consult the built-in monitor scheduling algorithm if resource access RPC are built inside the monitor. 2004. 297 * All rights reserved.Monitor ƒ Remark: Whether the monitor is correctly used? => Requirements for correct computations ƒ Processes always make their calls on the monitor in correct order. . Tei-Wei Kuo. ƒ No uncooperative process can access resource directly without using the access protocols. National Taiwan University.

ƒ Readers-Writers Locks ƒ Expensive in implementations. Tei-Wei Kuo. blocking is used. otherwise. ƒ Turnstile ƒ A queue structure containing threads blocked on a lock. National Taiwan University. 2004. . ƒ Priority inversion Æ priority inheritance 298 protocol for kernel threads * All rights reserved.OS Synchronization – Solaris 2 ƒ Semaphores and Condition Variables ƒ Adaptive Mutex ƒ Spin-locking if the lock-holding thread is running.

. Tei-Wei Kuo. ƒ Events – select all processes waiting for the event. ƒ Dispatcher Object ƒ State: signaled or non-signaled ƒ Mutex – select one process from its waiting queue to the ready queue. National Taiwan University. ƒ Interrupt disabling when access to global variables is done in a uniprocessor platform. 2004. 299 * All rights reserved.OS Synchronization – Windows 2000 ƒ General Mechanism ƒ Spin-locking for short code segments in a multiprocessor platform.

2004. . National Taiwan University. but the relationship between critical sections might also be meaningful! Æ Atomic Transactions ƒ Operating systems can be viewed as manipulators of data! 300 * All rights reserved. Tei-Wei Kuo.Atomic Transactions ƒ Why Atomic Transactions? ƒ Critical sections ensure mutual exclusion in data sharing.

Tei-Wei Kuo. 301 * All rights reserved. Atomicity: All or Nothing ƒ An aborted transaction must be rolled back. 2004.Atomic Transactions – System Model ƒ Transaction – a logical unit of computation ƒ A sequence of read and write operations followed by a commit or an abort. National Taiwan University. ƒ Beyond “critical sections” 1. . ƒ The effect of a committed transaction must persist and be imposed as a logical unit of operations.

Tei-Wei Kuo. Access the same object 2.Atomic Transactions – System Model 2. T0 R(A) W(A) R(B) W(B) T1 R(A) W(A) R(B) W(B) 302 Two operations Oi & Oj conflict if 1. National Taiwan University. Serializability: ƒ The order of transaction executions must be equivalent to a serial schedule. One of them is write * All rights reserved. 2004. .

National Taiwan University. T1 R(A) W(A) T0 R(A) W(A) R(B) W(B) T1 R(A) W(A) R(B) W(B) 303 . 2004.Atomic Transactions – System Model ƒ Conflict Serializable: ƒ S is conflict serializable if S can be transformed into a serial schedule by swapping nonconflicting operations. Tei-Wei Kuo. T0 R(A) W(A) R(B) W(B) R(B) W(B) * All rights reserved.

National Taiwan University. Tei-Wei Kuo. Shared-Mode: “Reads”. 304 * All rights reserved. . 2004. Exclusive-Mode: “Reads” & “Writes“ ƒ General Rule ƒ A transaction must receive a lock of an appropriate mode of an object before it accesses the object.Atomic Transactions – Concurrency Control ƒ Locking Protocols ƒ Lock modes (A general approach!) ƒ 1. ƒ 2. The lock may not be released until the last access of the object is done.

Atomic Transactions – Concurrency Control
Lock Request Yes Request compatible with the current lock? No

Locked? No Lock is granted

Yes

WAIT
305

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Atomic Transactions – Concurrency Control
ƒ When to release locks w/o violating serializability
R0(A) W0(A) R1(A) R1(B) R0(B) W0(B)

ƒ Two-Phase Locking Protocol (2PL) – Not Deadlock-Free
Growing Phase Shrinking Phase

serializable schedules
2PL schedules

ƒ How to improve 2PL?
ƒ Semantics, Order of Data, Access Pattern, etc.
306
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Atomic Transactions – Concurrency Control
ƒ Timestamp-Based Protocols
ƒ A time stamp for each transaction TS(Ti)
ƒ Determine transactions’ order in a schedule in advance!

ƒ A General Approach:
ƒ TS(Ti) – System Clock or Logical Counter
ƒ Unique?

ƒ Scheduling Scheme – deadlock-free & serializable ƒ W − timestamp (Q ) = Max Ti −W ( Q ) (TS (Ti ))
ƒ

R − timestamp(Q) = MaxTi − R (Q ) (TS (Ti ))

307

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Atomic Transactions – Concurrency Control
ƒ R(Q) requested by Ti Æ check TS(Ti) !
Rejected Granted Time W-timestamp(Q)

ƒ W(Q) requested by Ti Æ check TS(Ti) !
Rejected Granted Time Rejected Granted W-timestamp(Q) Time R-timestamp(Q)

ƒ Rejected transactions are rolled back and restated with a new time stamp.
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

308

Failure Recovery – A Way to
Achieve Atomicity
ƒ Failures of Volatile and Nonvolatile Storages!
ƒ Volatile Storage: Memory and Cache ƒ Nonvolatile Storage: Disks, Magnetic Tape, etc. ƒ Stable Storage: Storage which never fail.

ƒ Log-Based Recovery
ƒ Write-Ahead Logging
ƒ Log Records
< Ti starts > < Ti commits > < Ti aborts > < Ti, Data-Item-Name, Old-Value, New-Value> 309
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Failure Recovery
ƒ Two Basic Recovery Procedures:
Time
restart crash

ƒ undo(Ti): restore data updated by Ti ƒ redo(Ti): reset data updated by Ti

ƒ Operations must be idempotent! ƒ Recover the system when a failure occurs:
ƒ “Redo” committed transactions, and “undo” aborted transactions.
310
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Failure Recovery
ƒ Why Checkpointing?
ƒ The needs to scan and rerun all log entries to redo committed transactions.

ƒ CheckPoint
ƒ Output all log records, Output DB, and Write <check point> to stable storage! ƒ Commit: A Force Write Procedure

checkpoint
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

crash

Time

311

Contents
1. Introduction 2. Computer-System Structures 3. Operating-System Structures 4. Processes 5. Threads 6. CPU Scheduling 7. Process Synchronization 8. Deadlocks 9. Memory Management 10. Virtual Memory 11. File Systems
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

312

Chapter 8 Deadlocks

313

ƒ A System Model ƒ Competing processes – distributed? ƒ Resources: ƒ Physical Resources. ƒ Logical Resources. etc. files. memory.. 314 * All rights reserved. semaphores.g. CPU. National Taiwan University. .g. printers. etc.. 2004.Deadlocks ƒ A set of process is in a deadlock state when every process in the set is waiting for an event that can be caused by only another process in the set. e. Tei-Wei Kuo. e.

. Release ƒ Remarks ƒ ƒ No request should exceed the system capacity! Deadlock can involve different resource types! ƒ Several instances of the same type! 315 * All rights reserved. 2004. Request: Granted or Rejected 2.Deadlocks ƒ A Normal Sequence 1. Tei-Wei Kuo. National Taiwan University. Use 3.

. Tei-Wei Kuo. Mutual Exclusion – At least one resource must be held in a nonsharable mode! 2. Hold and Wait – Pi is holding at least one resource and waiting to acquire additional resources that are currently held by other processes! 316 * All rights reserved.Deadlock Characterization ƒ Necessary Conditions (deadlock Æ conditions or ¬ conditions Æ ¬ deadlock) 1. National Taiwan University. 2004.

and Pn wait P0. …. ….Deadlock Characterization 3. Circular Wait – There exists a set {P0. Pn-1 wait Pn. The four conditions are not completely independent! 317 * All rights reserved. P1wait P2. ƒ Remark: ƒ ƒ Condition 4 implies Condition 2. No Preemption – Resources are nonpreemptible! 4. 2004. National Taiwan University. Tei-Wei Kuo. Pn} of waiting process such that P0 wait P1. . P1.

Rm} Edges Request Edge: Pi Æ Rj Assignment Edge: Ri Æ Pj 318 P1 P2 P3 R2 R4 * All rights reserved. .…. Pn} Resource Type : {R1.Resource Allocation Graph System Resource-Allocation Graph R1 R3 Vertices Processes: {P1. National Taiwan University.…. Tei-Wei Kuo. 2004.

R1ÆP2. R2ÆP2. 2004. R4 } P1 P2 P3 ƒ Edges ƒ E = { P1ÆR1. R2. P2. P3 } ƒ R = { R1. R2ÆP1. R2:2. . 319 * All rights reserved. National Taiwan University. R3:1. R3. Tei-Wei Kuo. R4:3 ƒ Æ results in a deadlock.Resource Allocation Graph ƒ Example R1 R3 ƒ No-Deadlock ƒ Vertices ƒ P = { P1. P2ÆR3. R3ÆP3 } R2 R4 ƒ Resources ƒ R1:1.

Resource Allocation Graph ƒ Observation ƒ The existence of a cycle ƒ One Instance per Resource Type Æ Yes!! ƒ Otherwise Æ Only A Necessary Condition!! R1 P2 P3 P4 * All rights reserved. 2004. Tei-Wei Kuo. P1 R2 320 . National Taiwan University.

Make sure that the system always stays at a “safe” state! 321 * All rights reserved. Tei-Wei Kuo. Make sure that the system never enters a deadlock state! ƒ ƒ Deadlock Prevention: Fail at least one of the necessary conditions Deadlock Avoidance: Processes provide information regarding their resource usage.Methods for Handling Deadlocks ƒ Solutions: 1. . National Taiwan University. 2004.

Do recovery if the system is deadlocked.Methods for Handling Deadlocks 2. ƒ . Tei-Wei Kuo. Ignore the possibility of deadlock occurrences! ƒ Restart the system “manually” if the system “seems” to be deadlocked or stops functioning. 2004. National Taiwan University. ƒ ƒ Deadlock Detection Recovery 3. Note that the system may be “frozen” temporarily! 322 * All rights reserved.

National Taiwan University. . such as a printer. Tei-Wei Kuo. are intrinsically non-sharable?? 323 * All rights reserved.Deadlock Prevention ƒ Observation: ƒ Try to fail anyone of the necessary condition! ∵ ¬ (∧ i-th condition) → ¬ deadlock ƒ Mutual Exclusion ?? Some resources. 2004.

Tei-Wei Kuo. ƒ Release allocated resources before request additional resources! [ Tape Drive Æ Disk ] [ Disk & Printer ] Hold Them All Disk & Printer Tape Drive & Disk ƒ Disadvantage: ƒ Low Resource Utilization ƒ Starvation * All rights reserved.Deadlock Prevention ƒ Hold and Wait ƒ Acquire all needed resources before its execution. 2004. 324 . National Taiwan University.

. 2004. ƒ Related protocols are only applied to resources whose states can be saved and restored. ƒ Approach 1: Resource Request Satisfied? No Allocated resources are released Yes granted 325 * All rights reserved.g.Deadlock Prevention ƒ No Preemption ƒ Resource preemption causes the release of resources. CPU register & memory space. . Tei-Wei Kuo. e. instead of printers or tape drives. National Taiwan University.

Tei-Wei Kuo. * All rights reserved.Deadlock Prevention ƒ Approach 2 Resource Request Satisfied? Yes granted No Requested Resources are held by “Waiting” processes? Yes Preempt those Resources. No “Wait” and its allocated resources may be preempted. National Taiwan University. 2004. 326 .

order any # of instances of Ri ƒ Following requests of any # of instances of Rj must satisfy F(Rj) > F(Ri). .Deadlock Prevention ƒ Circular Wait A resource-ordering approach: F:RÆN Resource requests must be made in an increasing order of enumeration. National Taiwan University. ƒ Type 1 – strictly increasing order of resource requests. and so on. ƒ Initially. 327 * All rights reserved. 2004. * A single request must be issued for all needed instances of the same resources. Tei-Wei Kuo.

2004. e. .Deadlock Prevention ƒ Type 2 ƒ Processes must release all Ri’s when they request any instance of Rj if F(Ri) ≥ F(Rj) ƒ F : R Æ N must be defined according to the normal order of resource usages in a system. National Taiwan University.. Tei-Wei Kuo.g. F(tape drive) = 1 F(disk drive) = 5 ?? feasible ?? F(printer) = 12 328 * All rights reserved.

2004. National Taiwan University.Deadlock Avoidance ƒ Motivation: ƒ Deadlock-prevention algorithms can cause low device utilization and reduced system throughput! Î Acquire additional information about how resources are to be requested and have better resource allocation! ƒ Processes declare their maximum number of resources of each type that it may need. * All rights reserved. Tei-Wei Kuo. 329 .

Deadlock Avoidance ƒ A Simple Model ƒ A resource-allocation state <# of available resources. 2004. Tei-Wei Kuo. # of allocated resources. National Taiwan University.g. max demands of processes> ƒ A deadlock-avoidance algorithm dynamically examines the resource-allocation state and make sure that it is safe. ƒ e. . 330 * All rights reserved. the system never satisfies the circularwait condition..

…. Tei-Wei Kuo. If so. need ( Pi ) ≤ Available + ∑ allocated ( Pj ) j<i safe unsafe deadlock ƒ Safe State ƒ The existence of a safe sequence ƒ Unsafe Deadlocks are avoided if the system can allocate resources to each process up to its maximum request in some order. the system is in a safe state! * All rights reserved. National Taiwan University. P2. Pn> is a safe sequence if ∀ Pi . 2004.Deadlock Avoidance ƒ Safe Sequence ƒ A sequence of processes <P1. 331 .

(available.2). 332 .2)) How to ensure that the system will always remain in a safe state? * All rights reserved. Tei-Wei Kuo. 2004.Deadlock Avoidance ƒ Example: max needs P0 P1 P2 10 4 9 Allocated 5 2 2 Available 3 • The existence of a safe sequence <P1.3). ( P1. National Taiwan University. the system state is unsafe. P2>.5). ( P 2. • If P2 got one more. Q (( P0. P0.

National Taiwan University. 2004.Deadlock Avoidance – ResourceAllocation Graph Algorithm ƒ One Instance per Resource Type R1 •Request Edge Pi P1 R2 P2 •Assignment Edge Rj •Claim Edge Pi Rj 333 * All rights reserved. Rj resource allocated Pi request made resource release . Tei-Wei Kuo.

2004.Deadlock Avoidance – ResourceAllocation Graph Algorithm R1 P1 R2 P2 A cycle is detected! Î The system state is unsafe! • R2 was requested & granted! Safe state: no cycle Unsafe state: otherwise Cycle detection can be done in O(n2) 334 * All rights reserved. . Tei-Wei Kuo. National Taiwan University.

process Pi may request at most k instances of resource type Rj.j] = Max [i. there are k instances of resource type Ri available. ¾ Need [i. n: # of processes. 335 .j] = k. process Pi may need k more instances of resource type Rj. ƒ Allocation [n.m] ƒ If Max [i. 2004.j] = k. National Taiwan University.m] ƒ If Allocation [i. Tei-Wei Kuo.Deadlock Avoidance – Banker’s Algorithm ƒ Available [m] ƒ If Available [i] = k.m] ƒ If Need [i. ƒ Need [n.j] * All rights reserved. process Pi is currently allocated k instances of resource type Rj. m: # of resource types ƒ Max [n.j] = k.j] – Allocation [i.

then the system is in a safe state. and X≦ Y if X[i] ≦ Y[i] for all i. 1≦ i≦ n 2. Need[i] ≦ Work If no such i exist.Deadlock Avoidance – Banker’s Algorithm ƒ Safety Algorithm – A state is safe?? n: # of processes. m: # of resource types 1. Tei-Wei Kuo. 336 . X < Y if X ≦ Y and Y≠ X * All rights reserved. National Taiwan University. If Finish [i] = T for all i. then goto Step4 3. Work := Work + Allocation[i] Finish [i] := T. 2004. Finish [i] =F 2. Work := Available & Finish [i] := F. respectively. Goto Step2 4. Where Allocation[i] and Need[i] are the i-th row of Allocation and Need. Find an i such that both 1.

Have the system pretend to have allocated resources to process Pi by setting Available := Available – Requesti. If Requesti ≦ Available. and the old resource-allocation state is restored! 337 * All rights reserved. otherwise. otherwise. If Requesti ≦ Needi. Execute “Safety Algorithm”. then Goto Step2. then Goto Step3. 2004. Pi must wait. Needi := Needi – Requesti. . National Taiwan University. Trap 2. Tei-Wei Kuo. Pi must wait. Allocationi := Allocationi + Requesti. 3. otherwise. If the system state is safe. the request is granted.Deadlock Avoidance – Banker’s Algorithm ƒ Resource-Request Algorithm Requesti [j] =k: Pi requests k instance of resource type Rj 1.

2004.P0> is a safe sequence. Tei-Wei Kuo.P3. National Taiwan University. .P2. 338 * All rights reserved.P4.Deadlock Avoidance ƒ An Example Allocation A P0 P1 P2 P3 P4 0 2 3 2 0 B 1 0 0 1 0 C 0 0 2 1 2 A 7 3 9 2 4 Max B 5 2 0 2 3 C 3 2 2 2 3 A 7 1 6 0 4 Need B 4 2 0 1 3 C 3 2 0 1 1 A 3 Available B 3 C 2 • A safe state ∵ <P1.

2) ≦ (3.0.P4.P0.2) Requesti ≦ Available ((1.2. • Request0 = (0.0) is asked later. Tei-Wei Kuo. 2004. .2)) Allocation A P0 P1 P2 P3 P4 0 3 3 2 0 B 1 0 0 1 0 C 0 2 2 1 2 A 7 0 6 0 4 Need B 4 2 0 1 3 C 3 0 0 1 1 A 2 Available B 3 C 0 Æ Safe ∵ <P1. 339 * All rights reserved.0.P2> is a safe sequence! • If Request4 = (3.3.0) must be rejected because it results in an unsafe state. National Taiwan University. it must be rejected.Deadlock Avoidance Let P1 make a request Requesti = (1.3.P3.

ƒ Overheads: ƒ Cost of information maintenance ƒ Cost of executing a detection algorithm ƒ Potential loss inherent from a deadlock recovery 340 * All rights reserved. Tei-Wei Kuo. National Taiwan University. 2004.Deadlock Detection ƒ Motivation: ƒ Have high resource utilization and “maybe” a lower possibility of deadlock occurrence. .

2004. 341 . • The system needs to maintain the wait-for graph * All rights reserved.Deadlock Detection – Single Instance per Resource Type P5 R1 P1 R2 R3 P2 P4 R4 P3 R5 P1 P2 P3 P5 P4 A Resource-Allocation Graph Pi Rq Pj A Wait-For Graph Pi Pj • Detect an cycle in O(n2). Tei-Wei Kuo. National Taiwan University.

m]: current resource allocation to each process Request[1... .j] = k.Deadlock Detection – Multiple Instance per Resource Type ƒ Data Structures ƒ n: # of processes.n. Pi requests k more instances of resource type Rj 342 * All rights reserved. 1.. National Taiwan University.m]: # of available resource instances Allocation[1. m: # of resource types ƒ ƒ Available[1.m]: the current request of each process ƒ If Request[i.. Tei-Wei Kuo.. 1. 2004.n.

National Taiwan University. Finish[i] = F b. then Finish[i] = F. 2. n. If Finish[i] = F for some i. if Allocation[i] ≠ 0. …. If Finish[i] = F.Deadlock Detection – Multiple Instance per Resource Type 1. 2. otherwise. then the system is in a deadlock state. Work := Work + Allocation[i] Finish[i] := T Goto Step 2 4. Finish[i] =T. For i = 1. 2004. Find an i such that both a. Work := Available. Goto Step 4 3. . Tei-Wei Kuo. Request[i] ≦ Work If no such i. then process Pi is deadlocked. Complexity = O(m * n2) 343 * All rights reserved.

2004. * All rights reserved.Deadlock Detection – Multiple Instance per Resource Type ƒ An Example Allocation A P0 P1 P2 P3 P4 0 2 3 2 0 B 1 0 0 1 0 C 0 0 3 1 2 A 0 2 0 1 0 Request B 0 0 0 0 0 C 0 2 0 0 2 A 0 Available B 2 C 0 Î Find a sequence <P0. and P4 are 344 deadlocked. P3. National Taiwan University. P4> such that Finish[i] = T for all i. If Request2 = (0. .1) is issued. P2. then P1.0. P3. P2. Tei-Wei Kuo. P1.

Deadlock Detection – Algorithm Usage ƒ When should we invoke the detection algorithm? ƒ How often is a deadlock likely to occur? ƒ How many processes will be affected by a deadlock? overheads Every - + rejected processes affected ∞ + request - ƒ Time for Deadlock Detection? ƒ CPU Threshold? Detection Frequency? … 345 * All rights reserved. 2004. Tei-Wei Kuo. National Taiwan University. .

346 * All rights reserved. ƒ Preempt some resources from one or more deadlocked processes. 2004. National Taiwan University. . Tei-Wei Kuo. ƒ The system recover from the deadlock automatically. ƒ Possible Solutions ƒ Abort one or more processes to break the circular wait.Deadlock Recovery ƒ Whose responsibility to deal with deadlocks? ƒ Operator deals with the deadlock manually.

can we abort any process? Should we compensate any damage caused by aborting? 347 * All rights reserved. National Taiwan University. Tei-Wei Kuo. . 2004.Deadlock Recovery – Process Termination ƒ Process Termination ƒ Abort all deadlocked processes! ƒ Simple but costly! ƒ Abort one process at a time until the deadlock cycle is broken! ƒ Overheads for running the detection again and again. ƒ The difficulty in selecting a victim! But.

Tei-Wei Kuo. ƒ The numbers and types of resources used and needed by a process ƒ Process’s characteristics such as “interactive or batch” ƒ The number of processes needed to be aborted. National Taiwan University. 2004. 348 * All rights reserved.Deadlock Recovery – Process Termination ƒ What should be considered in choosing a victim? ƒ Process priority ƒ The CPU time consumed and to be consumed by a process. .

.Deadlock Recovery – Resource Preemption ƒ Goal: Preempt some resources from processes from processes and give them to other processes until the deadlock cycle is broken! ƒ Issues ƒ Selecting a victim: ƒ It must be cost-effective! ƒ Roll-Back ƒ How far should we roll back a process whose resources were preempted? ƒ Starvation ƒ Will we keep picking up the same process as a victim? 349 ƒ How to control the # of rollbacks per process efficiently? * All rights reserved. National Taiwan University. Tei-Wei Kuo. 2004.

2004.Deadlock Recovery – Combined Approaches ƒ Partition resources into classes that are hierarchically ordered. National Taiwan University. . ⇒ No deadlock involves more than one class ƒ Handle deadlocks in each class independently 350 * All rights reserved. Tei-Wei Kuo.

e.g. PCB → Prevention through resource ordering ƒ Central Memory: User Memory → Prevention through resource preemption ƒ Job Resources: Assignable devices and files ƒ → Avoidance ∵ This info may be obtained! Swappable Space: Space for each user process on the backing store → Pre-allocation ∵ the maximum need is known! 351 * All rights reserved. 2004.. . Tei-Wei Kuo. National Taiwan University.Deadlock Recovery – Combined Approaches Examples: ƒ Internal Resources: Resources used by the system.

Processes 5. Operating-System Structures 4. CPU Scheduling 7. Process Synchronization 8. Virtual Memory 11. File Systems * All rights reserved. National Taiwan University. 352 . Computer-System Structures 3. 2004. Threads 6. Introduction 2. Deadlocks 9. Tei-Wei Kuo.Contents 1. Memory Management 10.

Chapter 9 Memory Management 353 .

National Taiwan University. .Memory Management ƒ Motivation ƒ Keep several processes in memory to improve a system’s performance ƒ Selection of different memory management methods ƒ Application-dependent ƒ Hardware-dependent ƒ Memory – A large array of words or bytes. ƒ Memory is always too small! 354 * All rights reserved. each with its own address. Tei-Wei Kuo. 2004.

Memory Management ƒ The Viewpoint of the Memory Unit ƒ A stream of memory addresses! ƒ What should be done? ƒ Which areas are free or used (by whom) ƒ Decide which processes to get memory ƒ Perform allocation and de-allocation ƒ Remark: ƒ Interaction between CPU scheduling and memory allocation! * All rights reserved. 2004. Tei-Wei Kuo. 355 . National Taiwan University.

COM At load time: . National Taiwan University. x Relocatable address dynamically loaded system library Absolute address 356 * All rights reserved. 2004..Code is relocatable . Tei-Wei Kuo.Fixed while a program runs At execution time . .g.Background ƒ Address Binding – binding of instructions and data to memory addresses Binding Time Known at compile time.binding may change as a program run source program compiling object module other object modules linking load module system library loading in-memory binary memory image symbolic address e.“absolute code” MS-DOS *.All memory reference by a program will be translated . where a program will be in memory .

National Taiwan University. .Background Main Memory • Binding at the Compiling Time •A process must execute at a specific memory space • Binding at the Load Time • Relocatable Code • Process may move from a memory segment to another → binding is delayed till run-time 357 * All rights reserved. 2004. Tei-Wei Kuo.

2004. Tei-Wei Kuo. . National Taiwan University.Logical Versus Physical Address Logical Address CPU + Relocation Register Physical Address 346 The user program deals with logical addresses .Virtual Addresses (binding at the run time) 14346 Memory Address Register Memory 14000 Memory Management Unit (MMU) – “Hardware-Support” 358 * All rights reserved.

Logical Versus Physical Address ƒ A logical (physical) address space is the set of logical (physical) addresses generated by a process. Different memory mapping schemes need different MMU’s that are hardware devices. National Taiwan University. (slow down) ƒ Compile-time & load-time binding schemes results in the collapsing of logical and physical address spaces. . 2004. Physical addresses of a program is transparent to any process! ƒ MMU maps from virtual addresses to physical addresses. 359 * All rights reserved. Tei-Wei Kuo.

Dynamic Loading ƒ Dynamic Loading ƒ A routine will not be loaded until it is called. National Taiwan University. 2004. . ƒ Users may use OS-provided libraries to achieve dynamic loading 360 * All rights reserved. Tei-Wei Kuo. A relocatable linking loader must be called to load the desired routine and change the program’s address tables. ƒ Advantage ƒ Memory space is better utilized.

Simple 361 .Dynamic Linking ƒ Dynamic Linking Static Linking language library A small piece of code. called + stub. National Taiwan University. is used to locate or load program object module the appropriate routine binary program image Advantage Save memory space by sharing the library code among processes Æ Memory Protection & Library Update! * All rights reserved. 2004. Tei-Wei Kuo.

National Taiwan University. ƒ Example: Two overlays of a two-pass assembler Symbol table 20KB Certain relocation & linking algorithms are needed! common routines 30KB overlay driver 70KB Pass 1 * All rights reserved. 10KB Pass 2 80KB 362 . 2004.Overlays ƒ Motivation ƒ Keep in memory only those instructions and data needed at any given time. Tei-Wei Kuo.

Tei-Wei Kuo. it’s not easy to program a overlay structure properly! ⇒ Need some sort of automatic techniques that run a large program in a limited physical memory! 363 * All rights reserved. . National Taiwan University. ƒ Overlays can be achieved w/o OS support: ⇒ “absolute-address” code ƒ However. 2004.Overlays ƒ Memory space is saved at the cost of run-time I/O.

364 . National Taiwan University.Swapping OS swap out User Space Process p1 Process p2 swap in Main Memory Backing Store Should a process be put back into the same memory space that it occupied previously? ↔ Binding Scheme?! * All rights reserved. Tei-Wei Kuo. 2004.

2004.Swapping ƒ A Naive Way Pick up a process from the ready queue Dispatcher checks whether the process is in memory Yes Dispatch CPU to the process No Swap in the process Potentially High Context-Switch Cost: 2 * (1000KB/5000KBps + 8ms) = 416ms Transfer Time Latency Delay * All rights reserved. 365 . Tei-Wei Kuo. National Taiwan University.

⇒ = 100ms 1000k per sec + Users must keep the system informed of memory usage.g.Swapping ƒ The execution time of each process should be long relative to the swapping time in this case (e. National Taiwan University. 416ms in the last example)! 100k disk ƒ Only swap in what is actually used. I/O buffering Pi I/O buffering ?I/O? 366 . Tei-Wei Kuo.. 2004. Memory ƒ Who should be swapped out? OS 100k disk =100ms 1000k persec + ƒ “Lower Priority” Processes? ƒ Any Constraint? ⇒ System Design * All rights reserved.

and many processes are running! ƒ In Windows 3.Swapping ƒ Separate swapping space from the file system for efficient usage ƒ Disable swapping whenever possible such as many versions of UNIX – Swapping is triggered only if the memory usage passes a threshold. . 367 * All rights reserved. 2004. a swapped-out process is not swapped in until the user selects the process to run. National Taiwan University. Tei-Wei Kuo.1.

Contiguous Allocation – Single User 0000 a b OS User limit register relocation register a Unused 8888 ƒ ƒ b A single user is allocated as much memory as needed Problem: Size Restriction → Overlays (MS/DOS) 368 * All rights reserved. Tei-Wei Kuo. National Taiwan University. . 2004.

2004.Contiguous Allocation – Single User ƒ Hardware Support for Memory Mapping and Protection limit register relocation register CPU logical address < No trap Yes + physical address memory Disadvantage: Wasting of CPU and Resources ∵ No Multiprogramming Possible * All rights reserved. Tei-Wei Kuo. National Taiwan University. 369 .

National Taiwan University.g. 2004. OS/360 (or MFT) ƒ A process is allocated on an entire partition ƒ An OS Data Structure: Partitions size # location status 20k Partition 1 45k proc 1 proc 7 proc 5 Partition 2 60k Partition 3 Partition 4 90k 100k 1 2 “fragmentation” 3 4 25KB 15KB 30KB 10KB 20k 45k 60k 90k Used Used Used Free 370 * All rights reserved.Contiguous Allocation – Multiple Users ƒ Fixed Partitions ƒ Memory is divided into fixed partitions.. Tei-Wei Kuo. . e.

Tei-Wei Kuo. 2004.Contiguous Allocation – Multiple Users ƒ Hardware Supports ƒ Bound registers ƒ Each partition may have a protection key (corresponding to a key in the current PSW) ƒ Disadvantage: ƒ Fragmentation gives poor memory utilization ! 371 * All rights reserved. National Taiwan University. .

Tei-Wei Kuo. ƒ OS tables record free and used partitions 20k 40k 70k 90k 110k OS Process 1 Used Base = 20k size = 20KB user = 1 Base = 70k size = 20KB user = 2 free Process 2 Free Input Queue Base = 40k size = 30KB Base = 90k size = 20KB free P3 with a 40KB memory request ! * All rights reserved. 372 . 2004.Contiguous Allocation – Multiple Users ƒ Dynamic Partitions ƒ Partitions are dynamically created. National Taiwan University.

. Tei-Wei Kuo.Contiguous Allocation – Multiple Users ƒ Solutions for dynamic storage allocation : ƒ First Fit – Find a hole which is big enough Better in Time and Storage Usage ƒ Advantage: Fast and likely to have large chunks of memory in high memory locations ƒ Best Fit – Find the smallest hole which is big enough. → It might need a lot of search time and create lots of small fragments ! ƒ Advantage: Large chunks of memory available ƒ Worst Fit – Find the largest hole and create a new partition out of it! ƒ Advantage: Having largest leftover holes with lots of search time! 373 * All rights reserved. 2004. National Taiwan University.

. Tei-Wei Kuo. 2004. National Taiwan University.Contiguous Allocation Example – First Fit Process Memory Time (RR Scheduler with Quantum = 1) A job queue 400k OS 400k 1000k 2000k 2300k OS P1 P2 P3 Time = “0” 400k 1000k 2000k 2300k 2560k OS P1 P1 P2 P3 P4 P5 600KB 10 1000KB 5 300KB 20 700KB 8 500KB 15 P3 Time = 14 400k 900k 1000k P2 terminates & frees its memory 2560k Time = 0 2560k 400k 1000k 1700k 2000k 2300k 2560k OS P1 P4 P3 Time = “14” 400k 1000k 1700k 2000k 2300k 2560k OS P4 300KB OS P5 P4 P3 Time = “28” 374 P3 Time = 28 + 260KB 1700k 2000k 560KB 2300k P5? 2560k * All rights reserved.

b.Fragmentation – Dynamic Partitions ƒ External fragmentation occurs as small chunks of memory accumulate as a byproduct of partitioning due to imperfect fits. Merge adjacent free areas. National Taiwan University. Tei-Wei Kuo. ƒ ƒ Statistical Analysis For the First-Fit Algorithm: ƒ 1/3 memory is unusable – 50-percent rule Solutions: a. . 2004. Compaction Compact all free areas into one contiguous region Requires user processes to be relocatable 375 Any optimal compaction strategy??? * All rights reserved.

Fragmentation – Dynamic Partitions 0 300K 500K 600K 1000K 1200K 1500K 1900K 2100K OS P1 P2 400KB P3 300KB P4 200KB 0 300K 500K 600K 800K 1200K OS P1 P2 *P3 *P4 900K 0 300K 500K 600K 1000K 1200K OS P1 P2 *P4 P3 900K 0 300K 500K 600K OS P1 P2 900K 1500K 1900K 2100K 2100K 2100K P3 *P4 MOVE 600KB MOVE 400KB MOVE 200KB ƒ Cost: Time Complexity O(n!)?!! ƒ Combination of swapping and compaction ƒ Dynamic/static relocation * All rights reserved. 2004. Tei-Wei Kuo. National Taiwan University. 376 .

2004.002 bytes to P3 and have 2 bytes as an internal 377 fragmentation! * All rights reserved.Fragmentation – Dynamic Partitions ƒ Internal fragmentation: A small chunk of “unused” memory internal to a partition. National Taiwan University. Tei-Wei Kuo. OS P1 20. P3 request 20KB ?? give P3 20KB & leave a 2-byte free area?? .002 bytes P2 Reduce free-space maintenance cost Æ Give 20.

Fragmentation – Dynamic Partitions ƒ Dynamic Partitioning: ƒ Advantage: ⇒ Eliminate fragmentation to some degree ⇒ Can have more partitions and a higher degree of multiprogramming ƒ Disadvantage: ƒ Compaction vs Fragmentation ƒ The amount of free memory may not be enough for a process! (contiguous allocation) ƒ Memory locations may be allocated but never referenced. . Tei-Wei Kuo. National Taiwan University. 2004. ƒ Relocation Hardware Cost & Slow Down ⇒ Solution: Paged Memory! 378 * All rights reserved.

2004. ƒ The logical memory space of each process is divided into blocks of the same size called pages. * All rights reserved. 379 . ƒ The backing store is also divided into blocks of the same size if used. Tei-Wei Kuo. National Taiwan University.Paging ƒ Objective ƒ Users see a logically contiguous address space although its physical addresses are throughout physical memory ƒ Units of Memory and Backing Store ƒ Physical memory is divided into fixed-sized blocks called frames.

Paging – Basic Method Logical Address CPU p d Page Offset f d f Physical Address Page Table Page Number p d …… .. f …… Base Address of Page p 380 * All rights reserved. . Tei-Wei Kuo. National Taiwan University. 2004.

2004. it is from 512B or 16KB. National Taiwan University.Paging – Basic Method ƒ Address Translation page size page offset p m-n m d n max number of pages: 2m-n Logical Address Space: 2m Physical Address Space: ??? ƒ A page size tends to be a power of 2 for efficient address translation. ƒ The actual page size depends on the computer architecture. . Today. 381 * All rights reserved. Tei-Wei Kuo.

National Taiwan University. 2004. .Paging – Basic Method Page 0 0 4 A 8 0 1 2 3 1 2 12 B C D Logical Memory 3 16 5 6 1 2 Page Table 0 4 8 12 16 20 24 28 110 01 C D A B Frame 0 1 2 3 4 5 6 7 Logical Address 1*4+1=5 Physical Memory 01 01 Physical Address = 6 * 4 + 1 = 25 382 * All rights reserved. Tei-Wei Kuo.

ƒ 4-byte page table entry & 4KB per page Æ 232 * 212B = 244B = 16TB of physical memory Page Size Disk I/O Efficiency Page Table Maintenance Internal Fragmentation 383 * All rights reserved. Tei-Wei Kuo.Paging – Basic Method ƒ No External Fragmentation ƒ Paging is a form of dynamic relocation. 2004. ƒ The average internal fragmentation is about one-half page per process ƒ The page size generally grows over time as processes. National Taiwan University. * Example: 8KB or 4KB for Solaris. . and memory have become larger. data sets.

2004.Paging – Basic Method ƒ Page Replacement: ƒ An executing process has all of its pages in physical memory. . Tei-Wei Kuo. Æ Paging increases context-switch time! 384 * All rights reserved. ƒ Maintenance of the Frame Table ƒ One entry for each physical frame ƒ The status of each frame (free or allocated) and its owner ƒ The page table of each process must be saved when the process is preempted. National Taiwan University.

A Page Table 385 .Paging – Hardware Support ƒ Page Tables ƒ Where: Registers or Memory ƒ Efficiency is the main consideration! ƒ The use of registers for page tables ƒ The page table must be small! ƒ The use of memory for page tables ƒ Page-Table Base Register (PTBR) a * All rights reserved. 2004. National Taiwan University. Tei-Wei Kuo.

ƒ Disadvantages: ƒ Memory access is slowed by a factor of 2 ƒ Translation Look-aside buffers (TLB) ƒ Associate. value) – 16 ~ 1024 entries ƒ Less than 10% memory access time 386 * All rights reserved. . 2004. Tei-Wei Kuo.Paging – Hardware Support ƒ Page Tables on Memory ƒ Advantages: ƒ The size of a page table is unlimited! ƒ The context switch cost may be low if the CPU dispatcher merely changes PTBR. high-speed memory ƒ (key/tag. National Taiwan University. instead of reloading another page table.

Paging – Hardware Support ƒ Translation Look-aside Buffers(TLB): ƒ Disadvantages: Expensive Hardware and Flushing of Contents for Switching of Page Tables ƒ Advantage: Fast – Constant-Search Time key item value 387 * All rights reserved. 2004. . National Taiwan University. Tei-Wei Kuo.

. Tei-Wei Kuo. 2004. f Page Table • Update TLB if a TLB miss occurs! • Replacement of TLB entries might 388 be needed. TLB Miss * Address-Space Identifiers (ASID) in TLB for process matching? Protection? Flush? * All rights reserved. National Taiwan University. TLB TLB Hit Physical Memory f d Physical Address ……..Paging – Hardware Support Logical Address CPU p d Page# Frame# p ….

Tei-Wei Kuo. .Paging – Effective Memory Access Time ƒ Hit Ratio = the percentage of times that a page number is found in the TLB ƒ The hit ratio of a TLB largely depends on the size and the replacement strategy of TLB entries! ƒ Effective Memory Access Time ƒ Hit-Ratio * (TLB lookup + a mapped memory access) + (1 – Hit-Ratio) * (TLB lookup + a page table lookup + a mapped memory access) 389 * All rights reserved. 2004. National Taiwan University.

when hit ratio = 80% ƒ Effective access time = 0.98*120ns +0.02*220ns = 122 ns.2*220ns = 140 ns. .Paging – Effective Memory Access Time ƒ An Example ƒ 20ns per TLB lookup. National Taiwan University. when hit ratio = 98% ƒ Intel 486 has a 32-register TLB and claims a 98 percent hit ratio. 2004. Tei-Wei Kuo.8*120ns +0. 390 * All rights reserved. 100ns per memory access ƒ Effective Access Time = 0.

Paging – Protection & Sharing ƒ Protection y y y y v v 2 7 3 Page Table memory r/w/e dirty Is the page in memory? v 1 0 ƒ Use a Page-Table Length Register (PTLR) to indicate the size of the page table. 2004. 391 * All rights reserved. Valid-Invalid Bit Valid Page? Modified? r/w/e protected: 100r. Tei-Wei Kuo. 110rw. … . ƒ Unused Paged table entries might be ignored during maintenance. National Taiwan University. 010w.

2004. National Taiwan University. Tei-Wei Kuo.287 Page Table (PTLR entries?) 0 1 2 3 4 5 6 7 8 9 P0 P1 P2 P3 P4 P5 392 * All rights reserved.Paging – Protection & Sharing ƒ Example: a 12287-byte Process (16384=214) 0 2K 4K 6K 8K 10K 10.468 … P0 P1 P2 P3 P4 P5 Logical address p d 3 11 0 1 2 3 4 5 6 7 V V V V V V i i 2 3 4 7 8 9 0 0 12. .

ƒ Reentrant procedures can be saved! The non-modified nature of saved code must be enforced 393 ƒ Address referencing inside shared pages could be an issue. National Taiwan University. * * * * * * * :: data2 ed1 ed2 ed3 page 0 1 2 3 4 5 6 7 n ƒ Procedures which are executed often (e..g. Tei-Wei Kuo. Memory can be saved a lot. editor) can be divided into procedure + date. .Paging – Protection & Sharing P1 *ed1 *ed2 *ed3 * Data 1 * data1 Page Table 1 3 4 6 1 P2 *ed1 *ed2 *ed3 * Data 2 Page Table 2 3 4 6 7 * All rights reserved. 2004.

.g. National Taiwan University. . e. Tei-Wei Kuo. 32-bit address Æ 220 page entries Æ 4MB 4KB per page 4B per entries page table Æ Even the page table must be divided into pieces to fit in the memory! 394 * All rights reserved. 2004. 232 to 264 Bytes.Multilevel Paging ƒ Motivation ƒ The logical address space of a process in many modern computer system is very large.

2004. 395 . National Taiwan University.Multilevel Paging – Two-Level Paging Logical Address P1 P2 d P1 P2 d Outer-Page Table A page of page table Physical Memory PTBR Forward-Mapped Page Table * All rights reserved. Tei-Wei Kuo.

. Tei-Wei Kuo. Pn d P1 PTBR N pieces P2 … Pn Physical Memory d 1 * All rights reserved. National Taiwan University. 2004. + 1 +… + 1 + 1 = n+1 accesses 396 .Multilevel Paging – N-Level Paging ƒ Motivation: Two-level paging is not appropriate for a huge logical address space! Logical Address P1 P2 .

Tei-Wei Kuo. 4-level paging. 100ns memory access time. 2004. ƒ Effective access time = 0.02 X 520ns = 128ns ƒ SUN SPARC (32-bit addressing) Æ 3-level paging ƒ Motorola 68030 (32-bit addressing) Æ 4- level paging ƒ VAX (32-bit addressing) Æ 2-level paging 397 * All rights reserved. 20ns TLB access time. National Taiwan University.Multilevel Paging – N-Level Paging ƒ Example ƒ 98% hit ratio.98 X 120ns + 0. .

Hashed Page Tables ƒ Objective: ƒ To handle large address spaces ƒ Virtual address Æ hash function Æ a linked list of elements ƒ (virtual page #.g.. e. . 2004. 398 * All rights reserved. Tei-Wei Kuo. National Taiwan University. a pointer) ƒ Clustered Page Tables ƒ Each entry contains the mappings for several physical-page frames. 16. frame #.

Page Number. 399 . Tei-Wei Kuo. Offset> Logical Address CPU pid P d f d Physical Memory Physical Address pid: p An Inverted Page Table * All rights reserved. ƒ Virtual Address: <Process ID.Inverted Page Table ƒ Motivation ƒ A page table tends to be big and does not correspond to the # of pages residing in the physical memory. ƒ Each entry corresponds to a physical frame. 2004. National Taiwan University.

Inverted Page Table ƒ Each entry contains the virtual address of the frame. PowerPC. 400 . the page table of the corresponding process must be referenced. ƒ When no match is found. ƒ Example Systems: HP Spectrum. National Taiwan University. Tei-Wei Kuo. ƒ Entries are sorted by physical addresses. IBM RT. SUN UltraSPARC Logical Address CPU pid P d f d Physical Memory Physical Address pid: p An Inverted Page Table * All rights reserved. ƒ One table per system. 2004.

ƒ The use of Hash Table to eliminate lengthy table lookup time: 1HASH + 1IPT ƒ The use of an associate memory to hold recently located entries.Inverted Page Table ƒ Advantage ƒ Decrease the amount of memory needed to store each page table ƒ Disadvantage ƒ The inverted page table is sorted by physical addresses. Tei-Wei Kuo. whereas a page reference is in logical address. ƒ Difficult to implement with shared memory 401 * All rights reserved. . 2004. National Taiwan University.

2004. Tei-Wei Kuo.Segmentation ƒ Segmentation is a memory management scheme that support the user view of memory: ƒ A logical address space is a collection of segments with variable lengths. National Taiwan University. . Subroutine Stack Symbol Sqrt table Main program 402 * All rights reserved.

National Taiwan University. offset> 403 * All rights reserved. Tei-Wei Kuo.Segmentation ƒ Why Segmentation? ƒ Paging separates the user’s view of memory from the actual physical memory but does not reflect the logical units of a process! ƒ Pages & frames are fixed-sized. but segments have variable sizes. 2004. . ƒ For simplicity of representation. <segment name. offset> Æ <segmentnumber.

. Tei-Wei Kuo. National Taiwan University. 2004.Segmentation – Hardware Support ƒ Address Mapping s CPU s d limit base limit Segment Table base d < trap yes d + Physical Memory no 404 * All rights reserved.

Segment table STLR 405 . National Taiwan University.Segmentation – Hardware Support ƒ Implementation in Registers – limited size! ƒ Implementation in Memory ƒ Segment-table base register (STBR) ƒ Segment-table length register (STLR) ƒ Advantages & Disadvantages – Paging ƒ Use an associate memory (TLB) to improve the effective memory access time ! ƒ TLB must be flushed whenever a new segment table is used ! a STBR * All rights reserved. 2004. Tei-Wei Kuo.

National Taiwan University. 2004. . 406 * All rights reserved. ƒ Sharing occurs at the segment level. Tei-Wei Kuo.Segmentation – Protection & Sharing ƒ Advantage: ƒ Segments are a semantically defined portion of the program and likely to have all entries being “homogeneous”. Æ Logical units for protection ! ƒ Sharing of code & data improves memory usage. ƒ Example: Array. code. stack. etc. data.

2004. Tei-Wei Kuo.Segmentation – Protection & Sharing ƒ Potential Problems ƒ External Fragmentation ƒ Segments must occupy contiguous memory. 407 . National Taiwan University. ƒ Address referencing inside shared segments can be a big issue: Seg# offset Indirect addressing?!!! Should all shared-code segments have the same segment number? ƒ How to find the right segment number if the number of users sharing the segments increase! Æ Avoid reference to segment # * All rights reserved.

average segment sizes Size A byte * All rights reserved. External Fragmentation Overheads increases substantially! (base+limit “registers”) 408 .. Tei-Wei Kuo.Segmentation – Fragmentation ƒ Motivation: Segments are of variable lengths! Æ Memory allocation is a dynamic storage-allocation problem. e. 2004.g. ƒ best-fit? first-fit? worst-ft? ƒ External fragmentation will occur!! ƒ Factors. National Taiwan University.

Segmentation – Fragmentation ƒ Remark: ƒ Its external fragmentation problem is better than that of the dynamic partition method because segments are likely to be smaller than the entire process. . Tei-Wei Kuo. ƒ Internal Fragmentation?? 409 * All rights reserved. 2004. National Taiwan University.

. 2004. National Taiwan University. Tei-Wei Kuo. ƒ Paging has internal fragmentation. ƒ Segments are semantically defined portions of a program. Æ “Page” Segments ! 410 * All rights reserved.Segmentation with Paging ƒ Motivation : ƒ Segmentation has external fragmentation.

Max Segment Size = 4GB ƒ Tables: ƒ Local Descriptor Table (LDT) ƒ Global Descriptor Table (GDT) ƒ 6 microprogram segment registers for caching Selector Segment Offset Logical Address s 13 g p 1 2 sd 32 Linear Address * All rights reserved. 2004.Paged Segmentation – Intel 80386 ƒ 8K Private Segments + 8K Public Segments ƒ Page Size = 4KB. National Taiwan University. Tei-Wei Kuo. p1 10 p2 10 d 12 411 .

Paged Segmentation – Intel 80386 Logical Address 16 32 s+g+p sd Descriptor Table : Segment Length : Segment Base >no Trap f d Physical Memory Physical address : Segment table : + 10 10 Page Directory Base Register p1 . National Taiwan University. Page Table 412 . f 12 p1 p2 d Page Directory *Page table are limited by the segment lengths of their segments. * All rights reserved. Tei-Wei Kuo. 2004. p2 .

ƒ Address translation overheads increase! ƒ An entire process still needs to be in memory at once! Æ Virtual Memory!! 413 * All rights reserved. ƒ Variable size page tables. ƒ Segment need not be in contiguous memory. ƒ Segmented paging – segment the page table. 2004. National Taiwan University. Tei-Wei Kuo. .Paging and Segmentation ƒ To overcome disadvantages of paging or segmentation alone: ƒ Paged segments – divide segments further into pages.

g. etc. . ƒ Performance ƒ Fragmentation ƒ Multiprogramming Levels ƒ ƒ ƒ ƒ Relocation Constraints? Swapping: + Sharing?! Protection?! 414 * All rights reserved. STBR. Tei-Wei Kuo. 2004. e. National Taiwan University.. TLB.Paging and Segmentation ƒ Considerations in Memory Management ƒ Hardware Support.

Computer-System Structures 3. Deadlocks 9. CPU Scheduling 7. Threads 6. Processes 5. National Taiwan University. File Systems * All rights reserved. Tei-Wei Kuo. Virtual Memory 11. Introduction 2.Contents 1. Process Synchronization 8. Operating-System Structures 4. Memory Management 10. 415 . 2004.

Chapter 10 Virtual Memory 416 .

ƒ Motivation: ƒ An entire program in execution may not all be needed at the same time! ƒ e. error handling routines. certain program features. a large array. 2004. Tei-Wei Kuo. etc 417 * All rights reserved. . National Taiwan University.g.Virtual Memory ƒ Virtual Memory ƒ A technique that allows the execution of a process that may not be completely in memory.

ƒ Each user program may run faster because less I/O is needed for loading or swapping user programs. 418 * All rights reserved. ƒ The level of multiprogramming increases because processes occupy less physical memory. ƒ Implementation: demand paging. Tei-Wei Kuo. demand segmentation (more difficult). National Taiwan University.etc. 2004. . Users can concentrate on their problem programming.Virtual Memory ƒ Potential Benefits ƒ Programs can be much larger than the amount of physical memory.

ƒ A page fault occurs when a process references a non-memory-resident page. 419 * All rights reserved. Tei-Wei Kuo. Rather than swap in the entire process image into memory. 2004. . National Taiwan University.Demand Paging – Lazy Swapping ƒ Process image may reside on the backing store. Lazy Swapper only swap in a page when it is needed! ƒ Pure Demand Paging – Pager vs Swapper ƒ A Mechanism required to recover from the missing of non-resident referenced pages.

Page Table * All rights reserved.Demand Paging – Lazy Swapping 0 CPU p d f d 1 2 3 A B C D E F Logical Memory 4 6 9 v i v i i v i i 4-A 5 valid-invalid bit invalid page? non-memoryresident page? 6-C 7 8 9 -F . National Taiwan University. 2004. . . Tei-Wei Kuo. 420 Physical Memory .

i 5.A Procedure to Handle a Page Fault 3. Trap (valid disk-resident page) 1. Tei-Wei Kuo. 2004. Bring in the missing page 421 . Reset the Page Table Free Frame 4. Issue a ‘read” instruction & find a free frame OS 2. Reference CPU 6. National Taiwan University. Return to execute the instruction * All rights reserved.

.A Procedure to Handle A Page Fault ƒ Pure Demand Paging: ƒ Never bring in a page into the memory until it is required! ƒ Pre-Paging ƒ Bring into the memory all of the pages that “will” be needed at one time! ƒ Locality of reference 422 * All rights reserved. Tei-Wei Kuo. National Taiwan University. 2004.

423 * All rights reserved. National Taiwan University. ƒ Secondary Storage ƒ Swap space in the backing store ƒ A continuous section of space in the secondary storage for better performance. 2004. . Tei-Wei Kuo.Hardware Support for Demand Paging ƒ New Bits in the Page Table ƒ To indicate that a page is now in memory or not.

Crucial issues ƒ Example 1 – Cost in restarting an instruction ƒ Assembly Instruction: Add a. . fetch operands. etc ƒ Strategy: ƒ Get all pages and restart the instruction from the beginning! 424 * All rights reserved. decode. c ƒ Only a short job! ƒ Re-fetch the instruction. National Taiwan University. Tei-Wei Kuo. 2004. b. execute. save.

Crucial Issues
ƒ Example 2 – Block-Moving Assembly Instruction
MVC x, y, 4 x: A AB B C Page fault! C D Return?? D X is destroyed

ƒ MVC x, y, 256 y: ƒ IBM System 360/ 370 ƒ Characteristics ƒ More expensive ƒ “self-modifying” “operands” ƒ Solutions: ƒ Pre-load pages ƒ Pre-save & recover before page-fault services
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

425

Crucial Issues
ƒ Example 3 – Addressing Mode
(R2) + MOV (R2)+, -(R3) - (R3) When the page fault is serviced, R2, R3 are modified! - Undo Effects!
426
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Page Fault

Performance of Demand Paging
ƒ Effective Access Time:
ƒ ma: memory access time for paging ƒ p: probability of a page fault ƒ pft: page fault time (1 - p) * ma + p * pft

427
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Performance of Demand Paging
ƒ Page fault time - major components
ƒ Components 1&3 (about 103 ns ~ 105 ns)
ƒ Service the page-fault interrupt ƒ Restart the process

ƒ Component 2 (about 25ms)
ƒ Read in the page (multiprogramming! However, let’s get the taste!) ƒ pft ≈ 25ms = 25,000,000 ns

ƒ Effect Access Time (when ma = 100ns)
ƒ (1-p) * 100ns + p * 25,000,000 ns ƒ 100ns + 24,999,900ns * p
428
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Performance of Demand Paging
ƒ Example (when ma = 100ns)
ƒ p = 1/1000 ƒ Effect Access Time ≈ 25,000 ns → Slowed down by 250 times ƒ How to only 10% slow-down?
110 > 100 * (1-p) + 25,000,000 * p p < 0.0000004 p < 1 / 2,500,000

429
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Performance of Demand Paging
ƒ How to keep the page fault rate low?
ƒ Effective Access Time ≈ 100ns + 24,999,900ns * p

ƒ Handling of Swap Space – A Way to Reduce Page Fault Time (pft)
ƒ Disk I/O to swap space is generally faster than that to the file system.
ƒ Preload processes into the swap space before they start up. ƒ Demand paging from file system but do page replacement to the swap space. (BSD UNIX)
430
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Process Creation
ƒ Copy-on-Write
P1
Page Table 1

3 4 6 1

ƒ Rapid Process Creation and Reducing of New Pages for the New Process ƒ fork(); execve()
ƒ Shared pages Æ copy-on-write pages ƒ Only the pages that are modified are copied!
* data1 page 0 1 2 * * * * ed1 ed2 3 4 5 * * ?? ed3 6 7 :: n
431

P2
Page Table 2

3 4 6 1

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

* Windows 2000, Linux, Solaris 2 support this feature!

Process Creation
ƒ Copy-on-Write
ƒ zero-fill-on-demand
ƒ Zero-filled pages, e.g., those for the stack or bss.

ƒ vfork() vs fork() with copy-on-write
ƒ vfork() lets the sharing of the page table and pages between the parent and child processes. ƒ Where to keep the needs of copy-onwrite information for pages?
432
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

Memory-Mapped Files
ƒ File writes might not cause any disk write! ƒ Solaris 2 uses memory-mapped files for open(), read(), write(), etc.
P1 VM 1 2 3 4 5 6
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2004.

3 6 1 5 4 2 1 2 3 4 5 6
Disk File

P2 VM 1 2 3 4 5 6

433

National Taiwan University. . the average memory usage is close to the physical memory size if we increase a system’s multiprogramming level! 434 * All rights reserved. 2004. ƒ Total physical memory = 40 frames ƒ Run six processes of size equal to 10 frames but with only five frames.Page Replacement ƒ Demand paging increases the multiprogramming level of a system by “potentially” over-allocating memory. Tei-Wei Kuo. => 10 spare frames ƒ Most of the time.

2004. paging should be transparent to users? ƒ Swap out a process! ƒ Do page replacement! 435 * All rights reserved. and more memory is needed? ƒ Answers ƒ Kill a user process! ƒ But. National Taiwan University.Page Replacement ƒ Q: Should we run the 7th processes? ƒ How if the six processes start to ask their shares? ƒ What to do if all memory is in use. . Tei-Wei Kuo.

Tei-Wei Kuo.Page Replacement ƒ A Page-Fault Service ƒ Find the desired page on the disk! ƒ Find a free frame ƒ Select a victim and write the victim page out when there is no free frame! ƒ Read the desired page into the selected frame. 2004. and restart the user process. ƒ Update the page and frame tables. 436 * All rights reserved. . National Taiwan University.

. Tei-Wei Kuo. National Taiwan University.Page Replacement Logical Memory P1 PC 0 H Page Table 3 v 4 v 5 v i 0 1 2 3 OS OS D H M/B J A E 437 1 Load M 2 3 J P2 0 1 2 3 A B D E 6 v i 2 v 7 v 4 5 6 7 B M * All rights reserved. 2004.

438 . 2004. National Taiwan University.Page Replacement ƒ Two page transfers per page fault if no frame is available! Page Table 6 4 3 7 V V V V N N Y Y Modify Bit is set by the hardware automatically! Modify (/Dirty) Bit! To “eliminate” ‘swap out” => Reduce I/O time by one-half Valid-Invalid Bit * All rights reserved. Tei-Wei Kuo.

Page Replacement ƒ Two Major Pieces for Demand Paging ƒ Frame Allocation Algorithms ƒ How many frames are allocated to a process? ƒ Page Replacement Algorithms ƒ When page replacement is required. . select the frame that is to be replaced! ƒ Goal: A low page fault rate! ƒ Note that a bad replacement choice does not cause any incorrect execution! 439 * All rights reserved. 2004. National Taiwan University. Tei-Wei Kuo.

Page Replacement Algorithms ƒ Evaluation of Algorithms ƒ Calculate the number of page faults on strings of memory references. 2004. called reference strings. Tei-Wei Kuo. National Taiwan University. ƒ Reference strings are recorded as system traces: ƒ How to reduce the number of data? 440 * All rights reserved. . for a set of algorithms ƒ Sources of Reference Strings ƒ Reference strings are generated artificially.

National Taiwan University. ƒ Reduce memory references into page references ƒ If a page p is referenced. .Page Replacement Algorithms ƒ Two Observations to Reduce the Number of Data: ƒ Consider only the page numbers if the page size is fixed. ƒ Reduce consecutive page references of page p into one page reference. any immediately following references to page p will never cause a page fault. 2004. 441 * All rights reserved. Tei-Wei Kuo.

Page Replacement Algorithms ƒ Example XX XX page# offset 0100. Tei-Wei Kuo. 06 Does the number of page faults decrease when the number of page frames available increases? 442 * All rights reserved. 06. 0612. 01. 0432. 0104. 01. 06 01. 01. 2004. 0611 01. 0103. 01. 04. 01. . 04. 01. National Taiwan University. 06. 0101. 0101.

Each page is given a time stamp when it is brought into memory.FIFO Algorithm ƒ A FIFO Implementation 1. Tei-Wei Kuo. 2004. 2. 1 . National Taiwan University. Select the oldest page for replacement! reference 7 string page frames 7 0 7 0 1 7 0 1 FIFO queue 7 7 0 7 0 1 2 2 0 1 0 1 2 0 3 2 3 1 1 2 3 0 2 3 0 2 3 0 4 4 3 0 3 0 4 2 4 2 0 0 4 2 3 4 2 3 4 2 3 0 0 2 3 2 3 0 3 2 1 0 1 3 3 0 1 2 0 1 2 0 1 2 0 1 7 1 2 1 2 7 7 7 0 2 2 7 0 0 7 0 1 7 0 1 443 * All rights reserved.

2004. Tei-Wei Kuo. National Taiwan University.FIFO Algorithm ƒ The Idea behind FIFO ƒ The oldest page is unlikely to be used again. . ??Should we save the page which will be used in the near future?? ƒ Belady’s anomaly ƒ For some page-replacement algorithms. the page fault rate may increase as the number of allocated frames increases. 444 * All rights reserved.

Tei-Wei Kuo. .FIFO Algorithm Run the FIFO algorithm on the following reference: string: 1 2 3 4 1 2 5 1 2 3 4 5 3 frames 1 1 2 1 2 3 1 2 3 2 3 4 1 2 3 4 3 4 1 1 2 3 4 4 1 2 1 2 3 4 1 2 5 2 3 4 5 1 2 5 3 4 5 1 1 2 5 4 5 1 2 2 5 3 5 1 2 3 5 3 4 1 2 3 4 5 3 4 2 3 4 5 4 frames 1 1 2 Push out pages that will be used later! 445 * All rights reserved. 2004. National Taiwan University.

ÅÆ Future Prediction reference 7 string page frames 7 0 7 0 1 7 0 1 2 2 0 1 0 3 2 0 3 0 4 2 4 3 2 3 0 2 0 3 3 2 1 2 0 1 2 0 1 7 7 0 1 0 1 next 0 * All rights reserved.Optimal Algorithm (OPT) ƒ Optimality ƒ One with the lowest page fault rate. National Taiwan University. Tei-Wei Kuo. ƒ Replace the page that will not be used for the longest period of time. 2004. next 7 next 1 446 .

National Taiwan University. . Tei-Wei Kuo. 2004.Least-Recently-Used Algorithm (LRU) ƒ The Idea: ƒ OPT concerns when a page is to be used! ƒ “Don’t have knowledge about the future”?! ƒ Use the history of page referencing in the past to predict the future! S ? SR ( SR is the reverse of S !) 447 * All rights reserved.

Tei-Wei Kuo. 2004. National Taiwan University.448 * All rights reserved. .LRU Algorithm ƒ Example reference 7 string page frames 7 0 7 0 1 7 0 1 LRU queue 0 0 7 1 0 7 2 2 0 1 2 1 0 0 2 1 0 3 2 0 3 3 0 2 0 3 2 0 4 4 0 3 4 0 3 2 4 0 2 2 4 0 3 4 3 2 3 2 4 0 0 3 2 0 3 2 3 0 2 2 3 0 3 2 1 1 3 2 1 2 3 2 1 3 2 0 1 0 2 0 2 1 1 0 2 1 7 7 0 7 7 1 0 0 1 7 0 1 7 0 1 a wrong prediction! Remark: LRU is like OPT which “looks backward” in time.

Time of Last Use! Disk 449 . National Taiwan University.LRU Implementation – Counters Logical Address f d f d Physical Memory time CPU A Logical Clock p p … cnt++ frame # v/i tag …… Update the “time-of-use” field Page Table for Pi * All rights reserved. 2004. Tei-Wei Kuo.

ƒ Overflow prevention of the clock & the maintenance of the “time-of-use” field of each page table. ƒ Update the “time-of-use” field for each page reference. . Tei-Wei Kuo. 450 * All rights reserved. ƒ Search the LRU page for replacement. 2004. National Taiwan University.LRU Implementation – Counters ƒ Overheads ƒ The logical clock is incremented for every memory reference.

2004. National Taiwan University. Tei-Wei Kuo. … move Page Table Disk 451 Overheads: Stack maintenance per memory reference ~ no search for page replacement! .LRU Implementation – Stack Logical Address f d f d Physical Memory CPU Head p p frame # A LRU Stack … v/i …… Tail (The LRU page!) * All rights reserved.

2004. National Taiwan University. Tei-Wei Kuo.A Stack Algorithm memoryresident pages n frames available ⊆ memoryresident pages (n +1) frames available ƒ Need hardware support for efficient implementations. . ƒ Note that LRU maintenance needs to be done for every memory reference. 452 * All rights reserved.

Tei-Wei Kuo. National Taiwan University. 2004. instead of their order. ƒ ƒ ƒ ƒ Additional-Reference-Bit Algorithm Second-Chance Algorithm Enhanced Second Chance Algorithm Counting-Based Page Replacement 453 * All rights reserved. .LRU Approximation Algorithms ƒ Motivation ƒ No sufficient hardware support ƒ Most systems provide only “reference bit” which only indicates whether a page is used or not.

Additional-Reference-Bits Algorithm ƒ Motivation ƒ Keep a history of reference bits 1 0 … 0 1 reference bit * All rights reserved. National Taiwan University. 2004. 01101101 10100011 … 11101010 00000001 OS shifts all history registers right by one bit at each regular interval!! one byte per page in memory 454 . Tei-Wei Kuo.

455 . 2004. Tei-Wei Kuo. * All rights reserved.Additional-Reference-Bits Algorithm ƒ History Registers LRU (smaller value!) 00000000 00000001 Not used for 8 times MRU 11111110 11111111 Used at least once every time ƒ But. how many bits per history register should be used? ƒ Fast but cost-effective! ƒ The more bits. the better the approximation is. National Taiwan University.

Second-Chance (Clock) Algorithm Reference Bit Page 0 0 1 1 0 Reference Bit Page ƒ Motivation ƒ Use the reference bit only 0 0 0 0 0 ƒ Basic Data Structure: ƒ Circular FIFO Queue ƒ Basic Mechanism ƒ When a page is selected ƒ Take it as a victim if its reference bit = 0 ƒ Otherwise. Tei-Wei Kuo. clear the bit and advance to the next page 456 * All rights reserved. 2004. National Taiwan University. … 1 1 … … 1 1 … .

0) – recently used but not “dirty” (1. National Taiwan University. Tei-Wei Kuo. ƒ 4 Classes (reference bit. .0) – not recently used and not “dirty” (0. 2004.1) – recently used and “dirty” 457 * All rights reserved. modify bit) low priority high priority ƒ ƒ ƒ ƒ (0.1) – not recently used but “dirty” (1.Enhanced Second-Chance Algorithm ƒ Motivation: ƒ Consider the cost in swapping out” pages.

ƒ Macintosh Virtual Memory Management 458 * All rights reserved. => May have to scan the circular queue several times before find the right page. . National Taiwan University.Enhanced Second-Chance Algorithm ƒ Use the second-chance algorithm to replace the first page encountered in the lowest nonempty class. Tei-Wei Kuo. 2004.

Counting-Based Algorithms ƒ Motivation: ƒ Count the # of references made to each page. 459 * All rights reserved. National Taiwan University. ƒ Least Frequently Used Algorithm (LFU) ƒ LFU pages are less actively used pages! ƒ Potential Hazard: Some heavily used pages may no longer be used ! ƒ A Solution – Aging ƒ Shift counters right by one bit at each regular interval. . Tei-Wei Kuo. instead of their referencing times. 2004.

2004. National Taiwan University.Counting-Based Algorithms ƒ Most Frequently Used Algorithm (MFU) ƒ Pages with the smallest number of references are probably just brought in and has yet to be used! ƒ LFU & MFU replacement schemes can be fairly expensive! ƒ They do not approximate OPT very well! 460 * All rights reserved. . Tei-Wei Kuo.

Systems keep a pool of free frames b. c.Page Buffering ƒ Basic Idea a. National Taiwan University. a modified page is written out and reset its “modify bit”. When the selected page (victim) is later written out. ƒ Variation 1 a. Desired pages are first “swapped in” some pages in the pool. Whenever the paging device is idle. . 461 * All rights reserved. Tei-Wei Kuo. its frame is returned to the pool. 2004. Maintain a list of modified pages. b.

b. 462 * All rights reserved. 2004. Remember which page was in each frame of the pool. first check whether the desired page is there already. . ƒ Pages which were in frames of the pool must be “clean”. When a page fault occurs. Tei-Wei Kuo. National Taiwan University. ƒ “Swapping-in” time is saved! ƒ VAX/VMS with the FIFO replacement algorithm adopt it to improve the performance of the FIFO algorithm.Page Buffering ƒ Variation 2 a.

ƒ Page Buffering . Tei-Wei Kuo. ƒ When the free-frame list is exhausted.Frames to save “swapping” time 463 * All rights reserved. ƒ Variations ƒ O. National Taiwan University. can share with users some free frames for special purposes. ƒ All allocated frames are released by the ending process.S.Frame Allocation – Single User ƒ Basic Strategy: ƒ User process is allocated any free frame. . page replacement takes place. 2004. ƒ User process requests free frames from the free-frame list.

Ratios of Frames ∝ Priority Si : relative importance 3. where (sum <= m) & (Ai >= minimum # of frames required) 2. National Taiwan University.Frame Allocation – Multiple Users ƒ Fixed Allocation a. Combinations. * All rights reserved. n processes Æ m/n frames per process b. Ai ∝ (Si / S) x m. 2004. Proportional Allocation 1. 464 . Equal Allocation m frames. or others. Ratios of Frames ∝ Size S = Σ Si. Tei-Wei Kuo.

ƒ ADD A.C Æ 4 frames needed ƒ ADD (A). * All rights reserved. Tei-Wei Kuo.Frame Allocation – Multiple Users ƒ Dynamic Allocation a.B. where (A) is an indirect addressing. 2004. Allocated frames ∝ the multiprogramming level b. National Taiwan University. 465 . (B). (C) Æ 1+2+2+2 = 7 frames. Allocated frames ∝ Others ƒ The minimum number of frames required for a process is determined by the instruction-set architecture.

256 Æ 2 + 2 + 2 = 6 frames ƒ The spanning of the instruction and the operands. 466 * All rights reserved. Tei-Wei Kuo. 2004. National Taiwan University. .Frame Allocation – Multiple Users ƒ Minimum Number of Frames (Continued) ƒ How many levels of indirect addressing should be supported? 16 bits address 1 0 direct indirect ƒ It may touch every page in the logical address space of a process => Virtual memory is collapsing! ƒ A long instruction may cross a page boundary. MVC X. Y.

For example. Tei-Wei Kuo. 467 * All rights reserved. 2004.Frame Allocation – Multiple Users ƒ Global Allocation ƒ Processes can take frames from others. . National Taiwan University. high-priority processes can increase its frame allocation at the expense of the low-priority processes! ƒ Local Allocation ƒ Processes can only select frames from their own allocated frames Æ Fixed Allocation ƒ The set of pages in memory for a process is affected by the paging behavior of only that process.

Processes can not control their own page fault rates such that a process can affect each another easily. Tei-Wei Kuo. 2004. . National Taiwan University. 468 * All rights reserved.Frame Allocation – Multiple Users ƒ Remarks a.Global replacement generally results in a better system throughput b.

degree of multiprogramming . Tei-Wei Kuo. 2004.Thrashing ƒ Thrashing – A High Paging Activity: ƒ A process is thrashing if it is spending more time paging than executing. ƒ Why thrashing? ƒ Too few frames allocated to a process! CPU utilization thrashing Thrashing under a global pagereplacement algorithm Dispatch a new process low CPU utilization 469 * All rights reserved. National Taiwan University.

how do you know the right number of frames for a process? 470 * All rights reserved. 2004. Tei-Wei Kuo.Thrashing ƒ Solutions: ƒ Decrease the multiprogramming level Æ Swap out processes! ƒ Use local page-replacement algorithms ƒ Only limit thrashing effects “locally” ƒ Page faults of other processes also slow down. . ƒ Give processes as many frames as they need! ƒ But. National Taiwan University.

….…. hash tables) ƒ How do we know that we allocate enough frames to a process to accommodate its current locality? * All rights reserved. ƒ Localities are defined by the program structures and data structures (e.. 2004. Tei-Wei Kuo.Locality Model localityi = {Pi.Pj. National Taiwan University.1.2.Pi.2. an array.Pi.g. 471 .ni} localityj = {Pj.Pj.1.nj} control flow ƒ A program is composed of several different (overlapped) localities.

472 * All rights reserved. The minimum allocation Δ ∞ All touched pages may cover several localities.2. National Taiwan University. Tei-Wei Kuo.7} working-set(t2) = {3.6. 2004. .5.Working-Set Model Page references …2 6 1 5 7 7 7 7 5 1 6 2 3 4 1 2 3 4 4 4 3 4 3 4 4 4 Δ Δ working-set window t1 working-set window t2 working-set(t1) = {1.4} ƒ The working set is an approximation of a program’s locality.

2004. D>M D>M Suspend some processes and swap out their pages. Tei-Wei Kuo.Working-Set Model D = ∑ working − set − sizei ≤ M where M is the total number of available frames. “Safe” D≦M . National Taiwan University. and initiate new processes. Extra frames are available. 473 * All rights reserved.

.Working-Set Model ƒ The maintenance of working sets is expensive! ƒ Approximation by a timer and the reference bit 0 1 timer! shift or copy 1 0 reference bit …… …… …… …… in-memory history 474 ƒ Accuracy v. 2004.s. Timeout Interval! * All rights reserved. National Taiwan University. Tei-Wei Kuo.

.475 * All rights reserved. Tei-Wei Kuo.Page-Fault Frequency ƒ Motivation ƒ Control thrashing directly through the observation on the page-fault rate! increase # of frames! page-fault rate upper bound lower bound decrease # of frames! number of frames *Processes are suspended and swapped out if the number of available frames is reduced to that under the minimum needs. 2004. National Taiwan University.

National Taiwan University. 476 * All rights reserved. 2004.OS Examples – NT ƒ Virtual Memory – Demand Paging with Clustering ƒ Clustering brings in more pages surrounding the faulting page! ƒ Working Set ƒ A Min and Max bounds for a process ƒ Local page replacement when the max number of frames are allocated. ƒ Automatic working-set trimming reduces allocated frames of a process to its min when the system threshold on the available frames is reached. . Tei-Wei Kuo.

ƒ 4HZ Æ 100HZ when desfree is reached! minfree desfree lotsfree 100 slowscan ƒ Swapping starts when desfree fails for 30s. ƒ pageout runs for every request to a new page when minfree is reached. 477 * All rights reserved. Tei-Wei Kuo. 2004. . National Taiwan University.OS Examples – Solaris 8192 fastscan ƒ Process pageout first clears the reference bit of all pages to 0 and then later returns all pages with the reference bit = 0 to the system (handspread).

Cost of Page Fault Services 478 Not every page in the working set will be used! . Tei-Wei Kuo. National Taiwan University.Other Considerations ƒ Pre-Paging ƒ Bring into memory at one time all the pages that will be needed! ready processes swapped out resumed suspended processes ƒ Issue Do pre-paging if the working set is known! Pre-Paging Cost * All rights reserved. 2004.

National Taiwan University.Other Considerations ƒ Page Size Better Page Size Resolution for Locality & small large p d Internal 9)~16. Tei-Wei Kuo.384B(212) 512B(2 Fragmentation Smaller Page Table Size & Better I/O Efficiency ƒ Trends .Large Page Size ∵ The CPU speed and the memory capacity grow much faster than the disk speed! 479 * All rights reserved. . 2004.

4MB) + Solaris 2 (8KB or 4MB) 480 * All rights reserved. National Taiwan University. . 2004. Tei-Wei Kuo.Other Considerations ƒ TLB Reach ƒ TLB-Entry-Number * Page-Size ƒ Wish ƒ The working set is stored in the TLB! ƒ Solutions ƒ Increase the page size ƒ Have multiple page sizes – UltraSparc II (8KB .

Tei-Wei Kuo. but they are needed when a page fault occurs! ƒ More page faults for page tables will occur!!! 481 * All rights reserved. 2004. . National Taiwan University.Other Considerations ƒ Inverted Page Table ƒ The objective is to reduce the amount of physical memory for page tables.

2) A(128.2) 128x128 page . National Taiwan University.2) . ..128) A(128.128) less than 128 frames!! 128 pages 482 128 words A(1. A(1. for j:=1 to 128 for i:=1 to 128 A(i. . process has A(2.128.128) * All rights reserved.1) A(1. 2004.j):=0 A(2. var A: array [1. Tei-Wei Kuo. . .Other Considerations ƒ Program Structure ƒ Motivation – Improve the system performance by an awareness of the underlying demand paging..1) A(2. …… faults if the .128] of integer.1) A(128.1.

# of pages touched. etc. National Taiwan University. etc. etc. hash table. Tei-Wei Kuo.Other Considerations ƒ Program Structures: ƒ Data Structures ƒ Locality: stack. # of memory references. PASCAL. ƒ Programming Language ƒ Lisp. . ƒ Search speed. ƒ Compiler & Loader ƒ Separate code and data ƒ Pack inter-related routines into the same page ƒ Routine placement (across page boundary?) 483 * All rights reserved. 2004.

Tei-Wei Kuo. • DMA gets the following information of the buffer: • Base Address in Memory • Chunk Size • Could the buffer-residing pages be swapped out? 484 .I/O Interlock buffer Drive Physical Memory * All rights reserved. 2004. National Taiwan University.

National Taiwan University.I/O Interlock ƒ Solutions ƒ I/O Device ÅÆ System Memory ÅÆ User Memory ƒ Extra Data Copying!! ƒ Lock pages into memory ƒ The lock bit of a page-faulting page is set until the faulting process is dispatched! ƒ Lock bits might never be turned off! ƒ Multi-user systems usually take locks as “hints” only! 485 * All rights reserved. . Tei-Wei Kuo. 2004.

National Taiwan University. Tei-Wei Kuo. long-term delays in the execution of a program. . ƒ Solution: ƒ Go beyond locking hints Î Allow privileged users to require pages being locked into memory! 486 * All rights reserved. 2004.Real-Time Processing Predictable Behavior Virtual memory introduces unexpected.

compaction may be needed! ƒ Considerable overheads! 487 * All rights reserved. 2004. National Taiwan University. Tei-Wei Kuo.Demand Segmentation ƒ Motivation ƒ Segmentation captures better the logical structure of a process! ƒ Demand paging needs a significant amount of hardware! ƒ Mechanism ƒ Like demand paging! ƒ However. .

Sign up to vote on this title
UsefulNot useful