Introduction to Operating Systems

Similar documents
Memory management. Last modified: Adaptation of Silberschatz, Galvin, Gagne slides for the textbook Applied Operating Systems Concepts

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

Chapter 8: Memory-Management Strategies

Chapter 8: Main Memory

CHAPTER 8: MEMORY MANAGEMENT. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Chapter 7: Main Memory. Operating System Concepts Essentials 8 th Edition

Chapter 8: Main Memory. Operating System Concepts 9 th Edition

Chapter 8: Main Memory

CS307: Operating Systems

CS6401- Operating System UNIT-III STORAGE MANAGEMENT

Chapter 8: Memory- Manage g me m nt n S tra r t a e t gie i s

CS609 Final Term Solved MCQs with References Without Repetitions 14/02/2013

Chapter 8: Virtual Memory. Operating System Concepts

Memory and multiprogramming

Virtual Memory Outline

SHANDONG UNIVERSITY 1

Embedded Systems Dr. Santanu Chaudhury Department of Electrical Engineering Indian Institute of Technology, Delhi

Chapter 8 Main Memory

Background. Contiguous Memory Allocation

Chapter 9: Virtual-Memory

Chapter 9 Memory Management Main Memory Operating system concepts. Sixth Edition. Silberschatz, Galvin, and Gagne 8.1

The Operating System. Chapter 6

CS370 Operating Systems

CSE 4/521 Introduction to Operating Systems. Lecture 27 (Final Exam Review) Summer 2018

Memory management. Requirements. Relocation: program loading. Terms. Relocation. Protection. Sharing. Logical organization. Physical organization

CHAPTER 11: IMPLEMENTING FILE SYSTEMS (COMPACT) By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Operating System Concepts

CS3600 SYSTEMS AND NETWORKS

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Memory Management

Operating System Support

Addresses in the source program are generally symbolic. A compiler will typically bind these symbolic addresses to re-locatable addresses.

Chapter 8: Memory Management

Chapter 9: Virtual Memory

Computer Organization and Architecture. OS Objectives and Functions Convenience Making the computer easier to use

Chapter 8 Memory Management

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

Chapter 9: Virtual Memory

MEMORY MANAGEMENT/1 CS 409, FALL 2013

Chapter 8: Memory- Management Strategies. Operating System Concepts 9 th Edition

File System: Interface and Implmentation

CS399 New Beginnings. Jonathan Walpole

Memory Management. Dr. Yingwu Zhu

3.1 (a) The Main Features of Operating Systems

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

Main Memory (Part I)

Chapter 8: Memory Management

Chapter 8: Memory- Management Strategies. Operating System Concepts 9 th Edition

Chapter 8: Memory- Management Strategies

File Systems. OS Overview I/O. Swap. Management. Operations CPU. Hard Drive. Management. Memory. Hard Drive. CSI3131 Topics. Structure.

Chapter 8: Memory Management. Operating System Concepts with Java 8 th Edition

Chapter 8 Virtual Memory

8.1 Background. Part Four - Memory Management. Chapter 8: Memory-Management Management Strategies. Chapter 8: Memory Management

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Chapters 9 & 10: Memory Management and Virtual Memory

With regard to operating systems the kernel consists of the most frequently used functions in the operating system and it kept in main memory.

CS307: Operating Systems

Operating Systems. 09. Memory Management Part 1. Paul Krzyzanowski. Rutgers University. Spring 2015

Chapter 8: Main Memory

Chapter 8: Main Memory

CS370 Operating Systems

Chapter 8: Memory Management. Background Swapping Contiguous Allocation Paging Segmentation Segmentation with Paging

Operating Systems Unit 6. Memory Management

Memory Management. Reading: Silberschatz chapter 9 Reading: Stallings. chapter 7 EEL 358

Outlook. Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation Example: The Intel Pentium

IA32 Intel 32-bit Architecture

Chapter 9: Virtual Memory. Chapter 9: Virtual Memory. Objectives. Background. Virtual-address address Space

Chapter 1: Introduction. Operating System Concepts 9 th Edit9on

Chapter 9: Virtual Memory

Chapter 9: Virtual Memory

Chapter 5. File and Memory Management

Processes and Tasks What comprises the state of a running program (a process or task)?

Optimal Algorithm. Replace page that will not be used for longest period of time Used for measuring how well your algorithm performs

File Management By : Kaushik Vaghani

Segmentation with Paging. Review. Segmentation with Page (MULTICS) Segmentation with Page (MULTICS) Segmentation with Page (MULTICS)

FILE SYSTEMS. CS124 Operating Systems Winter , Lecture 23

First-In-First-Out (FIFO) Algorithm

Operating Systems Design Exam 2 Review: Spring 2011

CS 416: Opera-ng Systems Design March 23, 2012

CS370 Operating Systems

Move back and forth between memory and disk. Memory Hierarchy. Two Classes. Don t

Memory Management Ch. 3

Introduction to OS. File Management. MOS Ch. 4. Mahmoud El-Gayyar. Mahmoud El-Gayyar / Introduction to OS 1

Older geometric based addressing is called CHS for cylinder-head-sector. This triple value uniquely identifies every sector.

CS 3733 Operating Systems:

Module 8: Memory Management

Chapter 4. File Systems. Part 1

Process size is independent of the main memory present in the system.

6 - Main Memory EECE 315 (101) ECE UBC 2013 W2

Performance of Various Levels of Storage. Movement between levels of storage hierarchy can be explicit or implicit

Virtual Memory. control structures and hardware support

12: Memory Management

Objectives and Functions Convenience. William Stallings Computer Organization and Architecture 7 th Edition. Efficiency

stack Two-dimensional logical addresses Fixed Allocation Binary Page Table

UNIT III MEMORY MANAGEMENT

Chapter 9 Memory Management

COS 318: Operating Systems. Overview. Prof. Margaret Martonosi Computer Science Department Princeton University

Chapter 9: Virtual Memory. Operating System Concepts 9 th Edition

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 8: MEMORY

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

Chapter 8: Memory Management Strategies

Transcription:

Reading ICT106 Fundamentals of Computer Systems Topic 12 Introduction to Operating Systems Irvine (4 th Edition) Chapter 13 16-bit MS-DOS Chapter 14 Disk Fundamentals Chapter 2 Section 2.3 IA-32 Memory management ICT 106 _Week 12_06 2 Objectives To understand the components and functions of an operating system To understand the resource allocation functions of an operating system To understand the mechanisms by which an operating system manages programs and processes Objectives To understand the mechanisms by which an operating system manages the CPU To understand the mechanisms by which an operating system manages memory ICT 106 _Week 12_06 3 ICT 106 _Week 12_06 4 1

An Operating System (OS) consists of a set of sophisticated, coordinated and cooperative programs which make the hardware useful by providing an interface to the user and other programs which require the use of the computer's resources. Not all computers need an OS, eg some simple embedded applications. In such applications, a program in ROM controls some system. (eg, a car's engine management system, microprocessor controlled home electronic devices, bank ATM s etc) ICT 106 _Week 12_06 5 ICT 106 _Week 12_06 6 Services provided by an Operating System The services provided by an operating system (OS) can be broken into the following categories: 1. User interface and User Management: commands, GUI etc., login/logout, accounting info, access control, account maintenance 2. I/O Management : I/O services, device management 3. File System Management - providing an abstract view of secondary storage to the user. Functions include device maintenance, volume maintenance, creation/deletion, backup/recovery 4. Process Management - the running of programs (note: the OS itself is a program). Process creation/deletion, loading, inter-process communication ICT 106 _Week 12_06 7 ICT 106 _Week 12_06 8 2

5. Memory Management - very important task of OS - even the most simple OS (where there is only 1 process so that process management is not really needed) has to perform memory management. Involves memory allocation, swapping, address translation. 6. CPU Management - interrupt handling, CPU scheduling. Mechanisms to call OS services (User Interface) Calls to operating systems by the user: these are directed through an interface provided by the OS (DOS command line, Windows - menus, icons, dialog boxes) the provision of the User Interface (UI) is another task of an OS: Character UI (CUI) or Graphical UI (GUI). Calls to the OS by user programs: User written programs making use of OS Services (via system calls - recall INT 21H) ICT 106 _Week 12_06 9 ICT 106 _Week 12_06 10 System Calls Calls to the system procedures/functions But the code which constitutes the system routines is not part of the collection of user files The code to be executed may already be sitting within memory At link time, appropriate address/references can be made provided address of system routines are known. INT 21h is an example of a system call for one particular OS. System calls are also called software interrupts and are generated by the CPU executing an INT n instruction. The INT instruction is essentially a fancy variant of the CALL instruction Major advantage over CALL instruction relates to software portability ICT 106 _Week 12_06 11 ICT 106 _Week 12_06 12 3

Application programs contain calls to operating system services via software interrupts System software can be in physically different locations on different types of computers - don t need to know actual address of routine (eg. BIOS) Interrupt vectors provide the hooks (connections) between routines desired to be executed, and their actual location (you can check this by examining the IVT). ICT 106 _Week 12_06 13 Eg, using debug we can examine element 21h of the interrupt vector table, and then unassemble the instructions found at the address specified at element 21h Concept of software interrupt is reasonably standard for a wide range of operating systems: Macintosh, Unix, OS/2 Known as the TRAP instruction on some machines ICT 106 _Week 12_06 14 Cooking Services (I/O device Management) Every day common services Typical example is keyboard echo and backspace correction Done by software - two components: reading & writing Character buffering (interrupt routine) This OS preprocessing of characters before being received by an application program is known as cooked mode C's scanf function works in cooked mode, in which special chars are given special processing, eg, the BackSpace key which has ASCII value 8, is given special treatment, and a "backspace action" is performed. Prior to hitting the return key, user can revoke previously entered characters by using the BackSpace key ICT 106 _Week 12_06 15 ICT 106 _Week 12_06 16 4

In cooked mode no input is considered final until the user hits the return key Also, user will have to hit the return key after typing a command, instead of getting instant response raw mode is without preprocessing C Example: getchar & getch Use online help in Borland C/C++ for further details Also, the device drivers which control the operation of the various devices and their communication with the processor File System A file is a named collection of related information. It is an abstraction provided to the user by an OS File system: part of the OS which manages files on secondary memory (eg, disk) Disks and associated hardware allow for reading and writing of sectors of information ICT 106 _Week 12_06 17 ICT 106 _Week 12_06 18 A single disk has two sides (surfaces). Each side is divided into tracks. Tracks are numbered: 0 (outermost), 1(the next one), 2, and so on. A track is divided into sectors. A sector is the smallest unit of information that can be transferred to/from a disk. The mapping from name, contents, size, creation date etc. of a file to the physical locations on a disk is handled and maintained by the file management system Implementation of file systems can be different for different OSs (even on the same computer) Sectors on a disc are accessed by specifying platter, side, track and sector within track. Easiest to deal with logical sector - all sectors on a disk aggregated into a single numbering system: 0, 1, 2, 3... ICT 106 _Week 12_06 19 ICT 106 _Week 12_06 20 5

Different DOS services available for physical vs logical sector access (eg. absread (DOS) or DOS interrupt 25 ) Typical low level OS service provides for a number of sectors of information to be copied from/to memory to/from a disk Some OS do not provide to a user such low level access to disk, although the OS itself does this low level disk access all the time. File System Functions To allow users to create, update and delete their files (new, save, open, close, delete, rename, read, write etc.) To allow controlled access to other users files in order to share information, eg, read access, write access, execute access etc. To allow structuring of files in a manner appropriate for an application ICT 106 _Week 12_06 21 ICT 106 _Week 12_06 22 Management of secondary memory users need not worry where on disk the files are stored Device independence allow reference to files by logical names (symbolic) rather than having to use physical device names Provide backup and recovery facilities against system failures MS-DOS has an hierarchical, tree structured file system Top most level known as the root directory Some files are subdirectories (ie. data contents are essentially lists of the files contained within them) File descriptor: each file has a file descriptor (index entry) which contains information necessary to manage the file, ICT 106 _Week 12_06 23 ICT 106 _Week 12_06 24 6

Information about a File File name Location on disk (address) File type, eg, text, object, binary, etc. File attributes (access rights), eg, read, write, execute, delete Current size Date and time file created/last modified Mapping of logical file name to a physical location on disk is achieved via a file directory File directory is like a symbol table contains the above information about each named file. ICT 106 _Week 12_06 25 ICT 106 _Week 12_06 26 Implementation of File System under DOS To look at the details of the DOS implementation, it is essential to know the consecutive disc sector allocation as follows: Boot record File allocation table (FAT) Directory index sectors File data clusters (cluster 2, 3,...) Sector 0: boot record - normally read into memory upon power up, containing some data - BIOS parameter block (BPB) and a small program boot loader program which reads in and begins executing OS ICT 106 _Week 12_06 27 ICT 106 _Week 12_06 28 7

Some data contained in boot record includes: bytes per sector - bytes 0bh - 0ch sectors per cluster (a cluster is a set of consecutive sectors grouped to form one storage unit) - byte 0dh number of copies of file allocation table (usually 2 copies of FAT) - byte 10h FAT lists which clusters the files are stored in, and which clusters are currently unused and thus available for new files Two copies of FAT are stored on disk because if one of them gets corrupted, the other may be used to recover files. number of directory index entries (total number of files) - bytes 11h - 12h FAT size in sectors - bytes 16h - 17h ICT 106 _Week 12_06 29 ICT 106 _Week 12_06 30 Directory index entries (each 20h, 32bytes long) Location determined from size and number of copies of FAT (eg. 7 sectors, 2 copies => FAT 1-14 sectors) or (eg. 9 sectors, 2 copies => FAT 1-18 sectors) Directory index size: 224 entries * 32 bytes ==> 7168 bytes ==> 14 sectors Directory index: sectors 15 to 28 (for 7 sector FAT) Each entry contains following at the given byte offsets: b0..b7: Filename 8 chars. b8..ba: Extension 3 chars. bb: Attribute eg, read-only, hidden, system file, subdirectory, archive etc b1a:first cluster (2 bytes) Cluster 2 starts after directory index, and numbered consecutively from there, but remember to take into account number of sectors per cluster ICT 106 _Week 12_06 31 ICT 106 _Week 12_06 32 8

Looking at contents of first cluster of file, will verify it corresponds to the data in the start of the corresponding file First cluster indicated in directory index, remaining clusters which constitute the file are contained in the File allocation table FAT is a linked list data structure: entry for kth cluster contains link pointing to (k+1) cluster of file Unix and many other modern file systems (from user point of view) are largely similar to MS-DOS although they have no or higher limits on length of file names. Some (& most new) allow 255 chars for filenames and have no concept of extension. ICT 106 _Week 12_06 33 ICT 106 _Week 12_06 34 Process Management Useful to have several user programs alternating in execution, eg, background printing, clock, network popup messages Known as multi-tasking - it gives the appearance that a computer is running several programs simultaneously CPU is still only executing one program at any one time, although several programs may exist in memory at any one time. CPU switches between the tasks very rapidly. Two simplest forms of multi-tasking Co-operative - eg. Windows (uses TaskSwitch functions) Time-sharing MS-DOS provides limited multitasking facilities (TSR) but the hardware itself is not the limiting factor - it is the OS ICT 106 _Week 12_06 35 ICT 106 _Week 12_06 36 9

Process (or task as in multi-tasking) Fundamental to the concept of multitasking An instance of execution of a program - each instance is a process Could be same program code (eg. same code segment) but different data (eg. different data segments) Multiple processes Different programs - different code & data segments As examples, consider Windows - multiple instances of clock, notepad,... Unix facility ( & operator) to startup another program in the background ICT 106 _Week 12_06 37 ICT 106 _Week 12_06 38 Time sharing (CPU Scheduling) All code (programs) required to be executed is within memory or has to be brought into memory Executing Code + Data constitutes a Process States of a process: Active, Ready, Awaiting some action to complete (blocked) etc Active (running): a process is said to be active if it currently has the CPU Ready: a process is said to be in the ready state if it could use a CPU if one was available Blocked: a process is said to be in blocked state if it is waiting for some event to happen (eg, I/O transfer) before it can proceed ICT 106 _Week 12_06 39 ICT 106 _Week 12_06 40 10

Process Table OS maintains tables of data concerning all processes Each process currently in the system is represented by a data structure (process descriptor) which contains relevant information about the process: Unique identification of the process, eg process name Current state of the process (i.e., running, ready, blocked/asleep) The process s priority Addresses of code & data associated with the process Area to save process s registers ICT 106 _Week 12_06 41 ICT 106 _Week 12_06 42 Linked list or queue of all processes More sophisticated OS support priorities, time sharing intervals (CPU scheduling) etc Timer interrupts allow OS to periodically take a look around and decide whether or not to switch control of the CPU to another process Time slice or quantum of execution is the basis for providing equity in time of execution of processes Round Robin the simplest scheduling policy. The CPU time is divided into time-slices. A real-time clock generates pulses at regular and frequent intervals (typically 1/100 second). At each pulse, the OS performs the following steps: ICT 106 _Week 12_06 43 ICT 106 _Week 12_06 44 11

The currently active process is suspended OS checks the status of each process, in strict sequence, to see whether the process requires CPU time. The first process requiring CPU time gets its time slice When the end of the sequence is reached, the CPU returns to the start of the sequence and repeats the sequence again. Context switching the term used for switching the CPU between two processes Wait loop I/O is avoided - tasks requiring I/O use interrupt driven I/O while other tasks are given normal access to CPU Timer interrupt is absolutely essential otherwise OS has no opportunity to intervene and context switch. ICT 106 _Week 12_06 45 ICT 106 _Week 12_06 46 Context switches require OS to save the state of the currently executing process, and restoring the previous state of the task about to be switched into action Return from an interrupt is either to the same process OR to a different process upon a context switch Processes may end their turns before allocated time slice has ended by making a system call (eg. I/O access ==> I/O interrupt driven) Usually processes can be given a priority. The higher the priority the more time slices they can get. When interrupt driven I/O is combined with process priorities, the ability to put processes to sleep, actions of interdependent processes, etc, the scheduling or context switching algorithms can become quite complex ICT 106 _Week 12_06 47 ICT 106 _Week 12_06 48 12

The most important concept to remember is as follows: The OS has no power whatsoever when a user program is executing. The OS cannot simply step in and stop a user program; either an I/O device interrupt, timer interrupt, or the user program (via system call) results in the user program relinquishing control of the CPU to the OS. Memory Management (MM) Memory Management (MM) is required for the effective use of a computer even for a single tasking OS (eg. to hold the OS and user program) For a multi-tasking OS, MM is absolutely essential. ICT 106 _Week 12_06 49 ICT 106 _Week 12_06 50 In a computer system which allows multiple programs to be run concurrently, memory must be divided/shared by the resident part of the operating system (some times called the resident monitor) and a number of user programs which may be running (ie. processes). It is the function of the OS to perform this task of Memory Management. Why do we want to run multiple programs concurrently? To make as efficient use of the computer resources (typically the CPU) as possible. The CPU should not be kept running idle waiting for something to happen. ICT 106 _Week 12_06 51 ICT 106 _Week 12_06 52 13

Does the CPU actually stop running? The CPU goes through the fetch/execute cycle (FEC) from power up i.e. it runs something all the time. If there are no user programs to run, what is it running? Running the OS is poor use of CPU. User programs may not be running all the time. Such processes could be waiting for I/O - waiting for the user to process a key. Think of a word processor. Almost all of the word-processor s time is spent waiting for a key to be pressed. ICT 106 _Week 12_06 53 ICT 106 _Week 12_06 54 For a process to run, it must be in memory. We want to keep running as many processes as possible, which means we would need more memory One solution is to keep increasing memory by adding more RAM. Even though RAM is cheap these days, it still costs more than other solutions to the problem. Also, there is a limit to the amount of RAM that one can use - why is that? The size of the address bus puts an upper limit on amount of addressable memory. ICT 106 _Week 12_06 55 ICT 106 _Week 12_06 56 14

Another solution is swapping. So instead of sitting in memory while waiting for an I/O event, the process is swapped to disk so that space in memory is made available for another process. Swapping is used with different schemes for carving up memory for the different processes that are running. The OS occupies a fixed portion of memory. The rest of memory is partitioned for use by the user processes. One scheme has been to use fixed size partitions: Memory is divided into a number of partitions of some fixed sizes (fixed equal sizes, or fixed unequal sizes). For the scheme with fixed unequal sized partitions, a process brought into memory is placed into the smallest partition that can contain the process (best fit). ICT 106 _Week 12_06 57 ICT 106 _Week 12_06 58 Another approach is to use variable size partitions: Fixed size memory partitions are wasteful in terms of computer memory. A process may be much smaller than the smallest available partition resulting in internal fragmentation. Also, a process may be too big to fit in any partition. Fixed sized partitioning is generally not used today Another approach is to use variable size partitions: A process is given only as much space as needed when it is brought into memory. When things first start out (i.e. in the beginning) this approach looks all right. ICT 106 _Week 12_06 59 ICT 106 _Week 12_06 60 15

But over the life cycle of the MM technique, as processes terminate and other processes are spawned, little holes will develop in memory which are too small for any process to be slotted in. This is again wasting memory external fragmentation -because the amount of memory used up by these holes could be quite large. Another way to partition memory is to use fixed size small chunks of memory called frames. A program (process) is broken up into similar fixed size small chunks called pages. There is still wastage in this scheme but at most the wastage for each process is only a proportion of a frame. The OS maintains a list of available frames. ICT 106 _Week 12_06 61 ICT 106 _Week 12_06 62 Another advantage of this technique is that page frames need not be contiguous for a program to be loaded. This leads to better memory utilisation. To implement this scheme, the OS maintains a page table for each process. The table maps the process s page to the memory frames. Given a page number and relative address within the page, CPU hardware translates this to frame number and relative address using the page table. This technique for dealing with partitioned memory led to the development of virtual memory. Virtual Memory Virtual memory - the effective useable memory by user(s) and/or OS is larger than the main memory of the computer (actual RAM, core, internal,..) Extra memory and contents are swapped to/from disk from/to main memory Disk Swap area - special area (contiguous area) is created/allocated on the disk specifically for supporting this swapping Actual sectors allocated can affect performance ICT 106 _Week 12_06 63 ICT 106 _Week 12_06 64 16

Virtual Address Space - is the range of useable virtual addresses Physical Address Space - actual physical addresses corresponding to existing memory What does the physical address space depend on? amount of memory (eg. I have 128 Mb of Ram)? number of bits making up the address (size of address bus)? Virtual memory support & disk swapping etc is transparent to the user User program does not normally need to consider memory management. But to make efficient use of resources, an understanding of the concepts is needed. External user can be aware of virtual memory being utilised because of flashing HDD light. Program may take extra time ICT 106 _Week 12_06 65 ICT 106 _Week 12_06 66 Note however, knowledge of virtual memory can greatly affect program efficiency (ie. more or less execution time) The maximum execution speed of a program running in a virtual memory environment can be at most equal but never exceed the execution speed of the same program running on the same hardware but with virtual memory turned off. Very often, a program running in a virtual memory system is slower. Paging and Paged Memory Physical memory is broken up into fixed size chunks called frames Each page is of the same size and typically 256-4K bytes ICT 106 _Week 12_06 67 ICT 106 _Week 12_06 68 17

Example implementation 64k virtual address space with 16 4K pages Given any address ==> Page# Vaddress = 4bit page# + offset into page 16k of main memory with 4 4K page frames Given any main memory address ==> PageFrame# Paddress = 2 bit page frame# + page offset All pages cannot be in memory at any one time Page table provides mapping between pages and main memory/disk 1 bit flag: Page in memory or is not Several bits relating to location of page on disk (eg. could be logical sector number) Page frame # if in memory ICT 106 _Week 12_06 69 ICT 106 _Week 12_06 70 Vaddress access Split into Page# and PageOffset Go through page table to determine whether page in memory If not, read page from disk into a page frame in physical memory ( page fault --> interrupt) Get PageFrame# from table Paddress = PageFrame# +PageOffset Page replacement policy If all page frames are full and a required page not in memory? LRU - Least recently used page is a simple and most typical replacement policy Reuse the page frame which has been used least recently Thrashing is where a program generates page faults frequently and continuously ICT 106 _Week 12_06 71 ICT 106 _Week 12_06 72 18

Segmentation Some details already addressed - we have actually used segments - (remember stack segment, data segment?) Segmentation in the Intel Pentium (the Pentium has hardware support for both paging and segmentation) Each virtual (logical) address consists of a 16 bit selector and a 32 bit offset. 2 bits of the selector deal with memory protection or privilege and the other 14 bits take part in addressing. Together with the offset, 46 bits can be used for addressing, giving virtual memory space of 2 46 = 64 Terabytes. Without segmentation, virtual memory space is 2 32 = 4 Gigabytes. The physical address space is also 4 Gbytes as the address bus is 32 bits wide ICT 106 _Week 12_06 73 ICT 106 _Week 12_06 74 Local & Global Descriptor tables are used to support virtual memory LDT local to a program describes its segments corresponding to code, data, stack, etc GDT shared by all programs, typically system segments including operating system itself Each selector has bits relating to table index, local/global, and privilege level Some of the crucial MM issues specific to 80x86 machines are as follows: 1. How can many processes run the same code? (Ans: through memory sharing) 2. Can one process be prevented from accidentally writing to memory belonging to another? (Ans: use virtual addressing) 3. What if the collective size of all processes & OS exceeds available memory? (Ans: virtual memory) ICT 106 _Week 12_06 75 ICT 106 _Week 12_06 76 19

Memory Sharing (Issue 1) Segmented architecture of 80x86 is ideal for this Different processes can be allocated the same code segments, different data segments Virtual Addressing (Issue 2) In real mode 80x86 support 20bit addressing (00000H to FFFFFH = 1 MB) Address calculations typified by instruction address 16 * c(cs) + c(ip) ICT 106 _Week 12_06 77 ICT 106 _Week 12_06 78 In protected mode 80x86 machines support following 286-24 bit addresses ==> 16 MB 386/486 (and later) - 32 bit addresses ==> (range: 00000000H to FFFFFFFFH) ==> can address up to 4 GB of RAM The OS maintains a Local Descriptor Table (LDT) for each program (task) it loads into memory. When a program is loaded, the OS puts appropriate values in the segment registers and physical segment addresses into the LDT. The values in the segment registers are used to index the LDT. Address calculations are typified by instruction address. An LDT entry contains other information too. The CS register shown below also contains other information too) LDT [ c(cs) ] + c(ip) OS also maintains a Global Descriptor Tables (GDT) in memory to store info about all processes in the system Notation LDT[n] means contents of nth entry of LDT ICT 106 _Week 12_06 79 ICT 106 _Week 12_06 80 20

Entries in LDT are physical addresses (as well as other information), size of which is equal to address size of machine Other segments registers identical to CS: DS, SS, ES In protected mode, they do not contain segment address Instead they contain pointers (selectors) to entries in the LDT table Once segment address is calculated, offset calculations are same as before (add the two values) Even in protected mode, offsets are still 16bit for 286 machines, and segment size still restricted to max of 64k Protected mode for 386/486 (and later machines) have 32bit offsets, so a segment could fill entire memory space ICT 106 _Week 12_06 81 ICT 106 _Week 12_06 82 All registers (eg. IP, BX, BP...) are also 32bit and are called EIP, EBX, EBP,... etc Hardware has means to avoid slowdown in performance of continually having to read memory to access DT entries Virtual addressing refers to the fact that in protected mode, addresses only appear to have certain values. Any attempt to access addresses outside these values would cause memory violation. Physical addressing is the raw absolute address (continual mapping between virtual and physical addresses via selectors & DT entries). This is the address which is put on the address bus by the CPU. ICT 106 _Week 12_06 83 ICT 106 _Week 12_06 84 21

Associated with each DT entry, is also a size which has been allocated for that selector (segment) If a process attempts to access memory outside the allocated segment, an interrupt will be generated. This addresses MM Issue 2 Memory management and allocation is performed by OS, memory protection is handled by hardware (ie. latter can t be done by OS, as it is not executing) Virtual addressing through the DT also allows for support of the concept of virtual memory (Issue 3) Virtual memory make use of the disk, and physically moves memory contents to/from physical memory from/to special regions on the disk (Disk swapping) ICT 106 _Week 12_06 85 ICT 106 _Week 12_06 86 DT entries also contain a present bit which indicates whether or not the segment is physically in memory or needs to be first read from the disk into memory In reading a segment from the disk to memory, if there is not enough physical memory space, a segment in memory may first be required to be saved to disk, with the present bit being changed appropriately Handled by hardware, which generates an interrupt, allow user program to be temporarily suspended while operating system loads a segment from the disk Normally, support of this is completely transparent to the user. Note: Understand the general concepts only - no need to be able to implement any of these details. ICT 106 _Week 12_06 87 ICT 106 _Week 12_06 88 22