SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
Semaphores

qA semaphore is an object that consists of a
 counter, a waiting list of processes and two
 methods (e.g., functions): signal and wait.

                                 semaphore
method signal
                   counter
method wait
                  waiting list


                                                1
Semaphore Method: wait
        void wait(sem S)
        {
           S.count--;
           if (S.count < 0) {
              add the caller to the waiting list;
              block();
           }
        }
qAfter decreasing the counter by 1, if the counter
 value becomes negative, then
  vadd the caller to the waiting list, and then
  vblock itself.                                     2
Semaphore Method: signal
      void signal(sem S)
      {
         S.count++;
         if (S.count <= 0) {
            remove a process P from the waiting list;
            resume(P);
         }
      }
qAfter increasing the counter by 1, if the new
 counter value is not positive, then
  vremove a process P from the waiting list,
  vresume the execution of process P, and return
                                                        3
Important Note: 1/4
S.count--;                  S.count++;
if (S.count<0) {            if (S.count<=0) {
   add to list;                remove P;
   block();                    resume(P);
}                           }

qIf S.count < 0, abs(S.count) is the
 number of waiting processes.
qThis is because processes are added to (resp.,
 removed from) the waiting list only if the
 counter value is < 0 (resp., <= 0).
                                                  4
Important Note: 2/4
 S.count--;               S.count++;
 if (S.count<0) {         if (S.count<=0) {
    add to list;             remove P;
    block();                 resume(P);
 }                        }

qThe waiting list can be implemented with a
 queue if FIFO order is desired.
qHowever, the correctness of a program should
 not depend on a particular implementation of
 the waiting list.
qYour program should not make any assumption
 about the ordering of the waiting list.      5
Important Note: 3/4
 S.count--;                  S.count++;
 if (S.count<0) {            if (S.count<=0) {
    add to list;                remove P;
    block();                    resume(P);
 }                           }

qThe caller may be blocked in the call to wait().
qThe caller never blocks in the call to signal().
 If S.count > 0, signal() returns and the
 caller continues. Otherwise, a waiting process is
 released and the caller continues. In this case, two
 processes continue.
                                                   6
The Most Important Note: 4/4
S.count--;                 S.count++;
if (S.count<0) {           if (S.count<=0) {
   add to list;               remove P;
   block();                   resume(P);
}                          }

qwait() and signal() must be executed
 atomically (i.e., as one uninterruptible unit).
qOtherwise, race conditions may occur.
qHomework: use execution sequences to show
 race conditions if wait() and/or signal() is
 not executed atomically.
                                               7
Three Typical Uses of Semaphores
 qThere are three typical uses of semaphores:
  vmutual exclusion:
    Mutex (i.e., Mutual Exclusion) locks
  vcount-down lock:
    Keep in mind that semaphores have a counter.
  vnotification:
    Indicate an event has occurred.

                                             8
Use 1: Mutual Exclusion (Lock)
                     initialization is important
semaphore S = 1;
int         count = 0;
      Process 1                      Process 2
while (1) {                    while (1) {
    // do something entry         // do something
    S.wait();                     S.wait();
       count++; critical sections    count--;
    S.signal();                   S.signal();
    // do something exit          // do something
}                              }
  qWhat if the initial value of S is zero?
  qS is a binary semaphore (similar to a lock).
                                                   9
Use 2: Count-Down Counter
 semaphore    S = 3;

       Process 1                          Process 2
 while (1) {                     while (1) {
    // do something                   // do something
    S.wait();                         S.wait();
          at most 3 processes can be here!!!
    S.signal();                       S.signal();
    // do something                   // do something
 }                               }

qAfter three processes pass through wait(), this
 section is locked until a process calls signal().
                                                 10
Use 3: Notification
semaphore S1 = 1, S2 = 0;
      process 1              process 2
while (1) {            while (1) {
   // do something        // do something
   S1.wait();   notify    S2.wait();
      cout << “1”;           cout << “2”;
   S2.signal(); notify    S1.signal();
   // do something        // do something
}                      }
qProcess 1 uses S2.signal() to notify process
 2, indicating “I am done. Please go ahead.”
qThe output is 1 2 1 2 1 2 ……
qWhat if both S1 and S2 are both 0’s or both 1’s?
qWhat if S1 = 0 and S2 = 1?                     11
Lock Example: Dining
              Philosophers
§ Five philosophers are in a
  thinking - eating cycle.
§ When a philosopher gets
  hungry, he sits down, picks
  up two nearest chopsticks,
  and eats.
§ A philosopher can eat only
  if he has both chopsticks.
§ After eating, he puts down
  both chopsticks and thinks.
§ This cycle continues.
                                  12
Dining Philosopher: Ideas
qChopsticks are shared                   outer critical section
 items (by two philosophers)   left chop locked
 and must be protected.        Semaphore C[5] = 1;
qEach chopstick has a
                               C[i].wait();
 semaphore with initial        C[(i+1)%5].wait();
 value 1.
                                 has 2 chops and eats
qA philosopher calls
 wait() before picks up a      C[(i+1)%5].signal();
                               C[i].signal();
 chopstick and calls
 signal() to release it.
                                    inner critical section
                          right chop locked            13
Dining Philosophers: Code
semaphore    C[5] = 1;

philosopher i             wait for my left chop
while (1) {
     // thinking
     C[i].wait();           wait for my right chop
     C[(i+1)%5].wait();
     // eating              release my right chop
     C[(i+1)%5].signal();
     C[i].signal();         release my left chop
     // finishes eating
}
                     Does this solution work?
                                            14
Dining Philosophers: Deadlock!

§ If all five philosophers
  sit down and pick up
  their left chopsticks at
  the same time, this
  program has a circular
  waiting and deadlocks.
§ An easy way to remove
  this deadlock is to
  introduce a weirdo who
  picks up his right
  chopstick first!
                                      15
Dining Philosophers: A Better Idea
semaphore C[5] = 1;
philosopher i (0, 1, 2, 3)         Philosopher 4: the weirdo
while (1) {             while (1) {
   // thinking            // thinking
   C[i].wait();           C[(i+1)%5].wait();
   C[(i+1)%5].wait();     C[i].wait();
   // eating              // eating
   C[(i+1)%5].signal();   C[i].signal();
   C[i].signal();         C[(i+1)%5].signal();
   // finishes eating;    // finishes eating
}                       }
                  lock left chop     lock right chop
                                                               16
Dining Philosophers: Questions

qThe following are some important questions for
 you to work on.
  vWe choose philosopher 4 to be the weirdo.
   Does this choice matter?
  vShow that this solution does not cause circular
   waiting.
  vShow that this solution will not have circular
   waiting if we have more than 1 and less than 5
   weirdoes.
qThese questions may appear as exam problems.
                                               17
Count-Down Lock Example
        qThe naïve solution to the
         dining philosophers causes
         circular waiting.
        qIf only four philosophers are
         allowed to sit down, no
         deadlock can occur.
        qWhy? If all four of them sit
         down at the same time, the
         right-most philosopher can
         have both chopsticks!
        qHow about fewer than four?
         This is obvious.           18
Count-Down Lock Example
semaphore C[5]= 1;
semaphore Chair = 4;
        get a chair
while (1) {                this is a count-down lock
   // thinking             that only allows 4 to go!
   Chair.wait();
      C[i].wait();
      C[(i+1)%5].wait();
      // eating                      this is our old friend
      C[(i+1)%5].signal();
      C[i].signal();
   Chair.signal();
}                      release my chair
                                                      19
The Producer/Consumer Problem
                 qSuppose we have a
                  circular buffer of n slots.
                 qPointers in (resp., out)
                  points to the first empty
                  (resp., filled) slot.
                 qProducer processes keep
                  adding info into the
                  buffer
                 qConsumer processes keep
bounded-buffer    retrieving info from the
                  buffer.                  20
Problem Analysis
                      qA producer deposits info into
                       Buf[in] and a consumer
                       retrieves info from Buf[out].
                      qin and out must be advanced.
                      qin is shared among producers.
                      qout is shared among consumers.
                      qIf Buf is full, producers should
buffer is implemented  be blocked.
with an array Buf[ ] qIf Buf is empty, consumers
                       should be blocked.
                                                   21
qWe need a sem.
 to protect the
 buffer.
qA second sem.
 to block
 producers if the
 buffer is full.
qA third sem. to
 block
 consumers if
 the buffer is
 empty.

             22
Solution
no. of slots

semaphore NotFull=n, NotEmpty=0, Mutex=1;

producer                        consumer
while (1) {                while (1) {
  NotFull.wait();            NotEmpty.wait();
    Mutex.wait();              Mutex.wait();
      Buf[in] = x;               x = Buf[out];
      in = (in+1)%n;             out = (out+1)%n;
    Mutex.signal();            Mutex.signal();
  NotEmpty.signal();         NotFull.signal();
}                          }
               notifications
                                    critical section
                                                23
Question
qWhat if the producer code is modified as follows?
qAnswer: a deadlock may occur. Why?


            while (1) {
              Mutex.wait();
                NotFull.wait();
                  Buf[in] = x;
order changed     in = (in+1)%n;
                NotEmpty.signal();
              Mutex.signal();
            }
                                               24
The Readers/Writers Problem
qTwo groups of processes, readers and writers,
 are accessing a shared resource by the following
 rules:
  vReaders can read simultaneously.
  vOnly one writer can write at any time.
  vWhen a writer is writing, no reader can read.
  vIf there is any reader reading, all incoming
   writers must wait. Thus, readers have higher
   priority.

                                               25
Problem Analysis
qWe need a semaphore to block readers if a
 writer is writing.
qWhen a writer arrives, it must be able to know
 if there are readers reading. So, a reader count
 is required which must be protected by a lock.
qThis reader-priority version has a problem:
 bounded waiting condition may be violated if
 readers keep coming, causing the waiting
 writers no chance to write.

                                                26
qWhen a reader comes
 in, it increase the
 count.
qIf it is the 1st reader,
 waits until no writer is
 writing,
qReads data.
qDecreases the counter.
qNotifies the writer
 that no reader is
 reading if it is the last.
                      27
qWhen a writer
 comes in, it waits
 until no reader is
 reading and no
 writer is writing.
qThen, it writes data.
qFinally, notifies
 readers and writers
 that no writer is in.



                   28
Solution
semaphore Mutex = 1, WrtMutex = 1;
int       RdrCount;

reader                        writer
while (1) {                  while (1) {
  Mutex.wait();
    RdrCount++;
    if (RdrCount == 1) blocks both readers and writers
      WrtMutex.wait();         WrtMutex.wait();
  Mutex.signal();
  // read data                 // write data
  Mutex.wait();
    RdrCount--;
    if (RdrCount == 0)
      WrtMutex.signal();       WrtMutex.signal();
  Mutex.signal();
}                            }                    29

Contenu connexe

Tendances (20)

OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
 
Priority scheduling algorithms
Priority scheduling algorithmsPriority scheduling algorithms
Priority scheduling algorithms
 
Disk scheduling
Disk schedulingDisk scheduling
Disk scheduling
 
Distributed file system
Distributed file systemDistributed file system
Distributed file system
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Deadlock Avoidance in Operating System
Deadlock Avoidance in Operating SystemDeadlock Avoidance in Operating System
Deadlock Avoidance in Operating System
 
Dining philosopher
Dining philosopherDining philosopher
Dining philosopher
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-Systems
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Algorithm and pseudocode conventions
Algorithm and pseudocode conventionsAlgorithm and pseudocode conventions
Algorithm and pseudocode conventions
 
Process of operating system
Process of operating systemProcess of operating system
Process of operating system
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Three Address code
Three Address code Three Address code
Three Address code
 
Paging and segmentation
Paging and segmentationPaging and segmentation
Paging and segmentation
 
A* Search Algorithm
A* Search AlgorithmA* Search Algorithm
A* Search Algorithm
 
Deadlock detection and recovery by saad symbian
Deadlock detection and recovery by saad symbianDeadlock detection and recovery by saad symbian
Deadlock detection and recovery by saad symbian
 
Hashing
HashingHashing
Hashing
 

Similaire à Semaphores

OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitorssgpraju
 
Mca ii os u-2 process management & communication
Mca  ii  os u-2 process management & communicationMca  ii  os u-2 process management & communication
Mca ii os u-2 process management & communicationRai University
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationWayne Jones Jnr
 
UNIT III Process Synchronization.docx
UNIT III Process Synchronization.docxUNIT III Process Synchronization.docx
UNIT III Process Synchronization.docxkarthikaparthasarath
 
Csci101 lect03 algorithms_i
Csci101 lect03 algorithms_iCsci101 lect03 algorithms_i
Csci101 lect03 algorithms_iElsayed Hemayed
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop kapil078
 
Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014Béo Tú
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while LoopJayBhavsar68
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languageTanmay Modi
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfDrDineshenScientist
 

Similaire à Semaphores (20)

Behavioral modelling in VHDL
Behavioral modelling in VHDLBehavioral modelling in VHDL
Behavioral modelling in VHDL
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
 
Ch6
Ch6Ch6
Ch6
 
Os unit 3
Os unit 3Os unit 3
Os unit 3
 
Os3
Os3Os3
Os3
 
Mca ii os u-2 process management & communication
Mca  ii  os u-2 process management & communicationMca  ii  os u-2 process management & communication
Mca ii os u-2 process management & communication
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
 
UNIT III Process Synchronization.docx
UNIT III Process Synchronization.docxUNIT III Process Synchronization.docx
UNIT III Process Synchronization.docx
 
OSCh7
OSCh7OSCh7
OSCh7
 
OS_Ch7
OS_Ch7OS_Ch7
OS_Ch7
 
Csci101 lect03 algorithms_i
Csci101 lect03 algorithms_iCsci101 lect03 algorithms_i
Csci101 lect03 algorithms_i
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Cs problem [repaired]
Cs problem [repaired]Cs problem [repaired]
Cs problem [repaired]
 
Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
 
Monitors
MonitorsMonitors
Monitors
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdf
 

Plus de 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
 

Plus de 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
 

Dernier

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Dernier (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Semaphores

  • 1. Semaphores qA semaphore is an object that consists of a counter, a waiting list of processes and two methods (e.g., functions): signal and wait. semaphore method signal counter method wait waiting list 1
  • 2. Semaphore Method: wait void wait(sem S) { S.count--; if (S.count < 0) { add the caller to the waiting list; block(); } } qAfter decreasing the counter by 1, if the counter value becomes negative, then vadd the caller to the waiting list, and then vblock itself. 2
  • 3. Semaphore Method: signal void signal(sem S) { S.count++; if (S.count <= 0) { remove a process P from the waiting list; resume(P); } } qAfter increasing the counter by 1, if the new counter value is not positive, then vremove a process P from the waiting list, vresume the execution of process P, and return 3
  • 4. Important Note: 1/4 S.count--; S.count++; if (S.count<0) { if (S.count<=0) { add to list; remove P; block(); resume(P); } } qIf S.count < 0, abs(S.count) is the number of waiting processes. qThis is because processes are added to (resp., removed from) the waiting list only if the counter value is < 0 (resp., <= 0). 4
  • 5. Important Note: 2/4 S.count--; S.count++; if (S.count<0) { if (S.count<=0) { add to list; remove P; block(); resume(P); } } qThe waiting list can be implemented with a queue if FIFO order is desired. qHowever, the correctness of a program should not depend on a particular implementation of the waiting list. qYour program should not make any assumption about the ordering of the waiting list. 5
  • 6. Important Note: 3/4 S.count--; S.count++; if (S.count<0) { if (S.count<=0) { add to list; remove P; block(); resume(P); } } qThe caller may be blocked in the call to wait(). qThe caller never blocks in the call to signal(). If S.count > 0, signal() returns and the caller continues. Otherwise, a waiting process is released and the caller continues. In this case, two processes continue. 6
  • 7. The Most Important Note: 4/4 S.count--; S.count++; if (S.count<0) { if (S.count<=0) { add to list; remove P; block(); resume(P); } } qwait() and signal() must be executed atomically (i.e., as one uninterruptible unit). qOtherwise, race conditions may occur. qHomework: use execution sequences to show race conditions if wait() and/or signal() is not executed atomically. 7
  • 8. Three Typical Uses of Semaphores qThere are three typical uses of semaphores: vmutual exclusion: Mutex (i.e., Mutual Exclusion) locks vcount-down lock: Keep in mind that semaphores have a counter. vnotification: Indicate an event has occurred. 8
  • 9. Use 1: Mutual Exclusion (Lock) initialization is important semaphore S = 1; int count = 0; Process 1 Process 2 while (1) { while (1) { // do something entry // do something S.wait(); S.wait(); count++; critical sections count--; S.signal(); S.signal(); // do something exit // do something } } qWhat if the initial value of S is zero? qS is a binary semaphore (similar to a lock). 9
  • 10. Use 2: Count-Down Counter semaphore S = 3; Process 1 Process 2 while (1) { while (1) { // do something // do something S.wait(); S.wait(); at most 3 processes can be here!!! S.signal(); S.signal(); // do something // do something } } qAfter three processes pass through wait(), this section is locked until a process calls signal(). 10
  • 11. Use 3: Notification semaphore S1 = 1, S2 = 0; process 1 process 2 while (1) { while (1) { // do something // do something S1.wait(); notify S2.wait(); cout << “1”; cout << “2”; S2.signal(); notify S1.signal(); // do something // do something } } qProcess 1 uses S2.signal() to notify process 2, indicating “I am done. Please go ahead.” qThe output is 1 2 1 2 1 2 …… qWhat if both S1 and S2 are both 0’s or both 1’s? qWhat if S1 = 0 and S2 = 1? 11
  • 12. Lock Example: Dining Philosophers § Five philosophers are in a thinking - eating cycle. § When a philosopher gets hungry, he sits down, picks up two nearest chopsticks, and eats. § A philosopher can eat only if he has both chopsticks. § After eating, he puts down both chopsticks and thinks. § This cycle continues. 12
  • 13. Dining Philosopher: Ideas qChopsticks are shared outer critical section items (by two philosophers) left chop locked and must be protected. Semaphore C[5] = 1; qEach chopstick has a C[i].wait(); semaphore with initial C[(i+1)%5].wait(); value 1. has 2 chops and eats qA philosopher calls wait() before picks up a C[(i+1)%5].signal(); C[i].signal(); chopstick and calls signal() to release it. inner critical section right chop locked 13
  • 14. Dining Philosophers: Code semaphore C[5] = 1; philosopher i wait for my left chop while (1) { // thinking C[i].wait(); wait for my right chop C[(i+1)%5].wait(); // eating release my right chop C[(i+1)%5].signal(); C[i].signal(); release my left chop // finishes eating } Does this solution work? 14
  • 15. Dining Philosophers: Deadlock! § If all five philosophers sit down and pick up their left chopsticks at the same time, this program has a circular waiting and deadlocks. § An easy way to remove this deadlock is to introduce a weirdo who picks up his right chopstick first! 15
  • 16. Dining Philosophers: A Better Idea semaphore C[5] = 1; philosopher i (0, 1, 2, 3) Philosopher 4: the weirdo while (1) { while (1) { // thinking // thinking C[i].wait(); C[(i+1)%5].wait(); C[(i+1)%5].wait(); C[i].wait(); // eating // eating C[(i+1)%5].signal(); C[i].signal(); C[i].signal(); C[(i+1)%5].signal(); // finishes eating; // finishes eating } } lock left chop lock right chop 16
  • 17. Dining Philosophers: Questions qThe following are some important questions for you to work on. vWe choose philosopher 4 to be the weirdo. Does this choice matter? vShow that this solution does not cause circular waiting. vShow that this solution will not have circular waiting if we have more than 1 and less than 5 weirdoes. qThese questions may appear as exam problems. 17
  • 18. Count-Down Lock Example qThe naïve solution to the dining philosophers causes circular waiting. qIf only four philosophers are allowed to sit down, no deadlock can occur. qWhy? If all four of them sit down at the same time, the right-most philosopher can have both chopsticks! qHow about fewer than four? This is obvious. 18
  • 19. Count-Down Lock Example semaphore C[5]= 1; semaphore Chair = 4; get a chair while (1) { this is a count-down lock // thinking that only allows 4 to go! Chair.wait(); C[i].wait(); C[(i+1)%5].wait(); // eating this is our old friend C[(i+1)%5].signal(); C[i].signal(); Chair.signal(); } release my chair 19
  • 20. The Producer/Consumer Problem qSuppose we have a circular buffer of n slots. qPointers in (resp., out) points to the first empty (resp., filled) slot. qProducer processes keep adding info into the buffer qConsumer processes keep bounded-buffer retrieving info from the buffer. 20
  • 21. Problem Analysis qA producer deposits info into Buf[in] and a consumer retrieves info from Buf[out]. qin and out must be advanced. qin is shared among producers. qout is shared among consumers. qIf Buf is full, producers should buffer is implemented be blocked. with an array Buf[ ] qIf Buf is empty, consumers should be blocked. 21
  • 22. qWe need a sem. to protect the buffer. qA second sem. to block producers if the buffer is full. qA third sem. to block consumers if the buffer is empty. 22
  • 23. Solution no. of slots semaphore NotFull=n, NotEmpty=0, Mutex=1; producer consumer while (1) { while (1) { NotFull.wait(); NotEmpty.wait(); Mutex.wait(); Mutex.wait(); Buf[in] = x; x = Buf[out]; in = (in+1)%n; out = (out+1)%n; Mutex.signal(); Mutex.signal(); NotEmpty.signal(); NotFull.signal(); } } notifications critical section 23
  • 24. Question qWhat if the producer code is modified as follows? qAnswer: a deadlock may occur. Why? while (1) { Mutex.wait(); NotFull.wait(); Buf[in] = x; order changed in = (in+1)%n; NotEmpty.signal(); Mutex.signal(); } 24
  • 25. The Readers/Writers Problem qTwo groups of processes, readers and writers, are accessing a shared resource by the following rules: vReaders can read simultaneously. vOnly one writer can write at any time. vWhen a writer is writing, no reader can read. vIf there is any reader reading, all incoming writers must wait. Thus, readers have higher priority. 25
  • 26. Problem Analysis qWe need a semaphore to block readers if a writer is writing. qWhen a writer arrives, it must be able to know if there are readers reading. So, a reader count is required which must be protected by a lock. qThis reader-priority version has a problem: bounded waiting condition may be violated if readers keep coming, causing the waiting writers no chance to write. 26
  • 27. qWhen a reader comes in, it increase the count. qIf it is the 1st reader, waits until no writer is writing, qReads data. qDecreases the counter. qNotifies the writer that no reader is reading if it is the last. 27
  • 28. qWhen a writer comes in, it waits until no reader is reading and no writer is writing. qThen, it writes data. qFinally, notifies readers and writers that no writer is in. 28
  • 29. Solution semaphore Mutex = 1, WrtMutex = 1; int RdrCount; reader writer while (1) { while (1) { Mutex.wait(); RdrCount++; if (RdrCount == 1) blocks both readers and writers WrtMutex.wait(); WrtMutex.wait(); Mutex.signal(); // read data // write data Mutex.wait(); RdrCount--; if (RdrCount == 0) WrtMutex.signal(); WrtMutex.signal(); Mutex.signal(); } } 29