a1 os review sp09

Upload: mayuresh-kulkarni

Post on 04-Apr-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 a1 Os Review Sp09

    1/68

    Basic Operating System

    Concepts

    A Review

  • 7/29/2019 a1 Os Review Sp09

    2/68

    Main Goals of OS

    1. Resource Management: Disk, CPU cycles,etc. must be managed efficiently to

    maximize overall system performance2. Resource Abstraction: Software interface

    to simplify use of hardware resources

    3. Resource virtualization: Supports resourcesharinggives each process theappearance of an unshared resource

  • 7/29/2019 a1 Os Review Sp09

    3/68

    Fundamental Concepts

    System Calls

    Execution Modes

    Operational concepts that allow the operating

    system to maintain control and protection.

    Based on hardware features.

  • 7/29/2019 a1 Os Review Sp09

    4/68

    System Call

    An entry point to OS code

    Allows users to request OS services

    APIs/library functions usually provide aninterface to system calls

    e.g, language-level I/O functions map userparameters into system-call format

    Thus, the run-time support system of a prog.language acts as an interface betweenprogrammer and OS interface

  • 7/29/2019 a1 Os Review Sp09

    5/68

    Execution Modes

    (Dual Mode Execution)

    User mode vs. kernel (or supervisor) mode

    Protection mechanism: critical operations

    (e.g. direct device access, disablinginterrupts) can only be performed when the

    OS is in kernel mode

    Mode bit Privileged instructions

  • 7/29/2019 a1 Os Review Sp09

    6/68

    Mode Switching

    System calls cross the boundary.

    System call initiates mode switch from user to

    kernel mode Special instructionsoftware interrupt transfers

    control to a location in the interrupt vector

    OS executes kernel code, mode switch occurs

    again when control returns to user process

  • 7/29/2019 a1 Os Review Sp09

    7/68

    Processing a System Call*

    Switching between kernel and user mode is time

    consuming

    Kernel must Save registers so process can resume execution

    Verify system call name and parameters

    Call the kernel function to perform the service

    On completion, restore registers and return to caller

    Other overhead includes cache misses, prefetch , . . .

  • 7/29/2019 a1 Os Review Sp09

    8/68

    Review Topics

    Processes &Threads

    Scheduling

    Synchronization

    Memory Management

    File and I/O Management

  • 7/29/2019 a1 Os Review Sp09

    9/68

    Review of Processes

    Processes

    process image

    states and state transitions

    process switch (context switch)

    Threads

    Concurrency

  • 7/29/2019 a1 Os Review Sp09

    10/68

    Process Definition

    A process is an instance of a program inexecution.

    It encompasses the static concept ofprogram and the dynamic aspect ofexecution.

    As the process runs, its context (state)changesregister contents, memorycontents, etc., are modified by execution

  • 7/29/2019 a1 Os Review Sp09

    11/68

    Processes: Process Image

    The process image represents the current status of

    the process

    It consists of (among other things) Executable code

    Static data area

    Stack & heap area

    Process Control Block (PCB): data structure used to

    represent execution context, or state

    Other information needed to manage process

  • 7/29/2019 a1 Os Review Sp09

    12/68

    Process Execution States

    For convenience, we describe a process as

    being in one of several basic states.

    Most basic:

    Running

    Ready

    Blocked (or sleeping)

  • 7/29/2019 a1 Os Review Sp09

    13/68

    Process State Transition Diagram

    ready running

    blocked

    preempt

    dispatch

    wait for eventevent occurs

  • 7/29/2019 a1 Os Review Sp09

    14/68

    Other States

    New

    Exit

    Suspended (Swapped)

    Suspended blocked

    Suspended ready

  • 7/29/2019 a1 Os Review Sp09

    15/68

    Context Switch

    (sometimes called process switch)

    A context switch involves two processes:

    One leaves the Running state

    Another enters the Running state

    The status (context) of one process is saved;

    the status of the second process restored.

    Dont confuse with mode switch.

  • 7/29/2019 a1 Os Review Sp09

    16/68

    Concurrent Processes

    Two processes are concurrent if their

    executions overlap in time.

    In a uniprocessor environment,

    multiprogramming provides concurrency.

    In a multiprocessor, true parallel execution

    can occur.

  • 7/29/2019 a1 Os Review Sp09

    17/68

    Protection

    When multiple processes exist at the sametime, and execute concurrently, the OS must

    protect them from mutual interference. Memory protection (memory isolation)

    prevents one process from accessing thephysical address space of another process.

    Base/limit registers, virtual memory aretechniques to achieve memory protection.

  • 7/29/2019 a1 Os Review Sp09

    18/68

    Processes and Threads

    Traditional processes could only do one

    thing at a timethey were single-threaded.

    Multithreaded processes can (conceptually)do several things at oncethey have

    multiple threads.

    A thread is an execution context orseparately schedulable entity.

  • 7/29/2019 a1 Os Review Sp09

    19/68

    Threads

    Several threads can share the address space

    of a single process, along with resources

    such as files.

    Each thread has its own stack, PC, and TCB

    (thread control block)

    Each thread executes a separate section of thecode and has private data

    All threads can access global data of process

  • 7/29/2019 a1 Os Review Sp09

    20/68

    Threads versus Processes

    If twoprocesses want to access shared data

    structures, the OS must be involved.

    Overhead: system calls, mode switches, extraexecution time.

    Two threads in a single process can share

    global data automaticallyas easily as twofunctions in a single process.

  • 7/29/2019 a1 Os Review Sp09

    21/68

    Threads versus Processes

    Creating new processes, switching between

    processes, etc. is slower than performing

    same operations on threads (threads haveless state to manage)

    Summary: compared to using several

    processes, threads are a more economicalway to manage an application with parallel

    activities.

  • 7/29/2019 a1 Os Review Sp09

    22/68

    Threads as Lightweight Processes

    Kernel Threads Separate schedulable entity or basic unit

    of CPU utilization

    This kind of thread is sometimes called akernel thread because it is managed by theoperating system

    Examples: Windows XP, Linux, Mac OS,Solaris & some UNIX dialects all supportkernel threads

  • 7/29/2019 a1 Os Review Sp09

    23/68

    Types of Threads

    Kernel-level threads: are created &

    managed by the kernel, just as processes

    are. Mode switches are required when KLTare created, scheduled, switched, etc.

    User-level threads: are created by libraries

    that run at the user level. Thread creation,scheduling, etc., can be done at the cost of a

    function call.

  • 7/29/2019 a1 Os Review Sp09

    24/68

    Kernel-level threads versus user-

    level threads K-level threads have more overhead than

    user-level threads because OS is involved,

    but since the OS is aware of them, they canbe scheduled as independent entities.

    K-level threads can make system calls

    without blocking the entire process, so theyare suited for applications that require many

    system services.

  • 7/29/2019 a1 Os Review Sp09

    25/68

    Kernel-level threads versus user-

    level threads K-level threads can be scheduled in parallel

    on a multiprocessor

    U-level threads can determine their ownscheduling algorithms.

    Cooperative schedulingone thread yields to

    anotherTwo user-level threads cannot run in parallel

  • 7/29/2019 a1 Os Review Sp09

    26/68

    KLT and ULT

    KLT and ULT threads provide two ways tostructure user-level applications.

    Both ULT and KLT run in user mode. KLT do not have kernel-level priorities.

    Some operating systems are multithreaded

    and their threads may also be called kernelthreads.

    Not what we are talking about here.

  • 7/29/2019 a1 Os Review Sp09

    27/68

    Review Topics

    Processes &Threads

    Scheduling

    Synchronization

    Memory Management

    File and I/O Management

  • 7/29/2019 a1 Os Review Sp09

    28/68

    Process (Thread) Scheduling

    Process scheduling decides which processto dispatch (to the Run state) next.

    In a multiprogrammed system severalprocesses compete for a single processor

    Preemptive scheduling: a process can beremoved from the Run state before itcompletes or blocks (timer expires or higherpriority process enters Ready state).

  • 7/29/2019 a1 Os Review Sp09

    29/68

    Multiprogramming vs Multitasking

    Multi programming: Multiprogramming is the technique of running severalprograms at a time. Multiprogramming creates logical parallelism. The OSkeeps several jobs in memory simultaneously. It selects a job from the jobpool and starts executing it. When that job needs to wait for any i/o operationthe CPU is switched to another job. So the main idea here is that the CPU isnever idle.

    Multi tasking: Multitasking is the logical extension of multiprogramming.Aftera certain amount of time the CPU is switched to another job. The differenceis that the switching between jobs occurs so frequently that the users caninteract with each program while it is running. This concept is also known astime-sharing.

    Sometimes the two terms are used interchangeably.

    Multiprocessing: involves multiple processors

  • 7/29/2019 a1 Os Review Sp09

    30/68

    Scheduling Algorithms:

    FCFS (first-come, first-served): non-

    preemptive: process run until they complete

    or block themselves for event wait RR (round robin): preemptive FCFS, based

    on time slice

    Time slice = length of time process can runbefore being preempted

    Return to Ready state when preempted

  • 7/29/2019 a1 Os Review Sp09

    31/68

    Scheduling Goals

    Optimize turnaround time and/or response

    time

    Optimize throughput

    Avoid starvation (be fair)

    Respect priorities

    Static

    Dynamic

  • 7/29/2019 a1 Os Review Sp09

    32/68

    Review Topics

    Processes &Threads

    Scheduling

    Synchronization

    Memory Management

    File and I/O Management

  • 7/29/2019 a1 Os Review Sp09

    33/68

    Interprocess Communication

    Processes (or threads) that cooperate tosolve problems must exchange information.

    Two approaches:Shared memory

    Message passing (involves copying informationfrom one process address space to another)

    Shared memory is more efficient (nocopying), but isnt always possible.

  • 7/29/2019 a1 Os Review Sp09

    34/68

    Process/Thread Synchronization

    Concurrent processes are asynchronous: the

    relative order of events within the two

    processes cannot be predicted in advance. If processes are related (exchange

    information in some way) it may be

    necessary to synchronize their activity atsome points.

  • 7/29/2019 a1 Os Review Sp09

    35/68

    Process/Thread Synchronization

    Concurrent processes are asynchronous: the

    relative order of events within the two

    processes cannot be predicted in advance. If processes are related (exchange

    information in some way) it may be

    necessary to synchronize their activity atsome points.

  • 7/29/2019 a1 Os Review Sp09

    36/68

    Instruction Streams

    Process A: A1, A2, A3, A4, A5, A6, A7, A8, , Am

    Process B: B1, B2, B3, B4, B5, B6, , Bn

    Sequential

    I: A1, A2, A3, A4, A5, , Am, B1, B2, B3, B4, B5, B6, , Bn

    InterleavedII: B1, B2, B3, B4, B5, A1, A2, A3, B6, , Bn, A4, A5,

    III: A1, A2, B1, B2, B3, A3, A4, B4, B5, , Bn, A5, A6, , Am

  • 7/29/2019 a1 Os Review Sp09

    37/68

    Process Synchronization2 Types

    Correct synchronization may mean that we

    want to be sure that event 1 in process A

    happens before event 2 in process B. Or, it could mean that when one process is

    accessing a shared resource, no other

    process should be allowed to access thesame resource. This is the critical section

    problem, and requires mutual exclusion.

  • 7/29/2019 a1 Os Review Sp09

    38/68

    Mutual Exclusion

    A critical section is the code that accessesshared data or resources.

    A solution to the critical section problem

    must ensure that only one process at a timecan execute its critical section (CS).

    Two separate shared resources can be

    accessed concurrently.

  • 7/29/2019 a1 Os Review Sp09

    39/68

    Synchronization

    Processes and threads are responsible for

    their own synchronization, but

    programming languages and operating

    systems may have features to help.

    Virtually all operating systems provide

    some form of semaphore, which can be

    used for mutual exclusion and other forms

    of synchronization.

  • 7/29/2019 a1 Os Review Sp09

    40/68

    Semaphores

    Definition: A semaphore is an integer variable

    (S) which can only be accessed in the following

    ways: Initialize (S)

    P(S) // {wait(S)}

    V(S) // {signal(S)}

    The operating system must ensure that alloperations are indivisible, and that no other access

    to the semaphore variable is allowed

  • 7/29/2019 a1 Os Review Sp09

    41/68

    High-level Algorithms

    Assume S is a semaphore

    P(S): ifS > = 1 then S = S1

    else block the process on S queue

    V(S): ifsome processes are blocked on the

    queue forS then unblock a processelse S = S + 1

  • 7/29/2019 a1 Os Review Sp09

    42/68

    Enforcing Mutual Exclusion with

    Semaphores

    Process 1

    . . .

    P(mutex)

    execute critical section

    V(mutex)

    Process 2

    . . .

    P(mutex)

    execute critical section

    V(mutex)

    semaphore mutex = 1;

  • 7/29/2019 a1 Os Review Sp09

    43/68

    Other Mechanisms for Mutual

    Exclusion Spinlocks: a busywaiting solution in which

    a process wishing to enter a critical section

    continuously tests some lock variable to seeif the critical section is available. Various

    machine-language instructions can be used

    to implement spinlocks Disable interrupts before entering CS,

    enable after leaving

  • 7/29/2019 a1 Os Review Sp09

    44/68

    Deadlock

    A set of processes is deadlocked when each

    is in the Blocked state because it is waiting

    for a resource that is allocated to one of theothers.

    Deadlocks can only be resolved by agents

    outside of the deadlock

  • 7/29/2019 a1 Os Review Sp09

    45/68

    Wait-For Graphs

    Simple deadlocks can be modeled by wait-

    for graphs (WFG) where nodes represent

    processes and edges represent the waitingrelation between processes.

    P 1 P 2

    P1 waits for resource owned by P2

    P2 waits for resource owned by P1

  • 7/29/2019 a1 Os Review Sp09

    46/68

    Deadlock versus Starvation

    Starvation occurs when a process is repeatedlydenied access to a resource even though theresource becomes available.

    Deadlocked processes are permanently blockedbut starving processes may eventually get theresource being requested.

    In starvation, the resource being waited for iscontinually in use, while in deadlock it is notbeing used because it is assigned to a blockedprocess.

  • 7/29/2019 a1 Os Review Sp09

    47/68

    Causes of Deadlock

    Mutual exclusion (exclusive access)

    Wait while hold (hold and wait)

    No preemption

    Circular wait

  • 7/29/2019 a1 Os Review Sp09

    48/68

    Deadlock Management Strategies

    Prevention: design a system in which atleast one of the 4 causes can never happen

    Avoidance: allocate resources carefully, sothere will always be enough to allow allprocesses to complete (Bankers Algorithm)

    Detection: periodically, determine if adeadlock exists. If there is one, abort one ormore processes, or take some other action.

  • 7/29/2019 a1 Os Review Sp09

    49/68

    Analysis of Deadlock

    Management Most systems do not use any form of

    deadlock management because it is not cost

    effectiveToo time-consuming

    Too restrictive

    Exceptions: some transaction systems have

    roll-back capability or apply orderingtechniques to control acquiring of locks.

  • 7/29/2019 a1 Os Review Sp09

    50/68

    Review Topics

    Processes &Threads

    Scheduling

    Synchronization

    Memory Management

    File and I/O Management

  • 7/29/2019 a1 Os Review Sp09

    51/68

    Memory Management

    Introduction

    Allocation methods

    One user at a time

    Multiple processes, contiguous allocation

    Multiple processes, virtual memory

  • 7/29/2019 a1 Os Review Sp09

    52/68

    Memory Management - Intro

    Memory must be shared between the OS

    and user processes.

    OS must protect itself from users, and oneuser from another.

    OS must also manage the sharing of

    physical memory so that processes are ableto execute with reasonable efficiency.

  • 7/29/2019 a1 Os Review Sp09

    53/68

    Allocation Methods:

    Single Process Earliest systems used a simple approach:

    OS had a protected set of memory locations,

    the remainder of memory belonged to oneprocess at a time.

    Process owned all computer resources

    from the time it began until it completed

  • 7/29/2019 a1 Os Review Sp09

    54/68

    Allocation Methods:

    Multiple Processes, Contiguous Allocation Several processes resided in memory at one

    time (multiprogramming).

    The entire process image for each process isstored in a contiguous set of locations.

    Drawbacks:

    Limited number of processes at one time

    Fragmentation of memory

  • 7/29/2019 a1 Os Review Sp09

    55/68

    Process 1

    Process 2 Process 3

    Process 1

    unused

    unused

    Process 4

    Process 3

    unused

    Creation of Memory Fragments

  • 7/29/2019 a1 Os Review Sp09

    56/68

    Allocation Methods:

    Multiple Processes, Virtual Memory Motivation for virtual memory:

    to better utilize memory (reduce fragmentation)

    to increase the number of processes thatexecute concurrently

    Method:

    allow program to be loaded non-contiguouslyallow program to execute even if it is not

    entirely in memory.

  • 7/29/2019 a1 Os Review Sp09

    57/68

    Virtual Memory

    The OS software lets a process execute as if

    it is loaded into a contiguous set of

    addresses, when in fact this is not the case. Actually, the process address space is not

    contiguously stored and parts of it may not

    even be in memory at all.

  • 7/29/2019 a1 Os Review Sp09

    58/68

    Virtual Memory - Paging

    The address space of a program is divided

    intopagesa set of contiguous locations.

    Page size is a power of 2; often 4K or more.

    Memory is organized into pageframes

    Any page in a program can be loaded into

    any frame in memory, so no space iswasted.

  • 7/29/2019 a1 Os Review Sp09

    59/68

    Paging - continued

    General ideasave space by loading onlythose pages that a program needs now.

    Resultmore programs can be in memoryat any given time

    Problems:

    How to tell whats needed

    How to keep track of where the pages are

    How to translate virtual addresses to physical

  • 7/29/2019 a1 Os Review Sp09

    60/68

    Demand Paging

    How to Tell Whats Needed

    Demand paging loads a page only

    when there is a page faulta referenceto a location on the page

    The principle of locality ensures that

    page faults wont occur too frequently

  • 7/29/2019 a1 Os Review Sp09

    61/68

    Page TablesHow to

    Know Where Pages are Loaded Each process has a page table; an array

    stored in kernel space.

    Each page table entry (0, 1, 2, ) hasinformation about the corresponding

    page in the processs virtual address

    space.

  • 7/29/2019 a1 Os Review Sp09

    62/68

    Virtual Addresses

    Addresses in an executable are virtual

    assigned without regard to physical addresses

    During execution a virtual address must first

    be translated to the physical, or real,

    address.

    Performed by MMU, using data from page

    table.

  • 7/29/2019 a1 Os Review Sp09

    63/68

    Dynamic Address Translation

    Virtual addresses have two parts: the page

    number and the displacement (p, d).

    To translate into a physical address, thevirtual page number (p) is replaced with the

    physical frame number (f)

    This is easily handled in hardware

  • 7/29/2019 a1 Os Review Sp09

    64/68

    OS Responsibilities

    Maintain page tables

    Perform page replacement

  • 7/29/2019 a1 Os Review Sp09

    65/68

    Review Topics

    Processes &Threads

    Scheduling

    Synchronization

    Memory Management

    File and I/O Management

  • 7/29/2019 a1 Os Review Sp09

    66/68

    File Systems

    Maintaining a shared file system is a major

    job for the operating system.

    Single user systems require protectionagainst loss, efficient look-up service, etc.

    Multiple user systems also need to provide

    access control.

  • 7/29/2019 a1 Os Review Sp09

    67/68

    File SystemsDisk Management

    The file system is also responsible for

    allocating disk space and keeping track of

    where files are located. Disk storage management has many of the

    problems main memory management has,

    including fragmentation issues.

  • 7/29/2019 a1 Os Review Sp09

    68/68

    End of OS Review