real time operating system based design-1

Upload: prajna-sarswathi

Post on 25-Feb-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 Real time operating system based design-1

    1/121

    UNIT 5UNIT 5

    REAL TIME OPERATING SYSTEM (RTOS)

    BASED DESIGN-1

  • 7/25/2019 Real time operating system based design-1

    2/121

    10.1 BASICS OF OS10.1 BASICS OF OS

    OS acts as a bridge b/w user applications and task and the underlying

    system resources

    Primary functions of an OS are:

    Make the system convenient to use

    Organie and manage the system resources efficiently and correctly

    Operating system

    architecture

  • 7/25/2019 Real time operating system based design-1

    3/121

    10.1.1 The Kernel10.1.1 The Kernel

    !ore of the operating system

    "esponsible for managing system resources and communication

    between h/w and other services

    #ernel $ System %ibraries & Services

    Process management:

    Setting memory space for process'

    loading process(s code into memory space'

    allocating system resources'

    scheduling and managing e)ecution of processes' setting up and managing P!*'

    +P! and synchroniation'

    process termination and deletion

  • 7/25/2019 Real time operating system based design-1

    4/121

    10.1.1 The Kernel10.1.1 The Kernel

    Primar !emor management:

    ",M -processes & variables & shared data.

    Memory Management nit-MM. of kernel is responsible for :

    #eeping track of memory

    ,llocating and deallocating memory space

    File sstem management:

    0ile is a collection of related information

    1his service of kernel is responsible for :

    !reation' deletion and alteration of files

    !reation' deletion and alteration of directories

    Saving of files in secondary storage memory

    Providing automatic allocation of file space

    Providing a fle)ible naming convention for the files

  • 7/25/2019 Real time operating system based design-1

    5/121

    10.1.1 The Kernel10.1.1 The Kernel

    I"O Sstem #$e%ice& management:

    "outing +/O re2uests coming from different user application to the

    appropriate +/O devices of system

    ,ccess to +/O devices via ,P+

    #ernel maintains the list of all +/O devices of the system-3evice

    Manager.

    3evice 3river : system calls by which kernel interacts with +/O

    3evice Manager is responsible for:

    %oading and unloading device drivers

    4)changing information' and system specific control signals to and from

    devices

  • 7/25/2019 Real time operating system based design-1

    6/121

    10.1.1 The Kernel10.1.1 The Kernel

    Secon'ar Storage management:

    Managing secondary storage memory devices

    *ackup medium for programs and data "esponsibilities:

    3isk storage allocation

    3isk scheduling

    0ree disk space management

    Protection sstems:

    Multiple users with different level of access permissions

    +mplementing security policies

    Interr()t han'ler

  • 7/25/2019 Real time operating system based design-1

    7/121

    10.1.1.1 The Kernel S)ace an' User S)ace10.1.1.1 The Kernel S)ace an' User S)ace

    Memory space where kernel code is located5kernel space

    Memory space where user applications are located 5user space

    6irtual memorydemand paging techni2ue

    5Swapping act of loading code into and out of main memory

    +t happens between main memory and secondary storage memory

  • 7/25/2019 Real time operating system based design-1

    8/121

    10.1.1.* !onolithic Kernel an' !icro+ernel10.1.1.* !onolithic Kernel an' !icro+ernel

    Monolithic kernel :

    #ernel services run in kernel space

    4ffective utiliation of low level features of underlying system

    ,ny error or failure in any of the kernel module results in crashing of entire

    kernel application

    %+78' SO%,"+S' MS93OS

  • 7/25/2019 Real time operating system based design-1

    9/121

    10.1.1.* !onolithic Kernel an' !icro+ernel10.1.1.* !onolithic Kernel an' !icro+ernel

    Microkernel:

    Only essential set of OS services into kernel

    "est of OS services are implemented in programs called Servers

    Mach' 78' Mini) ;

  • 7/25/2019 Real time operating system based design-1

    10/121

    10.1.1.* !onolithic Kernel an' !icro+ernel10.1.1.* !onolithic Kernel an' !icro+ernel

    *enefits of Microkernel approach:

    ,o-(stness:

  • 7/25/2019 Real time operating system based design-1

    11/121

    10.* T)es o O)erating sstem10.* T)es o O)erating sstem

    /eneral P(r)ose O)erating Sstem#/POS&

    #ernel is more generalied and contains all services

    1here can be slow responsiveness of an application

    Personal !omputers/3esktops system

    4): =indows 8P/MS93OS

    ,eal Time O)erating Sstem#,TOS& 3eterministic timing behavior

  • 7/25/2019 Real time operating system based design-1

    12/121

    10.* T)es o O)erating sstem10.* T)es o O)erating sstem

    ,eal Time Kernel

    1ask/Process Management

    1ask/Process scheduling

    1ask/Process synchroniation

    4rror/4)ception handling

    Memory management +nterrupt handling

    1ime management

  • 7/25/2019 Real time operating system based design-1

    13/121

    Tas+"Process !anagement

    TCB contains:

    >? 1ask +3@? 1ask State

    ;? 1ask 1ype

    A? 1ask Priority

    B? 1ask !onte)t Pointer

    C? 1ask Memory Pointers

    D? 1ask System "esource Pointers

    E? 1ask Pointers

    !reates 1!* for a task 3elete 1!*

    "ead 1!* for getting state of task

    pdate 1!*

    Modify 1!* for changing priority of the task

  • 7/25/2019 Real time operating system based design-1

    14/121

    Tas+"Process Sche'(ling

    Tas+"Process Snchroniation

    rror"2ce)tion 3an'ling 4rrors occurred during e)ecution of tasks

    +nsufficient memory' timeouts' deadlocks' deadline missing' bus error' divide by

    ero' etc

    #ernel level e)ception$ea'loc+

    1ask level e)ceptionTimeo(t

    ,P+Fet%ast4rror-.=indows !4 "1OS

  • 7/25/2019 Real time operating system based design-1

    15/121

    !emor !anagement

    *%O!# based allocation

    0ree *uffer 2ueue Interr()t 3an'ling :

    Synchronous and ,synchronous

    Synchronousinterrupts occurring in sync with current task -3ivide by Gero.

    ,synchronous

    not in sync with currently e)ecuting task

  • 7/25/2019 Real time operating system based design-1

    16/121

    3ar' real time :

    "1OS strictly adhere to the timing constraint

    Missing deadlinecatastrophic results-data lose. A late answer is a wrong answer

    ,ir bag control system H ,nti lock brake system-,*S.

  • 7/25/2019 Real time operating system based design-1

    17/121

    Tas+s Process an' Threa'sTas+s Process an' Threa's

    Tas+ : Program in e)ecution and all related information maintained

    by OS for the program? #6OB&

    Process : Program or part of it in e)ecution' it re2uires various system

    resources

    Str(ct(re o )rocess: !oncurrent e)ecution of tasks

  • 7/25/2019 Real time operating system based design-1

    18/121

    Process states an' state TransitionProcess states an' state Transition

    Process 4ie Ccle

    Create' state

    ,ea' state

    ,(nning state

    Bloc+e' state Com)lete' state

    State Transition

  • 7/25/2019 Real time operating system based design-1

    19/121

    Process states an' state TransitionProcess states an' state Transition

    728or+s

    ,A$9

    PN$ -*%O!#43.

    $4A9-process is sleeping.

    SUSPN$ !icroC"OSII

    $O,!ANT -!"4,143.

    ,A$9

    ,UNNIN/ 8AITIN/

    INT,,UPT$

  • 7/25/2019 Real time operating system based design-1

    20/121

    T3,A$ST3,A$S

    Primitive that can e)ecute code

    Single se2uential flow of control within a process

    %ight weight process

    !emor organiation o a )rocess an' its threa's

  • 7/25/2019 Real time operating system based design-1

    21/121

    !U4TIT3,A$IN/!U4TIT3,A$IN/

    *etter memory utiliation

    Speeds up e)ecution

    of process

    !P utiliation

  • 7/25/2019 Real time operating system based design-1

    22/121

    T3,A$ STAN$A,$ST3,A$ STAN$A,$S

    POS+8 1hreads:

    Portable Operating System +nterface

    %ibraryPthreads -creation and management functions in 5!I.

    int pthreadJcreate-pthreadJt KnewJthreadJ+3'

    const pthreadJattrJt Kattribute'void K -KstartJfunction. -void K.'

    void Karguments.L

    int pthreadJoin-pthreadJt newJthread' void K KthreadJstatus.L

    "eturn value of N5successI

  • 7/25/2019 Real time operating system based design-1

    23/121

    2am)le 12am)le 1

    includepthread?hQ

    includestdlib?hQincludestdio?hQ

    void KnewJthread-void KthreadJargs.

    R

    int i' Lfor-$NL BL &&.

    R

    printf-5NNNNL i&&. L //sleep-.' delay-.

    T

    return 7%%L

    T

  • 7/25/2019 Real time operating system based design-1

    24/121

    2am)le 1#cont'&2am)le 1#cont'&

    int main-void.

    Rint i'L

    pthreadJt tcbL

    if -pthreadJcreate - Htcb' 7%%' newJ thread' 7%%..

    R

    printf-54rror in creating new threadnI.Lreturn 9>L

    T

    for-$NL BL &&.

    R

    printf-5hello an in main threadnI.Lfor-i$NL i>NNNNL i&&.L

    T

  • 7/25/2019 Real time operating system based design-1

    25/121

    2am)le 1#cont'&2am)le 1#cont'&

    if -pthreadJoin -tcb' 7%% . .

    Rprintf-54rror in thread oinnI.L

    return 9>L

    T

    return 9>LT

  • 7/25/2019 Real time operating system based design-1

    26/121

    2am)le 1#cont'&2am)le 1#cont'&

    Threa' Termination :

    >? 7atural termination -returnorpthread_exit().@? 0orced termination -pthread_cancel() .

  • 7/25/2019 Real time operating system based design-1

    27/121

    8in ;* Threa's8in ;* Threa's

    1hreads supported by =indows operating system

    %ibrary=in ;@ ,P+

  • 7/25/2019 Real time operating system based design-1

    28/121

    8in ;* Threa's8in ;* Threa's

    GetCurrenthread(void)

    GetCurrenthread!d(void) GethreadPriorit"(void)

    #ethreadPriorit"($A%&' hhread, int n Priorit")

  • 7/25/2019 Real time operating system based design-1

    29/121

    2am)le2am)le

    includewindows?hQ

    includestdio?hQ""Chil' Threa'

    void !hild1hread-void.

    R

    char iLfor -i$NL i$>NL &&i.

    R

    printf-54)ecuting !hild 1hread: !ounter $ VdnI' i.L

    Sleep-BNN.L

    T

    T

  • 7/25/2019 Real time operating system based design-1

    30/121

    2am)le#cont'&2am)le#cont'&

    ""Primar Threa'

    int main-int argc' charK argvWX.R

    L

    T

  • 7/25/2019 Real time operating system based design-1

    31/121

    2am)le#cont'&2am)le#cont'&

    ""Primar Threa'

    for-i$NL i$>NLi&&.R

    printf-54)ecuting main thread: !ounter$VdnI'i.L

    Sleep-BNN.L

    Treturn NL

    T

  • 7/25/2019 Real time operating system based design-1

    32/121

  • 7/25/2019 Real time operating system based design-1

    33/121

    6a%a Threa's6a%a Threa's

    1hreads supported by ava programming language

    Packageava?lang

    import ava?lang?KL

    public class My1hreade)tends 1hread

    R

    public void run-.

    R

    System?out?println-5

  • 7/25/2019 Real time operating system based design-1

    34/121

  • 7/25/2019 Real time operating system based design-1

    35/121

    6a%a Threa's6a%a Threa's

    My1hread?start-.

    "eady state My1hread?yield-.

    6oluntarily gives up e)ecution of thread

    "eady state

    My1hread?sleep->NN.

    0orces to sleep for the duration mentioned

    Suspend mode and again back "eady state

  • 7/25/2019 Real time operating system based design-1

    36/121

    Threa' )reem)tionThrea' )reem)tion

    Preempting currently running thread

    =hy preemptionY 1hread conte)t switching

    User 4e%el Threa'

    >? 7o kernel/OS support

    @? Process

    multiple user level thread

    OS treat as a single thread

    processhas to schedule each thread

    Kernel 4e%el Threa'

    >? OS treat as separate threads

    @? Scheduling is done by OS

  • 7/25/2019 Real time operating system based design-1

    37/121

    T3,A$ BIN$IN/ !O$4S

    !antoOne !o'el

    >? Solaris Freen 1hreads

    @? F7 Portable 1hread

    OnetoOne !o'el>? =indows 8P/71/@NNN

    @? %inu) 1hreads

    !anto!an !o'el

    >? =indows 71/@NNN

    @? 1hread0ibre

  • 7/25/2019 Real time operating system based design-1

    38/121

  • 7/25/2019 Real time operating system based design-1

    39/121

  • 7/25/2019 Real time operating system based design-1

    40/121

    !(lti)rocessing an' !(ltitas+ing!(lti)rocessing an' !(ltitas+ing

    !U4TIP,OCSSIN/ :

    4)ecuting multiple processes simultaneously Multiprocessor systemsmultiple !Ps

    !U4TIP,O/,A!!IN/ :

    Multiple programs in memory

    niprocessor systemsswitching !P among the processes

    Multitaskingability of !P to hold multiple programs in memory and switch

    !P

    +llusion of multiple tasks e)ecuting parallelly

  • 7/25/2019 Real time operating system based design-1

    41/121

    CONT

  • 7/25/2019 Real time operating system based design-1

    42/121

  • 7/25/2019 Real time operating system based design-1

    43/121

    T)es o !(ltitas+ingT)es o !(ltitas+ing

    COOP,ATI7 !U4TITASKIN/:

    !urrent 4)ecuting process has to release !P voluntarily Process can hold !P as much as it wants

    P,!PTI7 !U4TITASKIN/:

    4very process gets a chance to e)ecute

    =hen and how much timeYYY

    NONP,!PTI7 !U4TITASKIN/:

    Process can e)ecute until it terminates O" enters *locked/=aiting state O"

    waiting for an +/O or system resource

  • 7/25/2019 Real time operating system based design-1

    44/121

    Tas+ Sche'(lingTas+ Sche'(ling

    +t forms the basis of multitasking

    3etermining which task/process has to be e)ecuted at a given point of time Sche'(ler : kernel service / application that implements the scheduling

    algorithm

    "unning "eady

    Pre9emptive

    *locked/=aiting "eady

    Priority based

    pre9emptive

    "unning *locked/=aiting

    Pre9emptive /

    non pre9emptive

    "unning !ompleted

    Pre9emptive /

    non pre9emptive /

    cooperative

  • 7/25/2019 Real time operating system based design-1

    45/121

    Factors to -e consi'ere' '(ring selection oFactors to -e consi'ere' '(ring selection o

    Sche'(ling algorithm " criterionSche'(ling algorithm " criterionCPU UTI4I=ATION:

    +t should be high

    Measure of how much percentage !P is being ultilied

    T3,OU/3PUT:

    7o of process e)ecuted per unit time

    +t should be high

    TU,NA,OUN$ TI!:

    ,mount of time taken be a process for completing its e)ecution

    1ime waiting for memory & time in ready 2ueue & time spend for +/O & time

    spent for e)ecution

  • 7/25/2019 Real time operating system based design-1

    46/121

    Factors to -e consi'ere' '(ring selection oFactors to -e consi'ere' '(ring selection o

    Sche'(ling algorithm " criterionSche'(ling algorithm " criterion8AITIN/ TI!:

    ,mount of time spend in "eady 2ueue waiting for !P

    +t should be minimal

    ,SPONS TI!:

    1ime between submission of a process and the first response

    +t should be least

  • 7/25/2019 Real time operating system based design-1

    47/121

    7ario(s >UUS in OS7ario(s >UUS in OS

    6OB >UU:

    ,ll process in the system

    ,A$9 >UU:

    ,ll process ready for e)ecution and waiting for !P

    $7IC >UU:

    ,ll process waiting for an +/O device

  • 7/25/2019 Real time operating system based design-1

    48/121

  • 7/25/2019 Real time operating system based design-1

    49/121

    Non )reem)ti%e Sche'(lingNon )reem)ti%e Sche'(ling

    irst co*e irst #erve(C#) + ! scheduling ,llocate !P based on the order in which they enter "eady 2ueue

    1icketing "eservation system

    2am)le 1

    1hree Process with +3s P>'P@'P; with estimated completion time >N'B'D milliseconds

    respectively enters ready 2ueue in the order P>' P@' P;? !alculate waiting timeand turn around time for each process and also average waiting time and turn

    around time-,ssume no +/O waiting for the processes.

    2am)le *

    !alculate waiting time and turn around time for each process and also average waiting

    time and turn around time-,ssume no +/O waiting for the processes. for the

    above e)ample if process enters the "eady ueue in the order P@' P>' P;

  • 7/25/2019 Real time operating system based design-1

    50/121

    Non )reem)ti%e Sche'(lingNon )reem)ti%e Sche'(ling

    'ast co*e irst #erve('C#) + '! scheduling

    2am)le 1

    1hree Process with +3s P>'P@'P; with estimated completion time >N'B'D milliseconds

    respectively enters ready 2ueue in the order P>' P@' P;? -,ssume only P> is

    present in the "eady 2ueue when the scheduler picks it up and P@' P; entered"eady 2ueue after that.? 7ow a new process PA with estimated completion time

    Cms enters the "eady 2ueue after Bms of scheduling P>? calculate the waiting

    time and turn around time?

  • 7/25/2019 Real time operating system based design-1

    51/121

    Non )reem)ti%e Sche'(lingNon )reem)ti%e Sche'(ling

    #hortest -ob irst #cheduling

    2am)le 1

    1hree Process with +3s P>'P@'P; with estimated completion time >N'B'D milliseconds

    respectively enters ready 2ueue together? calculate the waiting time and turn

    around time for each process and also average waiting time and turn around

    time?2am)le *

    !alculate the waiting time and turn around time for each process and also average

    waiting time and turn around time for the above e)ample if a new process PA

    with estimated completion time @ms enters the "eady ueue after @ms of

    e)ecution of P@?&isadvantage.

    Starvation

    !annot predict ne)t shortest process in the "eady 2ueue for scheduling

  • 7/25/2019 Real time operating system based design-1

    52/121

    Non )reem)ti%e Sche'(lingNon )reem)ti%e Sche'(ling

    Priorit" based #cheduling

    =indows !4 -N9@BB priority numbers.

    2am)le 1

    1hree Process with +3s P>'P@'P; with estimated completion time >N'B'D milliseconds

    and priorities N';'@ -N highest priority and ; lowest priority. respectively enters

    ready 2ueue together? calculate the waiting time and turn around time for eachprocess and also average waiting time and turn around time?

    2am)le *

    !alculate the waiting time and turn around time for each process and also average

    waiting time and turn around time for the above e)ample if a new process PA

    with estimated completion time Cms and priority > enters the "eady ueue after

    Bms of e)ecution of P>?

    &isadvantage.

    Starvation -1ackled by ,F+7F.

  • 7/25/2019 Real time operating system based design-1

    53/121

    Preem)ti%e Sche'(lingPreem)ti%e Sche'(ling

    Pree*ptive #- #cheduling+ #hortest /e*aining i*e(#/)

    2am)le 1

    1hree Process with +3s P>'P@'P; with estimated completion time >N'B'D milliseconds

    respectively enters ready 2ueue together? , new process PA enters with

    estimated completion time @ms enters the "eady 2ueue after @ms? calculate thewaiting time and turn around time for each process and also average waiting

    time and turn around time?

  • 7/25/2019 Real time operating system based design-1

    54/121

    Preem)ti%e Sche'(lingPreem)ti%e Sche'(ling

    /ound /obin (//) #cheduling

    42ual chance to all

    0irst Process in the "eady 2ueue is selected for e)ecution

    +t is e)ecuted for pre9defined time

    ,nd ne)t process is selected when the previous process pre9defined time elapses

    or process completes

  • 7/25/2019 Real time operating system based design-1

    55/121

    /ound /obin (//) #cheduling i*ple*entation for /012 in" #

    includert)B>tny?hQ

    int counterNL

    int counter>L

    obN -. JtaskJN R

    osJcreateJtask ->.L

    while->. R

    counterN&&L

    T

    T

    ob> -. JtaskJ> R

    while->. R

    counter>&&L

    T

    T

    void func (void) _tas3_tas3_id

    24 tas3s

  • 7/25/2019 Real time operating system based design-1

    56/121

    /012 in" // #cheduling in #5A/ CA/& /A&/

    Creating the tas+s

    void checkJcardJtask -void. JtaskJ>

    R

    T

    void processJcardJtask -void. JtaskJ@

    R

    T

    void checkJserialJioJtask -void. JtaskJ;

    RT

    void processJserialJdataJtask -void. JtaskJA

    R

    T

    Chec3 presence of cardProcess data received fro* card6pdate displa"Chec3 serial port for co**and

    and data

    Process data received fro* serial

    port

  • 7/25/2019 Real time operating system based design-1

    57/121

    /012 in" // #cheduling in #5A/ CA/& /A&/

    Sche'(ling o the tas+s

    void startupJtask -void. JtaskJN

    R

    osJcreateJtask ->.L

    osJcreateJtask [email protected] -;.L

    osJcreateJtask -A.L

    osJdeleteJtask -N.L

    T

  • 7/25/2019 Real time operating system based design-1

    58/121

    /012 in" // #cheduling in #5A/ CA/& /A&/

    void checkJcardJtask -void. JtaskJ>

    Rwhile->.

    R

    if-card is present.

    osJsendJsignal [email protected]

    T

    void processJcardJtask -void. JtaskJ@

    Rwhile->.

    R

    osJwait>-#JS+F.L

    TT

  • 7/25/2019 Real time operating system based design-1

    59/121

    /012 in" // #cheduling in #5A/ CA/& /A&/

    void checkJserialJioJtask -void. JtaskJ;

    Rwhile->.

    R

    if-data is received.

    osJsendJsignal -A.

    T

    void processJserialJdataJtask -void. JtaskJA

    R

    while->.

    RosJwait>-#JS+F.L

    T

    T

  • 7/25/2019 Real time operating system based design-1

    60/121

    Preem)ti%e Sche'(lingPreem)ti%e Sche'(ling

    /ound /obin (//) #cheduling

    2am)le 1

    1hree Process with +3s P>'P@'P; with estimated completion time C'A'@

    milliseconds respectively enters ready 2ueue together +7 1'P@'P;? calculate the waiting time and turn around time

    for each process and also average waiting time and turn around

    time using "" algorithm with 1ime slice $@ms?

  • 7/25/2019 Real time operating system based design-1

    61/121

    Preem)ti%e Sche'(lingPreem)ti%e Sche'(ling

    Priorit" 7ased #cheduling

    2am)le 1

    1hree Process with +3s P>'P@'P; with estimated completion time >N'B'D

    milliseconds and priorities >';'@ -N highest priority and ; lowest

    priority. respectively enters ready 2ueue together? , new process

    PA with estimated completion time Cms and priority N enters the

    "eady ueue after Bms of e)ecution of P>? calculate the waiting

    time and turn around time for each process and also average

    waiting time and turn around time?

  • 7/25/2019 Real time operating system based design-1

    62/121

    Threa's Processes an' Sche'(lingThrea's Processes an' Sche'(ling

    Process 1

    includewindows?hQ

    includestdio?hQ

    void 1ask-void.

    Rwhile->.

    R

    //Perform task e)ecution time $D?B units

    Sleep->D?B.L

    //"epeat 1ask

    T

  • 7/25/2019 Real time operating system based design-1

    63/121

    Process 1

    void main-void.

    R

    3=O"3 idL

  • 7/25/2019 Real time operating system based design-1

    64/121

    Threa's Processes an' Sche'(lingThrea's Processes an' Sche'(ling

    Process *

    includewindows?hQ

    includestdio?hQ

    void 1ask-void.

    Rwhile->.

    R

    //Perform task e)ecution time $>N units

    Sleep-B.L

    //"epeat 1ask

    T

  • 7/25/2019 Real time operating system based design-1

    65/121

    Process *

    void main-void.

    R

    3=O"3 idL

  • 7/25/2019 Real time operating system based design-1

    66/121

    Process *

    Set1hreadPriority-h1hread'

    1

  • 7/25/2019 Real time operating system based design-1

    67/121

    I$4 P,OCSS#TASK&

    void +dleJProcess -void.

    R

    //simple wait doing nothing

    =hile->.L

    T

  • 7/25/2019 Real time operating system based design-1

    68/121

  • 7/25/2019 Real time operating system based design-1

    69/121

  • 7/25/2019 Real time operating system based design-1

    70/121

    Tas+ Comm(nicationTas+ Comm(nication

    7ased on the degree of interaction.

    1. Coo)erating )rocesses

    *. Com)eting )rocesses

    !ooperation through sharing-through some shared resources.

    !ooperation through communication-communicate for synchroniation.Share' !emor:

    =rite and read data from

    from memory

  • 7/25/2019 Real time operating system based design-1

    71/121

    Tas+ Comm(nicationTas+ Comm(nication

    !*ple*enting #hared 5e*or".

    1. Pi)es:

    !lient9server architecture

    Pipe server and pipe client

    nidirectional and bidirectional

    Microsoft =indows support two types of Pipes:

    ,nonymous Pipes -unnamed and unidirectional.

    7amed Pipes -named and unidirectional or bidirectional.

  • 7/25/2019 Real time operating system based design-1

    72/121

    !emor !a))e' O-?ects!emor !a))e' O-?ects

    Shared memory techni2ue

    Createile5apping($A%&' hile, 'P#C6/!8_A/!76#

    lpile5appingAttributes, &9/& flProtect, &9/&

    dw5axi*un#i:e$igh,&9/&dw5axi*un#i:e'ow,'PC#/lp%a*e)

    5ap;iewfile( $A%&' hile5appingb

  • 7/25/2019 Real time operating system based design-1

    73/121

  • 7/25/2019 Real time operating system based design-1

    74/121

    !emor !a))e' O-?ects!emor !a))e' O-?ects

    =include>stdio?h@

    =include>windows?h@

    void *ain()

    $A%&' hile5apB

    'P#/ h5ap;iewB

    hile5ap Createile5apping( ($A%&') D2, %6'',

    PAG_/A&9/!, E, EFEEE, 0(*e*or"*appedob

  • 7/25/2019 Real time operating system based design-1

    75/121

    !emor !a))e' O-?ects!emor !a))e' O-?ects

    h5ap;iew ('P#/) 5ap;iewfile (hile5ap, !'_5AP_9/!, E,E,E)B

    !f(%6''h5ap;iew)

    printf(ailed. rror codeHdIn,Get'astrror())B

    returnB

    J

    else

    printf(;irtual Address is Hd, h5ap;iew)B

    J

    6n*ap;iewfile(h5ap;iew)BClose$andle(hile5ap)B

    J

  • 7/25/2019 Real time operating system based design-1

    76/121

    !essage Passing!essage Passing

    3ifference between shared memory and message passing

    ,mount of data transferred

    Speed

    1. !essage @(e(e

    0+0O

    send(%a*e of the process to which *essage is to be sent, *essage)

    receive(%a*e of the process fro* which *essage is be sent,

    *essage)

    Synchronous

    Z 4nters waiting state

    ,synchronous

    Z 7o waiting for acceptance

  • 7/25/2019 Real time operating system based design-1

    77/121

    !essage Passing!essage Passing

    !essage @(e(e

    Post5essage($9%& h9nd, 6!% 5sg, 9PA/A5 wPara*,

    'PA/A5 lPara*)

    Posthread5essage(&9/& idhread, 6!% 5sg, 9PA/A5

    wPara*, 'PA/A5 lPara*)

    #end5essage($9%& h9nd, 6!% 5sg, 9PA/A5 wPara*,'PA/A5 lPara*)

    Create5sgKueue('PC9#/ lps:%a*e, 'P5#GK66P!%#

    lpptions)

  • 7/25/2019 Real time operating system based design-1

    78/121

    !essage Passing!essage Passing

    t"pedef 5#GK66P!%#_#

    &9/& dw#i:eB

    &9/& dwlagsB

    &9/& dw5ax5essagesB

    &9/& cb5ax5essagesB

    7' b/eadAccessB

    JB

  • 7/25/2019 Real time operating system based design-1

    79/121

    !essage Passing!essage Passing

    !essage @(e(e

    pen5sgKueue()

    9rite5sgKueue($A%&' h5sgK, 'P;!& lp7uffer, &9/&

    cb&ata#i:e, &9/& dwi*eut, &9/& dwlags)

    Z dwlags5#GK66_5#GA'/

    /ead5sgKueue($A%&' h5sgK, 'P;!& lp7uffer, &9/&

    cb7uffer#i:e, 'P&9/& lp%u*berf7"tes/ead, &9/&

    dwi*eut, &9/& pwlags)

    Get5sgKueue!nfo($A%&' h5sgK, 'P5#GK66!% lp!nfo)

  • 7/25/2019 Real time operating system based design-1

    80/121

  • 7/25/2019 Real time operating system based design-1

    81/121

    !ailBo2!ailBo2

    ,lternate form of Message 2ueue

    !reation and Subscribing

    Mailbo) server and mailbo) client

  • 7/25/2019 Real time operating system based design-1

    82/121

    SignallingSignalling

    Primitive way of communication between processes

    Signals do not carry any data

    4g "18B> 1iny OS

    osJsendJsignal

    osJwait

  • 7/25/2019 Real time operating system based design-1

    83/121

    ,emote Proce'(re Call # ,PC& an' Soc+ets,emote Proce'(re Call # ,PC& an' Soc+ets

    "emote +nvocation or "emote +nvocation Method-"M+.

    !lient Server applications and heterogeneous environments

    Server and client

    Synchronous-*locking.

    ,synchronous

    ,uthentication

    -34S' ;34S.

    Sockets-+741' 7+8.

    Stream Socket

    3atagram Socket

  • 7/25/2019 Real time operating system based design-1

    84/121

    Tas+ SnchroniationTas+ Snchroniation

    Multiple processessharing system resources H variables

    Shared memory -write and read.

    ne)pected results

    ,ct of making the processes aware of the access of shared resources by

    each process to avoid conflicts

    >? "acing

    @? 3eadlock

    ;? 3ining Philosophers Problem

    A? Producer9consumer/bounded buffer problem

    B? "eaders =riters problem

    C? Priority inversion

  • 7/25/2019 Real time operating system based design-1

    85/121

    1. ,acing1. ,acing

    includewindows?hQ

    includestdio?hQ

    char *ufferW>NX$R>'@';'A'B'C'D'E'['>NTL

    short int counter$NL

    void ProcessJ,-void. R

    int iL

    for-i$NL iBL i&&. R

    if-*ufferWiX QN.counter&&L

    T

    T

  • 7/25/2019 Real time operating system based design-1

    86/121

    1. ,acing1. ,acing

    void ProcessJ*-void. R

    int L

    for-$NL BL &&. R

    if-*ufferWX QN.

    counter&&LT

    T

  • 7/25/2019 Real time operating system based design-1

    87/121

    1. ,acing1. ,acing

    int main-.

    R

    3=O"3 idL

    !reate1hread-7%%'N'

    -%P1

  • 7/25/2019 Real time operating system based design-1

    88/121

    1. ,acing1. ,acing

    mov ea) ' dword ptr Webp9AXL

    add ea)'>L

    mov dword ptr Webp9AX ' ea)L

  • 7/25/2019 Real time operating system based design-1

    89/121

    1. ,acing1. ,acing

    Multiple process compete each other to access and manipulate

    shared data concurrently-incorrect results. Sol(tion:

    Make access and modification of shared variables mutually

    e)clusive

  • 7/25/2019 Real time operating system based design-1

    90/121

    *. $ea'loc+*. $ea'loc+

    7one of the processes can e)ecute

    C 'i i i $ 'l +

  • 7/25/2019 Real time operating system based design-1

    91/121

    Con'itions a%oring $ea'loc+Con'itions a%oring $ea'loc+

    !ombined occurrence of all the four condition-Coff*an!onditions.

    3escribed by 4 F !offman in >[D>

    Mutual 4)clusion

    Only one process can hold a resource at a time

  • 7/25/2019 Real time operating system based design-1

    92/121

    $ea'loc+ 3an'ling$ea'loc+ 3an'ling

    Ignore $ea'loc+s :

    ,ssume system design is deadlock free

    !ost of removing a deadlock is large

    4): 7+8

    $etect an' ,eco%er $ea'loc+s : 7ac3 up cars techni2ue

    OS keeps a resource graph in m/y

    pdating the graph

    1erminate the process or preempt the

    resource

    $ 'l + 3 'li$ 'l + 3 'li

  • 7/25/2019 Real time operating system based design-1

    93/121

    $ea'loc+ 3an'ling$ea'loc+ 3an'ling

    A%oi' $ea'loc+s :

    ,voiding by careful resource allocation techni2ues

    4): 1raffic %ight mechanisms to avoid traffic ams

    Pre%ent $ea'loc+s :7egating one of the four conditions

    Mutual 4)clusion

    Process should re2uest and be allocated all resource before e)ecution

    ,llocate resource only if the process does not hold resource currently 7o "esource Preemption

    "elease resources if re2uest made by a process cannot be fulfilled

    pdate the resource list

    "eschedule processes when it gets its old resources and new resource

    $ 'l + 3 'li$ 'l + 3 'li

  • 7/25/2019 Real time operating system based design-1

    94/121

    $ea'loc+ 3an'ling$ea'loc+ 3an'ling

    4i%eloc+ an' $ea'loc+s

    Star%ation

    ; Th $i i Phil h P -l; Th $i i Phil h P -l

  • 7/25/2019 Real time operating system based design-1

    95/121

    ;. The $ining Philoso)hers Pro-lem;. The $ining Philoso)hers Pro-lem

    0ive philosophers - n.

    4ating '

  • 7/25/2019 Real time operating system based design-1

    96/121

    ;. The $ining Philoso)hers Pro-lem;. The $ining Philoso)hers Pro-lem

    1hree different scenarios:

    Scenario 1:

    +nvolve in brainstorming together and try to eat together

    !ircular chain

    Starvation and deadlock

    ; Th $i i Phil h P -l; Th $i i Phil h P -l

  • 7/25/2019 Real time operating system based design-1

    97/121

    ;. The $ining Philoso)hers Pro-lem;. The $ining Philoso)hers Pro-lem

    1hree different scenarios:

    Scenario *:

    ,ll philosophers start brainstorming together

    "ace condition-une)pected results.

    ; Th $i i Phil h P -l; Th $i i Phil h P -l

  • 7/25/2019 Real time operating system based design-1

    98/121

    ;. The $ining Philoso)hers Pro-lem;. The $ining Philoso)hers Pro-lem

    1hree different scenarios:

    Scenario ;:

    Philosophers brainstorming together and trying to eat together

    %ivelock and starvation

    ; Th $i i Phil h P -l; The $ining Philoso)hers Pro-lem

  • 7/25/2019 Real time operating system based design-1

    99/121

    ;. The $ining Philoso)hers Pro-lem;. The $ining Philoso)hers Pro-lem

    Sol(tions:

    "ound "obin

    +mposing rules in accessing the forks by philosophers

    Philosopher ac2uires a semaphore - mute). before picking up ant fork

    P ' C "B ' ' B P -l Pro'(cer Cons(mer"Bo(n'e' B(er Pro-lem

  • 7/25/2019 Real time operating system based design-1

    100/121

    . Pro'(cerCons(mer"Bo(n'e' B(er Pro-lem. Pro'(cerCons(mer"Bo(n'e' B(er Pro-lem

    1wo process concurrently access a shared buffer with fi)ed sie

    1hread/Process producing dataProducer hread+Process

    1hread/Process consuming dataConsu*er hread+Process

    3ue to lack of synchroniation:

    7uffer overrun

    7uffer underDrun

    %ead to inaccurate data and data loss

    Pro'(cer Cons(mer"Bo(n'e' B(er Pro-lem Pro'(cer Cons(mer"Bo(n'e' B(er Pro-lem

  • 7/25/2019 Real time operating system based design-1

    101/121

    . Pro'(cerCons(mer"Bo(n'e' B(er Pro-lem. Pro'(cerCons(mer"Bo(n'e' B(er Pro-lem

    includewindow?hQ

    includestdio?hQ

    define 7 @N

    int bufferW7XL

    6oid producerJthread-void.R

    int )L

    while-true.R

    for-)$NL )7L )&&.R

    bufferW)X$rand-.V>NNNL

    printf-5Produced:*ufferWVdX$VdI' )' bufferW)X.L

    [email protected]

    T

    T

    T

    Pro'(cer Cons(mer"Bo(n'e' B(er Pro-lem Pro'(cer Cons(mer"Bo(n'e' B(er Pro-lem

  • 7/25/2019 Real time operating system based design-1

    102/121

    . Pro'(cerCons(mer"Bo(n'e' B(er Pro-lem. Pro'(cerCons(mer"Bo(n'e' B(er Pro-lem

    6oid consumerJthread-void.R

    int y$N' valueL

    while-true.R

    for-y$NL y7L y&&.R

    value$bufferWyXL

    printf-5!onsumed:*ufferWVdX$VdI' y' value.L

    [email protected]

    T

    T

    T

    Pro'(cer Cons(mer"Bo(n'e' B(er Pro-lem Pro'(cer Cons(mer"Bo(n'e' B(er Pro-lem

  • 7/25/2019 Real time operating system based design-1

    103/121

    . Pro'(cerCons(mer"Bo(n'e' B(er Pro-lem. Pro'(cerCons(mer"Bo(n'e' B(er Pro-lem

    int main-.

    R

    3=O"3 threadJidL

    !reate1hread-7%%'N'

    -%P1

  • 7/25/2019 Real time operating system based design-1

    104/121

    . Pro'(cerCons(mer"Bo(n'e' B(er Pro-lem. Pro'(cerCons(mer"Bo(n'e' B(er Pro-lem

    ISSUS:

    >? Producer thread is scheduled more fre2uently than consumer thread

    @? !onsumer thread is scheduled more fre2uently than Producer thread

    SO4UTIONS:

    >? Sleep and wake9up

    5 ,ea'ers 8riters Pro-lem5 ,ea'ers 8riters Pro-lem

  • 7/25/2019 Real time operating system based design-1

    105/121

    5. ,ea'ers8riters Pro-lem5. ,ea'ers8riters Pro-lem

    Processes competing for limited shared resources

    *anking system

    Multiple processes read data concurrentlyno impact

    Multiple processes read and write data concurrentlyimpact

    Priorit In%ersion Priorit In%ersion

  • 7/25/2019 Real time operating system based design-1

    106/121

    . Priorit In%ersion. Priorit In%ersion

    unlocked

  • 7/25/2019 Real time operating system based design-1

    107/121

    Priorit InheritancePriorit Inheritance

  • 7/25/2019 Real time operating system based design-1

    108/121

    Priorit InheritancePriorit Inheritance

    %ow priority task accessing shared resource re2uested by high priority

    task temporarily inherits the priority of high priority task from themoment high priority task raises the re2uest

    ,nd when the resource is released priority is set back to original value

  • 7/25/2019 Real time operating system based design-1

    109/121

    Priorit CeilingPriorit Ceiling

  • 7/25/2019 Real time operating system based design-1

    110/121

    Priorit CeilingPriorit Ceiling

    Priority is associated with each shared resource

    Priority $ priority of highest priority task using the resource

    !eiling priority

    *oosting the priority of low priority task to ceiling priority

    ,nd the priority is reset after it releases shared resource

  • 7/25/2019 Real time operating system based design-1

    111/121

    Tas+ Snchroniation Techni@(esTas+ Snchroniation Techni@(es

  • 7/25/2019 Real time operating system based design-1

    112/121

    Tas+ Snchroniation Techni@(esTas+ Snchroniation Techni@(es

    ,voiding conflicts in resource access

    4nsuring proper se2uence of operation across processes

    !ommunicating between the processes

    C,ITICA4 SCTION:

    !ode memory area holding program instructions for accessing shared

    resource

    Mutual e)clusion

    1 !(t(al 2cl(sion thro(gh B(s 8aiting " S)in 4oc+1. !(t(al 2cl(sion thro(gh B(s 8aiting " S)in 4oc+

  • 7/25/2019 Real time operating system based design-1

    113/121

    1. !(t(al 2cl(sion thro(gh B(s 8aiting " S)in 4oc+1. !(t(al 2cl(sion thro(gh B(s 8aiting " S)in 4oc+

    %ock variable for implementing mutual e)clusion

    %ock$ $ >in critical section

    %ock $ $ Nnot in critical section

    //inside main thread

    bool b0lagL //lock variable

    b0lag$0,%S4L

    //inside child threa

    if-b0lag $ $0,%S4.

    b0lag$1"4L

    *. !(t(al 2cl(sion thro(gh Slee) 8a+e()*. !(t(al 2cl(sion thro(gh Slee) 8a+e()

  • 7/25/2019 Real time operating system based design-1

    114/121

    *. !(t(al 2cl(sion thro(gh Slee) 8a+e()*. !(t(al 2cl(sion thro(gh Slee) 8a+e() !P is busy checking the lock to see whether they can proceed

    7ot feasible in embedded systems-power consumption. Sleep H =ake up mechanisms

    Process in critical section new process accessing critical section will

    goto #leep process owning critical section wakeup the new process

    when it lives the critical section

    2? #e*aphore .

    Process which wants to access shared resource first should ac2uire

    system obect to indicate other processes that the shared resource is

    currently ac2uired by it

    "esource

  • 7/25/2019 Real time operating system based design-1

    115/121

  • 7/25/2019 Real time operating system based design-1

    116/121

    C i i l S i Obj

  • 7/25/2019 Real time operating system based design-1

    117/121

    Critical Section Objects:

    !nitiali:eCritical#ection('PC/!!CA'_#C!%lpCritical#ection)

    nterCritical#ection('PC/!!CA'_#C!%lpCritical#ection) 9ait Lueue

    r"nterCritical#ection('PC/!!CA'_#C!%lpCritical#ection)

    'eaveCritical#ection('PC/!!CA'_#C!%lpCritical#ection)

    &eletenterCritical#ection('PC/!!CA'_#C!%lpCritical#ection)

    $7IC $,I7,S$7IC $,I7,S

  • 7/25/2019 Real time operating system based design-1

    118/121

    $7IC $,I7,S$7IC $,I7,S *ridge between OS and hardware

    ,bstracts hardware from user applications 4stablish connectivity' initialie hardware and transfer data

    =i9fi module' 0ile systems' Storage device interfaces

    Protocols different

    4ach hardware need a uni2ue driver component

    $7IC $,I7,S$7IC $,I7,S

  • 7/25/2019 Real time operating system based design-1

    119/121

    $7IC $,I7,S 7,73 0lash memory7,73 0lash 3river

    *uilt in drivers or Onboard drivers

    loaded during booting

    kept in",M

    +nstallable driversloaded on need basis

    3O8 TO C3OOS AN ,TOS3O8 TO C3OOS AN ,TOS

  • 7/25/2019 Real time operating system based design-1

    120/121

    O8 O C OOS N OSFUNCTIONA4 ,>UI,!NTS:

    Processor support Memory re2uirements- "OM HH ",M .

    "eal91ime !apabilities

    #ernel and interrupt latency-latency minimal.

    +P! and task synchroniation Modulariation support

    Support for 7etworking and !ommunication

    3evelopment %anguage support

    3O8 TO C3OOS AN ,TOS3O8 TO C3OOS AN ,TOS

  • 7/25/2019 Real time operating system based design-1

    121/121

    NONFUNCTIONA4 ,>UI,!NTS:

    !ustom developed or off the shelf !ost

    3evelopment and debugging tools availability

    4ase of use