Interprocess Communication: Types of Interaction
Interprocess Communication: Types of Interaction
2010
observer: reporter:
while TRUE { while TRUE { print (6)
observe; print_counter;
counter ++; counter=0; counter 7 7. is lost!
} }
counter 0
1
01.10.2010
Sharing Synchronization
P2:
critical section (CS): Part of code in a process in
P1: while TRUE {
which operations on shared resources are
<non-CS>
performed. mx_begin
while TRUE {
<CS ops>
<non-CS>
mx_end
mutual exclusion: only one process can execute a mx_begin <non-CS>
CS for a resource at a time <CS ops> }
mx_end
<non-CS>
}
2
01.10.2010
3
01.10.2010
4
01.10.2010
busy waiting used condition code register and “a” is assigned TRUE
mx_begin: test_and_set(busy);
when a process exits CS, no mechanism to
determine which other process enters next while (cc) {
indefinite waiting possible test_and_set(busy);
disabling interrupts }
mx_end: busy=FALSE;
interferes with scheduling algorithm of operating
system busy: shared variable
cc: local condition code
Semaphores Semaphores
hardware and software based solution s: semaphore variable
no busy waiting special operations:
does not waste CPU time P (wait): when entering CS: mutex_begin
semaphore is a special variable V (signal): when leaving CS: mutex_end
only access through using two special operations P(s): V(s):
special operations cannot be interrupted if (s > 0) if(anyone_waiting_on_s)
operating system carries out special operations s=s-1; activate_next_in_line;
else else
wait_on_s; s=s+1;
5
01.10.2010
Semaphores
possible deadlock scenario:
x, y: semaphore;
x=1; y=1;