Lecture 5 - Synchronization Flashcards
What is the difference between competing and cooperating concurrent processes?
What are each type’s properties?
How does the OS deal with each?
Competing
• Processes that do not work together cannot affect the execution of each other, but they can compete for devices and other resources
Properties: Deterministic; reproducible
* can stop and restart without side effects
* can proceed at arbitrary rate
->OS carefully allocates resources and isolates processes from each other
Cooperating
• Processes that are aware of each other, and directly (by exchanging messages) or indirectly (by sharing a common object) work together, may affect the execution of each other
Properties: Non-deterministic; May be irreproducible
* Share a common object or exchange messages
* Subject to race conditions –coming up
OS provides mechanisms to share resources
What’s the main problem with competing processes?
Some processes can starve
Why would we want Cooperating processes?
- Sharing resources (like a global task queue)
- Dividing a job into pieces and executing concurrently to run faster
- Solve problems modularly (like piping)
What problems can occur with interleaving cooperating threads?
Race conditions: Variables could be changed by the other threads.
How do you avoid Race Conditions?
Prohibit more than one thread from reading and writing shared data at the same time through:
Mutual exclusion: When one process is reading or writing a shared data, other processes should be prevented from doing the same
What is a Critical Section in a process?
Part of the processes that accesses shared data.
If we arrange the processes runs such that no two programs are in their critical sections at the same time, race conditions can be avoided.
(EXAM QUESTION) What are the 4 conditions for multiple programs to cooperate correctly and efficiently?
- No two processes may be simultaneously in their critical sections
- No assumptions be made about speeds or number of CPUs
- No process running outside its critical section may block other processes
- No process should have to wait forever to enter its critical section
(EXAM QUESTION) What are 3 requirements for critical sections?
- Critical region execution should be atomic i.e., its runs “all or none”
- Should not be interrupted in the middle
- For efficiency, we need to minimize the length of the critical sections
Most inefficient scenario: Whole program is a critical section – no multi-programming!
Why would disabling all interrupts work as a solution to concurrency in critical sections?
Why is it not a good idea?
Works:
• Because interrupts cause out-of-order execution due to interrupt servicing
• With interrupts disabled, a process will run until it yields the resource voluntarily
Not a good idea:
• Because OS won’t allow a user process to disable all interrupts – OS operation will be hindered too!
• Does not work on multiprocessors
What’s the problem with Strict Alternation?
Because one process couldn’t get into the critical section over and over unless the other process does so as well. If the other process isn’t interested in entering critical section, the first process starves.
What’s busy waiting? When should it be used?
What’s a spin lock?
When you’re continuously waiting for another variable, which sucks up the CPU time. Should be used when the expected wait is short.
A spin lock is a lock that uses busy waiting
Kernel level multi-threads are used for concurrent processing in a data-parallel application. The application has a critical section. If a single thread of execution is used the critical section takes 2 seconds and other parts take 6 seconds. We have a large dataset so we use 2 threads. What is the run time? We have an even larger dataset so we use 8 threads. What is the run time? Assume single CPU and ideal time sharing (no overhead). All threads starting at the same time.
2 threads: 18 seconds
Why does this mutual exclusion solution not work?
/* process 0 */ while (turn != 0); /* critical section */ turn = 1;
/* process 1 */ while (turn != 1); /* critical section */ turn = 0;
A Single key shared by the two processes
Each have their own key to the critical section, so they only grant access to themselves!
What’s a livelock?
Pretty much like a deadlock, but useless programs running instead of blocking (in mutual exclusion yielding)
Write Peterson’s Algorithm:
Why does it work?
/* process 0 */ flag[0] = true; turn = 1; while (flag[1] &&turn == 1); / * critical section */ flag[0] = false; /* remainder */
/* process 1 */ flag[1] = true; turn = 0; while (flag[0] &&turn == 0); /* critical section */ flag[1] = false; /* remainder */
It works because both while locks are exclusive because of the turn variable. No deadlock and live-lock can’t occur.
What are two benefits hardware synchronization can provide?
- > make the solution more efficient
- > make it scalable to more processes?
What does the CPU instruction:
TSL RX, LOCK
do?
Read the contents of memory location LOCK into register RX and stores a non-zero value at memory location LOCK.
This makes operation of reading and writing indivisible -atomic.
What are the advantages of Machine Instruction Mutual Exclusion?
- applicable to any number of processes
- can be used with single processor or multiple processors that share a single memory
- simple and easy to verify
- can be used to support multiple critical sections, i.e., define a separate variable for each critical section
What are the disadvantages of Machine Instruction Mutual Exclusion?
- Busy waiting is employed–process waiting to get into a critical section consumes CPU time
- Starvation is possible–selection of entering process is arbitrary when multiple processes are contending to enter