Title: Lecture 8: Concurrency: Mutual Exclusion and Synchronization(cont.)
1Lecture 8 Concurrency Mutual Exclusion and
Synchronization(cont.)
- Advanced Operating System
- Fall 2009
2Three Environments
- There is no central program to coordinate the
processes. The processes communicate with each
other through global variable. - Special hardware instructions
- There is a central program to coordinate the
processes.
3Hardware Support
Disable interrupt CS Enable interrupt
Wont work if we have multiprocessors
4Special Machine Instructions
- Modern machines provide special atomic hardware
instructions - Atomic non-interruptable
- Either test memory word and set value
- Or swap contents of two memory words
5Exchange(int register, int memory)
Exchange the contents of a register with that of
memory.
Shared Variable lock, initially 0 local
variable key
Process Pi Prefixi Keyi1 While(Keyi?0)
do exchange(Keyi,lock) CSi Lock0
suffixi
6Three Environments
- There is no central program to coordinate the
processes. The processes communicate with each
other through global variable. - Special hardware instructions
- There is a central program to coordinate the
processes.
7Semaphores
- A variable that has an integer value upon which 3
operations are defined. - Three operations
- A semaphore may be initialized to a nonnegative
value - The wait operation decrements the semaphore
value. If the value becomes negative, then the
process executing the wait is blocked - The signal operation increments the semaphore
value. If the value is not positive, then a
process blocked by a wait operation is unblocked. - Other than these 3 operations, there is no way to
inspect or manipulate semaphores.
8Wait(s) and Signal(s)
- Wait(s) is also called P(s)
-
- ss-1
- if (slt0) place this process in a waiting
queue -
- Signal(s) is also called V(s)
-
- ss1
- if(s?0) remove a process from the waiting
queue -
9Semaphore as General Synchronization Tool
- Counting semaphore integer value can range over
an unrestricted domain - Binary semaphore integer value can range only
between 0 and 1 can be simpler to implement - Also known as mutex locks
- Wait B(s) s is a binary semaphore
-
- if s1 then s0 else block this process
-
- Signal B(s)
-
- if there is a blocked process then unblock
a process else s1 -
- Can implement a counting semaphore S as a binary
semaphore
10Note
- The wait and signal primitives are assumed to be
atomic they cannot be interrupted and each
routine can be treated as an indivisible step.
11Mutual Exclusion provided by Semaphores
- Semaphore S // initialized to 1
- Pi
- prefixi
- wait (S)
- CSi
- signal (S)
- suffixi
- Signal B(s)
-
- if there is a blocked process
- then unblock a process
- else s1
-
- Wait B(s) s is a binary semaphore
-
- if s1
- then s0
- else block this process
-
12Message Passing (not covered)
- Direct Addressing
- Specific identifier of source and destination
processes - Send(destination, message)
- Receive(source, message)
- Indirect Addressing
- Messages are not sent directly from sender to
receiver but rather are sent to a shared data
structure consisting of queues that can
temporarily hold messages. - Such queues are generally referred to as
mailboxes. - Thus, for 2 processes to communicate, one process
sends a message to the appropriate mailbox and
the other process picks up the message from the
mailbox.
13Message Passing (cont.) (not covered)
- When a send primitive is executed in a process,
there are 2 possibilities - Either the sending process is blocked until the
message is received - Or it is not
- When a receive primitive is executed in a
process, there are 2 possibilities - If a message has previously been sent, the
message is received and execution continues - If there is no waiting message, then either
- The process is blocked until a message arrives,
or - The process continues to execute, abandoning the
attempt to receive. - Blocking send, blocking receive
- Nonblocking send, blocking receive
- Nonblocking send , nonblocking receive
14Mutual Exclusion (not covered)
- Create_mailbox(mutex)
- Send(mutex, null)
- Pi
- Prefixi
- Receive(mutex,msg)
- CSi
- Send(mutex,msg)
- Suffixi
Main process Mailbox name is mutex
15Two classical examples
- Producer and Consumer Problem
- Readers/Writers Problem
16Two classical examples
- Producer and Consumer Problem
- Readers/Writers Problem
17Producer and Consumer Problem
- Producer can only put something in when there is
an empty buffer - Consumer can only take something out when there
is a full buffer - Producer and consumer are concurrent processes
0
consumer
producer
N buffers
N-1
18Producer and Consumer Problem(cont.)
- Global Variable
- B0..N-1 an array of size N (Buffer)
- P a semaphore, initialized to N
- C a semaphore, initialized to 0
- Local Variable
- In a ptr(integer) used by the producer, in0
initially - Out a ptr(integer) used by the consumer, out0
initially
19Two classical examples
- Producer and Consumer Problem
- Readers/Writers Problem
20Readers/Writers Problem
- Suppose a data object is to be shared among
several concurrent processes. Some of these
processes want only to read the data object,
while others want to update (both read and write) - Readers Processes that read only
- Writers processes that read and write
- If a reader process is using the data object,
then other reader processes are allowed to use it
at the same time. - If a writer process is using the data object,
then no other process (reader or writer) is
allowed to use it simultaneously.
21Solve Readers/Writers Problem using wait and
signal primitives(cont.)
- Global Variable
- Wrt is a binary semaphore, initialized to 1 Wrt
is used by both readers and writers - For Reader Processes
- Mutex is a binary semaphore, initialized to
1Readcount is an integer variable, initialized
to 0 - Mutex and readcount used by readers only
22End of lecture 6
Thank you!