Semaphores

Semaphores - Announcements Cooperating Processes Operating...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Announcements Cooperating Processes Operating systems allow for the creation and concurrent execution of multiple processes & threads eases program complexity increases efficiency Can they work together? How? Messages? What about shared memory? Problems with concurrent execution Concurrent processes (or threads) often need to share data (maintained either in shared memory or files) and resources If there is no controlled access to shared data, some processes will obtain an inconsistent view of this data The action performed by concurrent processes will then depend on the order in which their execution is interleaved Consider two threads one doing x++ and the other doing x--. How many different values for x? The Critical-Section Problem Consider a system: n processes {P , P 1 , , P n-1 } Each process has a critical section changing common values updating tables etc. Access to those variables must be safe mutually exclusive The Critical-Section Problem A solution must satisfy 3 conditions: Mutual exclusion Progress Bounded waiting No assumptions can be made about speed Solutions execute some entry code and some exit code surrounding critical section. Critical Section Properties Mutual Exclusion At any time, at most one process can be in its critical section (CS) Progress Only processes that are not executing in their CS can participate in the decision of who will enter next in the CS. This selection cannot be postponed indefinitely Critical Section Properties Bounded Waiting After a process has made a request to enter its CS, there is a bound on the number of times that the other processes are allowed to enter their CS otherwise the process will suffer from starvation Of course there must also be no deadlock pthread_mutex int pthread_mutex_init( pthread_mutex_t *mutex_lock, const pthread_mutexattr_t *lock_attr); int pthread_mutex_lock( pthread_mutex_t *mutex_lock); int pthread_mutex_unlock( pthread_mutex_t *mutex_lock); int pthread_mutex_trylock( pthread_mutex_t *mutex_lock); #include <pthread.h> void *find_min(void *list_ptr) pthread_mutex_t minimum_value_lock; int minimum_value, partial_list_size; main(){ minimum_value = MIN_INT; pthread_init(); pthread_mutex_init(&minimum_value_lock, NULL); /*inititalize lists etc, create and join threads*/ } void *find_min(void *list_ptr){ int *partial_list_ptr, my_min = MIN_INT, i; partial_list_ptr = (int *)list_ptr; for (i = 0; i < partial_list_size; i++) if (partial_list_ptr[i] < my_min) my_min = partial_list_ptr[i]; pthread_mutex_lock(minimum_value_lock); if (my_min < minimum_value) minimum_value = my_min; pthread_mutex_unlock(minimum_value_lock); pthread_exit(0); } Locking Overhead Serialization points Minimize the size of critical sections Be careful Rather than wait, check if lock is available pthread_mutex_trylock If already locked, will return EBUSY Will require restructuring of code pthread_mutex_trylock...
View Full Document

Page1 / 49

Semaphores - Announcements Cooperating Processes Operating...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online