Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems.

Similar presentations


Presentation on theme: "1 Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems."— Presentation transcript:

1 1 Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation

2 2 When OS Involves Paging? Process creation Process execution Page faults Process termination

3 3 Paging in Process Creation Determine (initial) size of program and data Assign appropriate page frames Create page table Process is running  page table in memory Create swap area For the pages swapped out Record info about page table and swap area in process table

4 4 Paging During Process Running When a process is scheduled for execution Reset MMU Flush TLB (translation Lookaside Buffer) Copy or point to new process’ page table Bring some or all of new process’ pages into memory Reduce the number of page faults

5 5 Paging When Page Faults Read registers to identify virtual address causing the page fault Locate the page needed on disk Find available page frame Evict an old page if necessary Read in the page Execute the faulting instruction again

6 6 Paging When Process Exits Release page table Release pages and swap area on disk If some pages are shared by other processes, keep them.

7 7 Swap Area Swap area: reserved for processes’ pages A chunk of pages reserved on disk, same size of # of pages of the process Each process has its own swap area, recorded in process table Initialize swap area before a process runs Copy entire process image, or Load the process in memory and let it be paged out as needed

8 8 Swap Area for Growing Processes Process may grow after starting Data area and stack may grow Reserve separate swap areas for text (program), data, and stack Reserve nothing in advance Allocate disk space when pages swapped out De-allocate when page swapped in again Have to keep track of pages on disk A disk address per page, costly!

9 9 Comparison of Two Methods Paging to a static swap areaBacking up pages dynamically

10 10 Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation

11 11 A Motivating Example Many tables when compiling a program Symbol table: names and attributes of variables Constant table: integer/floating-point constants Parse tree: syntactic analysis Stack: for procedure calls within the compiler Each table needs contiguous chunks of virtual address space. But tables grow/shrink as compilation proceeds. How to manage space for these tables?

12 12 With One-Dimensional Address Take space from tables with an excess of room Tedious work Free programmers from managing expanding and contracting tables Segments: many completely independent address spaces Call stack Parse tree Constant table Source text Symbol table Virtual address space

13 13 Segments Segment: a two dimensional memory Each segment has a linear sequence of address (0 to some maximum) Different segments may have different lengths Segment lengths may change during execution Different segments can grow/shrink independently Address: segment number + address within the segment (offset)

14 14 Multiple Segments in A Process Segments are logical entities Programmers are aware of them. A segment may contain a procedure, or an array, but not a mixture of different types. Facilitate separate protection. Source text Constants Parse tree Call stack Symbol table

15 15 Paging Vs. Segmentation ConsiderationPagingSegmentation Aware by programmers?NOYes # of linear address spaces1Many Total address space > physical memory? Yes Distinguish and separately protect procedures and data? NoYes Accommodate fluctuating tables? NoYes Facilitate sharing among procedures? NoYes Why is this technique for? Get larger linear space than that of physical memory 1. Break programs/data into logically independent address space 2. Aid sharing and protection

16 16 Implementing Pure Segmentation External fragmentation Time 

17 17 Segmentation With Paging: MULTICS For large segments, only the “working set” should be kept in memory Paging segments: segment has own page table Each program has a segment table One entry (descriptor) per segment Segment table is itself a segment If (part of) a segment is in memory, its page table must be in memory Address: segment # + virtual page # + offset Segment #  page table Page table + virtual page #  page frame address Page frame address + offset  physical address

18 18 The MULTICS virtual memory …… Segment 2 descriptor Segment 1 descriptor Segment 0 descriptor …… Page 2 entry Page 1 entry Page 0 entry Page table for segment 2 …… Page 2 entry Page 1 entry Page 0 entry Page table for segment 0 Page frame Segment table Segment descriptor contains the memory address of the page table. #segment#pageoffset Virtual Address Page frame

19 19 Summary Fixed partitions Multiple queues Vs. single queue Degree of multiprogramming Relocation and protection Swapping Virtual memory Vs. physical memory Bitmap and linked list Holes

20 20 Summary (Cont.) Virtual memory Pages Vs. page frames Page tables Page replacement algorithms (aging and WSClock) Modeling paging systems Stack algorithms Predict page faults using distance string

21 21 Summary (Cont.) Design issues Local Vs. global allocations Load control to reduce thrashing Shared pages Implementation issues Page fault handling and swap area Segmentation Each segment has its own address space Advantages Pure segmentation and segmentation with paging

22 CMPT 300: Operating System Chapter 5 Input/Output

23 23 Outline Principles of I/O hardware Principles of I/O software I/O software layers Disks Clocks

24 24 Block and Character Devices Block devices: store info in fixed-size blocks Examples: disks Each block has its own address Each block can be read/written independently Data are read/write in the units of block Character devices: no block structure, deliver/accept a stream of characters Examples: keyboard, printers, network, mice Not addressable, no seek operation Devices not in the classification: clock

25 25 Huge Range in Speeds Many orders of magnitude in data rates Keyboard: 10 bytes/sec Mouse: 100 bytes/sec Laser printer: 100Kb/sec IDE disk: 5Mb/sec PCI bus: 528Mb/sec Challenge: how to design a general structure to control various I/O devices? Multi-bus system

26 26 Structure of I/O Units A mechanical component: the device itself Disk: plates, heads, motors, arm, etc. Monitor: tube, screen, etc. An electronic component: device controller, adaptor Disk: issuing commands to mechanical components, assembling, checking and transferring data Monitor: read characters to be displayed and generate electrical signals to modulate the CRT beam

27 27 Mechanical / Electronic Components Bus CPUMemory Video controller Keyboard controller Floppy disk controller Hard disk controller Monitor Keyboard Floppy disk Hard disk Mechanical components Electronic components

28 28 Device Controller Registers in I/O controllers CPU writes commands into registers CPU reads states of devices from registers Data buffer for transferring data How CPU distinguishes different registers and data buffers? I/O port number Memory-mapped I/O

29 29 I/O Port Number Each control register is assigned a unique I/O port number (8- /16-bit integer) Instruction IN and OUT IN REG, PORT OUT PORT, REG Separated address spaces IN R0, 4 and MOV R0, 4 are completely different. 0xFFFF… 0 Memory I/O ports

30 30 Memory-Mapped I/O Map all control registers into memory space Usually at the top of the address space Each register is assigned a unique memory address, e.g., 0xFF10 Hybrid scheme (used in Pentium) 0xFFFF… 0 0 Memory I/O ports Memory- mapped Hybrid I/O buffered data

31 31 Pros & Cons of Memory-Mapped I/O Advantages Easier for programming Easier to protect and share I/O devices Save time of accessing control registers Disadvantages Caching a control register is disastrous Disable caching for selected pages I/O devices cannot see the memory addresses with separate buses for memory and I/O devices (see next slide)

32 CPU Main Memory I/O CPU Main Memory I/O All addresses go here CPU reads/writes of memory go over this high-bandwidth bus Single-bus Dual-bus

33 33 Interrupts CPU Bus Interrupt controller Disk 1. Device finishes a work Keyboard Clock Printer 2. Controller issues interrupt 3. CPU acks interrupt

34 34 Interrupt Processing I/O devices raise interrupt by asserting a signal on a bus line assigned Multiple interrupts  the one with high priority goes first Interrupt controller interrupts CPU Put device # on address lines Device #  check interrupt vector table for interrupt handler (a program) Enable interrupts shortly after the handler starts

35 35 Direct Memory Access (DMA) Request data from I/O without DMA Device controller reads data from device It interrupts CPU when a byte/block of data available CPU reads controller’s buffer into main memory Too many interruptions, expensive DMA: direct memory access A DMA controller with registers read/written by CPU CPU programs the DMA: what to transfer where Source, destination and size DMA interrupts CPU only after all the data are transferred.

36 Operations of DMA CPU DMA controller Disk controller Main memory Bus Address Count Control Drive 1. CPU programs the DMA and controller Buffer 2. DMA requires transfer to memory 3. Data transferred 4. Ack Interrupt when done

37 37 Transfer Modes Word-at-a-time (cycle stealing) DMA controller acquires the bus, transfer one word, and releases the bus CPU waits for bus if data is transferring Cycle stealing: steal an occasional bus cycle from CPU once in a while Burst mode DMA holds the bus until a series of transfers complete More efficient since acquiring bus takes time Block the CPU from using bus for a substantial time

38 38 Outline Principles of I/O hardware Principles of I/O software I/O software layers Disks Clocks

39 39 Issues of The I/O Software Device independence Uniform naming Error handling Buffering Others

40 40 How to Perform I/O? Programmed I/O (Polling/Busy Waiting) Acquire I/O device (e.g., printer) Blocked if it is being used by another process Copy the buffer from use space to kernel space Step 1: keep checking the state until it is ready. Step 2: send one character to printer. Go to Step 1. Release the I/O. The CPU do all the work Simple Waste a lot of CPU time on busy waiting

41 41 An Example copy_from_user(buffer, p, count); for (i=0; i<count; i++){ while (*printer_status_reg!=READY); *printer_data_register=p[i]; } return_to_user(); // p is the kernel buffer // loop on every character // loop until ready // output one character Q: Why does OS copy the buffer from user space to kernel space?

42 42 Interrupt-Driven I/O Print system call copy_from_user(buffer, p, count); enable_interrupts(); while (*printer_status_reg!=READY); *printer_data_register=p[0]; scheduler(); Interrupt service procedure if (count==0){ unblock_user(); } else { *printer_data_register=p[I]; count--; i++; } acknowledge_interrupt(); return_from_interrupt(); Interrupt occurs on every character!

43 43 I/O Using DMA Too many interrupts in interrupt-driven I/O DMA reduces # of interrupts from 1/char to 1/buffer printed Print system call copy_from_user(buffer, p, count); set_up_DMA_controller(); scheduler(); Interrupt service procedure acknowledge_interrupt(); unblock_user(); return_from_interrupt();

44 44 Outline Principles of I/O hardware Principles of I/O software I/O software layers Disks Clocks

45 45 Layers Overview User-level I/O software Device-independent I/O software Device drivers Interrupt handlers Hardware

46 46 Interrupt Handlers Hide I/O interrupts deep in OS Device driver starts I/O and blocks (e.g., down a mutex) Interrupt wakes up driver Process an interrupt Save registers ( which to where?) Set up context (TLB, MMU, page table) Run the handler (usually the handler will be blocked) Choose a process to run next Load the context for the newly selected process Run the process Take considerable number of CPU instructions

47 47 Device Drivers Device-specific code for controlling I/O devices Written by manufacture, delivered along with device One driver for one (class) device(s) Position: part of OS kernel, below the rest of OS Interfaces for rest of OS Block device and character device have different interfaces

48 48 Logical Position of Device Drivers User program Rest of the OS Printer driver Printer controller printer User space Kernel space Hardware Devices

49 49 How to Install a Driver? Re-compile and re-link the kernel Drivers and OS are in a single binary program UNIX systems Often run by computer centers, devices rarely change Dynamically loaded during OS initialization Windows system Devices often change Difficult to obtain source code Users don’t know how to compile OS

50 50 Functions of Device Drivers Accept abstract read/write requests Error checking, parameter converting Check status, initialize device, if necessary Issue a sequence of commands May block and wait for interrupt Check error, return data Other issues: re-entrant, up-call, etc.

51 51 Device-Independent I/O Software Why device- independent I/O software? Perform I/O functions common to all devices Provide a uniform interface to user- level software Functions in device- independent software Uniform interfacing for devices drivers Buffering Error reporting Allocating and releasing dedicated devices Providing a device- independent block size User-level I/O software Device-independent I/O software Device drivers Interrupt handlers Hardware

52 52 Uniform Interfacing for Device Drivers New device  modify OS, no good Provide the same interface for all drivers Easy to plug a new driver In reality, not absolutely identical, but most functions are common Name I/O devices in a uniform way Mapping symbolic device names onto the proper driver Treat device name as file name in UNIX E.g., hard disk /dev/disk0 is a special file. Its i-node contains the major device number, which is used to locate the appropriate driver, and minor device number.

53 53 Buffering for Input Motivation: consider a process that wants to read data from a modem User process handles one character at a time. It blocks if a character is not available Each arriving character causes an interrupt User process is unblocked and reads the character. Try to read another character and block again. Many short runs in a process: inefficient! Overhead of context switching

54 54 Buffering in User Space User space Kernel space Buffering in user space Set a buffer in user process’ space User process is waked up only if the buffer is filled up by interrupt service procedure. More efficient. Can the buffer be paged out? Yes: where to put the next character? No: the pool of available pages shrink

55 55 Buffering in Kernel Two buffers: one in kernel and one in user Interrupt handler puts characters into the buffer in kernel space This buffer is locked When full, copy the kernel buffer to user buffer Bring in the user buffer if necessary Where to store the new arrived characters during page loading? User space Kernel space Buffering in kernel

56 56 Double Buffering in Kernel Two kernel buffers When the first one fills up, but before it has been emptied, the second one is used. Buffers are used in turn: while one is being copied to user space, the other is accumulating new input User space Kernel space Double buffering

57 57 Downside of Data Buffering Many sequential buffering slow down transmission Process A 1 2 Network controller Network 4 5 3 User space Kernel space Process B What’s it for?

58 58 Handling I/O Errors Programming errors: ask for something impossible E.g. writing a keyboard, reading a printer Invalid parameters, like buffer address Report an error code to caller Actual I/O error E.g. write a damaged disk block Handled by device driver and/or device-independent software System error E.g. root directory or free block list is destroyed display message, terminate system

59 59 Allocating Dedicated Devices Before using a device, make the system call open When the device is unavailable The call fails, or The caller is blocked and put on a queue Release the device by making the close system call

60 60 User-Space I/O Software Libraries: interface between user programs and system calls E.g., write(fd, buffer, nbytes), printf/scanf Spooling User-level, controlled by a daemon Eliminated unnecessarily waiting/deadlock User-level I/O software Device-independent I/O software Device drivers Interrupt handlers Hardware

61 61 Summary: I/O Software User-level I/O software Make I/O call; format I/O; spooling Device-independent OS software Naming, protection, blocking, buffering, allocation Device drivers Setup device registers; check status Interrupt handlers Wake up driver when I/O completed Hardware Perform I/O operation I/O request I/O reply


Download ppt "1 Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems."

Similar presentations


Ads by Google