You are on page 1of 55

CSC 203

OPERATING SYSTEM CONCEPTS

INTRODUCTION
Index
Component of a computer system Purpose of an OS Function of an OS Evolution of OS Early System Simple batched Systems Multiprogrammed batched Systems Time Sharing Desktop Systems Parallel Systems Distributed Systems Real time Systems Handheld System

Component of a computer system


Abstract view of the components of a computer system

User

User

User

User
Word processors, spreadsheets, compilers & web browsers

Compiler

Assembler

Text Editor

Database System

Application Programs Operating System Computer Hardware

Define the ways in which these resources are user to solve users computing problems Control & coordinate the use of the hardware among the various application programs for the various users.

CPU memory & IO devices Provides the basic computing resources for the system

Purpose of an OS
Provides an environments for user to execute programs To make solving user problems easier. Convenient for the user to use the computer system Efficient operation of the computer system especially for shared multi user system.

Function of an OS
A resource allocator o E.g. of resources: CPU time, memory space, file storage space, IO devices etc. o Decide on how to allocate these resources to specific program & users so that it can operate the computer system efficiently and fairly A control program o Manage the execution of user programs to prevents error & improper use of the computer o Concern with operation & control of IO devices.

Evolution of OS
Early System Simple Batch system Multiprogrammed Batch Systems Time Sharing System Personal Computer System Parallel System Distributed System Real Time Systems

Early System Physically large machine & run from console. Common Io device punch cards, paper tapes, card reads, line printers, magnetic tapes. Programmers have to know the machine language to do the programming. Programs loaded manually into the memory. High setup time and hob could not finish on time

Simple Batched Systems Job with similar needs were batched together and run through the computer as a group. o Less setup time - compiler does not changed too frequent. Resident monitor o 1st OS develop o Always in memory & controlled the operation automatically o o The speed of Io devices were constrained by the speed of the mechanical devices disadvantage : high delay between job submission & Job completion Off-line Processing o Introduced magnetic tape

Card Reader

CPU

Line Printer

On-line I/O operation

Card Reader

Tape Drives

CPU

Tape Drives

Line Printer

Off-line I/O operation

Spooling o Simultaneous peripheral online o Introduced the disk technology random access device o Introduced the concept of multiprogramming o Advantage: increase in system performance.

Disk

Disk acts as a buffer for reading far ahead on input devices and Storing output files until output device available

Card Reader

CPU

Line Printer

Multiprogrammed Batched Systems OS kept several jobs in memory All jobs entered were kept in a job pool. Selects job from job pool to memory ( job scheduling) Select jobs that were ready to execute from memory to be executed by CPU ( CPU scheduling) COU always had job to be executed high CPU utilization User could not interact with the job when it is executing.

Time Sharing Introduce multitasking System An online communication. o Provides on line communication with between user and system o User can interact with his programs while they are executing Multiprogramming and time sharing are the central themes of modern OS.

Desktop Systems PCs appeared in 1970s Initially designed for dedicate single user The earlier PCs were not multiprogramming or multitasking Goals of its OS changed with time maximizing user convenience & responsiveness OS was been upgrades to support multiprogramming & multitasking Hardware cost is low & affordable for individual user

Parallel Systems A centralized system or tightly couple system Consist of more the one processor but sharing the same bus, clock, memory and peripheral device. Multiprocessor system. Advantages o Increase throughput o Economical o Reliable TWO Types: Symmetric Multiprocessing Asymmetric multiprocessing

Distributed Systems Distributed Computations among several processors in several locations. A loosely coupled system. Each processors has its own local memory, bus & clock. Communicated with one another through communication line, e.g.: high speed buses or telephone lines Advantages; o Resource sharing o Speed up computation reliability o Communication E.g.: o LAN, MAN & WAN o Client server system & peer to peer system

Real Time Systems A special purpose OS Has a well detained and fixed time constraints Often used a control device in an dedicated application Two flavors; o Hard real time system Guarantees that critical task to be completed on time E.g.: robotics, industrial control o Soft real time system. A critical real time task gets priority over other task and retains that priority until completes E.g.: ATM machine, multimedia, VR

Handheld System Used in mobile device Convenience and portable Limitation o Memory size o Display screen o Speed of processor.

Computer Organization
Index
Computer system operation Bootstrap Interrupt Storage Structure IO Structure OS Architecture Single Processor Multiprocessor Clustered system

Computer System Operation

disks

Printer

Tape Devices

CPU

Disk Controller

Printer Controller

Tape Drive Controller


Device controller - In change of a specific type of device.

System bus provide access to share memory

Memory Controller

Ensure orderly access to memory

Memory

Synchronize access to memory

Bootstrap

Initialize all aspect of the system

Locate as kernel and load it into memory

Start executing the 1st program

Wait for an event to occur

Interrupt o Interrupt is signal sent CPU by an occurrence of an event. o Interrupt is triggered by : Hardware sent Signal Through the bus Software executed special instruction called system call or monitor call o Terms: Interrupt Vector an array of pointer that hold address of interrupt service routine for the various devices Trap a software generated interrupt normally caused an error or specific request from users. o Modern OS is interrupt driven o Steps in Handling Interrupts:

CPU stops whatever is doing and transfer execution to a fixed location. Interrupt service routine execute Resume interrupted computation.

Storage Structure
Main Memory : o Contains computer programs to be executed o The only large storage area that processor can access directly. Secondary Storage: o extension to main memory o able to hold large quantity of data permanently Storage device hierarchy:
Register Cache Main Memory

Electronic disk Magnetic disk

Optical disk Magnetic tapes

IO Structure
Device Controller: o In charge of specific type of IO devices o Has a local buffer and a set of special purpose register o Responsible for moving data between the peripheral devices the it controls and its local buffer IO operation: o CPU load s the appropriate registers within the device controller o Device controller examines the contents of registers and determined what action to be taken o Completion of IO by triggering on interrupts IO method:

Request process waiting Device driver Interrupt handler Hardware data Transfer

Request process waiting Device driver Interrupt handler Hardware data Transfer

Synchronous IO Simple case

Asynchronous IO Return control to user process without waiting for the IO completion Control is return to the user Increase system efficiency because it process when the IO operation is allows concurrent IO Operation completed OS knows exactly which device is Device status table is required to interrupting keep track which device is interrupting DMA Structure o Direct memory access. o Used for high speed IO devices, able to transmit information at close to memory speed o Device controller transfer blocks of data from buffer storage directly to main memory without CPU intervention o Only one interrupts per block rather than per byte generated by lower speed devices.

Computer System Architecture


Single processor systems o Used by most systems o One main CPU capable of executing a general purpose instruction set Multiprocessor systems o As known as parallel system or tightly couple system o Have 2 or more processors in close communication, sharing the computer bus, sometimes clock + memory + peripheral. o Advantages: high throughput, economy of Scale + reliability o Two type : Asymmetric multiprogramming Each processor is assigned specific task Define a master slave relationship Symmetric multiprogramming. Each processor performs all tasks.

Operating System Concepts


Index
OS operations Process management Main memory Storage management Protection & security Distributed system Special purpose system Special purposed system Computing environment OS services User OS interface System Calls System Programs System boot

OS operations
Sharing System resources requires OS to ensure that an incorrect program cannot cause other programs to execute in correctly. For hardware protection. o Dual Mode Operation To make sure proper execution of OS, we must be able to distinguish 2 separate modes of operations: User mode Kernel mode ( as known as supervisor mode, system mode, privileged mode) A bit; mode Bit will be added to the hardware to indicate the current mode: Kernel mode (0) User mode(1) Privileged instructions Machine instruction that may cause harm To prevents OS from errant User and errant user from one another. Only allowed to be executed in the kernel mode

Interrupt/ trap/system call

Kernel Mode

User Mode

Timer Why? Ensure that OS maintains control over the CPU Prevent a user program from getting stuck in an infinite loop or not calling system service & never return control to the OS Set to interrupt the computer after a specified period Fixed ( e.g. : 1 per 60 second) Variable (e.g. : 1 millisecond to 1 second)

Process management
Program does nothing unless its instructions are executed by the CPU called a process A process needs certain resources including CPU time, memory , files and IO devices to accomplish its task OS responsibilities o Creation and deletion of user and system processes o Suspension and resumption of processes o Provision of mechanisms for : Process synchronization Process communication Deadlock handling

Main memory Management


Memory is large array of work or byte, each with its own address. It is a repository of quickly accessible data shared by the CPU and IO devices. Main Memory is a volatile storage device OS responsibilities: o Keep track of memory location o Decide which processers are to be loaded into memory o Allocate and deal locates memory spaces.

Storage Management
OS provides a uniform, logical view of information storage convenient to the users OS abstracts from the physical properties of its storage devices to define a logical storage unit (file). OS maps file onto physical media and accesses these files via the storage devices Types of storage management: o File system management o Mass Storage management o IO Systems

File management
One of the most visible components of an OS A file is a collections of related information defined by its creator. Commonly file represents programs and data. Files are organized into directories to made the easier to use OS responsibilities : o Creating and deleting files o Creating and deleting directories to organize files o Supporting primitives for manipulating file and directories o Mapping files onto secondary storage backing up file on stable (nonvolatile)storage media.

Mass-Storage management
Most modern computer system use disk as the principal on-line storage medium for both programs and data OS responsibilities : o Free-space management o Storage allocation o Disk scheduling

IO systems
One purpose of OS is to hide the operations inside the hardware device from user. IO subsystem consists of: o A memory management components including buffering, caching, and spooling o General device driver interface o Drivers for specific hardware devices.

Protection and security


Protection: o Any mechanism for controlling the access of processes or users to the resources o Provide means for specification of the controls to be imposed and means for enforcement o Improve reliability by detecting latent errors at the interface between component subsystems.

Distributed system
A distributed system: o A Collection of physically separate computer systems that are networked to provide the users with access to the various resources that the system maintains. o Increases computation speed, functionality, data availability and reliability. Network o A common path between two or more systems o Vary according to the used of: Protocol (TCP, IP, ATM) Distance between nods (LAN, MAN,WAN) Transport media (copper wire, fibers trans, wireless) NOS o An OS that provides features such as file sharing access and a communication scheme that allow different processes on different computers to exchange messages Distributed OS provide less autonomous environment the different OS communicate closely enough to provide illusion that only a single OS id controlling the Network.

Special Purpose Computer


Class of computer systems whose functions are more limited and whose objective is to deal with limited computation domain o Real-time embedded systems Most Prevalent Form of computer existence they are found everywhere ( e.g. : car engines, microwave ovens). Have very specific task The systems they run on are primitive - OS provide limited features Little or no user intervention - monitoring and managing hardware devices o Multimedia systems Multimedia data Consist of audio + video files as well as conventional file Must be delivered (streamed) according to certain time restrictions (e.g. : 30 frames per second) Multimedia application : MP3 DVD movies Video conferencing Live webcasts o Handheld systems Includes PDAs and Cellular telephones Major challenge to developers size

Limitations Small amount of memory o Size: 512 kb to 128 mb o Critical task to OS returning all allocated memory back to the memory manager when the memory is not being use Slow processors o Small space small battery, less power Small display screen

Computing environment
Traditional computing Client- server computing Peer to peer computing Web based computing

Traditional computing PCs connected to a network Servers providing file + printing services Limited remote access + portability options Terminal were attached to mainframes Slow modem connections

Influence from the web technologies Companies establish portal web accessibility to internal servers. Network computer Handheld computer connect to wireless Great network connection speed at low cost Firewall

Client server computing

Client

Client

Client

Client

Server

General structure of a client server system Server system satisfy request generated by client systems Server system can be broadly categorized as o The computer server system Provides an interface to which client can send a request to perform an action + sends back results to the client E.g.: a server system can be broadly categorized as The computer server system o Provides an inter face to which clients can send a request to perform an action + sends back result to the client o E.g.: a server running a database that responds to clients request for data The file server system o Provides a file system interface where clients can create update, read and delete files. o E.g.: web server that delivers files to clients running web browser.

Pear to peer computing Each node nay acts as either a clients of a server. Advantage : services can be provided by several nodes throughout the network To participate in a P2P system, a node must 1st join the network of peers. Then it can begin providing services to and requesting services from other nodes in the network Play a role in the future of many services: searching, file exchange and email.

Web based computing Web computing has increased the emphasis on networking o Devices Has wire and wireless access Faster network connectivity, provided by either improved network technology, Optimized networks implementation or both o Rise of new categories of devices E.g.: load balancer - distribute network connections among a pool o similar servers o Increased the complexity of devices web-enabled

Operating system services


Set of services that provide functions that are helpful to the users: o User interface Command-line interface-uses text commands and a method for entering them Batch interface command and directives to control those commands are entered into files and those files are executed GUI window system with a pointing device to direct IO, chooses from menu, make selection & keyboard to enter text. o Program execution System must be able load a program into memory and run that program. Program must be able to end execution o IO operations OS provides a mean to do IO o File system manipulation o Communication implemented thru shared memory and message passing o Error detection OS must constantly aware of possible error For each type of error, OS should take appropriate action to ensure correct and consistent computing. Set of services for ensuring efficient operation of the system o Resource allocation When multiple users/ multiple jobs running at the same time, resources must be allocated to each of them E.g. : CPU scheduling o Accounting Keeping track of which users use how much and kids of resources Statistics are valuable tools to reconfigure the system to improve computing services

Protection and security Protection involve ensuring that all access to system resources is controlled Security from the outsides, defending external IO devices, invalid access attempts and recording all such of connections for detection of break-in.

User operating system interface


Two fundamental approaches o Command interpreter Command interpreter Allows user to directly enter commands that are to be performed by OS o Graphical User Interface (GUI) Allow users to interact with OS via a graphical user interface

Command Interpreter
Some OS include the command interpreter in the kernel Some (E.g. : windows XP & UNIX) treat the command interpreter as a special program that is running when a job is initiated or when a user first logs on On systems with multiple command interpreters to choose from the interpreters are known as shells. Main function of command interpreter to get executed the next user specified command (e.g.: manipulating files)

Graphical User Interfaces


User friendly Provides mouse based window and menu system as an interface Provides desktop metaphor, mouse is moved to position its pointer on images 9icons) on the screen that represent programs, files etc. Clicking a button on the mouse can invoke (folder or pull down a menu that contains commands

System Calls
Provide an interface to the services made available by OS Available as routines written in C & C++ & assembly language Three general methods to pass parameters to the OS: o Pass parameter in a table in memory + the table address is passed as a parameter o In a register o Push (store) the parameters onto the stack by the program + pop off the stack by operating system.

Type of system calls Can be grouped into 5 major categories : o Process control End, abort Load execute Create process, terminate process Get process, terminate process Get process attributes, set process attributes Wait events, signal event Allocate and free memory o File manipulation Create file, delete file Open, close Read, write, reposition Get file attributes, set file attributes o Device manipulation Request device, release device Read write, respond Get device attributes, set device attributes Logically attach or detach device o Information maintenance Get time / date, set time / date Get system data, set system data Get process, file or device attributes Set process, file or device attributes o Communications Create, delete communication connections Send, receive messages Transfer status information Attach or detach remote devices

System programs
Provide a convenient environment for program development and execution Divided into 6 categories: o File management create, delete, copy, rename and manipulate files and directories. o Status information ask for date, time, amount of available memory o File modification text editors to create modify the contents of files stored on storage devices o Programming language support compilers, assemble, debugger and interpreter for common PLs o Program loading an execution once a program is assembled or completed, it must be loaded to memory to be executed o Communications - provide the mechanism for creating visual connections among process, messages and to browse web pages etc.

System Boot
Procedure of stating a computer bay loading the kernel booting Bootstrap program / bootstrap loader locate the kernel

Loads the kernel to main memory

Start execution

Tasks performed by bootstrap programs; o Run diagnostic to determine state of the machine, if pass it continues the booting steps o Initialize all aspects of the system (CPU registers, device controller and contents of the main memory), then start OS.

Process Concepts
Index Overview Process Process state Process control block Process scheduling Scheduling queue Schedulers Context switch

Process
Process is a program in execution and an active entity. Process must progress in sequential fashion Program is a set of in structure and a passive entity A process includes o Program counter represents current activity o Stack contains temporary data o Data section contains global variables 2 types of process: o OS process execute the system codes o USER process execute user codes

Process State
At any one time, the process will be in one state and the sate changes as a process executes Only one process can be running at one time and many process may be ready and waiting 1. New the process is being created 2. Ready waiting to be assigned to a processor 3. Running instructions are being executed 4. Waiting waiting for some events to occur ( e.g. IO completions or reception of a signal) 5. Terminatefinished execution

Process control block (PCB)


Each process is represented in the OS by a PCB or called a Task Control Block An entity that defines a process to the OS. Each PCB is unique; there are no 2 PCBs alike. PCB contains: o Process state o Pointer o Process number (accounting information) o Program counter o CPU register o Memory limit(memory management information) o List of open files (IO status information) o CPU scheduling information

Process scheduling Queues


In a process cycle, a process might have to wait; the process will be placed in a queue. o Job queue Set of all process in the system, that are waiting t be moved into memory o Ready queue Set of all process residing in main memory, ready and waiting to be execute o Device queue Set of processes waiting for an IO device

Queuing Diagram

Schedulers
Process can be described as either: o 10 bound processes spends more time doing IO than computations; many long CPU burst. o CPU bound Process spends more time doing computations; few very long CPU bursts. OS selects process from one queue to another The selection of the process is carried out by scheduler: o Long term scheduler o Short term scheduler o Medium term scheduler

Long Term Scheduler


As known as Job scheduler Selects processes from the job queue and loads them into memory for execution Long term scheduler is invoked very in frequently ( once in a few minutes) and slow The long term scheduler controls the degree of multi programming (number of process in memory) and a combination of IO bound and CPU bound processes.

Short Term Scheduler


As known as CPU scheduler Select from among the processes that ready to execute and allocates the CPU to one of them Short term scheduler is invoked very frequently ( once in 100 milliseconds) and very fast

Medium Term Scheduler


The process will be swapped out from memory and swapped back in later for continuation This swapping scheme is to reduce the degree of multiprogramming and balance the process mixture.

Contact Switch
Happened when switching the CPU to another process Saving the state and loading the saved state for new process. Purely overhead system does no useful work while switching

CPU Scheduling
Index Basic concepts CPU IO burst Cycle CPU scheduler Preemptive Scheduling Dispatcher Scheduling Criteria Scheduling Algorithm FIFO SJF Priority Round Robin (RR)

Basic Concepts
CPU scheduling is the basic of multi programmed OS - to make sure CPU never idle and maximize CPU utilization The objective of multiprogramming is to have some processes running at all the time, in order to maximize CPU utilization.

CPU IO burst Cycle


Process Execution and IO wait. Process alternate between these 2 states. Process execution begins with a CPU burst the followed by IO burst and so on. The last CPU burst will end with a system request to terminate execution.

Alternating Sequence of CPU And I/O Bursts

CPU scheduler
As known as Short term scheduler Involved in selecting one of the process in the ready queue to be executed when the CPU become idle

Preemptive scheduling
CPU scheduling may take place when a process o Switches from running to waiting state (e.g. IO request) o Switches from running to ready state (e.g. Interrupt occurs) o Switches from waiting to ready state (e.g. completion of IO) o Terminates Two types of scheduling: o Non preemptive once the CPU has been allocated to a process allocated to a process keeps the CPU until terminate or switching to another state o Preemptive once the CPU has been allocated to a process, the process keeps the CPU until a certain conditions occur to release the CPU.

Dispatcher
The module that gives control of the process selected by the short term scheduler This function involves: o Switching context. o Switching the user mode o Jumping to the proper location in the user program to restart the program Dispatch latency o Time it take for the dispatcher to stop one process and start another running

Scheduling criteria
Criteria used to choose the appropriate scheduling algorithm: o CPU utilization keep the CPU as busy as possible o Throughput number of processes that complete their execution per time unit. o Turnaround tome Amount of time to execute a particular process Completion time arrival tome o Waiting time Amount of time a process has been waiting in the ready queue Starting time arrival time o Response time amount of time it takes from when a request is submitted until the 1st response is produced Target o Maximize the CPU utilization and throughput o Minimize turnaround time, waiting time and respond time

Scheduling algorithm
CPU scheduling deals with the problem of deciding which of the processes in the ready queue to be allocated to the CPU. Available algorithms: o FCFS(first come first served) o SJF ( shortest job first) o Priority o RR ( round Robin)

First Come First Served (FCFS)


The simplest CPU scheduling algorithm The first process that request for CPU is allocated first Non preemptive Problems: o Convoy Effect All the process wait for the one bog process to get off the CPU Lower CPU and CPU utilization o Not suitable for time sharing system o High average waiting time.

FCFS (Example) First-Come, First-Served (FCFS) Scheduling Process P1 P2 P3 Burst Time 24 3 3

Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is:

Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17

Suppose that the processes arrive in the order P2, P3 , P1 The Gantt chart for the schedule is:

Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case Convoy effect short process behind long process

Shortest job First (SJF)


CPU is assigned to the process that has the smallest CPU burst Time Use frequently in long- term scheduling and batch system. Advantage : minimum average waiting time Problems: o Difficult to know the length of the next CPU burst o Cannot be implemented in short term scheduling. Can either be : o Preemptive as known as shortest time remaining time first scheduling (SRTF) o Non Preemptive

SJF (example) Process Arrival Time P1 0.0 P2 2.0 P3 4.0 P4 5.0 SJF (preemptive) Burst Time 7 4 1 4

Average waiting time = (9 + 1 + 0 +2)/4 = 3

Process Arrival Time P1 0.0 P2 2.0 P3 4.0 P4 5.0 SJF (non-preemptive)

Burst Time 7 4 1 4

Average waiting time = (0 + 6 + 3 + 7)/4 = 4

Priority
A priority is associated with each process and the CPU is allocated to the process with the highest priority. Equals priority process are scheduled in FCFS Priority scheduling algorithm can be preemptive or non preemptive. Priority can be defined either: o Internally use some measurable quantities to compute the priority (e.g.: time limit + memory requirements) o Externally set by criteria outside the OS (e.g. importance of process and political issues) Problem: o Indefinite blocking or starvation a low priority process, gradually increase may not get the CPU Solution o Aging as the time progresses, gradually increase the priority of the process

Priority (Example)

Round Robin
Similar to FCFS, but it is preemptive Each process is assigned a unit of time called time quantum or time sliced (generally 10 to 100 milliseconds) In RR, the ready queue is a FIFO queue Suitable for rime sharing system The performance of this algorithm depends on the rime quantum: o A large time quantum performance will be the same as FCFS o A small time quantum increase context switching

Round Robin (Example) Process P1 P2 P3 P4 Burst Time 53 17 68 24

The Gantt chart is:

Memory Management
Index
Introduction To Memory Basic Hardware Base & Limit Registers Address Binding Logical Vs Physical Address Space Memory Management Unit (MMU) Dynamic Loading Dynamic Linking Shared Libraries Swapping Contiguous Memory Allocation Memory Mapping & Protection Memory Allocation Fragmentation Solutions For External Fragmentation Paging Segmentation

Introduction To Memory
Memory is central to the operation of the modern computer system. Memory consist of large array of words or bytes, each with its own address CPU fetches instructions from memory according to the value of the program counter. These instructions may cause additional loading from & storing to specific memory address. Memory unit sees only a stream of memory addresses; it does not know how they are generated or what they are for.

Basic Hardware
The only storage that CPU can access directly are : 1. Registers built into the processors itself generally accessible within one cycle of the CPU. 2. Main memory - may take many cycles of the CPU clock to complete Protection provided by the hardware o Why? Ensure correct operation has to protect the OS from access by user process & to protect user processes from one another o How? Make sure that each process has a separate memory space Ability to determine the range of legal addresses that the process may access & to ensure that the process can access only these legal address Implementation of base & limit registers.

Base & Limit Registers


Base register holds the smallest legal physical memory address. Limit register specifies the size of the range. Example: if the base register holds 300040 & limit register is 120900, then the program can legally access all address from 300040 through 42090. How it works? 1. CPU hardware compare every address generated in user mode with the registers 2. Any attempts by program executing in user mode to access OS memory or others users memory results in a trap to the OS

Address Binding
User programs go through several steps before being executed In each step, addresses may be represented in different ways Binding is the mapping from one address space to another

Binding of instructions & data to memory address can be done at any step along the way: 1. Compile time o o If it is know at compile time where the process will reside in memory, then absolute code can be generated If later the starting location charges, then it will be necessary to recompile the code

2. Load Time o o If it is not know at compile time where the process will reside in memory, compiler will generate relocatable code If later the starting location charges, we need only reload the user code

3. Execution time o o If the process can be moved during its execution from one memory segment to another, binding must be delayed until run time Requires special hardware

Logical Vs Physical Address Space

Memory Management Unit (MMU)


A hardware device that responsible in run time mapping from virtual to physical address Here, base register is called relocation registers Value of the relocation register is added to every address generated by a user process at the time it is sent to the memory User program deals with logical address, never sees the real physical address

Example: base is at 14000 An attempt by the user to address location 0 is dynamically relocated to location 14000 An attempt of access to location 346 is mapped to location 14346

Dynamic Loading
A routine is not loaded until it is called Advantage: better memory space utilization Useful when large amounts of codes are needed to handle infrequently occurring cases, such as error routines Dynamic loading does not require special support from the OS, user has to take the advantage of this method

Dynamic Linking
Linking is postpaid until execution time Usually used with system libraries, such as language subroutine library Stub o o o A small piece of code that indicates how to locate the appropriate memory-resident library routine or how to load the library is the routine is not already present Included in the image for library routine reference Replace in the image library routine reference executes the routine

Shared Libraries
A system that included version information of the libraries in both program & the library To prevent programs from executing new incompatible versions of the libraries

Swapping
A process must be in memory to be executed. However, a process can be swapped temporarily out of memory to a backing store & then brought back into the memory for continued execution. Swapping requires a backing store o A fast disk o Large enough to accommodate copies of all memory images for all users o Must provide direct access to these memory image Constraints: 1. Must make sure that the process is completely idle before it is swapped 2. Must provide direct access to these memory images

Contiguous Memory Allocation


A method of allocating the parts of the main memory in the most efficient way Memory is divided into 2 partitions: 1. Resident OS held in low memory with interrupt vector 2. User process held in high memory Subtopics: o Memory mapping & protection o Memory allocation o Fragmentation

Memory Mapping & Protection


Provided by using a relocation register with a limit register o Each logical address must be less than limited register Protect both the OS & the other users program & data from being modified by the running process

Memory Allocation
Multiple partition method o Simplest method for allocating memory o Memory is divide into several fixed sized partitions o Each partition contain exactly one process o Working scheme: When a partition is free, a process is selected from the input queue & is loaded into the free partition When the process terminates, the partition becomes available for another process o OS keeps a table indicating which parts of memory are available & which are occupied o Hole one large block of available memory Dynamic storage allocation o Concerns how to satisfy a request of size n from a list of free holes o The most common strategies to select a free hole from the set of available holes are: o First fit Allocated the first hole that is big enough Searching can start either at the beginning of the set of holes or where the previous first fit search ended Stop when we find hole that is large enough o Best fit Allocated the smallest hole that is big enough Search the entire list Produce the smallest leftover hole o Worst fit Allocated the largest hole Search the entire list Produce the largest leftover hole o First fix and best fix are better in terms of speed and storage utilization.

Fragmentation
External fragmentation o Results from first fit & best fit strategies o Exist when there is enough total memory space to satisfy a request but the available space are not contiguous Internal fragmentation o Happens when the memory allocated is slightly larger than the requested memory o The size difference is internal fragmentation

Solutions for External Fragmentation


Solutions: 1. Compaction o Shuffle the memory contents so as to place free memory contents together in one large block. o Only possible if relocation is dynamic & is done at execution time o Can be expensive 2. Permit the logical address space of the processes to be noncontiguous, thus allowing a process to be allocated physical memory wherever the latter is available. 2 techniques: paging & segmentations.

Paging
A memory scheme that permits the physical address space of a process to be noncontiguous Basic method 1. Breaking physical memory into fixed sized blocks called frames 2. Breaking logical memory into blocks of same size called page 3. When a process is to be executed, its pages are loaded into any memory frames from the backing store o Backing store is divided into fixed-sized blocks that are the same size as the memory frames

Every address generated by the CPU is divided into 2 parts a page number (p) and a page offset (d) The page number is used as an index into a page table The page table contains the base address of each page in physical memory The base address is combined with the page offset to define the physical memory address to be sent to the memory unit

Formula: Physical address = (frame number * frame size) + offset Number of pages = program size / page size Number of frames = memory size / frame size

Example: Given that page size of 4 bytes and physical memory of 32 bytes (8 pages), how to map logical memory to physical memory?

Segmentation
A memory management scheme that support user view of a memory A logical address space is a collection of segments Each segment has a name and a length The addresses specify both the segment name & the offset within the segments User needs to specify each address by 2 quantities: a segment name & an offset To simplify, segments are numbered & are referred to by a segment number, rather than a segment name. Logical address consist of 2 Couple: ((segment number), offset) Requires segment table to map the logical address space to physical memory. Segment table consists of: 1. Segment base - starting address where the segment resides in memory 2. Segment limit - specifies the length of the segments Segmentation causes external fragmentation. Physical address = segment base + offset Offset must be less than limit value in the segment table

Virtual Memory
Index
Background Demand Paging Page Replacement Page replacement algorithms: FIFO Optimal LRU Trashing

Background
Virtual memory o It is a technique that allows the execution of process that may not be completely in memory o Advantages: Program size can be larger than physical memory size Programmers need not worry about the amount of physical memory available o More programs can be run at the same time because user program take less physical memory o Can be implemented via Demand paging Demand Segmentation

Demand Paging
Concepts: o Pages of processes are resided on secondary storage, when it is needed for execution, a page will be swapped into memory Advantages: o Less IO needed o Less memory needed o More users Uses: 1. lazy swapper o Never swaps a page into memory unless that page will be needed 2. valid-invalid bit scheme Valid-invalid bit scheme o Used to distinguish between pages that are in memory & on the disk 1. Valid (v) page is legal and in memory 2. Invalid (I) either not valid / valid but on the disk

Page fault a trap to OS indicating the page is not in memory Procedure for handling page fault: 1. Check an internal table (PCB) to determine whether reference was a valid or invalid memory access 2. If reference was invalid, terminate the process. If valid but not yet brought in that page, page in the latter. 3. Find a free frame 4. Schedule a disk operation to read the desired page into newly allocated frame 5. When the disk read completes, modify the PCB & page table to indicate the page is in the memory 6. Restart instruction that was interrupted

Hardware to support demand paging: 1. Page table has the ability to mark an entry invalid through a valid invalid bit or special value of protection bits. 2. Secondary memory Holds those pages which are not present in main memory A high speed disk Known as swap space

Page Replacement
Approach: o If no frame is free, find one that is no currently being used & free it. How? o Free a frame by writing its contents to swap space and changing the page table to indicate the page is no longer in memory Modified page fault routine to include page replacement: o Find the location of the desired page on the disk o Find a free frame: If there is a free frame, used it. Otherwise, use a page replacement algorithm to select____________________ Write the victim page to the disk; change the page & frame tables accordingly o Read the desired page into the free frame; change the page and frame tables o Restart the user process

Page Replacement Algorithm


There are many page replacement algorithms & we want the lowest page fault rate Evaluate algorithm by running it on a particular string of memory references (reference string) & computing the number of page fault on that string Reference string = the string of memory reference Logical address reference string must be converted to page reference string before calculating the number of page faults Page reference string = each logical address reference string / page size

Page Replacement Algorithm (Fifo)


The simplest page replacement algorithm Replace the page that is the oldest in the memory Easy to program but the performance is not always good May cause Beladys Anamoly - the number of page faults increase if the number of frames assigned to the process increased

Page Replacement Algorithm (Optimal)


Has the lowest page fault rate of all algorithms and will never suffer from Beladys anomaly Replace the page that will not be used for the longest period of time (looking forward the page reference string) Difficult to implement because it requires future knowledge of the reference string

Page Replacement Algorithm (Least Recently Used)


LRU uses the recent past as an approximation of the near future, which enables us to replace the page that has not been used for the longest period of time LRU associates with each page the time of that pages last use Replace the page that has not been used for the longest period of time (looking backward the page reference string)

Thrashing
If a process does not have enough pages, the page fault rate is very high. This leads to: o Low CPU utilization o OS thinks that it needs to increase the degree of multiprogramming o Another process added to the system A process is trashing if it is spending more time paging than executing Thrashing results in severe performance problems o As the degree of multiprogramming increases, CPU utilization also increases, although more slowly, until a maximum is reached, CPU utilization drops sharply

File System Management


Index
Allocation Method 1. Contiguous 2. Linked 3. Indexed Free Space Management 1. Bit Vector / Bit Map 2. Linked List 3. Grouping 4. Counting

Allocation Method
Many files are stored on the same disk Problem: how to allocate space to these files so that disk space is utilized effectively + files can accessed quickly. 3 major methods for allocating disk space: 1. Contiguous 2. Linked 3. Indexed

Contiguous Allocation
Requires each file to occupy a set of continuous blocks on the disk Has minimal number of disk seeks Information about files on the disk is stored in a directory The directory entry for each file indicates the address of the starting block and the length of the area allocated for this file Allows sequential and direct access only. Commonly used the first fit and best fit strategies to select from a list of free holes File cannot grow and suffers external fragmentation

Linked Allocation
Each file is a linked list of disk blocks: blocks may be scattered anywhere on the disk The directory contains a pointer to the first and last block of the file Each block contains a pointer to the next block o These pointers are not made available to the users Disadvantages: o Can be used efficiently only for sequential access files o Space is required for the pointers o Reliability in case pointers are lost or damage Important variation of linked allocation o File Allocation Table (FAT) o Used by MS-DOS and OS/2

File Allocation Table (FAT)

Index Allocation
Each file has its own index block which consists of an array of disk block address Support direct Access Suffers from wasted of disk space pointer overhead is greater than pointer overhead of linked allocation

Free Space Management


Is about how free space management on the disk is being kept 4 ways: 1. Bit vector / bit map 2. Linked list 3. Grouping 4. Counting

Bit Vector / Bit Map


Each block is represented by a bit o Free block o Allocated block Example: Consider a block of disk where blocks 2,3,4,5,8,9,10,11,12,13,17,18,25,26 and 27 are free and the rest are allocated Free space management bit map would be: 0011110011111100011000000111 Advantages: simplicity + efficiency in finding the first free block on the disk

Linked List
To link together all the free disk blocks, keeping a pointer to the first free block in a special location on the disk and caching it in memory The first block contains a pointer to the next free disk block Not efficient cannot get continuous space easily

Grouping
Keep the address of the first free blocks and the number of n of free contiguous blocks that follow the first block o Each entry consist of a disk address and count

Counting
To store the addresses of n free blocks in the first free block The first n-1 of these blocks is actually free. The last block contains the addresses of another n free block, and so on. Advantage: addresses of a large number of free blocks can now be found quickly

You might also like