Transcript Part II

Chapter 2
Processes and Threads
Part II
2.1 Processes
2.2 Threads
2.3 Interprocess communication
2.4 Classical IPC problems
2.5 Scheduling
1
2.3 Interprocess Communication
2.3.1 Race Conditions
Two processes want to access shared memory (variable in) at same time
and they may put their files in the same slot
Race Condition: When more than one process shares the same memory
and the final result depends on the order of execution
2
2.3.2 Critical Regions (1)
Four conditions to provide mutual exclusion
1.
2.
3.
4.
No two processes simultaneously in critical
region
No assumptions made about speeds or
numbers of CPUs
No process running outside its critical region
may block another process
No process must wait forever to enter its
critical region
3
2.3.2 Critical Regions (2)
Mutual exclusion using critical regions
4
2.3.3 Mutual Exclusion with Busy Waiting (1)
• Disabling interrupts
– Each process disables all interrupts just after
entering its critical region(CR) and enables all
interrupts before leaving it
– Unsafe for user processes able to turn off all
interrupts
• Lock Variables
– Having a shared variable (lock), initially 0
– Before entering its CR the process checks lock, if
lock is1, it waits; if lock is 0, it sets lock to 1 and
enters its CR.
– Before leaving its CR, the process resets lock to 0
– It does not work, why?
5
2.3.3 Mutual Exclusion with Busy Waiting (2)
Strict Alternation
Proposed solution to critical region problem
(a) Process 0.
(b) Process 1.
Variable turn is shared by both processes
6
2.3.3 Mutual Exclusion with Busy Waiting(3)
Peterson's solution
7
Mutual Exclusion with Busy Waiting (4)
TSL(Test and Set Lock) Machine instruction
Format: TSL RX, LOCK
// LOCK is a shared variable, RX: a register
Function: RX  LOCK
LOCK  1
Since it is a machine instruction, so it is always executed atomically.
8
2.3.5 Semaphores (1)
• Definition:
Class Semaphore {
Private:
value: integer;
list: list of processes
Public:
void down() {
value = value –1;
if value < 0 {
list.insert(this process)
sleep();
}
}
void up() {
value = value + 1;
if value <= 0 {
p = list.remove();
wakeup(p);
}
}
9
Semaphore (2)
• Semaphores are system facilities
• The up() and down() operations are system
calls.
• The operating system guarantees that the
two operations are atomic operations. I.e.,
when an up() or down() is being executed,
no other up() and down() would be executed
on the same semaphore.
10
Semaphore: application
• Control execution sequence
– E.g., three processes: p1, p2 and p3, and p3’s stmnt3
must be executed after p1’stmnt1 and p2’s stmnt2 have
completed.
Semaphore seq = 0;
P1
p2
…
…
Stmnt1;
stmnt2
Seq.up();
Seq.up();
…
…
p3
…
Seq.down();
Seq.down();
stmnt3
• Mutual exclusion
Semaphore mutex = 1;
P1
Mutex.down();
Critical region;
Mutex.up();
p2
mutex.down();
critical region
mutex.up()
11
2.3.5 Semaphores: application
The producer-consumer problem using semaphores
12
2.3.5 Implementations of Semaphore (3)
• Disable all interrupts
• Use Peterson’s algorithm and treat the up()
and down() operations as critical regions.
13
2.3.6 Mutexes
Implementation of mutex_lock and mutex_unlock
14
2.3.8 Message Passing (1)
• Processes can communicate with each other via
message passing.
• The operating system provides two primitive
functions:
– Send(dest, &message);
– Receive(source, &message);
• Design issues:
– Sender waits for acknowledgement, no buffer needed
– When sender does not wait, the OS must maintain a
buffer
– Message scramble and loss
15
2.3.8 Message Passing (2)
The producer-consumer problem with N messages
16
2.4 Classical IPC Problems
2.4.0 The producer and consumer problem
17
2.4.1 Dining Philosophers (1)
• Philosophers eat/think
• Eating needs 2 forks
• Pick one fork at a time
18
2.4.1 Dining Philosophers (2)
A nonsolution to the dining philosophers problem
Deadlock? How? How to prevent deadlock
19
2.4.2 The Readers and Writers Problem (1)
• Multiple readers and writers need to access
the database
• Readers only read the content
• Writers modify the database
• Multiple readers may read at the same time
• Only one writer is allowed to write at a time
• When a writer is writing, no reader is allowed
to read
20
2.4.2 The Readers and Writers Problem (2)
21