DOC PREVIEW
Chico CSCI 372 - Thread Synchronization

This preview shows page 1-2-3-20-21-22-41-42-43 out of 43 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Thread SynchronizationThread Address SpaceProducer-ConsumerMutex Thread Operationspthread_mutex_initStatic InitializerProtecting CS with Mutexget_item/put_item – Version 1producer-consumer/main – Version 1sched_yieldsched_yield – Version 2Semaphore – Version 3Version 3 - Problemsproducer – Version 4consumer – Version 4Version 4 – ProblemsProducer – Version 5consumer – Version 5Version 5 – ProblemCondition Variablescond_wait and cond_signalInterleavingsCondition Variable OperationsRoundabout Way to Protect CSDecs – Condition Version 6producer – Condition Version 6consumer – Condition Version 6main – Condition Version 6Thread Signal Delivery (1)Thread Signal Delivery (2)pthread_killpthread_sigmaskcatch_siguser1, Version 7producer (top) – Signal Version 7producer (bottom) – Signal Version 7Consumer – Signal Version 7main – Signal Version 7sigwaitsiguser1_thread – Version 8producer (top) – Version 8producer (bottom) – Version 8consumer – Version 8main – Version 8Thread Synchronization•POSIX.1c supports–mutexes – for short-term locking–condition variables – for waiting on events of unbounded duration•Use of semaphores to synchronize threads is also possible•Signal handling in threaded programs presents complications which can be reduced if signal handlers are replaced with dedicated threadsThread Address Space•Threads are created within the address space of a process and share resources such as static variables and open file descriptors•When threads use shared resources, they must synchronize their activities to produce consistent results.Producer-Consumer•Examples:–Producer threads manufacture messages and deposits them in a FIFO queue – Consumer threads remove data items from queue–Producer threads generate print requests – Consumer threads are the printers–Scheduling queues in a multiprocessor system–Network message buffers used when routing messages through intermediate nodes of a WAN•Unbounded buffer – queue size is unlimited•Bounded buffer – queue is of unbounded size•Zero-sized buffer – there is no queue (rendezvous)Mutex Thread OperationsSYNOPSIS#include <pthread.h>int pthread_mutex_init(pthread_mutex_t *mutex, const pthread mutexattr_t *attr);int pthread_mutex_destroy(pthread_mutex_t *mutex);int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex);int pthread_mutex_unlock(pthread_mutex_t *mutex);•Program must declare variable of type pthread_mutex_t•Typically, mutex variables are static available to all threads in the processpthread_mutex_init#include <stdio.h>#include <pthread.h>#include <string.h>#include <errno.h>pthread_mutex_t my lock;if(!pthread_mutex_init(&my_lock, NULL);perror(“Could not initialize my_lock”);Static Initializer#include <pthread.h> pthread_mutex_t my_lock=PTHREAD_MUTEX_INITIALIZER•Static Initializer has following advantages over pthread_mutex_init–Usually more efficient–Guaranteed to be executed exactly once before any thread begins executionProtecting CS with Mutex#include <pthread.h>pthread_mutex_t my_lock=PTHREAD_MUTEX_INITIALIZERpthread_mutex_lock(&my_lock);/* critical section */pthread_mutex_unlock•Mutex must be released by the same thread that acquired it•Holds mutex only for a short period of time•Threads waiting for events of unpredictable duration, should use semaphores or condition variablesget_item/put_item – Version 1void get_item (int *itemp){ pthread_mutex_lock(&buffer_lock); Get *itemp from buffer; pthread_mutex_unlock(&buffer_lock); return; }void put_item (int item){ pthread_mutex_lock(&buffer_lock); Put item in buffer; pthread_mutex_unlock(&buffer_lock); return; }•Items will be placed in buffer even when the buffer is full•Items will be retrieved from buffer even when buffer is emptyproducer-consumer/main – Version 1void * producer(void *arg1){ int i; for(i <= SUMSIZE; i++) put_item(i*i); return NULL; }void *consumer(void *arg2){ int i, myitem; for (i = 1; i<- SUMSIZE; i++) get_item(&myitem); sum += myitem; return NULL; void main(void){ pthread_t prodtid; pthread_t constid;pthread_create(&constid,NULL,consumer,NULL)); pthread_create(&prodtid,NULL,producer,NULL));pthread_join(constid,NULL); pthread_join(prodtid,NULL); print out sum = sum of the squares; }sched_yield#include <sched.h>int sched_yield(void);•Returns 0 on success or –1 and sets errno on failure•Causes calling thread to lose the processorsched_yield – Version 2void *producer(void *arg1){ int i; for (i = 1; i <= SUMSIZE; i++) put_item(i*i); sched_yield(); return NULL; }void *consumer(void *arg2){ int i, myitem; for (i =1;i <= SUMSIZE; i++) { get_item(&myitem); sum += myitem; sched_yield(); } return NULL; } Version 2 causes the producer and consumer to take turns in strict alternationSemaphore – Version 3void *producer(void *arg1){ int i; for (i = 1; i <= SUMSIZE; i++) { sem_wait(&slots); put_item(i*i); sem_post(&items); }return NULL; }void *consumer(void *arg2)int i, myitem;{ for (i =1;i <= SUMSIZE; i++){ sem_wait(&items); get_item(&myitem); sem_post(&slots); sum += myitem;} return NULL; }Version 3 - Problems•Version 3 may not work correctly •Consider 1 producer and 2 consumer threads•The producer terminates after producing SUMSIZE items•Both consumers try to process SUMSIZE items•One or both consumers will block on empty bufferproducer – Version 4void *producer(void *arg1){ int i; for (i = 1; i <= SUMSIZE; i++) { sem_wait(&slots); put_item(i*i); sem_post(&items); } pthread_mutex_lock(&my_lock); producer_done = 1; pthread_mutex_unlock(&my_lock); return NULL; }void *consumer(void *arg2){ int myitem; for(;;) { pthread_mutex_lock(&mylock); if (producer_done) { pthread_mutex_unlock(&my_lock); if(sem_trywait(&items)) break; } else { pthread_mutex_unlock(&my_lock); sem_wait(&items); } get_item(&myitem); sem_post(&slots); sum += myitem; } return NULL; }consumer – Version 4Version 4 – Problems •After all items in the buffer have been consumed, one or both of the consumers can block on sem_wait(&items) before the producer sets producer_done to 1.•When the producer finishes, if a consumer is waiting on the items semaphore queue, there is no way to unblock it


View Full Document

Chico CSCI 372 - Thread Synchronization

Download Thread Synchronization
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Thread Synchronization and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Thread Synchronization 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?