Tutorial05 Solution

Embed Size (px)

Citation preview

  • 8/12/2019 Tutorial05 Solution

    1/23

    Technische Universitt Mnchen

    Chip Multicore ProcessorsTutorial 5

    Institute for Integrated Systems

    Theresienstr. 90

    Building N1www.lis.ei.tum.de

    S. Wallentowitz

  • 8/12/2019 Tutorial05 Solution

    2/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 52

    S. Wallentowitz

    Task 4.2: FiFo Implementation

    In this task you will implement a FiFo memory in software.

    a) Implement a (capacity bound) FiFo as circular buffer by using an array.b) Allow the parallel access to the FiFo by multiple threads using locks.

    c) Is it possible to use two locks for parallel access by reading and writing threads?

    d) Use a (POSIX) condition variable for synchronization.

    e) Can you implement this data structure in a non-blocking fashion? If not, how can you

    change the data structure to allow for it?

    f) Is it possible to implement an (unbounded) FiFo by using a linked list and without

    locking?

  • 8/12/2019 Tutorial05 Solution

    3/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 53

    S. Wallentowitz

    FiFo Implementation

    int data[N];

    unsigned int start;

    unsigned int end;

    void enqueue(int d) {

    while(((end+1)%N)==start)) {}

    data[end] = d;end = (end+1)%N;

    }

    int dequeue() {

    int d;

    while(end==start) {}

    d=data[start];start=(start+1)%N;

    return d;

    }

  • 8/12/2019 Tutorial05 Solution

    4/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 54

    S. Wallentowitz

    Repetition: Lock with Mutex

    void enqueue(int d) {

    mutex_lock(&lock);

    while(((end+1)%N)==start)) {}

    data[end] = d;

    end = (end+1)%N;

    mutex_unlock(&lock);}

    int dequeue() {

    int d;

    mutex_lock(&lock);

    while(end==start) {}

    d=data[start];

    start=(start+1)%N;

    mutex_unlock(&lock);

    return d;

    }

  • 8/12/2019 Tutorial05 Solution

    5/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 55

    S. Wallentowitz

    Repetition: Lock with Mutex, solve starvation issue

    void enqueue(int d) {mutex_lock(&lock);

    while(((end+1)%N)==start)) {

    mutex_unlock(&lock);

    mutex_lock(&lock);

    }data[end] = d;

    end = (end+1)%N;

    mutex_unlock(&lock);

    }

    int dequeue() {

    int d;

    mutex_lock(&lock);

    while(end==start) {

    mutex_unlock(&lock);

    mutex_lock(&lock);

    }

    d=data[start];start=(start+1)%N;mutex_unlock(&lock);

    return d;

    }

  • 8/12/2019 Tutorial05 Solution

    6/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 56

    S. Wallentowitz

    Reader and Writer Lock

    void enqueue(int d) {

    while(((end+1)%N)==start)) {}

    data[end] = d;

    end = (end+1)%N;

    }

    int dequeue() {

    int d;

    while(end==start) {}

    d=data[start];

    start=(start+1)%N;

    return d;

    }

  • 8/12/2019 Tutorial05 Solution

    7/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 57

    S. Wallentowitz

    Condition Variable

    void enqueue(int d) {

    mutex_lock(&rlock);

    if(((end+1)%N)==start)) {

    cond_wait(&wcond,&rlock);

    }

    data[end] = d;end = (end+1)%N;cond_signal(&rcond);

    mutex_unlock(&rlock);

    }

    cond_wait(cond_t*,mutex_t*)cond_signal(cond_t*)

    cond_broadcast(cond_t*)

    int dequeue() {

    int d;

    mutex_lock(&wlock);

    if(end==start) {

    cond_wait(&rcond,&wlock);

    }d=data[start];

    start=(start+1)%N;

    cond_signal(&wcond);

    mutex_unlock(&wlock);

    return d;

    }

  • 8/12/2019 Tutorial05 Solution

    8/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 58S. Wallentowitz

    Non-Blocking implementation

    void enqueue(int d) {

    while(((end+1)%N)==start)) {}

    data[end] = d;

    end = (end+1)%N;

    }

    int dequeue() {

    int d;

    while(end==start) {}

    d=data[start];

    start=(start+1)%N;

    return d;

    }

  • 8/12/2019 Tutorial05 Solution

    9/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 59S. Wallentowitz

    Non-Blocking implementation: Dequeue

    int dequeue() {

    int d;

    int tmp;

    while(end==start) {}

    do {

    tmp = start;d=data[tmp];

    } while(cas(start,tmp,(tmp+1)%N));

    return d;

    }

  • 8/12/2019 Tutorial05 Solution

    10/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 510S. Wallentowitz

    Non-Blocking implementation: Dequeue

    int dequeue() {

    int d;

    int tmp;

    do {

    tmp = start;

    if (tmp==end) continue;d = data[tmp];

    } while(cas(start,tmp,(tmp+1)%N));

    return d;

    }

  • 8/12/2019 Tutorial05 Solution

    11/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 511S. Wallentowitz

    Non-Blocking implementation: Enqueue

    void enqueue(int d) {

    int tmp;

    do {

    tmp = end;

    if(((tmp+1)%N)==start)) continue;

    data[tmp] = d;

    } while(end,tmp,(tmp+1)%N);}

  • 8/12/2019 Tutorial05 Solution

    12/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 512S. Wallentowitz

    Non-Blocking implementation: Enqueue

    void enqueue(int d) {

    int tmp;

    do {

    tmp = end;

    if(((tmp+1)%N)==start)) continue;

    } while(end,tmp,(tmp+1)%N);

    data[tmp] = d;}

    int dequeue() {

    int d;

    int tmp;

    do {

    tmp = start;

    if (tmp==end) continue;

    d = data[tmp];} while(cas(start,tmp,(tmp+1)%N));

    return d;

    }

  • 8/12/2019 Tutorial05 Solution

    13/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 513S. Wallentowitz

    Unbounded FiFo: Linked List

  • 8/12/2019 Tutorial05 Solution

    14/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 514S. Wallentowitz

    Thinkable Solutions for Linked-List FiFo

    Simple, fast, and practical non-blocking and blocking concurrent queue algorithms, M. Michael, M. Scott, PODC '96

    A Pragmatic Implementation of Non-blocking Linked-lists, T. Harris, LNCS 2180

  • 8/12/2019 Tutorial05 Solution

    15/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 515S. Wallentowitz

    Task 5.1: Semaphores

    Explain the principle of semaphores.

    How do semaphores relate to mutexes?

  • 8/12/2019 Tutorial05 Solution

    16/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 516S. Wallentowitz

    Semaphore

    Generalized concept for critical sections Multiple resources available

    Allow multiple threads to enter section

    Mutex as binary semaphore

    Avoid starvation by queue of waiting threads Two basic functions

    acquire():

    Acquire resource, or

    Wait if not sufficient resources

    Historic name: P() (prolaag)

    release(): Release resource

    Wake up next waiting thread

    Historic name: V() (verhoogen)

    queue Q;int count;

    init(int N) {

    count=N;

    }

    acquire() {

    count=count-1;

    if (count

  • 8/12/2019 Tutorial05 Solution

    17/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 517S. Wallentowitz

    Task 5.2: Readers-Writers Problem

    The Readers-Writers Problem is a concurrency problem. A shared

    ressource is used by threads reading from it and others writing from

    it. No thread should access the ressource while it is written, but it isallowed that several readers read from the resource concurrently.

    Develop an optimal solution to this problem using semaphores.

  • 8/12/2019 Tutorial05 Solution

    18/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 518S. Wallentowitz

    Readers-Writers Problem

    void read(){

    // the read action

    }

    void write() {

    // the write action

    }

  • 8/12/2019 Tutorial05 Solution

    19/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 519S. Wallentowitz

    Readers-Writers Problem, mutex solution

    semaphore sem = 1;

    void read(){

    acquire(sem);

    // the read action

    release(sem);}

    void write() {

    acquire(sem);

    // the write action

    release(sem);

    }

  • 8/12/2019 Tutorial05 Solution

    20/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 520S. Wallentowitz

    Readers-Writers Problem, 1. Solution

    semaphore rsem = 1;

    semaphore wsem = 1;

    void read(){

    acquire(rsem);

    // the read action

    release(rsem);

    }

    void write() {

    acquire(wsem);

    // the write action

    release(wsem);

    }

  • 8/12/2019 Tutorial05 Solution

    21/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 521S. Wallentowitz

    Readers-Writers Problem, 1. Solution

    semaphore rsem = 1;

    semaphore wsem = 1;

    void read(){

    acquire(rsem);

    acquire(wsem);

    // the read action

    release(wsem);

    release(rsem);

    }

    void write() {

    acquire(wsem);

    // the write actionrelease(wsem);

    }

  • 8/12/2019 Tutorial05 Solution

    22/23

    Technische Universitt Mnchen

    Institute for Integrated SystemsChip Multicore ProcessorsTutorial 522S. Wallentowitz

    Readers-Writers Problem, 1. Solution

    int counter = 0;

    void read(){

    acquire(rsem);

    counter++;

    if(counter==1) acquire(wsem);

    release(rsem);

    // the read action

    acquire(rsem);

    counter--;

    if(counter==0) release(wsem);

    release(rsem);

    }

    void write() {acquire(wsem);

    // the write action

    release(wsem);

    }

  • 8/12/2019 Tutorial05 Solution

    23/23