SlideShare a Scribd company logo
1 of 48
Download to read offline
Shared memory
Background
Cooperating processes provide information sharing and
computation speedup.
●
However, it can affect or be affected by other processes
executing in the system.
●
Concurrent access to shared data may result in data
inconsistency.
●
Maintaining data consistency requires mechanisms to ensure
the orderly execution of cooperating processes.
●
Suppose that we modify the producer-consumer code by
adding a variable counter, initialized to 0 and incremented
each time a new item is added to the buffer
●

Operating System 2011/2012
Bounded-Buffer n solution
...
item buffer[BUFFER_SIZE];
int counter = 0;
//Producer
while (1){
while (counter == BUFFER_SIZE) ; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
//Consumer
while (1){
while (counter == 0) ; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
}

Operating System 2011/2012
Problem statement
The statements
counter++;
counter--;
must be performed atomically.
Atomic operation means an operation that completes in its
entirety without interruption.

Operating System 2011/2012
Problem statement
The statement counter++ may be implemented in machine
language (Assembler) as:
register1 = counter
register1 = register1 + 1
counter = register1
The statement counter– – may be implemented as:
register2 = counter
register2 = register2 – 1
counter = register2

Operating System 2011/2012
Problem statement
If both the producer (P) and consumer (C) attempt to update
the buffer concurrently, the assembly language statements may
get interleaved. Interleaving depends upon how the producer
and consumer processes are scheduled.
t0(P) register1 = counter {register1 = 5}
t1(P) register1 = register1+1 {register1 = 6}
t2(C) register2 = counter {register2 = 5}
t3(C) register2 = register2−1 {register2 = 4}
t4(P) counter = register1 {counter = 6}
t5(C) counter = register1 {counter = 4}
Operating System 2011/2012
Race Condition
“Two processes both want to increment a counter. A gets the
current in value of counter, is interrupted by the dispatcher. B
gets counter (same value as A has) and increases it. When A
runs again it uses the old value of counter and overwrites
what B wrote and sets counter to the same value B set it to.”
Race condition: The situation where several processes access
and manipulate shared data concurrently. The final value of
the shared data depends upon which process finishes last.
To prevent race conditions, concurrent processes must be
synchronized.
Operating System 2011/2012
The Critical-Section Problem
n processes all competing to use some shared data
●
Each process has a code segment, called critical section, in
which the shared data is accessed.
●
Problem – ensure that when one process is executing in its
critical section, no other process is allowed to execute in its
critical section.
●

Operating System 2011/2012
Solution
A solution must satisfy the following three requirements:
1) Mutual Exclusion: if process Pi is executing in its critical
section, then no other processes can be executing in their
critical sections;
2) Progress: if no process is executing in its critical section
and there exist some processes that wish to enter their critical
section, then the selection of the processes that will enter the
critical section next cannot be postponed indefinitely;
3) Bounded Waiting: a bound must exist on the number of
times that other processes are allowed to enter their critical
sections after a process has made a request to enter its critical
section and before that request is granted.
Operating System 2011/2012
Solution
Design a protocol such that each process must request
permission to enter its critical section
Requirements
1) Mutual exclusion
2) Independence
3) No deadlock condition
4) No active wait
5) No starvation
6) Enable/disable interrupts

Operating System 2011/2012
Main idea
General structure of a typical process Pi .
do {
entry section
critical section
exit section
remainder section
} while (1);
Processes may share some common variables to synchronize
their actions.
Operating System 2011/2012
1° Requirement
Code
boolean free = true;
while (!FREE);
FREE = false;
critical section
FREE = true;

Operating System 2011/2012
1° Requirement
Time line
t0 (P1) while (!FREE);
t1 (P2) while (!FREE);
both processes find FREE = true
t2 (P2) FREE = false;
t3 (P1) FREE = false;
This solution does not represents an acceptable solution to the
problem of the mutual exclusion as it breaks the first
requirement.

Operating System 2011/2012
2° Requirement
enum {1, 2} TURN;
TURN = 1; /* initialize */
//P1
while (TURN != 1);
critical section
TURN = 2;

Operating System 2011/2012

//P2
while (TURN != 2);
critical section
TURN = 1;
2° Requirement
Processes are treated non uniformly. Priority is done the first
one (process P1);
●
The execution of the critical section is alternated, if the first
process is blocked before the critical section, the second one
could never enter.
●

This solution does not represents an acceptable solution to the
problem of the mutual exclusion as it breaks the second
requirement.

Operating System 2011/2012
3° Requirement
typedef enum {false, true} Boolean;
Boolean READY1, READY2;
READY1=READY2=false;

//P1
READY1 = true;
while (READY2);
critical section
READY1 = false;

Operating System 2011/2012

//P2
READY2 = true;
while (READY1);
critical section
READY2 = false;
3° Requirement
This solution satisfies 1° and 2° requirements.
(requirement n. 1)
●
P1 enter the critical section if READY1==true and
READY2==false
●
P2 enter the critical section if READY1==false and
READY2==true
(requirement n. 2)
●
if P1 is blocked outside the critical section, READY1 remain
false and P2 can enter
Operating System 2011/2012
3° Requirement
Time line
t0 (P2) READY2 = true;
t1 (P1) READY1 = true;
both processes set their value TRUE
t2 (P1) while (READY2);
t3 (P2) while (READY1);
●
Both processes check if the critical section if free
●
Both processes find it occupied and none enters the critical
section
●
This solution does not represents an acceptable solution to
the problem of the mutual exclusion as it breaks the third
requirement.
Operating System 2011/2012
4° Requirement
typedef enum {false, true} Boolean;
Boolean READY1, READY2;
enum {1, 2} TURN;
READY1=READY2=false;
//P1
//P2
READY1 = true;
READY2 = true;
TURN = 2;
TURN = 1;
while
while
(READY2&&TURN==2); (READY1&&TURN==1);
critical section

critical section

READY1 = false;

READY2 = false;

Operating System 2011/2012
4° Requirement
This solution satisfies 1°, 2° and 3° requirements.
●
This solution is valid only for two processes.
●
The second process remains active while can not access to
the critical section.
●
For efficiency reason the process might loose the possibility
to use the CPU while is waiting.
●
This solution does not represents an acceptable solution to
the problem of the mutual exclusion as it breaks the forth
requirement.
●

Operating System 2011/2012
5° Requirement
The solution must not delay forever or for very long time a
process (generally due to its characteristics such the necessity
to access to a large amount of resources)

Operating System 2011/2012
6° Requirement
Disable interrupts
critical section
Enable interrupts
Works only with mono-processors
●
This solution excludes not only processes of the same
Critical section but avoid any kind of parallelism
●
Makes the system insensitive to any external stimulus
●
This solution does not represents an acceptable solution to
the problem of the mutual exclusion as it breaks the sixth
requirement.
●

Operating System 2011/2012
Synchronization Hardware
On many systems, there are simple hardware instructions
that can be used in solving the CS problem
●
If we could prevent interrupts from occurring while a shared
variable was being modified the CS problem would be
solved
●
Not feasible in a multiprocessor environment – system
efficiency impacted by delays in getting message to all
processors not to enter their respective CSs
●
Many computer systems provide special hardware
instructions that enable testing and setting the contents of a
word or swapping the contents of two words atomically
●

Operating System 2011/2012
TestAndSet()
●

●

Shared data:
boolean lock = FALSE;
Both operations are indivisible - the CPU locks the memory
bus to prohibit other CPUs from accessing memory until it is
done
boolean TestAndSet(boolean &target) {
boolean rv = target; //operation 1
target = TRUE;
//operation 2
return rv;
}

Operating System 2011/2012
TestAndSet()
●

Process Pi
do {
while (TestAndSet(lock));
critical section
lock = FALSE;
remainder section
}

Operating System 2011/2012
Hardware Synchronization
Advantages
●
Applicable to any number of processes on either a single
processor or multiple processors sharing main memory
●
Simple and therefore easy to verify
●
Can be used to support multiple critical section; each CS has
its own variable

Operating System 2011/2012
Hardware Synchronization
Disadvantages
●
Busy waiting is employed, consumes processor time
●
Starvation is possible since selection of the next process to
run waiting on that variable is arbitrary
●
Deadlock is possible on a single processor machine – P1
executes special instruction and enters CS. P1 is interrupted
and P2, which has a higher priority, gets the CPU. P2 can’t
execute its CS since P1 is still in its CS – P2 goes into a busy
waiting loop. P1,however, will not be dispatched because it
has a lower priority than another ready process, P2.

Operating System 2011/2012
Semaphores – Dijkstra (1965)
Synchronization tool that does not require busy waiting
(spinlock).
●
Easier to generalize to more complex problems.
●
When one process modifies the semaphore value, no other
process can simultaneously modify that same semaphore
value.
●
Strong semaphore specifies the order in which processes are
removed from the queue and guarantees freedom from
starvation, a weak semaphore does not specify an order and
may be subject to starvation.
●

Operating System 2011/2012
Semaphores – Dijkstra (1965)
Semaphore S
●
initialized to a non-negative integer value
●
wait operation decrements the semaphore
value (if the value becomes negative then the
process is blocked)
●
signal operation increments the semaphore
value (if the value is not positive then a
process blocked by a wait is unblocked)
●
A queue is used to hold the processes waiting
on the semaphore and they are removed in a
FIFO order

Operating System 2011/2012
Semaphores Types
Counting semaphore – integer value can range over an
unrestricted domain.
●
full: counts the number of slots that are full
●
empty: counts the number of slots empty
Binary semaphore – integer value can range only between 0
and 1; can be simpler to implement.
●
mutex: makes sure the producer and consumer don’t access
the buffer at the same time
Wait() and Signal() are performed atomically
Operating System 2011/2012
Semaphore Implementation
wait(S){
S.value--;
if (S.value < 0){
block(); //suspends process that invoked it
}
}
signal(S){
S.value++;
if (S.value <= 0){
wakeup(P); //resumes operation of a blocked
process
}
}
Operating System 2011/2012
Semaphore Implementation
Mutual exclusion implementation with semaphores
Shared data:
semaphore mutex; //initially mutex = 1
Process Pi:
do {
wait(mutex);
critical section
signal(mutex);
remainder section
}while(1);
Operating System 2011/2012
???
Proc1

Proc2

Proc3

wait(lock)
wait(lock)
wait(lock)
signal(lock)

signal(lock)

signal(lock)

Operating System 2011/2012
Deadlock
Deadlock – two or more processes are waiting indefinitely for an
event that can be caused by only one of the waiting processes –
resource acquisition and release
Let S and Q be two semaphores initialized to 1
P0
P1
wait(S);
wait(Q);
wait(Q);
wait(S);
. .
. .
signal(S);
signal(Q);
signal(Q);
signal(S);

Operating System 2011/2012
Starvation
Starvation – indefinite blocking. A process may never be
removed from the semaphore queue in which it is suspended
(LIFO queue)
addProc()

removeProc()
Proc4

Proc4

Proc3

Proc3

Proc2

Proc2

Proc1

Proc1

Operating System 2011/2012
Bounded-Buffer Problem
Shared data
semaphore full, empty, mutex;
pool of n buffers, each can hold one item
●
mutex provides mutual exclusion to the buffer pool
●
empty and full count the number of empty and full buffers
●

Initially:
full = 0, empty = n, mutex = 1;

Operating System 2011/2012
Bounded­Buffer Problem
// Producer
do{
…
produce an item in nextp
…
wait(empty);
wait(mutex);
…
add nextp to buffer
…
signal(mutex);
signal(full);
}while(1);

Operating System 2011/2012

// Consumer
do {
wait(full)
wait(mutex);
…
remove an item from buffer to
nextc
…
signal(mutex);
signal(empty);
…
consume the item in nextc
…
}while(1);
Readers-Writers Problem
Sharing a data object (file or record) among several
concurrent processes.
●
If one processes wants to write and another process wants to
reads or write, synchronization problems can arise.
●
Require that the writers have exclusive access to the shared
object.
●

Shared data
semaphore mutex, wrt;
Initially
mutex = 1, wrt = 1, readcount = 0
Operating System 2011/2012
Readers-Writers Problem
readcount keeps track of how many processes are reading
the object
●
mutex provides mutual exclusion for changes to readcount
●
wrt provides mutual exclusion for the writers
●
when writer exits and signals wrt, either another waiting
writer executes or another waiting reader – the scheduler
decides
●

Operating System 2011/2012
Readers-Writers Problem
// Write process
wait(wrt);
…
writing is performed
…
signal(wrt);

Operating System 2011/2012

// Read process
wait(mutex);
readcount++;
if (readcount == 1)
wait(wrt);
signal(mutex);
…
reading is performed
…
wait(mutex);
readcount--;
if (readcount == 0)
signal(wrt);
signal(mutex):
Dining-Philosophers Problem
Processes are competing for exclusive access to a limited
number of resources
●
Philosophers eat/think
●
Eating needs 2 chopsticks
●
Pick up one chopstick at a time
●
How to prevent deadlock
Shared data
semaphore chopstick[5];
Initially all values are 1
Operating System 2011/2012
Dining-Philosophers Problem
Philosopher i:
do {
wait(chopstick[i])
wait(chopstick[(i+1) % 5])
eat
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
think
} while (1);

Operating System 2011/2012
Dining-Philosophers Problem
●

●

●

All could pick up left fork simultaneously, see right fork not
available , put down left fork simultaneously and repeat the
process - starvation
Each could wait a random amount of time before pick up
right fork but that is not a good solution for all applications
Could use a binary semaphore, would enable only one
philosopher to eat at a time

Operating System 2011/2012
Monitors
Even with semaphores, race conditions can still exist
●
Semaphores are sensitive to the order in which they are
decremented.
●
Suppose mutex was decremented before empty in the
producer’s code. If the buffer was full, the producer would
block with mutex = 0. When the consumer runs it finds
mutex = 0 and block as well. This is deadlock.

Operating System 2011/2012
Monitors
Monitors are high-level synchronization construct that allows
the safe sharing of an abstract data type among concurrent
processes.
●
collection of procedures, variables and data structures that
are grouped together in a special kind of module.
●
processes can enter the monitor by invoking one of its
procedures
●
the monitor’s internal data structures are accessible only by
the monitor’s procedures and not by external procedures
Mutual Exclusion: Only one process can be active in a
monitor at any instant
Operating System 2011/2012
Monitors
To implement tailor-made synchronization schemes within a
monitor use condition variables along with wait and signal
●
wait(c) – suspend activation of the calling process on condition c;
the monitor is now available for use by another process
●
signal(c) – resume execution of some process suspended after a
wait on the same condition. If there are several such processes,
choose one of them. If there are no such processes, do nothing.
(different from a semaphore)
●
The monitor may be entered by invoking any of its procedures,
we can think of it as having only one entry point that is guarded
so that only one process can be in the monitor at a time (other
processes that attempt to enter join a queue of suspended
processes waiting for monitor availability)
Operating System 2011/2012
Monitors
Java supports user level threads and also allows methods to be
grouped together into classes
The keyword synchronized on a method guarantees that once
any thread has started executing that method, no other thread
will be allowed to start executing any other synchronized
method in that class

Operating System 2011/2012
Monitors
●

●

●

●

●

To allow a process to wait within the monitor, a condition
variable must be declared, as condition x, y;
Condition variable can only be used with the operations wait and
signal.
The operation x.wait() means that the process invoking this
operation is suspended until another process invokes it
The x.signal() operation resumes exactly one suspended process.
If no process is suspended, then the signal operation has no effect.
As with semaphores, it is possible to make mistakes in
synchronization but, as all synchronization functions are confined
to the monitor, it is easier to verify the correctness of the code

Operating System 2011/2012

More Related Content

What's hot

INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxLECO9
 
Operating Systems: Process Scheduling
Operating Systems: Process SchedulingOperating Systems: Process Scheduling
Operating Systems: Process SchedulingDamian T. Gordon
 
RPC: Remote procedure call
RPC: Remote procedure callRPC: Remote procedure call
RPC: Remote procedure callSunita Sahu
 
8 memory management strategies
8 memory management strategies8 memory management strategies
8 memory management strategiesDr. Loganathan R
 
2. Distributed Systems Hardware & Software concepts
2. Distributed Systems Hardware & Software concepts2. Distributed Systems Hardware & Software concepts
2. Distributed Systems Hardware & Software conceptsPrajakta Rane
 
Contiguous Memory Allocation.ppt
Contiguous Memory Allocation.pptContiguous Memory Allocation.ppt
Contiguous Memory Allocation.pptinfomerlin
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37myrajendra
 
Huffman Coding Algorithm Presentation
Huffman Coding Algorithm PresentationHuffman Coding Algorithm Presentation
Huffman Coding Algorithm PresentationAkm Monir
 
Operating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksOperating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksMukesh Chinta
 
Reader/writer problem
Reader/writer problemReader/writer problem
Reader/writer problemRinkuMonani
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationWayne Jones Jnr
 
Fixed partitioning of memory
Fixed partitioning of memoryFixed partitioning of memory
Fixed partitioning of memoryJohn Scott Giini
 
Segmentation in Operating Systems.
Segmentation in Operating Systems.Segmentation in Operating Systems.
Segmentation in Operating Systems.Muhammad SiRaj Munir
 
Memory management ppt
Memory management pptMemory management ppt
Memory management pptManishaJha43
 

What's hot (20)

INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
 
Operating Systems: Process Scheduling
Operating Systems: Process SchedulingOperating Systems: Process Scheduling
Operating Systems: Process Scheduling
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
 
Opetating System Memory management
Opetating System Memory managementOpetating System Memory management
Opetating System Memory management
 
Distributed and clustered systems
Distributed and clustered systemsDistributed and clustered systems
Distributed and clustered systems
 
RPC: Remote procedure call
RPC: Remote procedure callRPC: Remote procedure call
RPC: Remote procedure call
 
8 memory management strategies
8 memory management strategies8 memory management strategies
8 memory management strategies
 
Swapping | Computer Science
Swapping | Computer ScienceSwapping | Computer Science
Swapping | Computer Science
 
2. Distributed Systems Hardware & Software concepts
2. Distributed Systems Hardware & Software concepts2. Distributed Systems Hardware & Software concepts
2. Distributed Systems Hardware & Software concepts
 
Contiguous Memory Allocation.ppt
Contiguous Memory Allocation.pptContiguous Memory Allocation.ppt
Contiguous Memory Allocation.ppt
 
Distributed systems scheduling
Distributed systems schedulingDistributed systems scheduling
Distributed systems scheduling
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37
 
Huffman Coding Algorithm Presentation
Huffman Coding Algorithm PresentationHuffman Coding Algorithm Presentation
Huffman Coding Algorithm Presentation
 
Operating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksOperating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and Deadlocks
 
Reader/writer problem
Reader/writer problemReader/writer problem
Reader/writer problem
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
 
Fixed partitioning of memory
Fixed partitioning of memoryFixed partitioning of memory
Fixed partitioning of memory
 
Segmentation in Operating Systems.
Segmentation in Operating Systems.Segmentation in Operating Systems.
Segmentation in Operating Systems.
 
Memory management ppt
Memory management pptMemory management ppt
Memory management ppt
 
Process synchronization in operating system
Process synchronization in operating systemProcess synchronization in operating system
Process synchronization in operating system
 

Similar to Shared Memory

Slides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdfSlides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdfGeekyHassan
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)Nagarajan
 
Galvin-operating System(Ch7)
Galvin-operating System(Ch7)Galvin-operating System(Ch7)
Galvin-operating System(Ch7)dsuyal1
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronizationVaibhav Khanna
 
ch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu studentch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu studentBhartiRani14
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronizationAbeera Naeem
 
Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxLecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxAmanuelmergia
 
ch5 [Autosaved].ppt
ch5 [Autosaved].pptch5 [Autosaved].ppt
ch5 [Autosaved].pptmonirJihad2
 
Chap3_Les processus_Partie II_Ouatik.fr.en.pdf
Chap3_Les processus_Partie II_Ouatik.fr.en.pdfChap3_Les processus_Partie II_Ouatik.fr.en.pdf
Chap3_Les processus_Partie II_Ouatik.fr.en.pdfDelonPierre
 
Synchronization in os.pptx
Synchronization in os.pptxSynchronization in os.pptx
Synchronization in os.pptxAbdullahBhatti53
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationAnas Ebrahim
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfAmanuelmergia
 
Distributed Coordination
Distributed CoordinationDistributed Coordination
Distributed Coordinationsiva krishna
 

Similar to Shared Memory (20)

Slides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdfSlides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdf
 
CH05.pdf
CH05.pdfCH05.pdf
CH05.pdf
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)
 
Galvin-operating System(Ch7)
Galvin-operating System(Ch7)Galvin-operating System(Ch7)
Galvin-operating System(Ch7)
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronization
 
ch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu studentch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu student
 
ch6_EN_BK_syn1.pdf
ch6_EN_BK_syn1.pdfch6_EN_BK_syn1.pdf
ch6_EN_BK_syn1.pdf
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
SYNCHRONIZATION
SYNCHRONIZATIONSYNCHRONIZATION
SYNCHRONIZATION
 
Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxLecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptx
 
ch5 [Autosaved].ppt
ch5 [Autosaved].pptch5 [Autosaved].ppt
ch5 [Autosaved].ppt
 
Lecture16-17.ppt
Lecture16-17.pptLecture16-17.ppt
Lecture16-17.ppt
 
Operating System
Operating SystemOperating System
Operating System
 
Processes
ProcessesProcesses
Processes
 
Chap3_Les processus_Partie II_Ouatik.fr.en.pdf
Chap3_Les processus_Partie II_Ouatik.fr.en.pdfChap3_Les processus_Partie II_Ouatik.fr.en.pdf
Chap3_Les processus_Partie II_Ouatik.fr.en.pdf
 
Ch7
Ch7Ch7
Ch7
 
Synchronization in os.pptx
Synchronization in os.pptxSynchronization in os.pptx
Synchronization in os.pptx
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Synchronization
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdf
 
Distributed Coordination
Distributed CoordinationDistributed Coordination
Distributed Coordination
 

More from Mustafa Ugur Oduncu (7)

Windows server 2008
Windows server 2008 Windows server 2008
Windows server 2008
 
Virtual Memory
Virtual MemoryVirtual Memory
Virtual Memory
 
Main Memory
Main MemoryMain Memory
Main Memory
 
Scheduling
SchedulingScheduling
Scheduling
 
Thread
ThreadThread
Thread
 
Operating Systems
Operating SystemsOperating Systems
Operating Systems
 
Linux fundamentals
Linux fundamentalsLinux fundamentals
Linux fundamentals
 

Recently uploaded

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 

Recently uploaded (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 

Shared Memory

  • 2. Background Cooperating processes provide information sharing and computation speedup. ● However, it can affect or be affected by other processes executing in the system. ● Concurrent access to shared data may result in data inconsistency. ● Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. ● Suppose that we modify the producer-consumer code by adding a variable counter, initialized to 0 and incremented each time a new item is added to the buffer ● Operating System 2011/2012
  • 3. Bounded-Buffer n solution ... item buffer[BUFFER_SIZE]; int counter = 0; //Producer while (1){ while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; } //Consumer while (1){ while (counter == 0) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; } Operating System 2011/2012
  • 4. Problem statement The statements counter++; counter--; must be performed atomically. Atomic operation means an operation that completes in its entirety without interruption. Operating System 2011/2012
  • 5. Problem statement The statement counter++ may be implemented in machine language (Assembler) as: register1 = counter register1 = register1 + 1 counter = register1 The statement counter– – may be implemented as: register2 = counter register2 = register2 – 1 counter = register2 Operating System 2011/2012
  • 6. Problem statement If both the producer (P) and consumer (C) attempt to update the buffer concurrently, the assembly language statements may get interleaved. Interleaving depends upon how the producer and consumer processes are scheduled. t0(P) register1 = counter {register1 = 5} t1(P) register1 = register1+1 {register1 = 6} t2(C) register2 = counter {register2 = 5} t3(C) register2 = register2−1 {register2 = 4} t4(P) counter = register1 {counter = 6} t5(C) counter = register1 {counter = 4} Operating System 2011/2012
  • 7. Race Condition “Two processes both want to increment a counter. A gets the current in value of counter, is interrupted by the dispatcher. B gets counter (same value as A has) and increases it. When A runs again it uses the old value of counter and overwrites what B wrote and sets counter to the same value B set it to.” Race condition: The situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. To prevent race conditions, concurrent processes must be synchronized. Operating System 2011/2012
  • 8. The Critical-Section Problem n processes all competing to use some shared data ● Each process has a code segment, called critical section, in which the shared data is accessed. ● Problem – ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section. ● Operating System 2011/2012
  • 9. Solution A solution must satisfy the following three requirements: 1) Mutual Exclusion: if process Pi is executing in its critical section, then no other processes can be executing in their critical sections; 2) Progress: if no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely; 3) Bounded Waiting: a bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted. Operating System 2011/2012
  • 10. Solution Design a protocol such that each process must request permission to enter its critical section Requirements 1) Mutual exclusion 2) Independence 3) No deadlock condition 4) No active wait 5) No starvation 6) Enable/disable interrupts Operating System 2011/2012
  • 11. Main idea General structure of a typical process Pi . do { entry section critical section exit section remainder section } while (1); Processes may share some common variables to synchronize their actions. Operating System 2011/2012
  • 12. 1° Requirement Code boolean free = true; while (!FREE); FREE = false; critical section FREE = true; Operating System 2011/2012
  • 13. 1° Requirement Time line t0 (P1) while (!FREE); t1 (P2) while (!FREE); both processes find FREE = true t2 (P2) FREE = false; t3 (P1) FREE = false; This solution does not represents an acceptable solution to the problem of the mutual exclusion as it breaks the first requirement. Operating System 2011/2012
  • 14. 2° Requirement enum {1, 2} TURN; TURN = 1; /* initialize */ //P1 while (TURN != 1); critical section TURN = 2; Operating System 2011/2012 //P2 while (TURN != 2); critical section TURN = 1;
  • 15. 2° Requirement Processes are treated non uniformly. Priority is done the first one (process P1); ● The execution of the critical section is alternated, if the first process is blocked before the critical section, the second one could never enter. ● This solution does not represents an acceptable solution to the problem of the mutual exclusion as it breaks the second requirement. Operating System 2011/2012
  • 16. 3° Requirement typedef enum {false, true} Boolean; Boolean READY1, READY2; READY1=READY2=false; //P1 READY1 = true; while (READY2); critical section READY1 = false; Operating System 2011/2012 //P2 READY2 = true; while (READY1); critical section READY2 = false;
  • 17. 3° Requirement This solution satisfies 1° and 2° requirements. (requirement n. 1) ● P1 enter the critical section if READY1==true and READY2==false ● P2 enter the critical section if READY1==false and READY2==true (requirement n. 2) ● if P1 is blocked outside the critical section, READY1 remain false and P2 can enter Operating System 2011/2012
  • 18. 3° Requirement Time line t0 (P2) READY2 = true; t1 (P1) READY1 = true; both processes set their value TRUE t2 (P1) while (READY2); t3 (P2) while (READY1); ● Both processes check if the critical section if free ● Both processes find it occupied and none enters the critical section ● This solution does not represents an acceptable solution to the problem of the mutual exclusion as it breaks the third requirement. Operating System 2011/2012
  • 19. 4° Requirement typedef enum {false, true} Boolean; Boolean READY1, READY2; enum {1, 2} TURN; READY1=READY2=false; //P1 //P2 READY1 = true; READY2 = true; TURN = 2; TURN = 1; while while (READY2&&TURN==2); (READY1&&TURN==1); critical section critical section READY1 = false; READY2 = false; Operating System 2011/2012
  • 20. 4° Requirement This solution satisfies 1°, 2° and 3° requirements. ● This solution is valid only for two processes. ● The second process remains active while can not access to the critical section. ● For efficiency reason the process might loose the possibility to use the CPU while is waiting. ● This solution does not represents an acceptable solution to the problem of the mutual exclusion as it breaks the forth requirement. ● Operating System 2011/2012
  • 21. 5° Requirement The solution must not delay forever or for very long time a process (generally due to its characteristics such the necessity to access to a large amount of resources) Operating System 2011/2012
  • 22. 6° Requirement Disable interrupts critical section Enable interrupts Works only with mono-processors ● This solution excludes not only processes of the same Critical section but avoid any kind of parallelism ● Makes the system insensitive to any external stimulus ● This solution does not represents an acceptable solution to the problem of the mutual exclusion as it breaks the sixth requirement. ● Operating System 2011/2012
  • 23. Synchronization Hardware On many systems, there are simple hardware instructions that can be used in solving the CS problem ● If we could prevent interrupts from occurring while a shared variable was being modified the CS problem would be solved ● Not feasible in a multiprocessor environment – system efficiency impacted by delays in getting message to all processors not to enter their respective CSs ● Many computer systems provide special hardware instructions that enable testing and setting the contents of a word or swapping the contents of two words atomically ● Operating System 2011/2012
  • 24. TestAndSet() ● ● Shared data: boolean lock = FALSE; Both operations are indivisible - the CPU locks the memory bus to prohibit other CPUs from accessing memory until it is done boolean TestAndSet(boolean &target) { boolean rv = target; //operation 1 target = TRUE; //operation 2 return rv; } Operating System 2011/2012
  • 25. TestAndSet() ● Process Pi do { while (TestAndSet(lock)); critical section lock = FALSE; remainder section } Operating System 2011/2012
  • 26. Hardware Synchronization Advantages ● Applicable to any number of processes on either a single processor or multiple processors sharing main memory ● Simple and therefore easy to verify ● Can be used to support multiple critical section; each CS has its own variable Operating System 2011/2012
  • 27. Hardware Synchronization Disadvantages ● Busy waiting is employed, consumes processor time ● Starvation is possible since selection of the next process to run waiting on that variable is arbitrary ● Deadlock is possible on a single processor machine – P1 executes special instruction and enters CS. P1 is interrupted and P2, which has a higher priority, gets the CPU. P2 can’t execute its CS since P1 is still in its CS – P2 goes into a busy waiting loop. P1,however, will not be dispatched because it has a lower priority than another ready process, P2. Operating System 2011/2012
  • 28. Semaphores – Dijkstra (1965) Synchronization tool that does not require busy waiting (spinlock). ● Easier to generalize to more complex problems. ● When one process modifies the semaphore value, no other process can simultaneously modify that same semaphore value. ● Strong semaphore specifies the order in which processes are removed from the queue and guarantees freedom from starvation, a weak semaphore does not specify an order and may be subject to starvation. ● Operating System 2011/2012
  • 29. Semaphores – Dijkstra (1965) Semaphore S ● initialized to a non-negative integer value ● wait operation decrements the semaphore value (if the value becomes negative then the process is blocked) ● signal operation increments the semaphore value (if the value is not positive then a process blocked by a wait is unblocked) ● A queue is used to hold the processes waiting on the semaphore and they are removed in a FIFO order Operating System 2011/2012
  • 30. Semaphores Types Counting semaphore – integer value can range over an unrestricted domain. ● full: counts the number of slots that are full ● empty: counts the number of slots empty Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement. ● mutex: makes sure the producer and consumer don’t access the buffer at the same time Wait() and Signal() are performed atomically Operating System 2011/2012
  • 31. Semaphore Implementation wait(S){ S.value--; if (S.value < 0){ block(); //suspends process that invoked it } } signal(S){ S.value++; if (S.value <= 0){ wakeup(P); //resumes operation of a blocked process } } Operating System 2011/2012
  • 32. Semaphore Implementation Mutual exclusion implementation with semaphores Shared data: semaphore mutex; //initially mutex = 1 Process Pi: do { wait(mutex); critical section signal(mutex); remainder section }while(1); Operating System 2011/2012
  • 34. Deadlock Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes – resource acquisition and release Let S and Q be two semaphores initialized to 1 P0 P1 wait(S); wait(Q); wait(Q); wait(S); . . . . signal(S); signal(Q); signal(Q); signal(S); Operating System 2011/2012
  • 35. Starvation Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended (LIFO queue) addProc() removeProc() Proc4 Proc4 Proc3 Proc3 Proc2 Proc2 Proc1 Proc1 Operating System 2011/2012
  • 36. Bounded-Buffer Problem Shared data semaphore full, empty, mutex; pool of n buffers, each can hold one item ● mutex provides mutual exclusion to the buffer pool ● empty and full count the number of empty and full buffers ● Initially: full = 0, empty = n, mutex = 1; Operating System 2011/2012
  • 37. Bounded­Buffer Problem // Producer do{ … produce an item in nextp … wait(empty); wait(mutex); … add nextp to buffer … signal(mutex); signal(full); }while(1); Operating System 2011/2012 // Consumer do { wait(full) wait(mutex); … remove an item from buffer to nextc … signal(mutex); signal(empty); … consume the item in nextc … }while(1);
  • 38. Readers-Writers Problem Sharing a data object (file or record) among several concurrent processes. ● If one processes wants to write and another process wants to reads or write, synchronization problems can arise. ● Require that the writers have exclusive access to the shared object. ● Shared data semaphore mutex, wrt; Initially mutex = 1, wrt = 1, readcount = 0 Operating System 2011/2012
  • 39. Readers-Writers Problem readcount keeps track of how many processes are reading the object ● mutex provides mutual exclusion for changes to readcount ● wrt provides mutual exclusion for the writers ● when writer exits and signals wrt, either another waiting writer executes or another waiting reader – the scheduler decides ● Operating System 2011/2012
  • 40. Readers-Writers Problem // Write process wait(wrt); … writing is performed … signal(wrt); Operating System 2011/2012 // Read process wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); … reading is performed … wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex):
  • 41. Dining-Philosophers Problem Processes are competing for exclusive access to a limited number of resources ● Philosophers eat/think ● Eating needs 2 chopsticks ● Pick up one chopstick at a time ● How to prevent deadlock Shared data semaphore chopstick[5]; Initially all values are 1 Operating System 2011/2012
  • 42. Dining-Philosophers Problem Philosopher i: do { wait(chopstick[i]) wait(chopstick[(i+1) % 5]) eat signal(chopstick[i]); signal(chopstick[(i+1) % 5]); think } while (1); Operating System 2011/2012
  • 43. Dining-Philosophers Problem ● ● ● All could pick up left fork simultaneously, see right fork not available , put down left fork simultaneously and repeat the process - starvation Each could wait a random amount of time before pick up right fork but that is not a good solution for all applications Could use a binary semaphore, would enable only one philosopher to eat at a time Operating System 2011/2012
  • 44. Monitors Even with semaphores, race conditions can still exist ● Semaphores are sensitive to the order in which they are decremented. ● Suppose mutex was decremented before empty in the producer’s code. If the buffer was full, the producer would block with mutex = 0. When the consumer runs it finds mutex = 0 and block as well. This is deadlock. Operating System 2011/2012
  • 45. Monitors Monitors are high-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. ● collection of procedures, variables and data structures that are grouped together in a special kind of module. ● processes can enter the monitor by invoking one of its procedures ● the monitor’s internal data structures are accessible only by the monitor’s procedures and not by external procedures Mutual Exclusion: Only one process can be active in a monitor at any instant Operating System 2011/2012
  • 46. Monitors To implement tailor-made synchronization schemes within a monitor use condition variables along with wait and signal ● wait(c) – suspend activation of the calling process on condition c; the monitor is now available for use by another process ● signal(c) – resume execution of some process suspended after a wait on the same condition. If there are several such processes, choose one of them. If there are no such processes, do nothing. (different from a semaphore) ● The monitor may be entered by invoking any of its procedures, we can think of it as having only one entry point that is guarded so that only one process can be in the monitor at a time (other processes that attempt to enter join a queue of suspended processes waiting for monitor availability) Operating System 2011/2012
  • 47. Monitors Java supports user level threads and also allows methods to be grouped together into classes The keyword synchronized on a method guarantees that once any thread has started executing that method, no other thread will be allowed to start executing any other synchronized method in that class Operating System 2011/2012
  • 48. Monitors ● ● ● ● ● To allow a process to wait within the monitor, a condition variable must be declared, as condition x, y; Condition variable can only be used with the operations wait and signal. The operation x.wait() means that the process invoking this operation is suspended until another process invokes it The x.signal() operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect. As with semaphores, it is possible to make mistakes in synchronization but, as all synchronization functions are confined to the monitor, it is easier to verify the correctness of the code Operating System 2011/2012