Docsity

Synchronization and Deadlock in Computer Systems: A Study of Semaphores and Shared Buffers, Slides of Computer System Design and Architecture

Partial preview of the text

Download Synchronization and Deadlock in Computer Systems: A Study of Semaphores and Shared Buffers and more Slides Computer System Design and Architecture in PDF only on Docsity!

Synchroniza+on: Advanced

15-­‐213: Introduc0on to Computer Systems

th

Lecture, Nov. 18, 2010

Instructors:

Randy Bryant and Dave O’Hallaron

Today

 Producer-­‐consumer problem

 Readers-­‐writers problem

 Thread safety

 Races

 Deadlocks

Producer-­‐Consumer Problem

 Common synchroniza+on paGern:

 Producer waits for empty slot , inserts item in buffer, and no0fies consumer

 Consumer waits for item , removes it from buffer, and no0fies producer

 Examples

 Mul0media processing:

 Producer creates MPEG video frames, consumer renders them

 Event-­‐driven graphical user interfaces

 Producer detects mouse clicks, mouse movements, and keyboard hits

and inserts corresponding events in buffer

 Consumer retrieves events from buffer and paints the display

producer

thread

shared

buffer

consumer

thread

Producer-­‐Consumer on 1-­‐element Buffer

#include “csapp.h” #define NITERS 5 void *producer(void *arg); void *consumer(void arg); struct { int buf; / shared var / sem_t full; / sems */ sem_t empty; } shared; int main() { pthread_t tid_producer; pthread_t tid_consumer; _/ Initialize the semaphores /_ Sem_init(&shared.empty, 0, 1); Sem_init(&shared.full, 0, 0); _/ Create threads and wait /_ Pthread_create(&tid_producer, NULL, producer, NULL); Pthread_create(&tid_consumer, NULL, consumer, NULL); Pthread_join(tid_producer, NULL); Pthread_join(tid_consumer, NULL); exit(0); }

Producer-­‐Consumer on an n -­‐element Buffer

 Requires a mutex and two coun+ng semaphores:

 mutex: enforces mutually exclusive access to the the buffer

 slots: counts the available slots in the buffer

 items: counts the available items in the buffer

 Implemented using a shared buffer package called sbuf.

sbuf Package -­‐ Declara+ons

#include "csapp.h”

typedef struct {

int buf; / Buffer array */

int n; /* Maximum number of slots */

int front; /* buf[(front+1)%n] is first item */

int rear; /* buf[rear%n] is last item */

sem_t mutex; /* Protects accesses to buf */

sem_t slots; /* Counts available slots */

sem_t items; /* Counts available items */

} sbuf_t;

void sbuf_init(sbuf_t *sp, int n);

void sbuf_deinit(sbuf_t *sp);

void sbuf_insert(sbuf_t *sp, int item);

int sbuf_remove(sbuf_t *sp);

sbuf.h

sbuf Package -­‐ Implementa+on

/* Insert item onto the rear of shared buffer sp */ void sbuf_insert(sbuf_t sp, int item) { P(&sp->slots); / Wait for available slot / P(&sp->mutex); / Lock the buffer / sp->buf[(++sp->rear)%(sp->n)] = item; / Insert the item / V(&sp->mutex); / Unlock the buffer / V(&sp->items); / Announce available item */ }

sbuf.c

Inser+ng an item into a shared buffer:

sbuf Package -­‐ Implementa+on

/* Remove and return the first item from buffer sp */ int sbuf_remove(sbuf_t sp) { int item; P(&sp->items); / Wait for available item / P(&sp->mutex); / Lock the buffer / item = sp->buf[(++sp->front)%(sp->n)]; / Remove the item / V(&sp->mutex); / Unlock the buffer / V(&sp->slots); / Announce available slot */ return item; }

sbuf.c

Removing an item from a shared buffer:

Readers-­‐Writers Problem

 Generaliza+on of the mutual exclusion problem

 Problem statement:

 Reader threads only read the object

 Writer threads modify the object

 Writers must have exclusive access to the object

 Unlimited number of readers can access the object

 Occurs frequently in real systems, e.g.,

 Online airline reserva0on system

 Mul0threaded caching Web proxy

Variants of Readers-­‐Writers

 First readers-­‐writers problem (favors readers)

 No reader should be kept wai0ng unless a writer has already been

granted permission to use the object.

 A reader that arrives a[er a wai0ng writer gets priority over the

writer.

 Second readers-­‐writers problem (favors writers)

 Once a writer is ready to write, it performs its write as soon as

possible

 A reader that arrives a[er a writer must wait, even if the writer is

also wai0ng.

 Starva5on (where a thread waits indefinitely) is possible

in both cases.

Case Study: Prethreaded Concurrent Server

Master

thread

Buffer

Accept ...

connec5ons Insert descriptors (^) Remove descriptors

Worker

thread

Worker

thread

Client

Client

Service client Service client

Pool of

worker

threads

Prethreaded Concurrent Server

sbuf_t sbuf; /* Shared buffer of connected descriptors */ int main(int argc, char *argv) { int i, listenfd, connfd, port; socklen_t clientlen=sizeof(struct sockaddr_in); struct sockaddr_in clientaddr; pthread_t tid; port = atoi(argv[1]); sbuf_init(&sbuf, SBUFSIZE); listenfd = Open_listenfd(port); for (i = 0; i < NTHREADS; i++) / Create worker threads */ Pthread_create(&tid, NULL, thread, NULL); while (1) { connfd = Accept(listenfd, (SA ) &clientaddr, &clientlen); sbuf_insert(&sbuf, connfd); / Insert connfd in buffer */ } }

echoservert_pre.c

Prethreaded Concurrent Server

static int byte_cnt; /* Byte counter / static sem_t mutex; / and the mutex that protects it */ static void init_echo_cnt(void) { Sem_init(&mutex, 0, 1); byte_cnt = 0; }

echo_cnt.c

echo_cnt ini+aliza+on rou+ne:

Prethreaded Concurrent Server

void echo_cnt(int connfd) { int n; char buf[MAXLINE]; rio_t rio; static pthread_once_t once = PTHREAD_ONCE_INIT; Pthread_once(&once, init_echo_cnt); Rio_readinitb(&rio, connfd); while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { P(&mutex); byte_cnt += n; printf("thread %d received %d (%d total) bytes on fd %d\n”, (int) pthread_self(), n, byte_cnt, connfd); V(&mutex); Rio_writen(connfd, buf, n); } }

Worker thread service rou+ne:

echo_cnt.c