Week 3
Interprocess Communication (IPC)
Processes need to be able to communicate to:
-Pass information which is relevant for a given multiprocess task
-Prevent trying to access exclusive resources simultaneously
-Work with an appropriate sequencing of events when there are dependencies (eg if A can
only run then B is finished, then B must be running first)
Inter Thread
Threads share memory so they can communicate through this.
However, they also need to prevent trying to access resources simultaneously, and work
with appropriate sequencing.
Race Conditions
Takes place when two or more processes are reading or writing some shared data, the final
result depends on which processes are running when. This can happen with processes as
they have some shared stored such as main memory.
Mutual Exclusion
A technique commonly used to avoid RC. It identifies the parts of the program - called critical
regions - where processes access shared resources and ensure that these processes are
not in their critical regions at the same time.
Critical Region Rules:
1. No two processes may be simultaneously inside their critical regions
2. No assumptions may be made about speeds to the number of CPUs
3. No process running outside its critical region may block other processes
4. No process should have to wait forever to enter its critical region
IPC with Busy Waiting
Lock Variable
● Var shared among processes
● When value is 0, means no process is in critical region
● Before process enters critical region, must check the lock
● Busy waiting as the while loop is waiting for the lock to change
, This has a problem as another process can read the lock before the original one has
changed its value. This allows two processes to proceed to their critical regions.
Strict Alternation
● Two processes share a variable “turn”. Program starts with it set to 0
● The process will only be allowed to enter its critical region when the variable is set to
its turn. Eg process a can only perform its critical tasks when turn is 0
● After one process has left the critical region, the variable will be set to a different
value, allowing another process to enter cr
● Guarantees that no matter the order in which the two processes are scheduled,
they’ll never be in critical region at the same time
● Only reasonable to apply strict alternation when the time a process spends waiting is
short
○ If a process is much slower than the other one, the fast one will be blocked for
a long time whilst the slow one is still in its non-critical region and cannot set
“turn” back to the other process.
○ Doesn’t comply with the rule that no process running outside its critical region
may block other processes
Peterson Algorithm
Before entering a critical region, a process indicates its interest in entering but offers the
opportunity to another one first. It calls enter_region with its own id number and might have
to wait until it is safe to enter. After leaving the region, the process calls leave_region to
notify it is done.
Interprocess Communication (IPC)
Processes need to be able to communicate to:
-Pass information which is relevant for a given multiprocess task
-Prevent trying to access exclusive resources simultaneously
-Work with an appropriate sequencing of events when there are dependencies (eg if A can
only run then B is finished, then B must be running first)
Inter Thread
Threads share memory so they can communicate through this.
However, they also need to prevent trying to access resources simultaneously, and work
with appropriate sequencing.
Race Conditions
Takes place when two or more processes are reading or writing some shared data, the final
result depends on which processes are running when. This can happen with processes as
they have some shared stored such as main memory.
Mutual Exclusion
A technique commonly used to avoid RC. It identifies the parts of the program - called critical
regions - where processes access shared resources and ensure that these processes are
not in their critical regions at the same time.
Critical Region Rules:
1. No two processes may be simultaneously inside their critical regions
2. No assumptions may be made about speeds to the number of CPUs
3. No process running outside its critical region may block other processes
4. No process should have to wait forever to enter its critical region
IPC with Busy Waiting
Lock Variable
● Var shared among processes
● When value is 0, means no process is in critical region
● Before process enters critical region, must check the lock
● Busy waiting as the while loop is waiting for the lock to change
, This has a problem as another process can read the lock before the original one has
changed its value. This allows two processes to proceed to their critical regions.
Strict Alternation
● Two processes share a variable “turn”. Program starts with it set to 0
● The process will only be allowed to enter its critical region when the variable is set to
its turn. Eg process a can only perform its critical tasks when turn is 0
● After one process has left the critical region, the variable will be set to a different
value, allowing another process to enter cr
● Guarantees that no matter the order in which the two processes are scheduled,
they’ll never be in critical region at the same time
● Only reasonable to apply strict alternation when the time a process spends waiting is
short
○ If a process is much slower than the other one, the fast one will be blocked for
a long time whilst the slow one is still in its non-critical region and cannot set
“turn” back to the other process.
○ Doesn’t comply with the rule that no process running outside its critical region
may block other processes
Peterson Algorithm
Before entering a critical region, a process indicates its interest in entering but offers the
opportunity to another one first. It calls enter_region with its own id number and might have
to wait until it is safe to enter. After leaving the region, the process calls leave_region to
notify it is done.