SlideShare a Scribd company logo
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

Applications of paralleL processing
Applications of paralleL processingApplications of paralleL processing
Applications of paralleL processing
Page Maker
 
Communication in Distributed Systems
Communication in Distributed SystemsCommunication in Distributed Systems
Communication in Distributed Systems
Dilum Bandara
 
02 protocol architecture
02 protocol architecture02 protocol architecture
02 protocol architecture
chameli devi group of institutions
 
Remote Procedure Call in Distributed System
Remote Procedure Call in Distributed SystemRemote Procedure Call in Distributed System
Remote Procedure Call in Distributed System
PoojaBele1
 
File models and file accessing models
File models and file accessing modelsFile models and file accessing models
File models and file accessing models
ishmecse13
 
Interprocess communication (IPC) IN O.S
Interprocess communication (IPC) IN O.SInterprocess communication (IPC) IN O.S
Interprocess communication (IPC) IN O.S
Hussain Ala'a Alkabi
 
message passing
 message passing message passing
message passing
Ashish Kumar
 
Clock synchronization in distributed system
Clock synchronization in distributed systemClock synchronization in distributed system
Clock synchronization in distributed system
Sunita Sahu
 
SYNCHRONIZATION
SYNCHRONIZATIONSYNCHRONIZATION
SYNCHRONIZATION
atanuanwesha
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
Harshana Madusanka Jayamaha
 
CS6601 DISTRIBUTED SYSTEMS
CS6601 DISTRIBUTED SYSTEMSCS6601 DISTRIBUTED SYSTEMS
CS6601 DISTRIBUTED SYSTEMS
Kathirvel Ayyaswamy
 
Point To Point Protocol
Point To Point ProtocolPoint To Point Protocol
Point To Point Protocol
Phan Vuong
 
Synchronization in distributed computing
Synchronization in distributed computingSynchronization in distributed computing
Synchronization in distributed computing
SVijaylakshmi
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem Solution
Bipul Chandra Kar
 
Os unit 3 , process management
Os unit 3 , process managementOs unit 3 , process management
Os unit 3 , process management
Arnav Chowdhury
 
Physical organization of parallel platforms
Physical organization of parallel platformsPhysical organization of parallel platforms
Physical organization of parallel platforms
Syed Zaid Irshad
 
Distributed Systems
Distributed SystemsDistributed Systems
Distributed Systems
Rupsee
 
6.distributed shared memory
6.distributed shared memory6.distributed shared memory
6.distributed shared memory
Gd Goenka University
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
Ravindra Raju Kolahalam
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
Adeel Rasheed
 

What's hot (20)

Applications of paralleL processing
Applications of paralleL processingApplications of paralleL processing
Applications of paralleL processing
 
Communication in Distributed Systems
Communication in Distributed SystemsCommunication in Distributed Systems
Communication in Distributed Systems
 
02 protocol architecture
02 protocol architecture02 protocol architecture
02 protocol architecture
 
Remote Procedure Call in Distributed System
Remote Procedure Call in Distributed SystemRemote Procedure Call in Distributed System
Remote Procedure Call in Distributed System
 
File models and file accessing models
File models and file accessing modelsFile models and file accessing models
File models and file accessing models
 
Interprocess communication (IPC) IN O.S
Interprocess communication (IPC) IN O.SInterprocess communication (IPC) IN O.S
Interprocess communication (IPC) IN O.S
 
message passing
 message passing message passing
message passing
 
Clock synchronization in distributed system
Clock synchronization in distributed systemClock synchronization in distributed system
Clock synchronization in distributed system
 
SYNCHRONIZATION
SYNCHRONIZATIONSYNCHRONIZATION
SYNCHRONIZATION
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
 
CS6601 DISTRIBUTED SYSTEMS
CS6601 DISTRIBUTED SYSTEMSCS6601 DISTRIBUTED SYSTEMS
CS6601 DISTRIBUTED SYSTEMS
 
Point To Point Protocol
Point To Point ProtocolPoint To Point Protocol
Point To Point Protocol
 
Synchronization in distributed computing
Synchronization in distributed computingSynchronization in distributed computing
Synchronization in distributed computing
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem Solution
 
Os unit 3 , process management
Os unit 3 , process managementOs unit 3 , process management
Os unit 3 , process management
 
Physical organization of parallel platforms
Physical organization of parallel platformsPhysical organization of parallel platforms
Physical organization of parallel platforms
 
Distributed Systems
Distributed SystemsDistributed Systems
Distributed Systems
 
6.distributed shared memory
6.distributed shared memory6.distributed shared memory
6.distributed shared memory
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 

Similar to Synchronization

14- Process Synchronization.pptx
14- Process Synchronization.pptx14- Process Synchronization.pptx
14- Process Synchronization.pptx
mobeenahmed49
 
Critical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy BijjamCritical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy Bijjam
Ramakrishna Reddy Bijjam
 
Shared Memory
Shared MemoryShared Memory
Shared Memory
Mustafa Ugur Oduncu
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
Sonali Chauhan
 
CH05.pdf
CH05.pdfCH05.pdf
CH05.pdf
ImranKhan880955
 
Ipc feb4
Ipc feb4Ipc feb4
Ipc feb4
Ruchi Sharma
 
6 Synchronisation
6 Synchronisation6 Synchronisation
6 Synchronisation
Dr. Loganathan R
 
OS Process synchronization Unit3 synchronization
OS Process synchronization Unit3  synchronizationOS Process synchronization Unit3  synchronization
OS Process synchronization Unit3 synchronization
subhamchy2005
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
Abeera Naeem
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's Algorithm
Souvik Roy
 
criticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdfcriticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdf
BhanuCharan9
 
Ch7
Ch7Ch7
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
Shipra Swati
 
Operating System
Operating SystemOperating System
Operating System
Subhasis Dash
 
Lecture16-17.ppt
Lecture16-17.pptLecture16-17.ppt
Lecture16-17.ppt
ssuserf67e3a
 
Cs problem [repaired]
Cs problem [repaired]Cs problem [repaired]
Cs problem [repaired]
Pradeep Kumar TS
 
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
DelonPierre
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)
Nagarajan
 
Monitors
MonitorsMonitors
Monitors
Mohd Arif
 
Os2 2
Os2 2Os2 2
Os2 2
issbp
 

Similar to Synchronization (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
 
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
 
OS Process synchronization Unit3 synchronization
OS Process synchronization Unit3  synchronizationOS Process synchronization Unit3  synchronization
OS Process synchronization Unit3 synchronization
 
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 dhcp
Mohd Arif
 
Arp and rarp
Arp and rarpArp and rarp
Arp and rarp
Mohd Arif
 
User datagram protocol
User datagram protocolUser datagram protocol
User datagram protocol
Mohd Arif
 
Project identification
Project identificationProject identification
Project identification
Mohd Arif
 
Project evalaution techniques
Project evalaution techniquesProject evalaution techniques
Project evalaution techniques
Mohd Arif
 
Presentation
PresentationPresentation
Presentation
Mohd Arif
 
Pointers in c
Pointers in cPointers in c
Pointers in c
Mohd Arif
 
Peer to-peer
Peer to-peerPeer to-peer
Peer to-peer
Mohd Arif
 
Overview of current communications systems
Overview of current communications systemsOverview of current communications systems
Overview of current communications systems
Mohd Arif
 
Overall 23 11_2007_hdp
Overall 23 11_2007_hdpOverall 23 11_2007_hdp
Overall 23 11_2007_hdp
Mohd Arif
 
Objectives of budgeting
Objectives of budgetingObjectives of budgeting
Objectives of budgeting
Mohd Arif
 
Network management
Network managementNetwork management
Network management
Mohd Arif
 
Networing basics
Networing basicsNetworing basics
Networing basics
Mohd Arif
 
Loaders
LoadersLoaders
Loaders
Mohd Arif
 
Lists
ListsLists
Lists
Mohd 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 platform
Mohd Arif
 
Ip sec and ssl
Ip sec and  sslIp sec and  ssl
Ip sec and ssl
Mohd Arif
 
Ip security in i psec
Ip security in i psecIp security in i psec
Ip security in i psec
Mohd Arif
 
Intro to comp. hardware
Intro to comp. hardwareIntro to comp. hardware
Intro to comp. hardware
Mohd Arif
 
Heap sort
Heap sortHeap sort
Heap sort
Mohd 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

Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
BibashShahi
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Pitangent Analytics & Technology Solutions Pvt. Ltd
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
Neo4j
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
Ivo Velitchkov
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
Antonios Katsarakis
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
Safe Software
 

Recently uploaded (20)

Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
Crafting Excellence: A Comprehensive Guide to iOS Mobile App Development Serv...
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
 

Synchronization

  • 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