SlideShare a Scribd company logo
PROCESS
SYNCHRONIZATION
IN
OPERATING SYSTEMS
1
By
RITU RANJAN SHRIVASTWA
Email: ranjanbhai.latest@gmail.com
TABLE OF CONTENTS
• What is Process Synchronization and why it is needed
• The Critical Section Problem
• Peterson’s Solution
• Synchronization Hardware
• Semaphores
• Applications of Semaphores
• Classical Problems of Synchronizations
• Synchronization Examples
• Monitors
• Atomic Transactions
• References
2
WHAT IS PROCESS
SYNCHRONIZATION?
• Several Processes run in an Operating System
• Some of them share resources due to which problems like
data inconsistency may arise
• For Example: One process changing the data in a
memory location where another process is trying to read
the data from the same memory location. It is possible
that the data read by the second process will be
erroneous
3
WHAT IS PROCESS
SYNCHRONIZATION?
• PRODUCER CONSUMER PROBLEM
(or Bounded-Buffer Problem)
• Problem: To ensure that the Producer should not add
DATA when the Buffer is full and the Consumer should
not take data when the Buffer is empty
4
Buffer
Producer
(process A)
Consumer
(process B)
WHAT IS PROCESS
SYNCHRONIZATION?
• SOLUTION PRODUCER CONSUMER PROBLEM
• Using Semaphores (In computer science, particularly in operating systems,
a semaphore is a variable or abstract data type that is used for controlling access, by
multiple processes, to a common resource in a concurrent system such as
a multiprogramming operating system.)
• Using Monitors (In concurrent programming, a monitor is a synchronization
construct that allows threads to have both mutual exclusion and the ability to wait
(block) for a certain condition to become true)
• Atomic Transactions (Atomic read-modify-write access to shared variables is
avoided, as each of the two Count variables is updated only by a single thread. Also,
these variables stay incremented all the time; the relation remains correct when their
values wrap around on an integer overflow)
5
RACE CONDITION
6
What if T5 happened before T4?
?
CRITICAL SECTION PROBLEM
• A section of code, common to n cooperating
processes, in which the processes may be
accessing common variables.
• A Critical Section Environment contains:
• Entry Section Code requesting entry into the critical section.
• Critical Section Code in which only one process can execute at
any one time.
• Exit Section The end of the critical section, releasing or allowing
others in.
• Remainder Section Rest of the code AFTER the critical section.
7
CRITICAL SECTION PROBLEM
• The critical section must ENFORCE ALL THREE
of the following rules:
• Mutual Exclusion: No more than one process can
execute in its critical section at one time.
• Progress: If no one is in the critical section and
someone wants in, then those processes not in their
remainder section must be able to decide in a finite
time who should go in.
• Bounded Wait: All requesters must eventually be let
into the critical section
8
CRITICAL SECTION PROBLEM
9
ENTRY SECTION
CRITICAL SECTION
EXIT SECTION
REMAINDER SECTION
PETERSON’S SOLUTION
• To handle the problem of Critical Section (CS), Peterson gave
an algorithm with a bounded waiting
• Suppose there are N processes (P1, P2, … PN) and each of
them at some point need to enter the Critical Section
• A FLAG[] array of size N is maintained which is by default false
and whenever a process need to enter the critical section it has
to set its flag as true, i.e. suppose Pi wants to enter so it will set
FLAG[i]=TRUE
• There is another variable called TURN which indicates the
process number which is currently to enter into the CS. The
process that enters into the CS while exiting would change the
TURN to another number from among the list of ready
processes
10
PETERSON’S SOLUTION
11
• If turn is 2 (say) then P2 enters the CS and while exiting
sets the turn as 3 and thus P3 breaks out of wait loop
FLAG[i] = false
Synchronization Hardware
• Problems of Critical Section are also solvable by hardware
• Uniprocessor systems disables interrupts while a Process Pi is
using the CS but it is a great disadvantage in multiprocessor
systems
• Some systems provide a lock functionality where a Process
acquires a lock while entering the CS and releases the lock
after leaving it. Thus another process trying to enter CS cannot
enter as the entry is locked. It can only do so if it is free by
acquiring the lock itself
• Another advanced approach is the Atomic Instructions (Non-
Interruptible instructions).
12
MUTEX LOCKS
• As the synchronization hardware solution is not easy to
implement fro everyone, a strict software approach called
Mutex Locks was introduced. In this approach, in the
entry section of code, a LOCK is acquired over the critical
resources modified and used inside critical section, and in
the exit section that LOCK is released.
• As the resource is locked while a process executes its
critical section hence no other process can access it
13
SEMAPHORES
• A more robust alternative to simple mutual exclusions is to
use semaphores, which are integer variables for which
only two ( atomic ) operations are defined, the wait and
signal operations, as shown in the following figure
• As given here, these are not atomic as written in "macro
code". We define these operations, however, to be atomic
(Protected by a hardware lock.)
14
WAIT ( S ):
while ( S <= 0 );
S = S - 1;
SIGNAL ( S ):
S = S + 1;
SEMAPHORES
• FORMAT:
wait ( mutex ); // Mutual exclusion: mutex init to 1.
CRITICAL SECTION
signal( mutex );
REMAINDER
• Note that not only must the variable-changing steps ( S-- and
S++ ) be indivisible, it is also necessary that for the wait
operation when the test proves false that there be no
interruptions before S gets decremented. It IS okay, however,
for the busy loop to be interrupted when the test is true, which
prevents the system from hanging forever
15
SEMAPHORES
• PROPERTIES
1. Simple
2. Works with many processes
3. Can have many different critical sections with different
semaphores
4. Each critical section has unique access semaphores
5. Can permit multiple processes into the critical section at
once, if desirable.
16
SEMAPHORES
• TYPES
Semaphores are mainly of two types:
1. Binary Semaphore
It is a special form of semaphore used for implementing mutual
exclusion, hence it is often called Mutex. A binary semaphore is
initialized to 1 and only takes the value 0 and 1 during execution of
a program.
2. Counting Semaphores
These are used to implement bounded concurrency.
17
SEMAPHORES
• Semaphores can be used to force synchronization (
precedence ) if the preceding process does a signal at the
end, and the follower does wait at beginning. For example,
here we want P1 to execute before P2.
P1: P2:
statement 1; wait ( synch );
signal ( synch ); statement 2;
• To PREVENT looping, we redefine the semaphore structure as:
typedef struct {
int value;
struct process *list; //linked list of PTBL waiting on S
} SEMAPHORE;
18
SEMAPHORES
• It's critical that these be atomic - in uniprocessors we can
disable interrupts, but in multiprocessors other
mechanisms for atomicity are needed.
19
SEMAPHORE s;
wait(s) {
s.value = s.value - 1;
if ( s.value < 0 ) {
add this process to s.L;
block;
}
}
SEMAPHORE s;
signal(s) {
s.value = s.value + 1;
if ( s.value <= 0 ) {
remove a process P from s.L;
wakeup(P);
}
}
SEMAPHORES
• DEADLOCK
• One important problem that can arise when using semaphores to block
processes waiting for a limited resource is the problem of deadlocks, which
occur when multiple processes are blocked, each waiting for a resource that
can only be freed by one of the other ( blocked ) processes, as illustrated in
the following example.
20
SEMAPHORES
• STARVATION
• Another problem to consider is that of starvation, in
which one or more processes gets blocked forever, and
never get a chance to take their turn in the critical section.
For example, in the semaphores above, we did not
specify the algorithms for adding processes to the waiting
queue in the semaphore in the wait( ) call, or selecting
one to be removed from the queue in the signal( ) call. If
the method chosen is a FIFO queue, then every process
will eventually get their turn, but if a LIFO queue is
implemented instead, then the first process to start
waiting could starve.
21
THE BOUNDED-BUFFER PROBLEM
• This is a generalization of the producer-consumer
problem wherein access is controlled to a shared group of
buffers of a limited size.
• In this solution, the two counting semaphores "full" and
"empty" keep track of the current number of full and empty
buffers respectively ( and initialized to 0 and N
respectively. ) The binary semaphore mutex controls
access to the critical section. The producer and consumer
processes are nearly identical - One can think of the
producer as producing full buffers, and the consumer
producing empty buffers.
22
THE BOUNDED-BUFFER PROBLEM
23
PRODUCER
CONSUMER
THE READERS-WRITERS PROBLEM
• THE READERS/WRITERS PROBLEM:
• This is the same as the Producer / Consumer problem except - we now
can have many concurrent readers and one exclusive writer.
• Locks: are shared (for the readers) and exclusive (for the writer).
• Two possible ( contradictory ) guidelines can be used:
• No reader is kept waiting unless a writer holds the lock (the readers have
precedence).
• If a writer is waiting for access, no new reader gains access (writer has
precedence).
• ( NOTE: starvation can occur on either of these rules if they are followed
rigorously.)
24
THE READERS-WRITERS PROBLEM
25
Reader:
do {
wait( mutex ); /* Allow 1 reader in entry*/
readcount = readcount + 1;
if readcount == 1 then wait(wrt ); /* 1st reader locks writer */
signal( mutex );
/* reading is performed */
wait( mutex );
readcount = readcount - 1;
if readcount == 0 then signal(wrt ); /*last reader frees writer */
signal( mutex );
} while(TRUE);
Writer:
do {
wait( wrt );
/* writing is performed */
signal( wrt );
} while(TRUE);
THE READERS/WRITERS PROBLEM:
BINARY_SEMAPHORE wrt = 1;
BINARY_SEMAPHORE mutex = 1;
int readcount = 0;
DINING PHILOSOPHERS PROBLEM
26
• Five philosophers sitting at a round dining
table to eat
• Each one has got a plate and one chopstick at
the right side of each plate
• To start eating each of them need a plate and
two chopsticks
• Thus, clearly there will be a deadlock and
starvation because of the limited number of
chopsticks
• This can be solved by either:
• Allow only 4 philosophers to be hungry at
a time
• Allow pickup only if both chopsticks are
available. ( Done in critical section )
• Odd # philosopher always picks up left
chopstick 1st, even # philosopher always
picks up right chopstick 1st
DINING PHILOSOPHERS PROBLEM
• One possible solution, as shown in the following code section, is to
use a set of five semaphores ( chopsticks[ 5 ] ), and to have each
hungry philosopher first wait on their left chopstick ( chopsticks[ i ] ),
and then wait on their right chopstick ( chopsticks[ ( i + 1 ) % 5 ] )
27
SYNCHRONIZATION EXAMPLES
• SYNCHRONIZATION IN WINDOWS
• SYNCHRONIZATION IN LINUX
28
MONITORS
• High-level synchronization construct that allows the safe
sharing of an abstract data type among concurrent
processes.
• Semaphores can be very useful for solving concurrency
problems, but only if programmers use them
properly. If even one process fails to abide by the proper
use of semaphores, either accidentally or deliberately,
then the whole system breaks down.
29
MONITORS
• A monitor is essentially a
class, in which all data is
private, and with the special
restriction that only one
method within any given
monitor object may be
active at the same time.
• An additional restriction is
that monitor methods may
only access the shared data
within the monitor and any
data passed to them as
parameters, i.e. they cannot
access any data external to
the monitor.
30
MONITORS
• In order to fully realize the potential of monitors, we
need to introduce one additional new data type,
known as a condition.
• A variable of type condition has only two legal
operations, wait and signal. I.e. if X was defined
as type condition, then legal operations would be
X.wait( ) and X.signal( )
• The wait operation blocks a process until some
other process calls signal, and adds the blocked
process onto a list associated with that condition.
• The signal process does nothing if there are no
processes waiting on that condition. Otherwise it
wakes up exactly one process from the
condition's list of waiting processes. ( Contrast
this with counting semaphores, which always
affect the semaphore on a signal call. )
31
ATOMIC TRANSACTIONS
• Database operations frequently need to carry out atomic
transactions, in which the entire transaction must either
complete or not occur at all.
• The classic example is a transfer of funds, which involves
withdrawing funds from one account and depositing them into
another - Either both halves of the transaction must complete,
or neither must complete.
• Operating Systems can be viewed as having many of the same
needs and problems as databases, in that an OS can be said
to manage a small database of process-related information. As
such, OSs can benefit from emulating some of the techniques
originally developed for databases.
32
ATOMIC TRANSACTIONS
• FUNDAMENTAL PRINCIPLES – A C I D
• Atomicity – to outside world, transaction happens
indivisibly
• Consistency – transaction preserves system invariants
• Isolated – transactions do not interfere with each other
• Durable – once a transaction “commits,” the changes are
permanent
33
REFERENCES
• https://www.cs.uic.edu/~jbell/CourseNotes/OperatingSyst
ems/5_Synchronization.html
• http://www.studytonight.com/operating-system/process-
synchronization
34

More Related Content

PPTX
DeadLock in Operating-Systems
PPTX
Process synchronization
PDF
Pipelining
PDF
operating system structure
PPTX
Threads (operating System)
PPTX
Operating system critical section
PPT
Chapter 6 - Process Synchronization
PPTX
Directory structure
DeadLock in Operating-Systems
Process synchronization
Pipelining
operating system structure
Threads (operating System)
Operating system critical section
Chapter 6 - Process Synchronization
Directory structure

What's hot (20)

PPTX
System calls
PPTX
Deadlock ppt
PDF
Semaphores
PPTX
contiguous memory allocation.pptx
PPTX
CPU Scheduling in OS Presentation
PPTX
Paging and segmentation
PPTX
process control block
PPT
Chapter 13 software testing strategies
PPTX
Process scheduling
PPTX
SCHEDULING ALGORITHMS
PDF
Deadlock Avoidance - OS
PPT
Symbol table management and error handling in compiler design
PPTX
Code Optimization
PPTX
Context model
PPTX
A Role of Lexical Analyzer
PPTX
cpu scheduling
PPT
Introduction to Compiler design
PDF
OS - Process Concepts
PPTX
Distributed operating system
PPT
Paging.ppt
System calls
Deadlock ppt
Semaphores
contiguous memory allocation.pptx
CPU Scheduling in OS Presentation
Paging and segmentation
process control block
Chapter 13 software testing strategies
Process scheduling
SCHEDULING ALGORITHMS
Deadlock Avoidance - OS
Symbol table management and error handling in compiler design
Code Optimization
Context model
A Role of Lexical Analyzer
cpu scheduling
Introduction to Compiler design
OS - Process Concepts
Distributed operating system
Paging.ppt
Ad

Viewers also liked (10)

PPT
OS Process Synchronization, semaphore and Monitors
PPT
Types dbms
PDF
Operating Systems - Synchronization
PPT
Process synchronization(deepa)
DOCX
Operating System Process Synchronization
PDF
Web technology
PDF
Unit II - 3 - Operating System - Process Synchronization
PPT
Web Tech
PPTX
Process synchronization in operating system
PPT
Process Synchronization And Deadlocks
OS Process Synchronization, semaphore and Monitors
Types dbms
Operating Systems - Synchronization
Process synchronization(deepa)
Operating System Process Synchronization
Web technology
Unit II - 3 - Operating System - Process Synchronization
Web Tech
Process synchronization in operating system
Process Synchronization And Deadlocks
Ad

Similar to Process synchronization in Operating Systems (20)

PDF
Lecture 5 process synchronization
PPTX
MODULE 3 process synchronizationnnn.pptx
PPTX
5 Inter Process note Communication.pptx
PDF
Ch5 process synchronization
PPTX
synchronization in operating system structure
PDF
operating System notes ipc monitor Processor and Process
PPT
Ipc feb4
PPTX
UNIT-2 - Concurrency & Interprocess Communicatio.pptx
DOCX
Critical section operating system
PDF
Lecture 5- Process Synchonization_revised.pdf
PPT
Ipc feb4
PPTX
794985751-Unit-3-Inter-Process-Communication.pptx
PPTX
Lecture 5- Process Synchronization (1).pptx
PPT
Process Synchronization -1.ppt
PPT
Section06-Syncopkojiojoijnnjkhuubgfffppt
PPTX
SYNCHRONIZATION IN MULTIPROCESSING
PPTX
Concurrency: Mutual Exclusion and Synchronization
PPTX
Mutual Exclusion using Peterson's Algorithm
PPT
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
Lecture 5 process synchronization
MODULE 3 process synchronizationnnn.pptx
5 Inter Process note Communication.pptx
Ch5 process synchronization
synchronization in operating system structure
operating System notes ipc monitor Processor and Process
Ipc feb4
UNIT-2 - Concurrency & Interprocess Communicatio.pptx
Critical section operating system
Lecture 5- Process Synchonization_revised.pdf
Ipc feb4
794985751-Unit-3-Inter-Process-Communication.pptx
Lecture 5- Process Synchronization (1).pptx
Process Synchronization -1.ppt
Section06-Syncopkojiojoijnnjkhuubgfffppt
SYNCHRONIZATION IN MULTIPROCESSING
Concurrency: Mutual Exclusion and Synchronization
Mutual Exclusion using Peterson's Algorithm
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt

Recently uploaded (20)

PDF
Yogi Goddess Pres Conference Studio Updates
PDF
RMMM.pdf make it easy to upload and study
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Updated Idioms and Phrasal Verbs in English subject
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PDF
Trump Administration's workforce development strategy
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Cell Types and Its function , kingdom of life
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Cell Structure & Organelles in detailed.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Yogi Goddess Pres Conference Studio Updates
RMMM.pdf make it easy to upload and study
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Updated Idioms and Phrasal Verbs in English subject
Final Presentation General Medicine 03-08-2024.pptx
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Trump Administration's workforce development strategy
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Module 4: Burden of Disease Tutorial Slides S2 2025
Cell Types and Its function , kingdom of life
202450812 BayCHI UCSC-SV 20250812 v17.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
History, Philosophy and sociology of education (1).pptx
Weekly quiz Compilation Jan -July 25.pdf
01-Introduction-to-Information-Management.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
LDMMIA Reiki Yoga Finals Review Spring Summer
Cell Structure & Organelles in detailed.
Final Presentation General Medicine 03-08-2024.pptx

Process synchronization in Operating Systems

  • 2. TABLE OF CONTENTS • What is Process Synchronization and why it is needed • The Critical Section Problem • Peterson’s Solution • Synchronization Hardware • Semaphores • Applications of Semaphores • Classical Problems of Synchronizations • Synchronization Examples • Monitors • Atomic Transactions • References 2
  • 3. WHAT IS PROCESS SYNCHRONIZATION? • Several Processes run in an Operating System • Some of them share resources due to which problems like data inconsistency may arise • For Example: One process changing the data in a memory location where another process is trying to read the data from the same memory location. It is possible that the data read by the second process will be erroneous 3
  • 4. WHAT IS PROCESS SYNCHRONIZATION? • PRODUCER CONSUMER PROBLEM (or Bounded-Buffer Problem) • Problem: To ensure that the Producer should not add DATA when the Buffer is full and the Consumer should not take data when the Buffer is empty 4 Buffer Producer (process A) Consumer (process B)
  • 5. WHAT IS PROCESS SYNCHRONIZATION? • SOLUTION PRODUCER CONSUMER PROBLEM • Using Semaphores (In computer science, particularly in operating systems, a semaphore is a variable or abstract data type that is used for controlling access, by multiple processes, to a common resource in a concurrent system such as a multiprogramming operating system.) • Using Monitors (In concurrent programming, a monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true) • Atomic Transactions (Atomic read-modify-write access to shared variables is avoided, as each of the two Count variables is updated only by a single thread. Also, these variables stay incremented all the time; the relation remains correct when their values wrap around on an integer overflow) 5
  • 6. RACE CONDITION 6 What if T5 happened before T4? ?
  • 7. CRITICAL SECTION PROBLEM • A section of code, common to n cooperating processes, in which the processes may be accessing common variables. • A Critical Section Environment contains: • Entry Section Code requesting entry into the critical section. • Critical Section Code in which only one process can execute at any one time. • Exit Section The end of the critical section, releasing or allowing others in. • Remainder Section Rest of the code AFTER the critical section. 7
  • 8. CRITICAL SECTION PROBLEM • The critical section must ENFORCE ALL THREE of the following rules: • Mutual Exclusion: No more than one process can execute in its critical section at one time. • Progress: If no one is in the critical section and someone wants in, then those processes not in their remainder section must be able to decide in a finite time who should go in. • Bounded Wait: All requesters must eventually be let into the critical section 8
  • 9. CRITICAL SECTION PROBLEM 9 ENTRY SECTION CRITICAL SECTION EXIT SECTION REMAINDER SECTION
  • 10. PETERSON’S SOLUTION • To handle the problem of Critical Section (CS), Peterson gave an algorithm with a bounded waiting • Suppose there are N processes (P1, P2, … PN) and each of them at some point need to enter the Critical Section • A FLAG[] array of size N is maintained which is by default false and whenever a process need to enter the critical section it has to set its flag as true, i.e. suppose Pi wants to enter so it will set FLAG[i]=TRUE • There is another variable called TURN which indicates the process number which is currently to enter into the CS. The process that enters into the CS while exiting would change the TURN to another number from among the list of ready processes 10
  • 11. PETERSON’S SOLUTION 11 • If turn is 2 (say) then P2 enters the CS and while exiting sets the turn as 3 and thus P3 breaks out of wait loop FLAG[i] = false
  • 12. Synchronization Hardware • Problems of Critical Section are also solvable by hardware • Uniprocessor systems disables interrupts while a Process Pi is using the CS but it is a great disadvantage in multiprocessor systems • Some systems provide a lock functionality where a Process acquires a lock while entering the CS and releases the lock after leaving it. Thus another process trying to enter CS cannot enter as the entry is locked. It can only do so if it is free by acquiring the lock itself • Another advanced approach is the Atomic Instructions (Non- Interruptible instructions). 12
  • 13. MUTEX LOCKS • As the synchronization hardware solution is not easy to implement fro everyone, a strict software approach called Mutex Locks was introduced. In this approach, in the entry section of code, a LOCK is acquired over the critical resources modified and used inside critical section, and in the exit section that LOCK is released. • As the resource is locked while a process executes its critical section hence no other process can access it 13
  • 14. SEMAPHORES • A more robust alternative to simple mutual exclusions is to use semaphores, which are integer variables for which only two ( atomic ) operations are defined, the wait and signal operations, as shown in the following figure • As given here, these are not atomic as written in "macro code". We define these operations, however, to be atomic (Protected by a hardware lock.) 14 WAIT ( S ): while ( S <= 0 ); S = S - 1; SIGNAL ( S ): S = S + 1;
  • 15. SEMAPHORES • FORMAT: wait ( mutex ); // Mutual exclusion: mutex init to 1. CRITICAL SECTION signal( mutex ); REMAINDER • Note that not only must the variable-changing steps ( S-- and S++ ) be indivisible, it is also necessary that for the wait operation when the test proves false that there be no interruptions before S gets decremented. It IS okay, however, for the busy loop to be interrupted when the test is true, which prevents the system from hanging forever 15
  • 16. SEMAPHORES • PROPERTIES 1. Simple 2. Works with many processes 3. Can have many different critical sections with different semaphores 4. Each critical section has unique access semaphores 5. Can permit multiple processes into the critical section at once, if desirable. 16
  • 17. SEMAPHORES • TYPES Semaphores are mainly of two types: 1. Binary Semaphore It is a special form of semaphore used for implementing mutual exclusion, hence it is often called Mutex. A binary semaphore is initialized to 1 and only takes the value 0 and 1 during execution of a program. 2. Counting Semaphores These are used to implement bounded concurrency. 17
  • 18. SEMAPHORES • Semaphores can be used to force synchronization ( precedence ) if the preceding process does a signal at the end, and the follower does wait at beginning. For example, here we want P1 to execute before P2. P1: P2: statement 1; wait ( synch ); signal ( synch ); statement 2; • To PREVENT looping, we redefine the semaphore structure as: typedef struct { int value; struct process *list; //linked list of PTBL waiting on S } SEMAPHORE; 18
  • 19. SEMAPHORES • It's critical that these be atomic - in uniprocessors we can disable interrupts, but in multiprocessors other mechanisms for atomicity are needed. 19 SEMAPHORE s; wait(s) { s.value = s.value - 1; if ( s.value < 0 ) { add this process to s.L; block; } } SEMAPHORE s; signal(s) { s.value = s.value + 1; if ( s.value <= 0 ) { remove a process P from s.L; wakeup(P); } }
  • 20. SEMAPHORES • DEADLOCK • One important problem that can arise when using semaphores to block processes waiting for a limited resource is the problem of deadlocks, which occur when multiple processes are blocked, each waiting for a resource that can only be freed by one of the other ( blocked ) processes, as illustrated in the following example. 20
  • 21. SEMAPHORES • STARVATION • Another problem to consider is that of starvation, in which one or more processes gets blocked forever, and never get a chance to take their turn in the critical section. For example, in the semaphores above, we did not specify the algorithms for adding processes to the waiting queue in the semaphore in the wait( ) call, or selecting one to be removed from the queue in the signal( ) call. If the method chosen is a FIFO queue, then every process will eventually get their turn, but if a LIFO queue is implemented instead, then the first process to start waiting could starve. 21
  • 22. THE BOUNDED-BUFFER PROBLEM • This is a generalization of the producer-consumer problem wherein access is controlled to a shared group of buffers of a limited size. • In this solution, the two counting semaphores "full" and "empty" keep track of the current number of full and empty buffers respectively ( and initialized to 0 and N respectively. ) The binary semaphore mutex controls access to the critical section. The producer and consumer processes are nearly identical - One can think of the producer as producing full buffers, and the consumer producing empty buffers. 22
  • 24. THE READERS-WRITERS PROBLEM • THE READERS/WRITERS PROBLEM: • This is the same as the Producer / Consumer problem except - we now can have many concurrent readers and one exclusive writer. • Locks: are shared (for the readers) and exclusive (for the writer). • Two possible ( contradictory ) guidelines can be used: • No reader is kept waiting unless a writer holds the lock (the readers have precedence). • If a writer is waiting for access, no new reader gains access (writer has precedence). • ( NOTE: starvation can occur on either of these rules if they are followed rigorously.) 24
  • 25. THE READERS-WRITERS PROBLEM 25 Reader: do { wait( mutex ); /* Allow 1 reader in entry*/ readcount = readcount + 1; if readcount == 1 then wait(wrt ); /* 1st reader locks writer */ signal( mutex ); /* reading is performed */ wait( mutex ); readcount = readcount - 1; if readcount == 0 then signal(wrt ); /*last reader frees writer */ signal( mutex ); } while(TRUE); Writer: do { wait( wrt ); /* writing is performed */ signal( wrt ); } while(TRUE); THE READERS/WRITERS PROBLEM: BINARY_SEMAPHORE wrt = 1; BINARY_SEMAPHORE mutex = 1; int readcount = 0;
  • 26. DINING PHILOSOPHERS PROBLEM 26 • Five philosophers sitting at a round dining table to eat • Each one has got a plate and one chopstick at the right side of each plate • To start eating each of them need a plate and two chopsticks • Thus, clearly there will be a deadlock and starvation because of the limited number of chopsticks • This can be solved by either: • Allow only 4 philosophers to be hungry at a time • Allow pickup only if both chopsticks are available. ( Done in critical section ) • Odd # philosopher always picks up left chopstick 1st, even # philosopher always picks up right chopstick 1st
  • 27. DINING PHILOSOPHERS PROBLEM • One possible solution, as shown in the following code section, is to use a set of five semaphores ( chopsticks[ 5 ] ), and to have each hungry philosopher first wait on their left chopstick ( chopsticks[ i ] ), and then wait on their right chopstick ( chopsticks[ ( i + 1 ) % 5 ] ) 27
  • 28. SYNCHRONIZATION EXAMPLES • SYNCHRONIZATION IN WINDOWS • SYNCHRONIZATION IN LINUX 28
  • 29. MONITORS • High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. • Semaphores can be very useful for solving concurrency problems, but only if programmers use them properly. If even one process fails to abide by the proper use of semaphores, either accidentally or deliberately, then the whole system breaks down. 29
  • 30. MONITORS • A monitor is essentially a class, in which all data is private, and with the special restriction that only one method within any given monitor object may be active at the same time. • An additional restriction is that monitor methods may only access the shared data within the monitor and any data passed to them as parameters, i.e. they cannot access any data external to the monitor. 30
  • 31. MONITORS • In order to fully realize the potential of monitors, we need to introduce one additional new data type, known as a condition. • A variable of type condition has only two legal operations, wait and signal. I.e. if X was defined as type condition, then legal operations would be X.wait( ) and X.signal( ) • The wait operation blocks a process until some other process calls signal, and adds the blocked process onto a list associated with that condition. • The signal process does nothing if there are no processes waiting on that condition. Otherwise it wakes up exactly one process from the condition's list of waiting processes. ( Contrast this with counting semaphores, which always affect the semaphore on a signal call. ) 31
  • 32. ATOMIC TRANSACTIONS • Database operations frequently need to carry out atomic transactions, in which the entire transaction must either complete or not occur at all. • The classic example is a transfer of funds, which involves withdrawing funds from one account and depositing them into another - Either both halves of the transaction must complete, or neither must complete. • Operating Systems can be viewed as having many of the same needs and problems as databases, in that an OS can be said to manage a small database of process-related information. As such, OSs can benefit from emulating some of the techniques originally developed for databases. 32
  • 33. ATOMIC TRANSACTIONS • FUNDAMENTAL PRINCIPLES – A C I D • Atomicity – to outside world, transaction happens indivisibly • Consistency – transaction preserves system invariants • Isolated – transactions do not interfere with each other • Durable – once a transaction “commits,” the changes are permanent 33