{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Semaphores - Announcements Cooperating Processes Operating...

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

View Full Document Right Arrow Icon
Announcements
Background image of page 1

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

View Full Document Right Arrow Icon
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?
Background image of page 2
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?
Background image of page 3

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

View Full Document Right Arrow Icon
The Critical-Section Problem Consider a system: n processes {P 0 , 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
Background image of page 4
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.
Background image of page 5

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

View Full Document Right Arrow Icon
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
Background image of page 6
Critical Section Properties Bounded Waiting After a process has made a request to enter it’s 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
Background image of page 7

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

View Full Document Right Arrow Icon
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);
Background image of page 8
#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); }
Background image of page 9

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

View Full Document Right Arrow Icon
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
Background image of page 10
pthread_mutex_trylock /* Finding k matches in a list */
Background image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}