SlideShare a Scribd company logo
1 of 27
Download to read offline
Part II
Process Management
Chapter 6: Process Synchronization


                                1
Process Synchronization

Why is synchronization needed?
Race Conditions
Critical Sections
Pure Software Solutions
Hardware Support
Semaphores
Monitors
Message Passing
                                 2
Why is Synchronization Needed? 1/4

        int    Count = 10;

        Process 1     Process 2

        Count++; Count--;

        Count = ? 9, 10 or 11?



                                  3
Why is Synchronization Needed? 2/4
   int   Count = 10;

   Process 1              Process 2

   LOAD Reg, Count        LOAD Reg, Count
   ADD   #1               SUB   #1
   STORE Reg, Count       STORE Reg, Count



  The problem is that the execution flow may be
  switched in the middle!
                                                  4
Why is Synchronization Needed? 3/4

         Process 1                   Process 2
  Inst     Reg   Memory    Inst       Reg     Memory

LOAD        10       10
                          LOAD         10        10
                          SUB           9        10
ADD         11       10
STORE       11       11   erases the previous value 11

                          STORE         9         9
                                                         5
Why is Synchronization Needed? 4/4
         Process 1                   Process 2
  Inst     Reg   Memory    Inst       Reg     Memory

LOAD        10       10
ADD         11       10
                          LOAD         10         10
                          SUB           9         10
                          STORE         9         9
STORE       11       11   erases the previous value 9
                                                        6
Race Conditions
 A Race Condition occurs, if
   two or more processes/threads access and
   manipulate the same data concurrently, and
   the outcome of the execution depends on the
   particular order in which the access takes
   place.
Synchronization is needed to prevent race
conditions from happening.
Synchronization is a difficult topic. Don’t miss
a class; otherwise, you will miss a lot of things.
                                                     7
Critical Section and Mutual Exclusion
   A critical section is a section of code in which a
   process accesses shared resources.
   Thus, the execution of critical sections must be
   mutually exclusive (e.g., at most one process can
   be in its critical section at any time).
   The critical-section problem is to design a
   protocol that processes can use to cooperate.
                                       critical sections
       int    count; // shared


count++;           count--;           cout << count;
                                                       8
The Critical Section Protocol
do {                   A critical section protocol
                       consists of two parts: an
       entry section
                       entry section and an exit
                       section.
   critical section
                       Between them is the
       exit section    critical section that must
                       run in a mutually
                       exclusive way.
} while (1);

                                                     9
Solutions to the Critical Section Problem

 Any solution to the critical section problem
 must satisfy the following three conditions:
   Mutual Exclusion
   Progress
   Bounded Waiting
 Moreover, the solution cannot depend on
 CPU’s relative speed and scheduling policy.


                                                10
Mutual Exclusion
If a process P is executing in its critical section,
then no other processes can be executing in their
critical sections.
The entry protocol should be capable of
blocking processes that wish to enter but cannot.
Moreover, when the process that is executing in
its critical section exits, the entry protocol must
be able to know this fact and allows a waiting
process to enter.
                                                 11
Progress
If no process is executing in its critical section
and some processes wish to enter their critical
sections, then
   Only those processes that are waiting to enter
   can participate in the competition (to enter
   their critical sections).
   No other process can influence this decision.
   This decision cannot be postponed indefinitely.

                                               12
Bounded Waiting
After a process made a request to enter its
critical section and before it is granted the
permission to enter, there exists a bound on the
number of times that other processes are
allowed to enter.
Hence, even though a process may be blocked
by other waiting processes, it will not be waiting
forever.

                                                13
Software Solutions for
        Two Processes
Suppose we have two processes, P0 and P1.
Let one process be Pi and the other be Pj, where
j = 1- i. Thus, if i = 0 (resp., i = 1), then j = 1
(resp., j = 0).
We want to design the enter-exit protocol for a
critical section so that mutual exclusion is
guaranteed.


                                                 14
Algorithm I: 1/2
                                            Global variable
process Pi                                  turn controls who
do {         if it is not my turn, I wait
                                            can enter the
                          enter             critical section.
   while (turn != i);                       Since turn is either
                                            0 or 1, only one can
    critical section
                                            enter.
   turn = j; exit
                                            However, processes
                                            are forced to run in
} while (1);                                an alternating way.

 I am done, it is your turn now                             15
Algorithm I: 2/2
                                            This solution does
process Pi
                                            not fulfill the
do {         if it is not my turn, I wait
                                            progress condition.
                          enter             If Pj exits by setting
                                            turn to i and
   while (turn != i);
                                            terminates, Pi can
    critical section                        enter but cannot
   turn = j; exit                           enter again.
                                            Thus, an irrelevant
                                            process can block
} while (1);                                other processes
                                            from entering a
 I am done, it is your turn now             critical section. 16
Algorithm II: 1/2
bool     flag[2];
                                      Variable flag[i]
         I am interested
do {
                                      is the “state” of
               wait for you           process Pi:
                                      interested or not-
    flag[i] = TRUE;           enter   interested.
    while (flag[j]);                  Pi expresses its
       critical section               intention to enter,
                                      waits for Pj to exit,
    flag[i] = FALSE; exit             enters its section,
                                      and finally changes
                                      to “I am out” upon
}          I am not interested        exit.
                                                        17
Algorithm II: 2/2
bool   flag[2];                    The correctness of this
       I am interested             algorithm is timing
do {
              wait for you         dependent!
                                   If both processes set
   flag[i] = TRUE;           enter flag[i] and
   while (flag[j]);                flag[j] to TRUE at
                                   the same time, then
     critical section              both will be looping at
   flag[i] = FALSE; exit           the while forever
                                   and no one can enter.
                                   Bounded waiting does
}         I am not interested      not hold.
                                                     18
Algorithm III: a Combination 1/4
            bool flag[2]; // process Pi
            int turn;
                      I am interested
            do {                      yield to you first

                flag[i] = TRUE;               enter
                turn = j;
                while (flag[j] && turn == j);
I am done
                critical section      wait while you are
                                      interested and it is
                flag[i] = FALSE; exit
                                      your turn.

            }                                              19
Algorithm III: Mutual Exclusion 2/4
       flag[i] = TRUE;               process Pi
       turn = j;
       while (flag[j] && turn == j);
If both processes are in their critical sections, then
    flag[j] && turn == j (Pi) and flag[i] && turn
    == i (Pj) are both FALSE.
    flag[i] and flag[j] are both TRUE
    Thus, turn == i and turn == j are FALSE.
    Since turn can hold one value, only one of turn == i
    or turn == j is FALSE, but not both.
  We have a contradiction and Pi and Pj cannot be in their
  critical sections at the same time.
                                                       20
Algorithm III: Progress 3/4
      flag[i] = TRUE;               process Pi
      turn = j;
      while (flag[j] && turn == j);

If Pi is waiting to enter, it must be executing its
while loop.
Suppose Pj is not in its critical section:
   If Pj is not interested in entering, flag[j] was
   set to FALSE when Pj exits. Thus, Pi may enter.
   If Pj wishes to enter and sets flag[j] to TRUE,
   it will set turn to i and Pi may enter.
In both cases, processes that are not waiting do not
block the waiting processes from entering.          21
Algorithm III: Bounded Waiting 4/4
        flag[i] = TRUE;               process Pi
        turn = j;
        while (flag[j] && turn == j);
   When Pi wishes to enter:
     If Pj is outside of its critical section, then flag[j] is
     FALSE and Pi may enter.
      If Pj is in its critical section, eventually it will set
      flag[j] to FALSE and Pi may enter.
      If Pj is in the entry section, Pi may enter if it reaches
      while first. Otherwise, Pj enters and Pi may enter
      after Pj sets flag[j] to FALSE and exits.
   Thus, Pi waits at most one round!
                                                                  22
Hardware Support

There are two types of hardware
synchronization supports:
   Disabling/Enabling interrupts: This is slow
  and difficult to implement on multiprocessor
  systems.
   Special privileged machine instructions:
     Test and set (TS)
     Swap
     Compare and Swap (CS)
                                             23
Interrupt Disabling
do {                              Because interrupts are
                                  disabled, no context
                       entry      switch will occur in a
       disable interrupts         critical section.
                                  Infeasible in a
       critical section           multiprocessor system
                                  because all CPUs must
       enable interrupts          be informed.
                           exit   Some features that
} while (1);                      depend on interrupts
                                  (e.g., clock) may not
                                  work properly.
                                                      24
Special Machine Instructions
Atomic: These instructions execute as one
uninterruptible unit. More precisely, when such
an instruction runs, all other instructions being
executed in various stages by the CPUs will be
stopped (and perhaps re-issued later) until this
instruction finishes.
Thus, if two such instructions are issued at the
same time, even though on different CPUs, they
will be executed sequentially.
Privileged: These instructions are, in general,
privileged, meaning they can only execute in
supervisor or kernel mode.
                                               25
The Test-and-Set Instruction
bool TS(bool *key)      bool   lock = FALSE;
{
   bool save = *key;    do {
   *key = TRUE;
   return save;                            entry
}                           while (TS(&lock));
                            critical section
 Mutual exclusion is
 obvious as only one TS   lock = FALSE; exit
 instruction can run at a
 time.
 However, progress and } while (1);
 bounded waiting may
 not be satisfied.                           26
Problems with Software and
      Hardware Solutions
All of these solutions use busy waiting.
Busy waiting means a process waits by executing a tight
loop to check the status/value of a variable.
Busy waiting may be needed on a multiprocessor system;
however, it wastes CPU cycles that some other processes
may use productively.
Even though some personal/lower-end systems may allow
users to use some atomic instructions, unless the system
is lightly loaded, CPU and system performance can be
low, although a programmer may “think” his/her
program looks more efficient.
So, we need a better solution.
                                                     27

More Related Content

What's hot

15. Transactions in DBMS
15. Transactions in DBMS15. Transactions in DBMS
15. Transactions in DBMSkoolkampus
 
Computer architecture pipelining
Computer architecture pipeliningComputer architecture pipelining
Computer architecture pipeliningMazin Alwaaly
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitorssgpraju
 
Petri Nets: Properties, Analysis and Applications
Petri Nets: Properties, Analysis and ApplicationsPetri Nets: Properties, Analysis and Applications
Petri Nets: Properties, Analysis and ApplicationsDr. Mohamed Torky
 
Critical Section in Operating System
Critical Section in Operating SystemCritical Section in Operating System
Critical Section in Operating SystemMOHIT AGARWAL
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's typesNishant Joshi
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-SystemsVenkata Sreeram
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Ravindra Raju Kolahalam
 
Operating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksOperating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksMukesh Chinta
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler designAnul Chaudhary
 
First-Come-First-Serve (FCFS)
First-Come-First-Serve (FCFS)First-Come-First-Serve (FCFS)
First-Come-First-Serve (FCFS)nikeAthena
 
User datagram protocol (udp)
User datagram protocol (udp)User datagram protocol (udp)
User datagram protocol (udp)Ramola Dhande
 

What's hot (20)

15. Transactions in DBMS
15. Transactions in DBMS15. Transactions in DBMS
15. Transactions in DBMS
 
Computer architecture pipelining
Computer architecture pipeliningComputer architecture pipelining
Computer architecture pipelining
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
 
Petri Nets: Properties, Analysis and Applications
Petri Nets: Properties, Analysis and ApplicationsPetri Nets: Properties, Analysis and Applications
Petri Nets: Properties, Analysis and Applications
 
Critical Section in Operating System
Critical Section in Operating SystemCritical Section in Operating System
Critical Section in Operating System
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's types
 
Multiple access protocol
Multiple access protocolMultiple access protocol
Multiple access protocol
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-Systems
 
Data link layer
Data link layer Data link layer
Data link layer
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Operating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksOperating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and Deadlocks
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
 
The medium access sublayer
 The medium  access sublayer The medium  access sublayer
The medium access sublayer
 
First-Come-First-Serve (FCFS)
First-Come-First-Serve (FCFS)First-Come-First-Serve (FCFS)
First-Come-First-Serve (FCFS)
 
User datagram protocol (udp)
User datagram protocol (udp)User datagram protocol (udp)
User datagram protocol (udp)
 
Chapter 3 - Processes
Chapter 3 - ProcessesChapter 3 - Processes
Chapter 3 - Processes
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 

Similar to Process Synchronization Techniques

14- Process Synchronization.pptx
14- Process Synchronization.pptx14- Process Synchronization.pptx
14- Process Synchronization.pptxmobeenahmed49
 
Critical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy BijjamCritical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy BijjamRamakrishna Reddy Bijjam
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionBipul Chandra Kar
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process SynchronizationSonali Chauhan
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronizationAbeera Naeem
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmSouvik Roy
 
criticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdfcriticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdfBhanuCharan9
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process SynchronizationShipra Swati
 
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
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)Nagarajan
 
Os2 2
Os2 2Os2 2
Os2 2issbp
 

Similar to Process Synchronization Techniques (20)

14- Process Synchronization.pptx
14- Process Synchronization.pptx14- Process Synchronization.pptx
14- Process Synchronization.pptx
 
Critical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy BijjamCritical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy Bijjam
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem Solution
 
Shared Memory
Shared MemoryShared Memory
Shared Memory
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
CH05.pdf
CH05.pdfCH05.pdf
CH05.pdf
 
Ipc feb4
Ipc feb4Ipc feb4
Ipc feb4
 
6 Synchronisation
6 Synchronisation6 Synchronisation
6 Synchronisation
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's Algorithm
 
criticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdfcriticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdf
 
Ch7
Ch7Ch7
Ch7
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
Operating System
Operating SystemOperating System
Operating System
 
Lecture16-17.ppt
Lecture16-17.pptLecture16-17.ppt
Lecture16-17.ppt
 
Cs problem [repaired]
Cs problem [repaired]Cs problem [repaired]
Cs problem [repaired]
 
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
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)
 
Monitors
MonitorsMonitors
Monitors
 
Os2 2
Os2 2Os2 2
Os2 2
 

More from Mohd Arif

Bootp and dhcp
Bootp and dhcpBootp and dhcp
Bootp and dhcpMohd Arif
 
Arp and rarp
Arp and rarpArp and rarp
Arp and rarpMohd Arif
 
User datagram protocol
User datagram protocolUser datagram protocol
User datagram protocolMohd Arif
 
Project identification
Project identificationProject identification
Project identificationMohd Arif
 
Project evalaution techniques
Project evalaution techniquesProject evalaution techniques
Project evalaution techniquesMohd Arif
 
Presentation
PresentationPresentation
PresentationMohd Arif
 
Pointers in c
Pointers in cPointers in c
Pointers in cMohd Arif
 
Peer to-peer
Peer to-peerPeer to-peer
Peer to-peerMohd Arif
 
Overview of current communications systems
Overview of current communications systemsOverview of current communications systems
Overview of current communications systemsMohd Arif
 
Overall 23 11_2007_hdp
Overall 23 11_2007_hdpOverall 23 11_2007_hdp
Overall 23 11_2007_hdpMohd Arif
 
Objectives of budgeting
Objectives of budgetingObjectives of budgeting
Objectives of budgetingMohd Arif
 
Network management
Network managementNetwork management
Network managementMohd Arif
 
Networing basics
Networing basicsNetworing basics
Networing basicsMohd Arif
 
Iris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformIris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformMohd Arif
 
Ip sec and ssl
Ip sec and  sslIp sec and  ssl
Ip sec and sslMohd Arif
 
Ip security in i psec
Ip security in i psecIp security in i psec
Ip security in i psecMohd Arif
 
Intro to comp. hardware
Intro to comp. hardwareIntro to comp. hardware
Intro to comp. hardwareMohd Arif
 

More from Mohd Arif (20)

Bootp and dhcp
Bootp and dhcpBootp and dhcp
Bootp and dhcp
 
Arp and rarp
Arp and rarpArp and rarp
Arp and rarp
 
User datagram protocol
User datagram protocolUser datagram protocol
User datagram protocol
 
Project identification
Project identificationProject identification
Project identification
 
Project evalaution techniques
Project evalaution techniquesProject evalaution techniques
Project evalaution techniques
 
Presentation
PresentationPresentation
Presentation
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Peer to-peer
Peer to-peerPeer to-peer
Peer to-peer
 
Overview of current communications systems
Overview of current communications systemsOverview of current communications systems
Overview of current communications systems
 
Overall 23 11_2007_hdp
Overall 23 11_2007_hdpOverall 23 11_2007_hdp
Overall 23 11_2007_hdp
 
Objectives of budgeting
Objectives of budgetingObjectives of budgeting
Objectives of budgeting
 
Network management
Network managementNetwork management
Network management
 
Networing basics
Networing basicsNetworing basics
Networing basics
 
Loaders
LoadersLoaders
Loaders
 
Lists
ListsLists
Lists
 
Iris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformIris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platform
 
Ip sec and ssl
Ip sec and  sslIp sec and  ssl
Ip sec and ssl
 
Ip security in i psec
Ip security in i psecIp security in i psec
Ip security in i psec
 
Intro to comp. hardware
Intro to comp. hardwareIntro to comp. hardware
Intro to comp. hardware
 
Heap sort
Heap sortHeap sort
Heap sort
 

Recently uploaded

Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessWSO2
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfAarwolf Industries LLC
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sectoritnewsafrica
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Nikki Chapple
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 

Recently uploaded (20)

Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with Platformless
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdf
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 

Process Synchronization Techniques

  • 1. Part II Process Management Chapter 6: Process Synchronization 1
  • 2. Process Synchronization Why is synchronization needed? Race Conditions Critical Sections Pure Software Solutions Hardware Support Semaphores Monitors Message Passing 2
  • 3. Why is Synchronization Needed? 1/4 int Count = 10; Process 1 Process 2 Count++; Count--; Count = ? 9, 10 or 11? 3
  • 4. Why is Synchronization Needed? 2/4 int Count = 10; Process 1 Process 2 LOAD Reg, Count LOAD Reg, Count ADD #1 SUB #1 STORE Reg, Count STORE Reg, Count The problem is that the execution flow may be switched in the middle! 4
  • 5. Why is Synchronization Needed? 3/4 Process 1 Process 2 Inst Reg Memory Inst Reg Memory LOAD 10 10 LOAD 10 10 SUB 9 10 ADD 11 10 STORE 11 11 erases the previous value 11 STORE 9 9 5
  • 6. Why is Synchronization Needed? 4/4 Process 1 Process 2 Inst Reg Memory Inst Reg Memory LOAD 10 10 ADD 11 10 LOAD 10 10 SUB 9 10 STORE 9 9 STORE 11 11 erases the previous value 9 6
  • 7. Race Conditions A Race Condition occurs, if two or more processes/threads access and manipulate the same data concurrently, and the outcome of the execution depends on the particular order in which the access takes place. Synchronization is needed to prevent race conditions from happening. Synchronization is a difficult topic. Don’t miss a class; otherwise, you will miss a lot of things. 7
  • 8. Critical Section and Mutual Exclusion A critical section is a section of code in which a process accesses shared resources. Thus, the execution of critical sections must be mutually exclusive (e.g., at most one process can be in its critical section at any time). The critical-section problem is to design a protocol that processes can use to cooperate. critical sections int count; // shared count++; count--; cout << count; 8
  • 9. The Critical Section Protocol do { A critical section protocol consists of two parts: an entry section entry section and an exit section. critical section Between them is the exit section critical section that must run in a mutually exclusive way. } while (1); 9
  • 10. Solutions to the Critical Section Problem Any solution to the critical section problem must satisfy the following three conditions: Mutual Exclusion Progress Bounded Waiting Moreover, the solution cannot depend on CPU’s relative speed and scheduling policy. 10
  • 11. Mutual Exclusion If a process P is executing in its critical section, then no other processes can be executing in their critical sections. The entry protocol should be capable of blocking processes that wish to enter but cannot. Moreover, when the process that is executing in its critical section exits, the entry protocol must be able to know this fact and allows a waiting process to enter. 11
  • 12. Progress If no process is executing in its critical section and some processes wish to enter their critical sections, then Only those processes that are waiting to enter can participate in the competition (to enter their critical sections). No other process can influence this decision. This decision cannot be postponed indefinitely. 12
  • 13. Bounded Waiting After a process made a request to enter its critical section and before it is granted the permission to enter, there exists a bound on the number of times that other processes are allowed to enter. Hence, even though a process may be blocked by other waiting processes, it will not be waiting forever. 13
  • 14. Software Solutions for Two Processes Suppose we have two processes, P0 and P1. Let one process be Pi and the other be Pj, where j = 1- i. Thus, if i = 0 (resp., i = 1), then j = 1 (resp., j = 0). We want to design the enter-exit protocol for a critical section so that mutual exclusion is guaranteed. 14
  • 15. Algorithm I: 1/2 Global variable process Pi turn controls who do { if it is not my turn, I wait can enter the enter critical section. while (turn != i); Since turn is either 0 or 1, only one can critical section enter. turn = j; exit However, processes are forced to run in } while (1); an alternating way. I am done, it is your turn now 15
  • 16. Algorithm I: 2/2 This solution does process Pi not fulfill the do { if it is not my turn, I wait progress condition. enter If Pj exits by setting turn to i and while (turn != i); terminates, Pi can critical section enter but cannot turn = j; exit enter again. Thus, an irrelevant process can block } while (1); other processes from entering a I am done, it is your turn now critical section. 16
  • 17. Algorithm II: 1/2 bool flag[2]; Variable flag[i] I am interested do { is the “state” of wait for you process Pi: interested or not- flag[i] = TRUE; enter interested. while (flag[j]); Pi expresses its critical section intention to enter, waits for Pj to exit, flag[i] = FALSE; exit enters its section, and finally changes to “I am out” upon } I am not interested exit. 17
  • 18. Algorithm II: 2/2 bool flag[2]; The correctness of this I am interested algorithm is timing do { wait for you dependent! If both processes set flag[i] = TRUE; enter flag[i] and while (flag[j]); flag[j] to TRUE at the same time, then critical section both will be looping at flag[i] = FALSE; exit the while forever and no one can enter. Bounded waiting does } I am not interested not hold. 18
  • 19. Algorithm III: a Combination 1/4 bool flag[2]; // process Pi int turn; I am interested do { yield to you first flag[i] = TRUE; enter turn = j; while (flag[j] && turn == j); I am done critical section wait while you are interested and it is flag[i] = FALSE; exit your turn. } 19
  • 20. Algorithm III: Mutual Exclusion 2/4 flag[i] = TRUE; process Pi turn = j; while (flag[j] && turn == j); If both processes are in their critical sections, then flag[j] && turn == j (Pi) and flag[i] && turn == i (Pj) are both FALSE. flag[i] and flag[j] are both TRUE Thus, turn == i and turn == j are FALSE. Since turn can hold one value, only one of turn == i or turn == j is FALSE, but not both. We have a contradiction and Pi and Pj cannot be in their critical sections at the same time. 20
  • 21. Algorithm III: Progress 3/4 flag[i] = TRUE; process Pi turn = j; while (flag[j] && turn == j); If Pi is waiting to enter, it must be executing its while loop. Suppose Pj is not in its critical section: If Pj is not interested in entering, flag[j] was set to FALSE when Pj exits. Thus, Pi may enter. If Pj wishes to enter and sets flag[j] to TRUE, it will set turn to i and Pi may enter. In both cases, processes that are not waiting do not block the waiting processes from entering. 21
  • 22. Algorithm III: Bounded Waiting 4/4 flag[i] = TRUE; process Pi turn = j; while (flag[j] && turn == j); When Pi wishes to enter: If Pj is outside of its critical section, then flag[j] is FALSE and Pi may enter. If Pj is in its critical section, eventually it will set flag[j] to FALSE and Pi may enter. If Pj is in the entry section, Pi may enter if it reaches while first. Otherwise, Pj enters and Pi may enter after Pj sets flag[j] to FALSE and exits. Thus, Pi waits at most one round! 22
  • 23. Hardware Support There are two types of hardware synchronization supports: Disabling/Enabling interrupts: This is slow and difficult to implement on multiprocessor systems. Special privileged machine instructions: Test and set (TS) Swap Compare and Swap (CS) 23
  • 24. Interrupt Disabling do { Because interrupts are disabled, no context entry switch will occur in a disable interrupts critical section. Infeasible in a critical section multiprocessor system because all CPUs must enable interrupts be informed. exit Some features that } while (1); depend on interrupts (e.g., clock) may not work properly. 24
  • 25. Special Machine Instructions Atomic: These instructions execute as one uninterruptible unit. More precisely, when such an instruction runs, all other instructions being executed in various stages by the CPUs will be stopped (and perhaps re-issued later) until this instruction finishes. Thus, if two such instructions are issued at the same time, even though on different CPUs, they will be executed sequentially. Privileged: These instructions are, in general, privileged, meaning they can only execute in supervisor or kernel mode. 25
  • 26. The Test-and-Set Instruction bool TS(bool *key) bool lock = FALSE; { bool save = *key; do { *key = TRUE; return save; entry } while (TS(&lock)); critical section Mutual exclusion is obvious as only one TS lock = FALSE; exit instruction can run at a time. However, progress and } while (1); bounded waiting may not be satisfied. 26
  • 27. Problems with Software and Hardware Solutions All of these solutions use busy waiting. Busy waiting means a process waits by executing a tight loop to check the status/value of a variable. Busy waiting may be needed on a multiprocessor system; however, it wastes CPU cycles that some other processes may use productively. Even though some personal/lower-end systems may allow users to use some atomic instructions, unless the system is lightly loaded, CPU and system performance can be low, although a programmer may “think” his/her program looks more efficient. So, we need a better solution. 27