Semaphors, Mutexes, Monitors, Condition Variables
Difference between Mutex and Semaphore
1. A mutex is binary semaphore 2. A mutex can only be used on one thread, while a semaphor can be used across multiple processes/threads 3. Only the thread with the lock can unlock the mutex, but any thread can increment the semaphore can be considered less secure .
What are some logical errors can could happen with condition variables?
1. Failing to lock the mutex before calling pthread_cond_wait() may cause it NOT to block. 2. Failing to unlock the mutex after calling pthread_cond_signal() may not allow a matching pthread_cond_wait() routine to complete 3. Calling pthread_cond_signal() before calling pthread_cond_wait().
What are the requirements for mutual exclusion?
1. Only one process at a time is allowed into the critical section 2. A process that halts in it's non critical section must do so without intefering w/other processes 3. It must not be possible for a process requiring access to a critical section to be delayed indefinitely --> no possible deadlock or starvation 4. When no process is in a critical section, any process that requests entry must enter without delay 5. No assumptions are made about relative process speeds or number of processors 6. A process is inside its critical section for a finite time only
What are the two things needed to implement a Semaphore?
1. SemWait and SemSignal, need to be able to increment and decrement the variable 2. Functions need to be implemented atomically 3. A queue
What are the three approaches to maintain mutual exclusion?
1. Software Approach - No support from OS/programming languages 2. Machine instructions 3. Support from OS/Programming Language
What are the two ways to initialize a condition variable?
1. Statically, when it is declared 2. Dynamically, with the pthread_cond_init() routine.
What is a Race Condition?
A situation in which multiple threads or processes write to a shared data item and the final result depends on the relative timing of their execution
What is a Deadlock?
A situation in which two or more competing actions are waiting for the other to finish and thus neither does.
Why do we need to lock and unlock the mutex when dealing with condition variables?
Need to lock the mutex before calling wait() Need to unlock the mutex before calling signal()/broadcast() Need to call wait() before calling signal()
What are the positives and negatives for Disabling interrupts?
Positives: guarantees mutual exclusion Negatives: efficiency is down graded does not work in a multithreaded system
Semaphore
Used for signaling - Needs to be able to Transmit a signal via semSignal and receive a signal via semWait. -Semaphores are usually implemented via an integer value -semWait(s) --> decrements the integer value, if the value is less than 0, the process is blocked. -semSignal(s) --> increments the integer value, if the value is greater than 0, the process can run. The value shows the number of processes that can run or are being blocked.
Mutex
a lock that protects access to a shared data resource only one thread can lock a mutex variable at any time threads must take turns accessing the shared data structure a binary semaphore
Condition variables
a way to synchronize threads condition variable used with a mutex lock proper locking and unlocking of the mutex is important can broadcast to all threads or signal to one thread
What does "atomically" mean?
actions need to be operated in an "all or none"
Monitors
allows programmers to put a monitor lock on any object it allows the ability to block a process from entering a critical a section until a condition is true tasks can signal each other notifying that the condition has become true
pthread_cond_broadcast()
if you want to signal more than one thread
Waiting and signaling
wait() blocks the calling thread until the specified condition is signaled signal() signal's another thread which is waiting on the condition variable
Livelock
A thread often acts in response to the action of another thread. If the other thread's action is also a response to the action of another thread, then livelock may result. As with deadlock, livelocked threads are unable to make further progress. The main difference of livelock from deadlock is that threads are not going to be blocked, instead they will try to respond to each other continuously.
What are the entercritical and exitcritical functions?
Given functions to maintain mutual exclusion