CS 25200: Systems Programming Lecture 26: Classic Synchronization Problems Dr. Jef Turkstra 2018 Dr. Jeffrey A. Turkstra 1
Announcements Lab 5 posted this evening Web server Password protection SSL cgi-bin File listings etc 2018 Dr. Jeffrey A. Turkstra 2
Lecture 26 Producer/consumer problem Readers and writers problem 2018 Dr. Jeffrey A. Turkstra 3
sem_wait (atomic) void sem_wait(semaphore S) { if (s->count > 0) { s->count--; return; add(s->q, current_thread); sleep(); // re-dispatch return; 2018 Dr. Jeffrey A. Turkstra 4
sem_post (atomic) void sem_post(semaphore S) { if (isempty(s->q)) { s->count++; else { thread = removefirst(s->q); wakeup(thread); // put thread on ready q return; 2018 Dr. Jeffrey A. Turkstra 5
Producer/consumer Suppose we have a circular bufer that is of a fixed sizee Want multiple threads to communicate using this bufer Common paradigm in device drivers and pipes Although usually have a pool of bufers instead of a single bufer 2018 Dr. Jeffrey A. Turkstra 6
Circular bufer 20 byte bufer with 14 elements G H I J K L M N A B C D E F Tail Head 2018 Dr. Jeffrey A. Turkstra 7
Interface enqueue() - add an item to the queue Block if queue is full dequeue() - remove an item from the queue Block if empty Need condition synchronizeation empty and full Do we need mutual exclusion? 2018 Dr. Jeffrey A. Turkstra 8
Implementation #include <pthread.h> #defne MAXSIZEE 32 typedef struct { char queue[maxsizee]; int head; int tail; pthread_mutex_t mutex; sem_t emptysem; sem_t fullsem; BoundedBufer; 2018 Dr. Jeffrey A. Turkstra 9
Initialization BoundedBufer ; pthread_mutex_init(& ->mutex, NULL); sem_init(& ->emptysem, 0, 0); sem_init(& ->fullsem, 0, MAXSIZEE); ->head = 0; ->tail = 0; 2018 Dr. Jeffrey A. Turkstra 10
enqueue() and dequeue() void enqueue(int val) { sem_wait(& ->fullsem); pthread_mutex_lock(& ->mutex); ->queue[tail] = val; ->tail = (tail + 1) % MAXSIZEE; pthread_mutex_unlock(& ->mutex); sem_post(& ->emptysem); int dequeue() { sem_wait(& ->emptysem); pthread_mutex_lock(& ->mutex); int val = queue[head]; head = (head + 1) % MAXSIZEE; pthread_mutex_unlock(& ->mutex); sem_post(& ->fullsem); return val; 2018 Dr. Jeffrey A. Turkstra 11
Bounded bufer notes emptysem represents the number of items in the queue fullsem represents the number of (empty) spaces in the queue If this seems backwards, you can switch them So, do we still need mutexes? 2018 Dr. Jeffrey A. Turkstra 12
Readers and writers problem Allow concurrent reads, but exclusive writes Constraints: Writers can proceed only if no active readers or writers Readers can proceed only if no active writers Need shared variables (state variables) to keep track of who is reading and writing Used in: databases, lookup tables, dictionaries, etc 2018 Dr. Jeffrey A. Turkstra 13
Interface read_lock() - obtain access to read Wait if writer holds lock read_unlock() - signal completion of reading write_lock() - obtain access to write Wait if any readers or writer holding the lock write_unlock() - signal completion of writing 2018 Dr. Jeffrey A. Turkstra 14
Implementation typedef struct { int readers; sem_t semaccess; pthread_mutex_t mutex; rwlock_t; rwlock_t rwlock; sem_init(&rwlock->semaccess, 0, 1); pthread_mutex_init(&rwlock->mutex); 2018 Dr. Jeffrey A. Turkstra 15
Readers void read_lock() { pthread_mutex_lock(&rwlock->mutex); rwlock->readers++; if (rwlock->readers == 1) { // First reader, o tain access sem_wait(&rwlock->semaccess); pthread_mutex_unlock(&rwlock->mutex); void read_unlock() { pthread_mutex_lock(&rwlock->mutex); rwlock->readers--; if (rwlock->readers == 0) { // Last reader, allow one writer to proceed sem_post(&rwlock->semaccess); pthread_mutex_unlock(&rwlock->mutex); 2018 Dr. Jeffrey A. Turkstra 16
Writers void write_lock() { sem_wait(&rwlock->semaccess); void write_unlock() { sem_post(&rwlock->semaccess); 2018 Dr. Jeffrey A. Turkstra 17
Notes Mutexes and semaphores are fair Thread that is queued first wakes up first Spin locks are not fair This implementation sufers from potential starvation of writers May take a long time for a writer to be able to write if readers is always > 0 2018 Dr. Jeffrey A. Turkstra 18
Questions? 2018 Dr. Jeffrey A. Turkstra 19