370 #5 - Concurrency, Interprocess Communication and Deadlock

122 cards   |   Total Attempts: 182
  

Cards In This Set

Front Back
What is the problem of concurrency?
Sharing resources - some resources can only be safely used by one thread at a time.
Race Condition
Any situation where the order of execution of threads can cause different results. Programmers have no control.
How many threads do we want to be active in a critical section?
Only one.
Mutual Exclusion
Ensuring only one thread is active in a critical section.
Two causes of starvation due to mutual exclusion
  • Deadlock
  • Indefinite postponement - priority too low or just unlucky
Spin-Lock // Busy Wait
Waste of time - no work is actually being executed but the processor is being used and the process's time slice is being used up.
Question 7
What are the three problems with this attempt at locking?
  1. It's a spin-lock/busy wait
  2. No control over who gets the lock when it is freed - no queue or priority
  3. It doesn't work! Thread knows that locked is now false and so is about to set locked to true but it's time slice ends before it can do so (time between while loop and setting locked value), which it will do as soon as it starts running again. While that process is waiting, another thread comes along and sees locked is false and sets it to be true. Thus two threads can set locked to be true, resulting in it no longer being a critical section.
Peterson's Solution to attempt 1
Answer 8
  • Only works on shared memory multiprocessors if instruction reordering can be turned off, otherwise need hardware help with memory barriers
  • Two writes don't get interleaved at some minimum write size, the hardware allows only one processor access at a time.
  • Only for two threads where flag and turn are shared between the two threads (i and j)
Problem with Peterson's Solution
Aside from all of the restrictions and that it only works with two threads, there is still a busy wait.
Bakery Algorithm - solves general case for more than two threads
Each thread is given a number indicating when it requests the lock i.e. scheduling number. These are not unique because they can be accessed concurrently, so we append the pid to it to make it unique. This number orders the access for the resource.
Interrupt Priority Level - way to solve Peterson's solution problem with gap between checking locked value and setting locked value
  • Raise the interrupt priority level to stop any other process from running while the lock is being tested/checked
  • Doesn't allow preemption of processor - thread is not swapped out while running the code testing the lock
  • Not used anymore due to disadvantages
3 Disadvantages of Interrupt Priority Level
  1. Heavy-handed: not all processes at the current interrupt priority level need to be stopped
  2. Doesn't work efficiently on multiprocessors
  3. A message requesting the IPL change must be sent to all processors, in some circumstances all other processors must wait
Test and Set - solution using hardware
Answer 13
  • Atomic/indivisible instructions used that appear interruptable - if this instruction is doing something, this entire instruction will execute without it's state being altered or being accessed by an outside thread
  • testAndSet(lockVariable) - returns the value of that variable as it currently is and simultaneously sets the variable to be true
  • Works for multiprocessors
  • Still results in a busy wait
Fairness of Spin Locks without Priorities
  • Each thread can be made to wait while another thread gets access to the resource more than once
  • Threads do not get access before any threads that request it later
  • Indefinite postponement is possible
  • A queue would help
Fairness of Spin Locks with Priorities
  • Should threads with higher priorities get prior access to resources?
  • Makes the priority mechanism more effective
  • Increases the chance of indefinite postponement