Commit 891d74b4 authored by Sadman's avatar Sadman
Browse files

Add more of Chap 5

parent a9f2269d
Loading
Loading
Loading
Loading
+87 −19
Original line number Diff line number Diff line
@@ -9,19 +9,19 @@ Chapter 5 - Concurrency, Mutual Exclusion and Synchronization
- Structured application (app can be a set of concurrent processes)
- OS structure (OS is a set of processes or threads)
- Key terms:
  - critical section: A section of code within process that requires access to
  - **critical section**: A section of code within process that requires access to
    shared resources and which may not be executed while another process is in a
    corresponding section of code.
  - deadlock: A situation in wich two or more processes are unable to proceed
  - **deadlock**: A situation in wich two or more processes are unable to proceed
    because each is waiting for one of the others to do something
  - livelock: two or more processes continously change their state in response to
  - **livelock**: two or more processes continously change their state in response to
    changes in the other process(es) without doing any useful work
  - mutual exclusion: the requirement that when one process is in a critical section
  - **mutual exclusion**: the requirement that when one process is in a critical section
    that accesses shared resources, no other process may be in a critical section
    that accesses any of those shared resources
  - race condition: multiple threads or processes read and write a shared data
    item and the final result depends on the relative timeing of their execution
  - starvation: a runnable process is overlooked indefinitely by the scheduler;
  - **race condition**: multiple threads or processes read and write a shared data
    item and the final result depends on the relative timing of their execution
  - **starvation**: a runnable process is overlooked indefinitely by the scheduler;
    although it is able to proceed, it is never chosen

- Difficulties:
@@ -32,23 +32,91 @@ Chapter 5 - Concurrency, Mutual Exclusion and Synchronization
- OS Concerns:
  - Keep track of various processes
  - Allocation and deallocate resources
    - Processor time
    - Memory
    - Files
    - I/O devices
    	* Processor time
    	* Memory
    	* Files
    	* I/O devices
  - Protect data and resources
  - Functions of process must be independent of the speed of execution of other
    concurrent processes
  - Functions of process must be independent of the speed of execution of other concurrent processes

- Interaction among process:
  1. Processes unaware of each other
  2. Processes indirectly aware of each other
  3. Process directly aware of each other

---


| Degree of Awareness       | Relationship          | Influence that one Process has on the other | Potential Control problems |
|:-------------------------:|:---------------------:|:--------------------------------------------
| Process unaware           | Competition           |Results of one independent of others, timing
                                                        might be affected
| Processes indirectly aware| Cooperation by sharing|Results of one may depend on info obtained by
                                                        others, timing may be affected
| Process directly aware    | " by communication    |
|:-------------------------:|:---------------------:|:--------------------------------------------:|:--------------------------:|
| Process unaware of each other | Competition           |Results of one independent of others, timing                                                        might be affected | Mutual exclusion, Deadlock (renewable resource), starvation|
| Processes indirectly aware of each other | Cooperation by sharing|Results of one may depend on info obtained by                                                        others, timing may be affected | Mutual exlusion, deadlock (renewable resource), starvation, data coherence |
| Process directly aware of each other | " by communication | Results of one process may depend on info obtained on others, time may be affected | Deadlock(consumable resource), starvation  |
---

Concepts for competition for resources  
- Mutual exclusion
  - Critical sections
    - only one program at a time allowed in the section
    - e.g. one process at a time is allowed to send command to the printer
- Deadlock
- Starvation

### Mutual Exclusion

Requirements for MutEx:
1. **Enforcement** Only one process at a time is allowed in the critical section
    for a resource
2. A process that halts in its noncritical section must do so without interfering
    with other processes
3. No deadlock or starvation
4. A process must not be delayed to a critical section when there is no other
    process using it
5. No assumptions are made about relative process speeds or number of processes
6. A process remains inside its critical section for a finite time only

Mechanisms:
- Software: **Dekker's Algorithm** (and many others)
  - Requires no special instructions
  - Highly portable
  - Slow
- Hardware: Special instructions on ISA

Hardware Support:
- Interrupt disabling as a means for mutex:
  - A process runs until it invokes an OS service or until it is interrupted
  - Disabling interrupts guarantees mutex in a uniprocessor system
  - Porcessor is limited in its ability to interleave programs

- Special Machine instructions:
  - Performed in a single instruction cycle
  - Access to the memory ocation is blocked by any other instructions

Advantages of hardware implementatino:
  - Applicable to any number of processes on either a single or multi processors
    sharing memory
  - It is simple, and therefore easy to verify
  - It can be used to support multiple critical sections

Disadvantages:
  - Busy-waiting consumes processor time
  - Starvation is possible when a process leaves a critical section and more than
    one process is waiting
  - Deadlock (if a low priority process has the critical region and a higher prio
    needs, the higher prio process will obtain the processor to wait for the 
    critical region
  - Pipeline stalls

### Advanced Concurrency Control Mechanisms

**Semaphores**: a concurrency control mechanism based on a special variable used
    for signalling.
- If a process is waiting for a signal, it is suspend until that signal is sent
- Semaphore is a variable that has an integer value
  - May be initialized to be nonnegative
  - Wait operation decrements the value (`semWait() )
  - Signal operation increments it (`semSignal()`)
  - *No other way to access the semaphore*
  - In general, anyone can call `semWait()` and `semSignal()`
  - For a mutex, only the process with the lock can release the lock