Process Management - Synchronization
Process Management - Synchronization
1
Areas of OS Responsibility
Hardware
CPU (managed by processes)
Memory
I/O Devices
File Systems
Security
Networking
2
Inter-Process Communication
Processes executing concurrently in the operating
system may be either independent processes or
cooperating processes
Processes frequently need to communicate with other
processes.
In general, data is Shared between processes to be
able to communicate with each other.
In this case, we need to apply mutual exclusion to
avoid Race Conditions.
Can processes communicate in a different way?
Yes, through Message Passing
But, not always efficient.
3
Race Conditions
Issue arises when more than one process
share some resource. Correct outcome
depends on relative ordering of events.
5
Critical Sections
Non-Atomic operations Can be Interrupted
Example:
Inserting an element in a linked-list
if (head == NULL)
head = new_elem ;
else
tail next = new_elem ;
tail = new_elem ;
6
Mutual Exclusion
The critical-section problem is to design a protocol that
the processes can use to cooperate.
If interrupted between test and assignments, elements
can get lost
Need to make test and assignments atomic
Use Mutual Exclusion (Mutex) locks
Lock resources by putting lock/unlock operations around
critical sections
Example:
mutex_lock(&lock);
if (head == NULL)
head = new_elem ;
else
tail next = new_elem ;
tail = new_elem ;
mutex_unlock(&lock);
7
Requirements for efficient
solution of Race Conditions
No two process simultaneously in their
critical section.
No assumption about speeds or the
number of CPUs.
No process running outside its critical
section may block other processes.
No process should have to wait forever to
enter its critical section.
8
Mutual Exclusion
Interrupt Control
Disable interrupts prior to entering critical
section/ Enable on leaving
No other thread of control can execute this
code while interrupts are disabled on a single
processor
Limitations:
Can’t be allowed for user processes security
Can’t work for multiple processors
9
Mutual Exclusion with Busy Waiting
1. Test and Set (TSL) instruction
Atomically tests a variable and sets it
Example:
enter_region:
Tsl register, flag
Cmp register, #0
Atomic This type of lock is called a
instruction Jnz enter_region Spin Lock
ret
Tests the previous value of the
leave_region: lock
move flag, #0
ret
Works on multiple processors
Busy waiting (consumes CPU time)
10
Mutual Exclusion
Notes:
InterruptControl and TSL instructions require
special hardware features.
11
Solution to Critical-Section
Problem
Mutual Exclusion
Progress
Bounded Waiting
12
Mutual Exclusion with Busy Waiting
2. Software-based lock: 1st attempt
// 0: lock is available, 1: lock is held by a process
int flag = 0;
mutex_lock() mutex_unlock()
{ {
while (flag == 1) flag = 0;
; // spin wait }
flag = 1;
}
Idea: use one flag, test then set, if unavailable,
spin-wait (or busy-wait)
Why it doesn’t work?
Not safe: both processes can be in critical section
13
Mutual Exclusion with Busy Waiting
2. Software-based locks: 2nd attempt
// 1: a process wants to enter critical section, 0: it
doesn’t
int flag[2] = {0, 0};
mutex_lock() mutex_unlock()
{ {
flag[self] = 1; // I need lock // not any more
while (flag[1- self] == 1) flag[self] = 0;
; // spin wait }
}
17
Mutual Exclusion without Busy Waiting
1. Wakeup and Sleep
Previous techniques
waste CPU time
Unexpected effects (Priority Inversion
Problem)
19
Mutual Exclusion without Busy Waiting
2. Semaphores
Defined by two operations on semaphore s:
down (Semaphore S) // Acquire Resource , atomic operation
{ S--;
if (S < 0) {add this process to waiting list;
block();
}
}
0 1 N-1
Producer Consumer
22
Unconstrained
Access!!
23
Mutual exclusion on
shared buffer
System
calls
Ensures producer
blocks if buffer full
Ensures consumer
blocks if buffer empty
24
Mutual Exclusion without Busy Waiting
3. Monitors
Semaphores may seem to be easy to implement
but you can end up with a piece of code with lots
of downs & ups functions embedded within your
code.
This is error prone.
To make it easier to write correct programs, a
higher level synchronization primitive called a
Monitor was proposed.
A Monitor hides mutual exclusion.
25
Mutual Exclusion without Busy Waiting
3. Monitors
A Monitor:
is a collection of procedures, variables, and data
structures that are all grouped together in a special
kind of module or package.
Processes may call the procedures in a
monitor whenever they want to,
but they cannot directly access the monitor's
internal data structures from procedures declared
outside the monitor. 26
Mutual Exclusion without Busy Waiting
3. Monitors
Only one process can be active in a monitor at
any instant.
28
Language
Construct
Less Error-Prone
29
Problems of Monitors:
Not supported by most languages
Can’t work for distributed systems with multiple
CPUs each having its private memory.
30
Recall: Inter-Process Communication
Processes frequently need to communicate
with other processes.
In general, data is Shared between
processes to be able to communicate with
each other.
Inthis case, we need to apply mutual exclusion to
avoid race conditions.
Can processes communicate in a different
way?
Yes, through Message Passing
But, not always efficient.
31
Message Passing
Can be used for synchronization
Based on send and receive operations
send and receive are system calls.
For messages with large data:
Canbe very slow and inefficient than shared
memory.
32
Message Passing
Move data between processes p: dest process id
m: message
Sending a message:
send(p,m);
p: src process id
Receiving a message m: message
receive(p,m);
33
May block if there are
no empty messages
available
34