CS2106 Lec4 IPC I

download CS2106 Lec4 IPC I

of 50

Transcript of CS2106 Lec4 IPC I

  • 8/8/2019 CS2106 Lec4 IPC I

    1/50

    1

  • 8/8/2019 CS2106 Lec4 IPC I

    2/50

    Lecture 4

    InterprocessCommunication

    31 August, 2010

    2

  • 8/8/2019 CS2106 Lec4 IPC I

    3/50

    why IPC?

    3

  • 8/8/2019 CS2106 Lec4 IPC I

    4/50

    message passing

    4

  • 8/8/2019 CS2106 Lec4 IPC I

    5/50

    shared memory

    5

  • 8/8/2019 CS2106 Lec4 IPC I

    6/50

    UNIX IPC

    6

  • 8/8/2019 CS2106 Lec4 IPC I

    7/50

    signal

    7

  • 8/8/2019 CS2106 Lec4 IPC I

    8/50

    #include

    :

    //pidistheprocessIDtosendthe

    //signalto//sigistheIDofthesignal.

    kill(pid,sig);

    kill(pid,SIGTERM);

    8

  • 8/8/2019 CS2106 Lec4 IPC I

    9/50

    at the receiver, default

    actions are defined for

    each signals

    9

  • 8/8/2019 CS2106 Lec4 IPC I

    10/50

    but reaction to most

    signals can be

    customized

    10

  • 8/8/2019 CS2106 Lec4 IPC I

    11/50

    Cfunction pointers

    11

  • 8/8/2019 CS2106 Lec4 IPC I

    12/50

    //functiontakingina

    //char*andreturninganint.

    intfoo(char*name){...}

    //declareafunctionpointer

    //initializeafunctionpointer

    //callthefunction

    12

  • 8/8/2019 CS2106 Lec4 IPC I

    13/50

    return

    type

    var

    name

    arg

    type(* ) ( )

    13

  • 8/8/2019 CS2106 Lec4 IPC I

    14/50

    function pointers can be:

    (i) passed into function,(ii) returned

    (iii) defined as new type

    14

  • 8/8/2019 CS2106 Lec4 IPC I

    15/50

    #include

    void*wont_die(intsig){return;}:

    void(*prev_handler)(int);

    prev_handler=signal(SIGKILL,wont_die);

    15

  • 8/8/2019 CS2106 Lec4 IPC I

    16/50

  • 8/8/2019 CS2106 Lec4 IPC I

    17/50

    #include

    :

    intfiles[2];

    pipe(files);

    //noweverythingwewriteintofile[1]

    //canbereadfromfile[0].

    17

  • 8/8/2019 CS2106 Lec4 IPC I

    18/5018

  • 8/8/2019 CS2106 Lec4 IPC I

    19/50

    Flickr Photo Some rights reserved by Darwin Bell

    19

    http://www.flickr.com/photos/darwinbell/http://www.flickr.com/photos/darwinbell/http://creativecommons.org/licenses/by/2.0/http://creativecommons.org/licenses/by/2.0/
  • 8/8/2019 CS2106 Lec4 IPC I

    20/50

    race conditions

    20

  • 8/8/2019 CS2106 Lec4 IPC I

    21/50

    demo

    21

  • 8/8/2019 CS2106 Lec4 IPC I

    22/50

    mutual exclusion

    only one process can

    access a sharedresource at a time

    22

  • 8/8/2019 CS2106 Lec4 IPC I

    23/50

    critical region

    23

  • 8/8/2019 CS2106 Lec4 IPC I

    24/50

    processes typically alternate

    between critical and non-critical region.

    24

  • 8/8/2019 CS2106 Lec4 IPC I

    25/50

    while (1) enter( )

    critical_region leave( )

    noncritical_region

    25

  • 8/8/2019 CS2106 Lec4 IPC I

    26/50

    how to implemententer( ) and leave( )

    26

  • 8/8/2019 CS2106 Lec4 IPC I

    27/50

    lock variable

    27

  • 8/8/2019 CS2106 Lec4 IPC I

    28/50

    enter( )while (lock);

    lock = 1;

    leave( )lock = 0;

    28

  • 8/8/2019 CS2106 Lec4 IPC I

    29/50

    while (lock);

    lock = 1;

    while (lock);

    lock = 1;

    process A process B

    29

  • 8/8/2019 CS2106 Lec4 IPC I

    30/50

    interrupts

    30

  • 8/8/2019 CS2106 Lec4 IPC I

    31/50

    enter( )disable interrupt

    leave( )

    enable interrupt

    31

  • 8/8/2019 CS2106 Lec4 IPC I

    32/50

    PetersonsAlgorithm

    32

  • 8/8/2019 CS2106 Lec4 IPC I

    33/50

    enter( )interested[A] = 1

    turn =B

    while (turn ==B&& interested[B]);

    leave( )interested[A] = 0

    process A

    33

  • 8/8/2019 CS2106 Lec4 IPC I

    34/50

    enter( )interested[B] = 1

    turn =A

    while (turn ==A&& interested[A]);

    leave( )interested[B] = 0

    process B

    34

  • 8/8/2019 CS2106 Lec4 IPC I

    35/50

    i [A] = 1

    t = B

    while (t is B && i [B]);

    process A process B

    i [B] = 1t = A

    while (t is A && i [A]);

    35

  • 8/8/2019 CS2106 Lec4 IPC I

    36/50

    i [A] = 1

    t = B

    while (t is B

    && i [B]);

    process A process B

    i [B] = 1

    t = Awhile (t is A && i [A]);

    36

  • 8/8/2019 CS2106 Lec4 IPC I

    37/50

    atomic instructions

    37

  • 8/8/2019 CS2106 Lec4 IPC I

    38/50

    TSL R, lock

    in one atomic step,

    copy lock to R &set lock to 1

    38

  • 8/8/2019 CS2106 Lec4 IPC I

    39/50

    enter( )TSLR,lock

    CMPR,#0

    JNEenter

    RET

    leave( )MOVlock,#0

    39

  • 8/8/2019 CS2106 Lec4 IPC I

    40/50

    test&set(lock)

    return lock and

    set lock to 1

    40

  • 8/8/2019 CS2106 Lec4 IPC I

    41/50

    enter( )while (test&set(lock));

    leave( )

    lock = 0;

    41

  • 8/8/2019 CS2106 Lec4 IPC I

    42/50

    XCHG R, lock

    in one atomic step, swap

    values of two locations

    42

  • 8/8/2019 CS2106 Lec4 IPC I

    43/50

    enter( )

    MOVR,#1XCHGR,lock

    CMPR,#0

    JNEenter

    RET

    leave( )MOVlock,#0

    43

  • 8/8/2019 CS2106 Lec4 IPC I

    44/50

    while (1) enter( )

    critical_region leave( )

    noncritical_region

    44

  • 8/8/2019 CS2106 Lec4 IPC I

    45/50

    producer -

    consumer

    problem

    45

  • 8/8/2019 CS2106 Lec4 IPC I

    46/50

    producer consumer

    46

  • 8/8/2019 CS2106 Lec4 IPC I

    47/50

    while (1)

    if (buffer is full) sleep

    produce if (buffer was empty)

    wake up consumer

    producer

    47

  • 8/8/2019 CS2106 Lec4 IPC I

    48/50

    while (1)

    if (buffer is empty) sleep

    consume if (buffer was full)

    wake up producer

    consumer

    48

  • 8/8/2019 CS2106 Lec4 IPC I

    49/50

    while (1)

    if (buffer is empty)

    sleepconsume

    if (buffer was full)

    wakeup producer

    while (1)

    if (buffer is full)

    sleep

    produce if (buffer was empty)

    wakeup consumer

    49

  • 8/8/2019 CS2106 Lec4 IPC I

    50/50

    Flickr Photo So e rights reser ed by fofurasfelinas

    http://www.flickr.com/photos/fofurasfelinas/http://creativecommons.org/licenses/by-nc-nd/2.0/