OS Slides Upto 15 Aug 2013

Embed Size (px)

Citation preview

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    1/146

    Operating Systems

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    2/146

    Few Questions

    1. Did all the computers have same configuration?

    CPU (Make, Cores, Speed)

    Memory (Make, Model, Capacity)

    Disk (Make, Capacity, Rotational Speed etc)

    Devices (Mouse??, Keyboard??, Pendrive, LCD, etc.)

    2. Did all the Applications run on all the systems?

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    3/146

    Few Questions

    Name a few Operating Systems on which you

    have worked..

    Windows

    Linux

    Unix

    Mac

    BSD

    etc.

    Why so many ??? Where do they differ???

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    4/146

    Few Questions

    Does the programmer have to alter every

    program for every piece of hardware?

    Does a faulty program crash everything?

    Does every program have access to all the

    hardware?

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    5/146

    Operating Systems

    Software

    -------Instruction Set-------

    Hardware

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    6/146

    Operating Systems

    Applications

    ----------------- (Logical Interface)Operating System

    ----------------- (Physical Interface)

    Hardware

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    7/146

    Broadly

    What are the components of the System?

    Hardware

    Operating System

    Applications

    Users

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    8/146

    Few Questions

    Why study Operating Systems?

    Build, modify, or administer an operating

    system

    Understand system performance Behavior of OS impacts entire machine

    Tune workload performance

    Apply knowledge across many areas

    Computer architecture, programming languages, data structures

    and algorithms, and performance modeling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    9/146

    Role..

    Role #1: Provide standard Library (i.e., abstractresources)

    What is a resource?

    Anything that has identity (e.g., CPU, memory, disk)

    Advantages of standard library Allow applications to reuse common facilities

    Make different devices look the same Challenges

    What are the correct abstractions?

    How much of hardware should be exposed?

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    10/146

    Role..

    Role #2: Resource coordinator (I.e., manager)

    Advantages of resource coordinator

    Virtualize resources so, multiple users orapplications can share

    Protect applications from one another

    Provide efficient and fair access to resources

    Challenges What are the correct mechanisms?

    What are the correct policies?

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    11/146

    OS Components

    Kernel: Core components of the OS Program Execution (Process Management)

    Interrupts

    Modes

    Memory Management

    Virtual Memory Multitasking

    Disk access and File Systems

    Device Drivers

    Networking

    Enables processes to communicate with one another Security

    Authentication, Authorization (Access Controls)

    User Interface Shell ( CLI or GUI)

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    12/146

    Functions

    Depends on

    User Expectations

    Technology Changes

    Should adapt both Change abstractions provided to users

    Change algorithms to implement those abstractions

    Change low-level implementation to deal with hardware

    Lead to Evolution of Many Operating Systems

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    13/146

    Evolution of the OS

    Two distinct phases of history

    Phase 1: Computers are expensive

    Goal: Use computers time efficiently

    Maximize throughput (I.e., jobs per second) Maximize utilization (I.e., percentage busy)

    Phase 2: Computers are inexpensive

    Goal: Use peoples time efficiently

    Minimize response time

    Trade-off between Usability (ease-of-use) and Resource Utilization

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    14/146

    First commercial systems

    1950s Hardware Enormous, expensive, and slow

    Input/Output: Punch cards and line printers

    Goal of OS

    Get the hardware working Single operator/programmer/user runs and debugs interactively

    OS Functionality Standard library only (no sharing or coordination of resources)

    Monitor that is always resident; transfer control to programs

    Problems Inefficient use of hardware (throughput and utilization)

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    15/146

    Batch Processing

    Goal of OS: Better throughput and utilization

    Batch: Group of jobs submitted together Operator collects jobs; orders efficiently; runs one at a

    time

    Advantages Reduce setup costs over many jobs

    Keep machine busy

    Improves throughput and utilization

    Problems User must wait until batch is done for results

    Machine idle when job is reading from cards and writing toprinters

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    16/146

    Spooling

    Hardware Mechanical I/O devices much slower than CPU

    Read 17 cards/sec vs. execute 1000s instructions/sec

    Goal of OS Improve performance by overlapping I/O with CPU execution

    Spooling: Simultaneous Peripheral Operations On-Line

    1. Read card punches to disk

    2. Compute (while reading and writing to disk)

    3. Write output from disk to printer

    OS Functionality Buffering and interrupt handling

    Problem Machine idle when job waits for I/O to/from disk

    Mismatch in speed between devices (producer & consumer) is to be

    addressed.

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    17/146

    Multiprogrammed Batch Systems

    Observation: Spooling provides pool of ready jobs

    Goal of OS

    Improve performance by always running a job

    Keep multiple jobs resident in memory

    When job waits for disk I/O, OS switches to another job

    OS Functionality

    Job scheduling policies

    Memory management and protection

    Advantage: Improves throughput and utilization

    Disadvantage: Machine not interactive

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    18/146

    Inexpensive Peripherals

    1960s Hardware Expensive mainframes, but inexpensive keyboards and monitors

    Enables text editors and interactive debuggers

    Goal of OS

    Improve users response time OS Functionality

    Time-sharing: switch between jobs to give appearance of dedicatedmachine

    More complex job scheduling

    Concurrency control and synchronization Advantage

    Users easily submit jobs and get immediate feedback

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    19/146

    Inexpensive Personal Computers

    1980s Hardware Entire machine is inexpensive

    One dedicated machine per user

    Goal of OS Give user control over machine

    Advantages Simplicity

    Works with little main memory performance is predictable

    Disadvantages No time-sharing or protection between jobs

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    20/146

    Inexpensive, Powerful Computers

    1990s+ Hardware

    PCs with increasing computation and storage

    Users connected to the web

    Goal of OS Allow single user to run several applications

    simultaneously

    Provide security from malicious attacks

    Efficiently support web servers

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    21/146

    Current Systems

    Summary:

    OS changes due to both hardware and users

    Current trends Multiprocessors

    Networked systems

    Virtual machines

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    22/146

    Classification is based on:

    Interface

    Number of users

    Response time and program / job execution

    Classification of Operating System

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    23/146

    Character Based:- Commands are entered using

    keyboard only to perform an operation

    Ex:-UNIX, MS-DOS

    Graphical User Interface :-Either keyboard or

    mouse is used to perform an operation

    Ex:- Windows, Macintosh, LINUX

    Classification - Interface

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    24/146

    User Interface

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    25/146

    Creation of file in MS-DOS Mode: C:\> COPY CON filename.txt

    Then press enter key

    Type the data

    Press control key+z , to save the file

    Creation of file in Windows mode:

    Click on the following

    Start Programs Accessories notepad

    Now notepad window will be opened, type the data and

    save the file

    Example

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    26/146

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    27/146

    Batch Processing

    Multi Programming

    Time Sharing

    Real time

    Distributed Hybrid

    Based on Response Time and Mode of

    Program Execution

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    28/146

    Learning Assignment

    Mainframe Systems

    Desktop Systems

    Multiprocessor Systems

    Distributed Systems

    Clustered Systems

    Real-Time Systems

    Handheld SystemsAim :

    Understanding various types of Operating Systems.

    Identifying sufficient Examples for each

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    29/146

    Note

    All the Assignments / Projects / Tutorials etc.

    will be discussed during Tutorial Sessions

    asked for explaining

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    30/146

    OS Design

    User goals

    operating system should be convenient to use,

    easy to learn, reliable, safe, and fast.

    System goals

    operating system should be easy to design,

    implement, and maintain, as well as flexible,reliable, error-free, and efficient.

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    31/146

    OS Implementation

    Traditionally written in assembly language,operating systems can now be written in higher-level languages.

    Code written in a high-level language: can be written faster.

    is more compact.

    is easier to understand and debug.

    An operating system is far easier toport(move tosome other hardware) if it is written in a high-level language.

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    32/146

    Design & Implementation

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    33/146

    Design & Implementation

    Layering also make it easier to enhance the operating

    system

    one entire layer can be replaced without affecting

    other parts of the system

    This structure also allows the operating system to be

    debugged starting at the lowest layer, adding one

    layer at a time until the whole system works correctly

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    34/146

    Design & Implementation

    The operating system is divided into a numberof layers (levels),

    each built on top of lower layers.

    The bottom layer (layer 0), is the hardware;

    the highest (layer N) is the user interface.

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    35/146

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    36/146

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    37/146

    Learning Assignment

    What is the layered architecture for the

    following Operating Systems:

    Windows

    UNIX

    LINUX

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    38/146

    Program Execution

    Simple Program: (test.c)

    main(){

    printf(%d, fn());}

    int fn(){

    int d = 5 / 0 ;malloc(); //incorrect

    }

    Open (file)

    Create process

    Request device, release device

    Call sub-routineWrite

    Terminate process

    Wait event, signal event (interrupt)

    Memory alloc & de-alloc

    Close (file)

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    39/146

    Operating System Services

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    40/146

    Services

    Program execution

    I/O operations

    File-system manipulation

    Communications

    exchange of information between processes executing either on thesame computer or on different systems tied together by a network.Implemented via shared memoryor message passing.

    Error detection

    Resource allocation

    Accounting

    Protection

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    41/146

    The applications that a user writes normally run on

    top of an operating system

    In most operating systems, a program cannot access

    the resources directly

    The programs run by the user, request the operating

    system to do a specific task on behalf of a program

    System Calls

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    42/146

    For example, if the program wants to open a file, it request

    the operating system to please open the file with file

    name /usr/read.txt

    The kernel replies to the request suitably

    It opens the file if the calling application has permissions

    If the application does not have permissions, the operating

    system simply ignores the request, giving back an error

    System Calls

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    43/146

    The process of calling the kernel is referred to as

    system call invocation

    Call to the kernel please open the file is known

    as a system call

    The kernel calls appropriate device drivers toaccess the hardware

    System Calls

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    44/146

    User program

    Kernel has control over

    Hardware resources

    Device

    Kernel has control over

    Hardware resources

    Device

    System call

    Device driver

    Kernel has control over

    Hardware resources

    Device

    Kernel has control over

    Hardware resources

    Kernel has control over

    Hardware resources

    Device

    Kernel has control over

    Hardware resources

    Device

    Kernel has control over

    Hardware resources

    DeviceDevice

    Kernel has control over

    Hardware resources

    Device

    Kernel has control over

    Hardware resources

    Device

    Kernel has control over

    Hardware resources

    Device

    Kernel has control over

    Hardware resources

    System Calls

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    45/146

    Types of System Calls

    Process control

    end, abort

    load, execute

    create process, terminate process

    get process attributes, set process attributes wait (for time)

    wait event, signal event

    allocate and free memory

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    46/146

    File Management

    create file, delete file

    open, close

    read, write, reposition

    get file attributes, set file attributes

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    47/146

    Device Management

    request device, release device

    read, write, reposition

    get device attributes, set device attributes

    logically attach or detach devices

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    48/146

    Information Maintenance

    get time or date, set time or date

    get system data, set system data

    get process, file or device attributes

    set process, file or device attributes

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    49/146

    Communications

    create, delete communication connection

    send, receive messages

    transfer status information

    attach or detach remote devices

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    50/146

    Learning Assignment

    What are the various system calls provided in

    the following operating systems:

    Windows

    Unix

    Linux

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    51/146

    Process

    Process = A program under execution

    ProgramPassive entity

    Process Active entity

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    52/146

    Process States

    Process changes its state while it executes

    The state of a process is defined in part by the currentactivity of a process

    Each process may be in one of the following states

    New

    Ready

    Running

    Waiting

    Terminated

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    53/146

    NEW

    READY

    WAITING

    RUNNING

    TERMINATED

    Admitted

    Interrupt

    Scheduler dispatch

    I/O or Event wait

    Exit

    I/O or Event

    completion

    Process States

    Job Queue : set of all processes in thesystem

    Ready Queue : set of all processes ready

    and waiting for executing

    Device Queue : set of processes waiting for

    an I/O

    Long-term scheduler (or job scheduler)selects which processes should be brought into

    the ready queue.

    Short-term scheduler (or CPU scheduler)

    selects which process should be executed next

    and allocates CPU.

    A process can be I/O bound or CPU bound

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    54/146

    Queueing Diagram

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    55/146

    Operating System

    Context Switching

    When CPU switches to another process, the

    system must save the state of the old process

    and load the saved state for the new process.

    Context-switch time is overhead; the system

    does NO useful work while switching.

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    56/146

    Process Control BlockPointer:

    addr of another process

    Process States :

    new, ready,running, waiting,halted

    Process Number :

    Identification

    Program Counter :

    Addr of next instruction

    Registers :

    Status of the register values

    Memory Limits :

    Base register, limit register

    Page Tables, Segment Tables

    List of Open Files :

    Opened files and their current status

    List of devices opened

    h l

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    57/146

    Save state into PCB0

    Reload state from PCB1

    Save state into PCB1

    Reload state from PCB0

    Interrupt or system callidle

    executing

    executing

    executing

    idle

    idle

    Process 0 Process 1

    Context Switching Example

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    58/146

    Various Queues - Implementation

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    59/146

    Learning Assignment

    Identify various possibilities of creating a process

    terminating a process

    List various commands used for creating andterminating a process in

    Windows

    Linux

    Unix

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    60/146

    Inter-Process Communication (IPC)

    Independentprocess cannot affect or be affectedby the execution of another process.

    Cooperating process can affect or be affected bythe execution of another process

    Advantages of process cooperation Information sharing (several users access same info)

    Computation speed-up (Multiple Processors) Modularity (Separate functions using Threads)

    Convenience (editing, printing, compiling in parallel)

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    61/146

    Two Models: Shared Memory

    Producer-Consumer Problem

    Message Passing Mail Box

    Client Server Model

    - Socket Programming

    - Remote Procedure Calls (RPC)

    - Remote Method Invocation (RMI)

    Needs Proper Synchronization

    Inter-Process Communication (IPC)

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    62/146

    Producer-Consumer Problem

    Paradigm for cooperating processes,producer

    process produces information that is

    consumed by a consumerprocess.

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    63/146

    Buffering

    Queue of messages attached to the link can be

    implemented in one of three ways.

    1.Zero capacity 0 messages

    Sender must wait for receiver.2.Bounded capacity finite length ofn messages

    Sender must wait if link full.

    3.Unbounded capacity infinite lengthSender never waits.

    Bounded Buffer Shared Memory

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    64/146

    Shared data#define BUFFER_SIZE 10

    Typedef struct {

    . . .

    } item;

    item buffer[BUFFER_SIZE];

    int in = 0;

    int out = 0;

    Bounded-Buffer Shared-Memory

    Based Producer Consumer Problem

    item nextProduced;

    while (1) {

    while (((in + 1) % BUFFER_SIZE) == out)

    ; /* do nothing */

    buffer[in] = nextProduced;

    in = (in + 1) % BUFFER_SIZE;

    }

    item nextConsumed;

    while (1) {

    while (in == out)

    ; /* do nothing */

    nextConsumed = buffer[out];

    out = (out + 1) % BUFFER_SIZE;

    }

    Producer

    Consumer

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    65/146

    Ready Queue

    FIFO??

    Can be implemented as:

    FIFO

    Priority Queues

    Trees

    Simply Linked Lists

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    66/146

    Multi-programmed Environment

    Multi-programmed OS

    Objective:

    Maximize Utilization

    Simple Concept: Utilize I/O time

    Process Time ( Burst Time )

    CPU Execution Time (Cycle) (CPU Burst)

    I/O Time ( I/O Burst )

    CPU Scheduler (Short-term Scheduler)

    Schedule Processes in a proper order to get Maximum

    Utilization

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    67/146

    Process Scheduling

    Criteria: CPU Utilization : Make CPU as busy as possible

    Throughput : Measure of work done

    Number of processes completed / Unit time

    Turn around Time : Measure of time

    Time submission Completion

    Waiting Time : Time spent in waiting (for I/O) Response Time :

    Measure of work done (interactive systems time sharing )

    Time submission to First response.

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    68/146

    Process Scheduling

    Preemptive Scheduling

    Non-Preemptive Scheduling

    NEW

    READY

    WAITING

    RUNNING

    TERMINATED

    Admitted

    Interrupt

    Scheduler dispatch

    I/O or Event wait

    Exit

    I/O or Event

    completion

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    69/146

    The CPU scheduling, selects a process in the readyqueue, for execution using scheduling algorithms

    Scheduling algorithm can be classified into

    First come, first served scheduling

    Shortest job first scheduling

    Priority scheduling

    Round robin scheduling

    Multilevel queue scheduling

    Multilevel feedback queue scheduling

    Scheduling algorithms

    FCFS Scheduling Algorithm

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    70/146

    It is the simplest scheduling algorithmIt is implemented by First in First out (FIFO) Queue

    Whenever the process enters into the ready queue PCB

    is linked onto the tail of the queue

    FCFS Scheduling Algorithm

    FCFS

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    71/146

    Consider an example of processes P1, P2, P3 arriving at

    time instant 0 and their CPU burst times are shown below:

    Process Burst time (msecs)

    P1 24

    P2 3

    P3 3

    The Gantt chart below shows the result

    0 24 27 30

    P1 P2 P3

    FCFS

    FCFS

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    72/146

    Average waiting time and average turn around time arecalculated using the example in previous slide Waiting time = stating time arrival time

    The waiting time for process P1

    P1= 0 0 = 0msec The waiting time for process P2

    P2 = 24 0 = 24msec

    The waiting time for process P3

    P3 = 27 0 = 27msec

    Average waiting time=(0+24+27)/3=17msec

    FCFS

    FCFS - Disadvantages

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    73/146

    Average turn around time=(24+27+30)=27msec

    The average waiting time measured under FCFS policy is long

    (i.e. 17 m sec)

    For example in previous slide if processes arrive in the order P2,

    P3, P1 it results in the following Gantt chart

    The average waiting time is (6+0+3)/3=3msec

    P2 P3 P1

    0 3 6 30

    FCFS - Disadvantages

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    74/146

    FCFS scheduling algorithm is Non preemptive

    So, trouble some for time sharing system

    Each user gets a share of CPU at regular intervals in

    timesharing system

    FCFS - Disadvantages

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    75/146

    A different approach to CPU scheduling is the

    Shortest Job First scheduling algorithm (SJF)

    This algorithm associates with each process the

    length of the processes next CPU burst

    When CPU is available it is given to the process that

    has the smallest next CPU burst

    Shortest Job First Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    76/146

    The next CPU bursts of two process are same then

    FCFS scheduling is used to break the tie

    More appropriate term for this scheduling

    algorithm is shortest next CPU burst algorithm

    Shortest Job First Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    77/146

    Example:

    A set of processes with the length of the CPU

    burst time given in milliseconds is shown below

    Process Burst time

    P1 6

    P2 8

    P3 7

    P4 3

    Shortest Job First Scheduling

    Shortest Job First Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    78/146

    Above Processes are scheduled using SJF algorithm in

    the form of a Gantt chart

    P4 P1 P3 P2

    0 3 9 16 24

    Shortest Job First Scheduling

    Shortest Job First Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    79/146

    The waiting time for process P1 = 3 milliseconds

    The waiting time for process P2 = 16 milliseconds

    The waiting time for process P3 = 9 milliseconds

    The waiting time for process P4 = 0 milliseconds

    Average Waiting Time = 3+16+9+0 / 4 = 7 milliseconds

    Shortest Job First Scheduling

    d

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    80/146

    The scheduling algorithm is probably optimal

    Minimum average time for a given set of processes

    Decreases waiting time for short processes

    Increases waiting time for long processes

    Average waiting time is decreased

    SJF algorithm is used frequently in long term CPU

    scheduling

    SJF - Advantages

    S i d

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    81/146

    Difficulty in knowing the length of the next CPU

    request or CPU burst

    SJF algorithm cannot be used for short term CPUscheduling

    SJF - Disadvantages

    SJF P i

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    82/146

    SJF algorithm can be Preemptive

    The next CPU burst of new arriving process is shorter than

    the currently executing process

    then SJF algorithm preempts currently executing process

    Preemptive SJF scheduling

    Shortest Remaining Time First scheduling

    SJF - Preemptive

    SJF P i

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    83/146

    SJF - Preemptive

    Process execution time arrival time

    P1 20 0

    P2 25 15

    P3 10 30

    P4 15 45

    P1 P2 P3 P2 P40 20 30 40 55 70

    What is Preemptive and Non-Preemptive Solution ?

    SJF

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    84/146

    SJF

    Preemptive Non-Preemptive

    Average Wait Time : 06.25 07.50

    Average Turn-Around Time : 23.75 25.00

    P i it S h d li

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    85/146

    Priority scheduling algorithm is special case of SJF

    A priority is associated with each process

    Priorities are indicated by fixed range of numbers

    In this scheduling CPU is allocated to the process with

    the highest priority

    Equal priority process scheduled in FCFS order

    Priority Scheduling

    Priority Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    86/146

    Consider the following set of processes assumed to have

    arrived at time 0, in order P1,P2,P3-----P5

    with the length of the CPU burst time given in milliseconds

    and their associated priorities

    Process Burst time PrioritiesP1 10 3

    P2 1 1

    P3 2 4P4 1 5

    P5 5 2

    y g

    Priority Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    87/146

    0 1 6 16 18 19

    The average waiting time is 8.2 milliseconds

    i.e. (0+1+6+16+18)/5 = 8.2 milliseconds

    Priority can be defined internally or externally

    P2 P5 P1 P3 P4

    Priority Scheduling

    Priority Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    88/146

    Internally priorities are set (by OS )

    some measurable quantity or quantities to compute the

    priority of a process

    Time limits, Memory requirements, Open files , The ratio of average I/O burst to

    average CPU burst

    Externally priorities are set (outside OS)

    process type (ex: system or application), department

    sponsoring (public sector or private sector), political factors,

    amount paid for the computer use etc.

    Priority Scheduling

    Priority Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    89/146

    Priority scheduling

    Preemptive or Non Preemptive

    When a process arrives at the ready queue, its priority is

    compared with the priority of currently running

    process

    Preemptive allot the CPU if the priority of the newly arrived process is

    higher than the priority of the currently running processes

    Priority Scheduling

    Priority Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    90/146

    NonPreemptive

    put the new process at the head of the ready queue

    A major problem with this is indefinite blocking

    Starvation

    A solution to this problem is Aging

    A technique which gradually increases the priority of

    processes that wait in the system for a long time

    Priority Scheduling

    Round-Robin Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    91/146

    g

    for Time Sharing Systems

    Preemptive only

    Time Quantum

    A small unit of time (or time slice)

    generally from 10 to 100 mille seconds

    Round-Robin Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    92/146

    The average waiting time under Round Robin policy

    is often long

    Consider the following set of processes that arrive

    at time 0, with the

    length of the CPU burst time given in milliseconds:Process Burst Time

    P1 24

    P2 3

    P3 3

    Time Quantum = 4

    Round-Robin Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    93/146

    Round-Robin Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    94/146

    The average waiting time

    17 / 3= 5.66 ms

    The average turn-around time

    47/3 = 15.66 ms

    Solve with time slice 8, 12, 16, 20, and 24 ?

    Draw the comparison graphs with points, 4 to 24.

    What is the conclusion?

    What if the time slice is very small (say 1 )?

    What if the time slice is very large (say 25 in this example)?

    Round-Robin Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    95/146

    Performance of RR scheduling depends

    on the size of time quantum

    If time quantum is extremely very large

    RR approach is same as FCFs

    If it is extremely small

    RR approach is called Processor Sharing

    Round Robin Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    96/146

    g

    Turnaround

    time need NOT

    improve as the

    size of timequantum

    increases.

    Turn-around-Time Vs Quantum

    Round Robin Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    97/146

    g

    How many context switches involved in each case of the problem

    discussed in previous slides?

    Context Switches

    Round Robin Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    98/146

    Round Robin Scheduling

    If the context switch time is added Average turn-around time increases for a small

    time quantum (since, more context switches are

    added) Time Quantum should be large compared to

    Context Switching Time

    A Design Thumb Rule: 80% of the CPU burstshould be smaller than time quantum

    Multilevel Queue Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    99/146

    processes are classified into different groups fore ground (interactive) process

    back ground ( batch ) processes

    Foreground processes have higher priority than

    background

    A multilevel queue scheduling algorithm partitions the

    Ready Queue into several separate queues

    Each Queue has its own scheduling policy

    g

    Multilevel Queue Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    100/146

    Example:

    Multi level queue Scheduling algorithm with five queues:

    1. System Processes

    2. Interactive Processes

    3. Interactive Editing Processes

    4. Batch Processes

    5. Student Processes

    no process in batch queue could run unless the queues forSystem, Interactive & Interactive Editing are empty

    g

    Multilevel Queue Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    101/146

    Interactive Editing Processes

    System Processes

    Interactive Processes

    Batch Processes

    Student Processes

    Lowest Priority

    Highest Priority

    Example :

    Multilevel Queue Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    102/146

    Example: Foreground queue RR Policy

    Back ground queue FCFS Policy

    In addition, there must be scheduling among queues

    fixed priority preemptive scheduling

    Fore ground queue has priority over background queue

    Multilevel Queue Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    103/146

    It separates the CPU processes with different CPU burst

    characteristics

    The processes are permanently assigned to one queue

    based on following properties

    Memory Size

    Process Priority

    Process Type

    Multilevel Feedback Queue Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    104/146

    Consider a multilevel feedback queue scheduler with

    three queues

    quantum=8q0

    quantum=16q1

    FCFSq2

    This scheduling algorithm gives the highest priority to any

    process with a CPU burst of 8 milliseconds or less

    Multilevel Feedback Queue Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    105/146

    The scheduler first executes all processes in queue 0 When empty it executes all processes in queue 1 and so

    on

    A way to implement Aging.

    Depends on:

    No. of Queues, Scheduling Policy of each Queue, Method used

    to upgrade a process, method used to degrade a process,method used to introduce a process (which queue), inter-

    scheduling between the queues.

    Reading Assignment

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    106/146

    Reading Assignment

    Identify various other states of a process (thandiscussed in the class) and understand them

    Understand the various schedulingmechanism like

    Highest Response Ratio Next Algorithm

    Feedback Algorithm (paper will be uploaded)

    Fair-Share Scheduling Algorithm

    Reading Assignment

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    107/146

    Reading Assignment...

    What are the scheduling algorithms used (andinterpreted) in various Operating Systems like

    Windows

    Unix Linux

    You can also specify the algorithms used for

    General Purpose OS Real-Time OS etc.

    Process Address Space

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    108/146

    Process Address Space

    Process Address Space

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    109/146

    Process Address Space

    Process Address Space

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    110/146

    Process Address Space

    Logical Address Space

    Physical Address Space

    Process Address Space

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    111/146

    Process Address Space

    Memory Management Contiguous Allocation

    Non-Contiguous Allocation

    Paging Segmentation

    Process Hierarchy

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    112/146

    Process Hierarchy

    Structured as a multilevel hierarchy

    Lowest level

    Virtualize CPU for all processes

    Virtual memory

    Virtualize memory for all processes.

    Single virtual memory shared by all processes

    Separate virtual memory for each process

    Process Hierarchy

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    113/146

    Process Hierarchy

    Process Hierarchy

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    114/146

    Process Hierarchy

    Init Initial process whoseprocess ID is always 0 and it

    will always be running

    All the other processes are

    children to init process

    Each process has a parent

    and can have many siblings

    Thread

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    115/146

    Thread

    A thread is a single sequence stream within in a process. Because threads have some of the properties of

    processes, they are sometimes called lightweightprocesses

    In a process, threads allow multiple executions ofstreams (multi-threading)

    Threads are popular way to improve application throughparallelism.

    Threads in a process share the same address space

    Multithreading

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    116/146

    Multithreading

    Operating system supports multiple threads ofexecution within a single process

    MS-DOS supports a single thread

    UNIX supports multiple user processes but onlysupports one thread per process

    Windows 2000, Solaris, Linux, Mach, and OS/2support multiple threads

    Multithreading

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    117/146

    Multithreading

    Benefits

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    118/146

    Benefits

    Responsiveness Users often like to do several things at a time

    Web browser while reading the page, an image is loaded

    Resource Sharing

    Memory and other resources Within the same address space

    Economy A server (e.g. file server) serves multiple requests

    Context-switching is faster

    Utilization of MP architectures Multiple CPUs sharing the same memory

    Increases concurrency

    Benefits

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    119/146

    Benefits

    Takes less time to create a new thread than a process Less time to terminate a thread than a process

    Less time to switch between two threads within the

    same process Since threads within the same process share memory

    and files, they can communicate with each other

    without invoking the kernel

    Multithreaded system

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    120/146

    Multithreaded system

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    121/146

    Thread Control Block (TCB)

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    122/146

    Thread Control Block (TCB)

    State Registers

    Status

    Program Counter

    Stack

    Code

    Thread States (in Java)

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    123/146

    ( )

    Thread Types

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    124/146

    yp

    User Threads

    Created by user supported above kernel

    NOT managed by kernel

    Kernel Threads

    Created by kernel Supported & Managed by Kernel

    Recent Operating Systems support Kernel Threads

    Models were proposed to represent a relationship between User

    Threads and Kernel Threads. Many to One

    One to One

    Many to Many

    Two Level

    Thread Types

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    125/146

    yp

    Threading Issues

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    126/146

    g

    Semantics of fork() and exec() system calls. Duplicate process (Single Threaded or Multi-threaded? Replace the entire process including threads

    Thread cancellation Asynchronous Cancellation

    Deferred Cancellation

    Signal handling signal particular event has occurred

    Delivered to process but, To a particular thread or all the threads

    Thread pools How many threads ? unlimited ?

    Pool starting of the process

    No. of threads? No. of CPUs, memory etc.

    Thread specific data Transaction processing

    Multiple Processors

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    127/146

    p

    Single Instruction Single Data (SISD)

    Single Instruction Multiple Data (SIMD)

    Multiple Instruction Single Data (MISD)

    Multiple Instruction Multiple Data (MIMD)

    Categories of Computer Systems

    Multiple Processors

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    128/146

    p

    MP OS Design Considerations

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    129/146

    g

    Simultaneous concurrent processes or threads Scheduling

    Synchronization

    Memory Management

    Reliability and Fault Tolerance

    Microkernels

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    130/146

    Small operating system core Contains only essential operating systems functions

    Many services traditionally included in the operatingsystem are now external subsystems

    device drivers

    file systems

    virtual memory manager

    windowing system

    security services

    Benefits of a Microkernel Organization

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    131/146

    Uniform interface on request made by aprocess

    All services are provided by means of messagepassing

    Extensibility

    Allows the addition of new services

    Flexibility

    New features added

    Existing features can be subtracted

    Benefits of a Microkernel Organization

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    132/146

    Portability Changes needed to port the system to a new

    processor is changed in the microkernel - not in

    the other services

    Reliability

    Modular design

    Small microkernel can be rigorously tested

    Benefits of a Microkernel Organization

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    133/146

    Distributed system support Message are sent without knowing what the

    target machine is

    Object-oriented operating system Components are objects with clearly defined

    interfaces that can be interconnected to form

    software

    Microkernel Design

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    134/146

    Low-level memory management mapping each virtual page to a physical page

    frame

    Inter-process communication I/O and interrupt management

    Multiple-Processors - Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    135/146

    Multiple CPUs Homogeneous processors

    Heterogeneous processors

    CPU scheduling - more complex Load sharing

    Issues

    Any Processor available Reserved Processor (Ex:- One processor I/O attached)

    Multiple-Processors - Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    136/146

    Approaches Asymmetric multiprocessing

    Symmetric multiprocessing

    Asymmetric multiprocessing

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    137/146

    Master - Slave Master (Scheduling)

    Scheduling decisions,

    I/O processing, and

    other system activities Slave

    Execute User Code

    Simple

    Because only one processor accesses system datastructures

    Symmetric multiprocessing

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    138/146

    Self Scheduling Hardware logic (Processor selector) Ready Queue

    Common / All Processors

    Separate / Each Processor

    Careful! Two processors Update same Data Structure

    Ensure! Two processors choose same process?

    Popular, Efficient Virtually, all recent OSs adopted

    Symmetric multiprocessing

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    139/146

    Symmetric multiprocessing

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    140/146

    Issues Processor Affinity

    Load Balancing

    Symmetric Multi-Threading

    Symmetric multiprocessing

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    141/146

    Issues Processor Affinity

    Process has the affinity for the processor on which it is

    currently executing

    Migration is costly due to

    Cache memory invalidated (for the current process)

    Re-populated (for the received process)

    Soft affinity if required, can migrate between processors

    Hard affinity migration not permitted

    Symmetric multiprocessing

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    142/146

    Issues Load Balancing

    Work Load should be balanced few may be busy few may

    be idle

    Distribute the workload evenly

    Necessary for processors with separate Ready Queue

    Two Approaches:

    Push Migration - Periodically check Push the load on idle processor Pull Migration Periodically check Idle processor pulls the proces

    Symmetric multiprocessing

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    143/146

    Issues Symmetric Multi-Threading

    Several threads concurrently on different processors

    Logical Processors (instead of Physical Processors) Symmetric Multi-Threading (SMT)

    Or, Hyper-threading Technology

    Intel Core i3/i5/i7, Itanium, Pentium 4 and XeonCPUs

    Reading Assignment

    http://en.wikipedia.org/wiki/Intel_Corehttp://en.wikipedia.org/wiki/Itaniumhttp://en.wikipedia.org/wiki/Pentium_4http://en.wikipedia.org/wiki/Xeonhttp://en.wikipedia.org/wiki/CPUhttp://en.wikipedia.org/wiki/CPUhttp://en.wikipedia.org/wiki/Xeonhttp://en.wikipedia.org/wiki/Pentium_4http://en.wikipedia.org/wiki/Itaniumhttp://en.wikipedia.org/wiki/Intel_Core
  • 7/27/2019 OS Slides Upto 15 Aug 2013

    144/146

    Hyper Threading Super Threading

    Thread Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    145/146

    User level threads handled by thread libraries

    Kernel level threads

    handled by kernel itself

    Mapping is required!

    Assignment is given as part of Project

    Real-Time Scheduling

  • 7/27/2019 OS Slides Upto 15 Aug 2013

    146/146

    Hard real-time systems required to complete a critical task within a

    guaranteed amount of time.

    Soft real-time systems requires that critical processes receive priority

    over less fortunate ones.

    Explore More !!!