SlideShare uma empresa Scribd logo
1 de 69
Baixar para ler offline
The OS as a gatekeeper
Objective
 To discuss various mechanisms to ensure
  the orderly execution of cooperating
  processes that share a logical address space
  so that data is consistently maintained.
Concept of concurrency
 Existence of several simultaneous processes.
  May be observed in situations like:

   Multi-user OS where different users execute
    programs concurrently

   Single-user OS where input/output devices go
    simultaneously with program execution

   Some processors whose activities are concurrent,
    example, fetch next instruction is done
    simultaneously with the execution of an instruction.
bounded buffer
            PRODUCER                              CONSUMER

while (true) {                           while (true) {

    /* produce an item and put in          while (count == 0)
             nextProduced */                          do nothing

     while (count == BUFFER_SIZE)          nextConsumed = buffer [out];
                   do nothing;
                                           out = (out + 1) % BUFFER_SIZE;
       buffer [in] = nextProduced;
                                            count--;
      in = (in + 1) % BUFFER_SIZE;
                                          /* consume the item in
      count++;                                      nextConsumed */
}                                    }
Atomic Operation
 The statements count ++ and count -- must
  be performed atomically.

 Atomic operation means an operation that
  completes in its entirety without interruption.

 May not function correctly when executed
  concurrently.
Atomic Operation
 If both the producer and consumer attempt to
  update the buffer concurrently, the assembly
  language may get interleaved

 Interleaving depends upon how the producer
  and consumer processes are scheduled.
Atomic Operation
 count++ could be implemented as
     MOV AX, COUNT        register1 = count
    INC AX                register1 = register1 + 1
    MOV COUNT, AX         count = register1

 count-- could be implemented as
    MOV BX, COUNT         register2 = count
    DEC BX                register2 = register2 - 1
    MOV COUNT, BX         count = register2
Now consider this interleaving…
    Consider this execution interleaving with “count = 5”
     initially:
     S0: producer execute      register1 = count
                {register1 = 5}
     S1: producer execute      register1 = register1 + 1
                {register1 = 6}
     S2: consumer execute register2 = count
                {register2 = 5}
     S3: consumer execute register2 = register2 - 1
                {register2 = 4}
     S4: producer execute       count = register1
              {count = 6 }
     S5: consumer execute     count = register2
              {count = 4}
Race Condition
 Situation where several processes access and
  manipulate shared data concurrently. The final
  value of the shared data depends upon which
  process finishes last.

 To prevent race conditions, concurrent
  processes must be synchronized.
The Critical Section (CS) Problem
 Consider n processes all competing to use
  some shared data.

 Each process has a code segment, called
  critical section in which shared data is
  accessed.

 Ensure that when one process is executing in
  its critical section, no other process is allowed
  to execute in its critical condition.
The Critical Section (CS) Problem
 Execution of critical sections by the processes
  is mutually exclusive in time.

 Design protocol:
   Each process must request permission to enter its
    CS (entry section)
   The CS is followed by an exit section
   The remaining code is in the remainder section.
Solution to Critical-Section
 Problem
1.Mutual Exclusion
2.Progress
3.Bounded Waiting
Solution to Critical-Section
 Problem
1. Mutual Exclusion
   - If process Pi is executing in its critical section,
      then no other processes can be executing in
      their critical sections

           CS
                P1        P2       P
                                   n
Solution to Critical-Section
Problem
Progress
 - If no process is executing in its critical section
 and there exist some processes that wish to
 enter their critical section, then the selection of
 the processes that will enter the critical section
 next cannot be postponed indefinitely.

                    CS              P
               P                    2
               1
                                    P
Solution to Critical-Section
Problem
1. Bounded Waiting
  - No process should wait arbitrarily long to enter
  its CS.

                   CS              P
              P                    2
              1
                                   P
                                   n
Note:
 No assumptions are made about relative
  process speeds or number of CPU’s

 A process in one CS should not block others
  in entering a different CS.
Sample Scenario – Milk Issue


Time   Person A                      Person B
3:00   Look in fridge, Out of milk    -------------
3:05   Leave for store                -------------
3:10   Arrive at store               Look in fridge, Out of Milk
3:15   Buy Milk                      Leave for store
3:20   Leave store                   Arrive at store
3:25   Arrive home, put milk away    Buy milk
3:30    -------------                Leave Store
3:35    -------------                Arrive home, oops
Observation
 Someone gets milk, but not EVERYONE (too
  much milk!)
 This shows that when cooperating processes are
  not synchronized, they may face unexpected
  “timing errors”

 Mutual Exclusion - ensures that only one
  process (or person) is doing a thing at one time,
  thus avoiding data inconsistency. All others
  should be prevented from modifying shared data
  until the current process finishes.

  E.g. only one person buys milk at a time.
Solution 1 – Attempt to computerize Milk
Buying
PROCESS A AND B
                       This solution works for some
If (NOmilk)              people because the first
{                        three lines are performed
    if (NOnote)          ATOMICALLY but does
    {                    not work otherwise.
        Leave note;
        Buy milk;      What happens when the 2
        Remove note;    processes execute
    }                   concurrently?
}
Solution 2 – Attempt to use two notes
      PROCESS A                   PROCESS B
Leave (NOTEa);              Leave (NOTEb);

If (NO_NOTEb)               If (NO_NOTEa)
{                           {
    if (NOmilk) Buy Milk;      if (NOmilk) Buy Milk;
}                           }

Remove (NOTEa);             Remove (NOTEb);

     WHAT CAN YOU SAY ABOUT THIS SOLUTION?
Solution 3:
      PROCESS A                  PROCESS B

Leave (NOTEa);              Leave (NOTEb)

If (NOnoteB)                While (NOTEa) do
{                            nothing;
    if (NOmilk) Buy Milk;
}                           if (NOmilk) Buy Milk;

Remove (NOTEa);             Remove (NOTEb);

    In case of tie, who will buy first?
Critique for Solution 3
 Process B will always be the first to buy the
  milk in case of a tie.
 Process B consumes CPU cycles while
  waiting.
 More complicated if extended to many
  processes.
Algorithm Template
 do {                       ENTRY SECTION – Each
                            process must request
        ENTRY_SECTION
                            permission to enter its CS

        CRITICAL_SECTION

         EXIT_SECTION
                            EXIT SECTION – Process
                            leaves the CS thereby
                            informing other processes
        Remainder_Section   that the CS is free for some
                            other process to enter
 } while (TRUE)
Two Process Solution
Algo 1: Strict Alteration Shared var Turn <either a or b>
         PROCESS A                        PROCESS B
repeat                           repeat
   While (turn==B) do              While (turn==A) do
              nothing;                         nothing;
   <critical section A>
                                   <critical section B>
   Turn = B;
                                   Turn = A;
until false
                                 until false

Which of the following requirement is violated?
     Mutual Exclusion       Progress           Bounded Waiting
Algorithm 2
     Shared Var pAinside, pBinside; FALSE
        PROCESS A                        PROCESS B
While (TRUE) {                  While (TRUE) {
 While (pBinside) do             While (pAinside) do
               nothing;                        nothing;
    pAinside = True;                pBinside = True;
    <critical section A>            <critical section B>
    pAinside = False;               pBinside = False;
}                               }

Which of the following requirement is violated?
       Mutual Exclusion     Progress          Bounded Waiting
Algorithm 3
    Shared Var pAtrying, pBtrying : FALSE

        PROCESS A                        PROCESS B
While (TRUE) {                  While (TRUE) {
 pAtrying = True;                pBtrying = True;
    While (pBtrying) do             While (pAtrying) do
                   nothing;                       nothing;
    <critical section A>            <critical section B>
    pAtrying = False;               pBtrying = False;
}                               }
Which of the following requirement is violated?
      Mutual Exclusion        Progress        Bounded Waiting
Dekker’s Algorithm - Elegant solution to
 mutual exclusion problem
             Shared var pAtrying pBtrying = False
         PROCESS A                             PROCESS B
While (TRUE) {                       While (TRUE) {
  pAtrying = True;                     pBtrying = True;
  While (pBtrying)                     While (pAtrying)
     if (turn==B) {                       if (turn==A) {
       pAtrying = False;                      pBtrying = False;
       while (turn==B) do nothing;            while (turn==A) do nothing;
       pAtrying = True                        pBtrying = True
     }                                     }
  <critical section>                   <critical section>
  turn==B;                             turn==A;
  pAtrying = False;                    pBtrying = False;
}                                    }
Peterson’s Algorithm: Much simpler than
    Dekker’s Algorithm
               Shared var pAtrying pBtrying = False
         PROCESS A                       PROCESS B
While (TRUE)                     While (TRUE)
{                                {
  pAtrying = True;                 pBtrying = True;
  turn= B;                         turn= A;
  While (pBtrying && turn ==B)     While (pAtrying && turn ==A)
         do nothing;                      do nothing;

     <critical section A>            <critical section A>

     pAtrying = False;               pBtrying = False;
}                                }
Note
 Both Dekker’s and Peterson’s algorithms
  are correct but they only work for 2 processes,
  similar to the last solution of the “too-much-
  milk” problem.
Multiple Process Synchronization
 Solution
 Hardware Support – special instructions
  Many CPU’s today provide hardware
   instructions to read, modify and store a word
   atomically. Most common instructions with
   this capability are:
   TAS – (Test and Set ) – Motorolla 68k
   CAS – (Compare and Swap) – IBM 370 and
    Motorola 68k
   XCHG – (exchange) –x86
Mutual Exclusion with Test-&-Set
(TAS)
                           Shared data:
boolean TestAndSet               boolean lock = false;
(boolean &target)
                           Process Pi
{                          do {
    boolean rv = target;   while (TestAndSet(lock)) do
    target = true;                  nothing( );
    return rv;
}                              <critical section>

                               lock = false;
                               remainder section
                           }
Mutual Exclusion with SWAP and
                 XCHG
Atomically swap two      Shared data (initialized to false):
variables.
                                 boolean lock;
void Swap(boolean &a,            boolean waiting[n];
        boolean &b)      Process Pi
                         do {
{                        key = true;
     boolean temp = a;      while (key == true)
     a = b;                      Swap(lock,key);
     b = temp;              critical section
}                           lock = false;
                            remainder section
                         }
Multiple Process Synchronization
Solution
 The basic idea is to be able to read the
  contents of a variable (memory location) and
  set it to something else all in one execution
  cycle hence not interruptible.

 The use of these special instructions makes
  the programming task easier and improves
  efficiency.
Semaphores
 Synchronization tool that does not require
  busy waiting

 Semaphore S – integer variable

 Two standard operations modify S: wait()
  and signal()
A Semaphore…
 Essentially an integer S (initially S>0) and a
  queue of processes, initially empty

 Synchronization variable (guard) that takes on
  non-negative integer values with only two
  atomic operations
Two Atomic Operations

Wait (S)                  PROBEREN:
  While s ≤ 0 do no_op;     Probe/Test
  S--

Signal (S)                VERHOGEN:
  S++                      release
                            “make higher”
Solution to too much milk using
semaphore
                 Process A & B
      Semaphore OkToBuyMilk = 1;

      Wait (OkToBuyMilk);
      If (NoMilk)
          {
             Buy Milk;
          }
      Signal (OkToBuyMilk)
Critical Section of n Processes
 Shared data: semaphore mutex (initially 1)

  do{
     wait (mutex);
        < critical section >
     signal (mutex);
     remainder section
  }while (true)
Critique: CS of n Processes
 The above algorithm requires busy-wait.
 To overcome busy waiting, the process will
  block itself (placed in the waiting - queue
  associated with the semaphore)

Define a semaphore as a record
  typedef struct {
        int value;
        struct process *L;
  } semaphore;
Example #1:
 There are 6             Synchronize the execution of the
  processes waiting                 two CPUs:
  in the ready queue           CPU1:              CPU2:
  (P1,P2…P6)             P1;                P4;
 Let us assume that                        wait (s1);
   P5 can only be
                         P2;                P5;
    executed if P1 and
    P2 have already      signal (s1);
    completed their
                         wait (s2);
    tasks
   P3 will only be      P3;                P6;
    executed if P6 is                       signal (s2);
    finished
Exercise #2:
 Consider the following code which is being
  executed by 3 CPU’s. This program computes
  the value of X using the Quadratic formula.

 Synchronize the execution of the CPU’s by
  using semaphore.
X = -B± √(B2 – 4 A C)
 Exercise #2 Codes:                                    2A
CPU1:               Readln (C);                  CPU2:
Readln (A);         FourAC = 4*A*C;
TwiceA = A + A;     If (FourAC > BSquare); Imaginary
Readln (B);         SquareRoot = sqrt(abs(Bsquare – FourAC)
Bsquare = B * B;    Root1 = (MinusB + SquareRoot)/TwiceA;
MinusB = -1 * B;    Root2 = (MinusB - SquareRoot)/TwiceA;


CPU3:
If (Imaginary) writeln (“No real roots”)
Else writeln (“the roots are”, root1, root2”)
Solution to Exercise 2                                      X = -B± √(B2 – 4 A C)
                                                                       2A
             CPU1:                    Readln (C);                             CPU2:
  Readln (A);                           Wait(S1);
    Signal(S1);                       FourAC = 4*A*C;
  TwiceA = A + A;                        Wait(S2);
  Readln (B);                         If (FourAC > BSquare) then Imaginary =TRUE;
  Bsquare = B * B;                    SquareRoot = sqrt(abs(Bsquare – FourAC)
   Signal (S2);                         Wait(S3);
  MinusB = -1 * B;                    Root1 = (MinusB + SquareRoot)/TwiceA;
  Signal (S3);                        Root2 = (MinusB - SquareRoot)/TwiceA;
                                        Signal (S4);


        Wait (S4);                                            CPU3
  If (Imaginary) writeln (“No real roots”)
  Else writeln (“the roots are”, root1, root2”)
Possible uses of semaphores
 Mutual Exclusion
  – Initializes the semaphore to one (1)

   Example:
               Wait (Mutex);

                 < Critical section; >

               Signal (Mutex);
Possible uses of semaphores
 Synchronization of cooperating processes
  (signalling)
     – initializes the semaphore to zero.

   Example
               Pi:                 Pj
               …                   …
               …                   …
               A                   wait (flag)
               Signal (flag)       B
Possible uses of semaphore
 Managing multiple instances of a resource
   - Initializes the semaphore to the number of
   instances.
Types of semaphores
 Binary
  – semaphore with an integer value of 0 or 1

 Counting
  – semaphore with an integer value ranging
    between 0 and an arbitrarily large number.
  - Initial value represents the number of units of
    the critical resources that are available.
  - Also known as general semaphore.
Classical Problems of
  Synchronization
Bounded-Buffer Problem:
   Two processes share a common, fixed size
    buffer. One of them, the producer puts
    information into the buffer, and the other, the
    consumer, takes out information from the
    buffer. When the producer wants to put a new
    item in the buffer but it is already full then allow
    the producer to sleep to be awakened when the
    consumer has removed one or more items.
    Similarly to the consumer, it goes to sleep if the
    buffer is empty until the producer puts
    something in the buffer.
shared data
   semaphore full = 0,         empty = n,          mutex
 = 1;
        Producer:                       Consumer:
do {                          do {
   …                          wait ( full )
   produce an item in nextp       wait ( mutex );
   …                               …
                                  remove item from buffer to
   wait ( empty );            nextc
   wait ( mutex );                 …
    …                             signal ( mutex );
   add nextp to buffer            signal ( empty );
    …                              …
   signal ( mutex );              consume item in nextc
   signal ( full );                …
} while (TRUE);                } while (TRUE);
Readers-Writers Problem
   This problem models access to a database with
    many competing processes wishing to read and
    write into it (imagine an airline reservation
    system). It is possible to have many processes
    reading the database at the same time (for
    query purposes). But if one process is writing
    (modifying), no other process shall access to
    the database, not even readers. With this
    condition in mind, how will you program the
    reader and writers?
Illustration: Readers and Writers
               Writers Area


                    wrt
    Readers
     Area      Critical section
              wrt
Readers-Writers: Solution
Types
   Algorithm 1:
       Readers have higher priority than writers

   Algorithm 2:
    Writers have higher priority than readers
Algorithm #1:
   Readers have higher priority than writers

    If a writer appears while several readers are in
    the database, the writer must wait. If new
    readers keep appearing so that there is always
    at least one reader in the database, the writer
    must keep waiting until no more readers are
    interested in the database.
Solution to Readers/Writers:
Algorithm #1
 shared data semaphore mutex = 1, wrt = 1;
 int readcount = 0; Readers:
Writers:                  do {
                              wait ( mutex );
do {                          readcount + +;
   wait(wrt);                 if ( readcount == 1 ) wait ( wrt );
     …                        signal ( mutex );
                               …
   writing is performed       reading is performed
     …                         …
   signal(wrt);               wait ( mutex );
                              readcount - -;
} while (TRUE)                if (readcount == 0 ) signal ( wrt );
                              signal ( mutex ):
                          } while (TRUE)
Algorithm 2:
   How about if we prioritize Writers over
    readers? Reprogram the above codes.

   Seatwork….
      Writers have higher priority than readers
Dining-Philosophers
    Problem
 Five philosophers spend their lives thinking and
  eating.
 Philosophers share a common circular table
  surrounded by five chairs, each belonging to
  one philosopher.
 At the center of the table, there is a bowl of rice,
  and the table is laid with five single chopsticks.
Dining-Philosophers cont…
 When a philosopher thinks, he does not interact
  with his colleagues.
 When a philosopher gets hungry, he tries to
  pick up two chopsticks that are closest to him.
 Philosopher could pick up only one chopstick at
  a time.
 Obviously, he cannot pick up a chopstick that is
  already in the hand of his neighbor.
 When a hungry philosopher has his both
  chopsticks, he eats without releasing them.
 When he is finished, he puts down the
  chopsticks and starts thinking again.
Dining-Philosophers
diagram
        0       4



    1               3


            2
Dining Philosophers: Initial Solution
   Shared data semaphore chopstick[5]; {Initially all values are 1}
   Philosopher i:
      do {
          wait ( chopstick [ i ] )               { his left chopstick}
          wait ( chopstick [ ( i + 1) % 5 ] )    { his right chopstick}
              …
             eat
              …
          signal ( chopstick [ i ] );                    0           4
          signal ( chopstick [ ( i + 1) % 5 ] );               0
              …                                            1        4
             think
              …                                      1       2 3        3
          } while (TRUE);
                                                            2
Question: What is wrong with this solution?
Solution for Dining Philosophers…
Shared data:
  state [ 5 ] of (thinking, hungry, eating);
                           // init to all thinking//
  self [ 5 ]: semaphore;              { init to all 0 }
  mutex: semaphore;                   { init to 1 }
  left = (i + 4) % 5;                 { left neighbor }
  right = (i + 1) % 5;                { right neighbor }
Solution for Dining Philosophers…
Philosopher ( i ) {
                               void test ( int i ) {
                                  if ( (state [ left ] ! = eating) &&
state [ i ] = hungry;                (state [ i ] == hungry) &&
wait ( mutex );                      (state [ right ] != eating)) {
   test [ i ];                             state [ i ] = eating;
signal (mutex);                            signal (self [ i ] );
if (state [ i ] != eating )           }
        wait ( self [ i ] );    }
                               void putdown ( int i ) {
    … eating …
                                  state [ i ] = thinking;
    wait ( mutex );               wait ( self [ i ] );
      putdown [ i ];
    signal ( mutex );                 // test left and right neighbors
}                                  test ( left );
                                   test ( ( right );
                               }
Sleeping – Barbers
Problem:
 A barber shop consists of a waiting room with
  5 chairs and the barber room containing the
  barber chair.
 If no customer to serve, the barber goes to
  sleep to be awakened by the customer.
 If a customer enters the barber shop but all
  chairs are occupied, the customer leaves the
  shop; otherwise he sits in one of the free
  chairs.
 Write the program to coordinate the actions of
  the barber and the customer.
Sleeping – Barbers Solution
    Shared data: customer = 0, barber = 0,            mutex = 1;
                 int waiting = 0; CHAIR = 5;

Barber:                      Customer:
while (TRUE) {                   while ( TRUE ) {
      wait ( customer );          wait ( mutex );
      wait ( mutex );             if ( waiting < CHAIR) {
        waiting - -;                    waiting + +;
        signal ( barber );              signal ( customer );
      signal ( mutex );                 signal ( mutex );
      ....                              wait ( barber );
      cut hair();                       …..
}                                       get haircut();
                                    } else signal ( mutex);
                             }
Exercise #1:
   Discuss the correctness of the Use_resource( )
    function below for two process solution:

int free = TRUE;
use_resource ( )
{
    while ( ! free );   /* wait */
    free = TRUE;
       /* use the resource – CRITICAL SECTION */
    free = FALSE;
}
Exercise #2:
   Discuss the correctness of the Use_resource( )
    function below for two process solution:

          char buffer [ n ];
          int full, empty;      { initially full = empty = TRUE }

empty_buffer( )                         fill_buffer( )
{                                       {
     while ( ! full ) do nothing( );        while ( ! empty ) do nothing( );
     full = TRUE;                           empty = TRUE;

               /* critical section */               /* critical section */

     empty ( buffer );                      fill (buffer );
     empty = FALSE;                         full = FALSE;
}                                       }
Exercise #3: Crossing       Baboons
Problem
 There are two groups of baboons, one from the
  east and the other from the west.
 A one-way bridge is attached between the east
  cave where the east baboons live and west
  cave where the west baboons reside.
 Whoever group reaches the foot of the bridge
  first, will cross the bridge first signaling the
  other group not to cross.
 With this condition in mind, how will you
  synchronize the action of the two groups of
  baboons to cross the bridge?
Exercise #4:Cigarette-Smoker
Problem
   There are three smoker processes and one agent
    process.
   Each smoker continuously rolls a cigarette and then
    smokes it. But to roll and smoke a cigarette, the
    smoker needs three ingredients: tobacco, paper, and
    matches.
   One of the smoker processes has paper, another has
    tobacco, and the third has matches.
   Agent has an infinite supply of all three materials. He
    places two of the ingredients on the table.
   The smoker who has the remaining ingredient then
    makes and smokes a cigarette, signaling the agent on
    completion.
   The agent then puts out another two of the three
    ingredients and the cycle repeats. Write a program to
    synchronize the agent and the smokers.
End of Presentation

Mais conteúdo relacionado

Mais procurados

OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitorssgpraju
 
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINTAnne Lee
 
Synchronization
SynchronizationSynchronization
SynchronizationMohd Arif
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronizationvinay arora
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Ra'Fat Al-Msie'deen
 
Operating System-Ch6 process synchronization
Operating System-Ch6 process synchronizationOperating System-Ch6 process synchronization
Operating System-Ch6 process synchronizationSyaiful Ahdan
 
Critical section problem in operating system.
Critical section problem in operating system.Critical section problem in operating system.
Critical section problem in operating system.MOHIT DADU
 
Bakery algorithm
Bakery algorithmBakery algorithm
Bakery algorithmUm e Farwa
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronizationSaad11233
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process SynchronizationSonali Chauhan
 
Synchronization hardware
Synchronization hardwareSynchronization hardware
Synchronization hardwareSaeram Butt
 
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
 
Bakery algorithm in operating system
Bakery algorithm in operating systemBakery algorithm in operating system
Bakery algorithm in operating systemMehdi Hussain
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmSouvik Roy
 

Mais procurados (20)

OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
 
6 Synchronisation
6 Synchronisation6 Synchronisation
6 Synchronisation
 
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
 
Synchronization
SynchronizationSynchronization
Synchronization
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 
Operating System-Ch6 process synchronization
Operating System-Ch6 process synchronizationOperating System-Ch6 process synchronization
Operating System-Ch6 process synchronization
 
Critical section problem in operating system.
Critical section problem in operating system.Critical section problem in operating system.
Critical section problem in operating system.
 
Bakery algorithm
Bakery algorithmBakery algorithm
Bakery algorithm
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
SYNCHRONIZATION
SYNCHRONIZATIONSYNCHRONIZATION
SYNCHRONIZATION
 
Synchronization hardware
Synchronization hardwareSynchronization hardware
Synchronization hardware
 
OS_Ch7
OS_Ch7OS_Ch7
OS_Ch7
 
Critical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy BijjamCritical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy Bijjam
 
Bakery algorithm in operating system
Bakery algorithm in operating systemBakery algorithm in operating system
Bakery algorithm in operating system
 
Monitors
MonitorsMonitors
Monitors
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's Algorithm
 
OSCh7
OSCh7OSCh7
OSCh7
 

Destaque

Module 3 law of contracts
Module 3  law of contractsModule 3  law of contracts
Module 3 law of contractsGichelle Amon
 
Obligations and contracts
Obligations and contractsObligations and contracts
Obligations and contractsJecko Bechayda
 
Law On Obligations and Contracts (midterm exam)
Law On Obligations and Contracts (midterm exam)Law On Obligations and Contracts (midterm exam)
Law On Obligations and Contracts (midterm exam)Denni Domingo
 
Jojo obligation and contracts ppt.
Jojo obligation and contracts ppt.Jojo obligation and contracts ppt.
Jojo obligation and contracts ppt.jojoisanan_mendoza
 
Image segmentation ppt
Image segmentation pptImage segmentation ppt
Image segmentation pptGichelle Amon
 

Destaque (9)

Delivery
DeliveryDelivery
Delivery
 
Os module 2 d
Os module 2 dOs module 2 d
Os module 2 d
 
Kerberos
KerberosKerberos
Kerberos
 
Module 3 law of contracts
Module 3  law of contractsModule 3  law of contracts
Module 3 law of contracts
 
Obligations and contracts
Obligations and contractsObligations and contracts
Obligations and contracts
 
Law On Obligations and Contracts (midterm exam)
Law On Obligations and Contracts (midterm exam)Law On Obligations and Contracts (midterm exam)
Law On Obligations and Contracts (midterm exam)
 
Network security
Network securityNetwork security
Network security
 
Jojo obligation and contracts ppt.
Jojo obligation and contracts ppt.Jojo obligation and contracts ppt.
Jojo obligation and contracts ppt.
 
Image segmentation ppt
Image segmentation pptImage segmentation ppt
Image segmentation ppt
 

Semelhante a Os module 2 c

Semelhante a Os module 2 c (20)

Lecture16-17.ppt
Lecture16-17.pptLecture16-17.ppt
Lecture16-17.ppt
 
09 sinkronisasi proses
09 sinkronisasi proses09 sinkronisasi proses
09 sinkronisasi proses
 
CH05.pdf
CH05.pdfCH05.pdf
CH05.pdf
 
Synchronization in os.pptx
Synchronization in os.pptxSynchronization in os.pptx
Synchronization in os.pptx
 
Slides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdfSlides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdf
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronization
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
 
Os2
Os2Os2
Os2
 
ch6_EN_BK_syn1.pdf
ch6_EN_BK_syn1.pdfch6_EN_BK_syn1.pdf
ch6_EN_BK_syn1.pdf
 
Ipc feb4
Ipc feb4Ipc feb4
Ipc feb4
 
Ch7: Process Synchronization
Ch7: Process SynchronizationCh7: Process Synchronization
Ch7: Process Synchronization
 
Os3
Os3Os3
Os3
 
Ch6
Ch6Ch6
Ch6
 
Galvin-operating System(Ch7)
Galvin-operating System(Ch7)Galvin-operating System(Ch7)
Galvin-operating System(Ch7)
 
os4-2_cop.ppt
os4-2_cop.pptos4-2_cop.ppt
os4-2_cop.ppt
 
U3-PPT-1 (1).ppt
U3-PPT-1 (1).pptU3-PPT-1 (1).ppt
U3-PPT-1 (1).ppt
 
Os2 2
Os2 2Os2 2
Os2 2
 
Shared Memory
Shared MemoryShared Memory
Shared Memory
 
Distributed Coordination
Distributed CoordinationDistributed Coordination
Distributed Coordination
 

Mais de Gichelle Amon (17)

Lec3 final
Lec3 finalLec3 final
Lec3 final
 
Lec 3
Lec 3Lec 3
Lec 3
 
Lec2 final
Lec2 finalLec2 final
Lec2 final
 
Lec 4
Lec 4Lec 4
Lec 4
 
Lec1 final
Lec1 finalLec1 final
Lec1 final
 
Transport triggered architecture
Transport triggered architectureTransport triggered architecture
Transport triggered architecture
 
Time triggered arch.
Time triggered arch.Time triggered arch.
Time triggered arch.
 
Subnetting
SubnettingSubnetting
Subnetting
 
Os module 2 ba
Os module 2 baOs module 2 ba
Os module 2 ba
 
Lec5
Lec5Lec5
Lec5
 
Addressing
AddressingAddressing
Addressing
 
6 spatial filtering p2
6 spatial filtering p26 spatial filtering p2
6 spatial filtering p2
 
5 spatial filtering p1
5 spatial filtering p15 spatial filtering p1
5 spatial filtering p1
 
Medical image analysis
Medical image analysisMedical image analysis
Medical image analysis
 
Presentation2
Presentation2Presentation2
Presentation2
 
Harvard architecture
Harvard architectureHarvard architecture
Harvard architecture
 
Micro channel architecture
Micro channel architectureMicro channel architecture
Micro channel architecture
 

Último

Q2 2024 APCO Geopolitical Radar - The Global Operating Environment for Business
Q2 2024 APCO Geopolitical Radar - The Global Operating Environment for BusinessQ2 2024 APCO Geopolitical Radar - The Global Operating Environment for Business
Q2 2024 APCO Geopolitical Radar - The Global Operating Environment for BusinessAPCO
 
Ethical stalking by Mark Williams. UpliftLive 2024
Ethical stalking by Mark Williams. UpliftLive 2024Ethical stalking by Mark Williams. UpliftLive 2024
Ethical stalking by Mark Williams. UpliftLive 2024Winbusinessin
 
Borderless Access - Global B2B Panel book-unlock 2024
Borderless Access - Global B2B Panel book-unlock 2024Borderless Access - Global B2B Panel book-unlock 2024
Borderless Access - Global B2B Panel book-unlock 2024Borderless Access
 
AMAZON SELLER VIRTUAL ASSISTANT PRODUCT RESEARCH .pdf
AMAZON SELLER VIRTUAL ASSISTANT PRODUCT RESEARCH .pdfAMAZON SELLER VIRTUAL ASSISTANT PRODUCT RESEARCH .pdf
AMAZON SELLER VIRTUAL ASSISTANT PRODUCT RESEARCH .pdfJohnCarloValencia4
 
Lecture_6.pptx English speaking easyb to
Lecture_6.pptx English speaking easyb toLecture_6.pptx English speaking easyb to
Lecture_6.pptx English speaking easyb toumarfarooquejamali32
 
Fabric RFID Wristbands in Ireland for Events and Festivals
Fabric RFID Wristbands in Ireland for Events and FestivalsFabric RFID Wristbands in Ireland for Events and Festivals
Fabric RFID Wristbands in Ireland for Events and FestivalsWristbands Ireland
 
Plano de marketing- inglês em formato ppt
Plano de marketing- inglês  em formato pptPlano de marketing- inglês  em formato ppt
Plano de marketing- inglês em formato pptElizangelaSoaresdaCo
 
Tata Kelola Bisnis perushaan yang bergerak
Tata Kelola Bisnis perushaan yang bergerakTata Kelola Bisnis perushaan yang bergerak
Tata Kelola Bisnis perushaan yang bergerakEditores1
 
Borderless Access - Global Panel book-unlock 2024
Borderless Access - Global Panel book-unlock 2024Borderless Access - Global Panel book-unlock 2024
Borderless Access - Global Panel book-unlock 2024Borderless Access
 
IIBA® Melbourne - Navigating Business Analysis - Excellence for Career Growth...
IIBA® Melbourne - Navigating Business Analysis - Excellence for Career Growth...IIBA® Melbourne - Navigating Business Analysis - Excellence for Career Growth...
IIBA® Melbourne - Navigating Business Analysis - Excellence for Career Growth...AustraliaChapterIIBA
 
Data skills for Agile Teams- Killing story points
Data skills for Agile Teams- Killing story pointsData skills for Agile Teams- Killing story points
Data skills for Agile Teams- Killing story pointsyasinnathani
 
MoneyBridge Pitch Deck - Investor Presentation
MoneyBridge Pitch Deck - Investor PresentationMoneyBridge Pitch Deck - Investor Presentation
MoneyBridge Pitch Deck - Investor Presentationbaron83
 
Graham and Doddsville - Issue 1 - Winter 2006 (1).pdf
Graham and Doddsville - Issue 1 - Winter 2006 (1).pdfGraham and Doddsville - Issue 1 - Winter 2006 (1).pdf
Graham and Doddsville - Issue 1 - Winter 2006 (1).pdfAnhNguyen97152
 
NASA CoCEI Scaling Strategy - November 2023
NASA CoCEI Scaling Strategy - November 2023NASA CoCEI Scaling Strategy - November 2023
NASA CoCEI Scaling Strategy - November 2023Steve Rader
 
Cracking the ‘Business Process Outsourcing’ Code Main.pptx
Cracking the ‘Business Process Outsourcing’ Code Main.pptxCracking the ‘Business Process Outsourcing’ Code Main.pptx
Cracking the ‘Business Process Outsourcing’ Code Main.pptxWorkforce Group
 
PDT 88 - 4 million seed - Seed - Protecto.pdf
PDT 88 - 4 million seed - Seed - Protecto.pdfPDT 88 - 4 million seed - Seed - Protecto.pdf
PDT 88 - 4 million seed - Seed - Protecto.pdfHajeJanKamps
 
Entrepreneurship & organisations: influences and organizations
Entrepreneurship & organisations: influences and organizationsEntrepreneurship & organisations: influences and organizations
Entrepreneurship & organisations: influences and organizationsP&CO
 
Michael Vidyakin: Introduction to PMO (UA)
Michael Vidyakin: Introduction to PMO (UA)Michael Vidyakin: Introduction to PMO (UA)
Michael Vidyakin: Introduction to PMO (UA)Lviv Startup Club
 
Borderless Access - Global Panel book-unlock 2024
Borderless Access - Global Panel book-unlock 2024Borderless Access - Global Panel book-unlock 2024
Borderless Access - Global Panel book-unlock 2024Borderless Access
 
A flour, rice and Suji company in Jhang.
A flour, rice and Suji company in Jhang.A flour, rice and Suji company in Jhang.
A flour, rice and Suji company in Jhang.mcshagufta46
 

Último (20)

Q2 2024 APCO Geopolitical Radar - The Global Operating Environment for Business
Q2 2024 APCO Geopolitical Radar - The Global Operating Environment for BusinessQ2 2024 APCO Geopolitical Radar - The Global Operating Environment for Business
Q2 2024 APCO Geopolitical Radar - The Global Operating Environment for Business
 
Ethical stalking by Mark Williams. UpliftLive 2024
Ethical stalking by Mark Williams. UpliftLive 2024Ethical stalking by Mark Williams. UpliftLive 2024
Ethical stalking by Mark Williams. UpliftLive 2024
 
Borderless Access - Global B2B Panel book-unlock 2024
Borderless Access - Global B2B Panel book-unlock 2024Borderless Access - Global B2B Panel book-unlock 2024
Borderless Access - Global B2B Panel book-unlock 2024
 
AMAZON SELLER VIRTUAL ASSISTANT PRODUCT RESEARCH .pdf
AMAZON SELLER VIRTUAL ASSISTANT PRODUCT RESEARCH .pdfAMAZON SELLER VIRTUAL ASSISTANT PRODUCT RESEARCH .pdf
AMAZON SELLER VIRTUAL ASSISTANT PRODUCT RESEARCH .pdf
 
Lecture_6.pptx English speaking easyb to
Lecture_6.pptx English speaking easyb toLecture_6.pptx English speaking easyb to
Lecture_6.pptx English speaking easyb to
 
Fabric RFID Wristbands in Ireland for Events and Festivals
Fabric RFID Wristbands in Ireland for Events and FestivalsFabric RFID Wristbands in Ireland for Events and Festivals
Fabric RFID Wristbands in Ireland for Events and Festivals
 
Plano de marketing- inglês em formato ppt
Plano de marketing- inglês  em formato pptPlano de marketing- inglês  em formato ppt
Plano de marketing- inglês em formato ppt
 
Tata Kelola Bisnis perushaan yang bergerak
Tata Kelola Bisnis perushaan yang bergerakTata Kelola Bisnis perushaan yang bergerak
Tata Kelola Bisnis perushaan yang bergerak
 
Borderless Access - Global Panel book-unlock 2024
Borderless Access - Global Panel book-unlock 2024Borderless Access - Global Panel book-unlock 2024
Borderless Access - Global Panel book-unlock 2024
 
IIBA® Melbourne - Navigating Business Analysis - Excellence for Career Growth...
IIBA® Melbourne - Navigating Business Analysis - Excellence for Career Growth...IIBA® Melbourne - Navigating Business Analysis - Excellence for Career Growth...
IIBA® Melbourne - Navigating Business Analysis - Excellence for Career Growth...
 
Data skills for Agile Teams- Killing story points
Data skills for Agile Teams- Killing story pointsData skills for Agile Teams- Killing story points
Data skills for Agile Teams- Killing story points
 
MoneyBridge Pitch Deck - Investor Presentation
MoneyBridge Pitch Deck - Investor PresentationMoneyBridge Pitch Deck - Investor Presentation
MoneyBridge Pitch Deck - Investor Presentation
 
Graham and Doddsville - Issue 1 - Winter 2006 (1).pdf
Graham and Doddsville - Issue 1 - Winter 2006 (1).pdfGraham and Doddsville - Issue 1 - Winter 2006 (1).pdf
Graham and Doddsville - Issue 1 - Winter 2006 (1).pdf
 
NASA CoCEI Scaling Strategy - November 2023
NASA CoCEI Scaling Strategy - November 2023NASA CoCEI Scaling Strategy - November 2023
NASA CoCEI Scaling Strategy - November 2023
 
Cracking the ‘Business Process Outsourcing’ Code Main.pptx
Cracking the ‘Business Process Outsourcing’ Code Main.pptxCracking the ‘Business Process Outsourcing’ Code Main.pptx
Cracking the ‘Business Process Outsourcing’ Code Main.pptx
 
PDT 88 - 4 million seed - Seed - Protecto.pdf
PDT 88 - 4 million seed - Seed - Protecto.pdfPDT 88 - 4 million seed - Seed - Protecto.pdf
PDT 88 - 4 million seed - Seed - Protecto.pdf
 
Entrepreneurship & organisations: influences and organizations
Entrepreneurship & organisations: influences and organizationsEntrepreneurship & organisations: influences and organizations
Entrepreneurship & organisations: influences and organizations
 
Michael Vidyakin: Introduction to PMO (UA)
Michael Vidyakin: Introduction to PMO (UA)Michael Vidyakin: Introduction to PMO (UA)
Michael Vidyakin: Introduction to PMO (UA)
 
Borderless Access - Global Panel book-unlock 2024
Borderless Access - Global Panel book-unlock 2024Borderless Access - Global Panel book-unlock 2024
Borderless Access - Global Panel book-unlock 2024
 
A flour, rice and Suji company in Jhang.
A flour, rice and Suji company in Jhang.A flour, rice and Suji company in Jhang.
A flour, rice and Suji company in Jhang.
 

Os module 2 c

  • 1. The OS as a gatekeeper
  • 2. Objective  To discuss various mechanisms to ensure the orderly execution of cooperating processes that share a logical address space so that data is consistently maintained.
  • 3. Concept of concurrency  Existence of several simultaneous processes. May be observed in situations like:  Multi-user OS where different users execute programs concurrently  Single-user OS where input/output devices go simultaneously with program execution  Some processors whose activities are concurrent, example, fetch next instruction is done simultaneously with the execution of an instruction.
  • 4. bounded buffer PRODUCER CONSUMER while (true) { while (true) { /* produce an item and put in while (count == 0) nextProduced */ do nothing while (count == BUFFER_SIZE) nextConsumed = buffer [out]; do nothing; out = (out + 1) % BUFFER_SIZE; buffer [in] = nextProduced; count--; in = (in + 1) % BUFFER_SIZE; /* consume the item in count++; nextConsumed */ } }
  • 5. Atomic Operation  The statements count ++ and count -- must be performed atomically.  Atomic operation means an operation that completes in its entirety without interruption.  May not function correctly when executed concurrently.
  • 6. Atomic Operation  If both the producer and consumer attempt to update the buffer concurrently, the assembly language may get interleaved  Interleaving depends upon how the producer and consumer processes are scheduled.
  • 7. Atomic Operation  count++ could be implemented as MOV AX, COUNT register1 = count INC AX register1 = register1 + 1 MOV COUNT, AX count = register1  count-- could be implemented as MOV BX, COUNT register2 = count DEC BX register2 = register2 - 1 MOV COUNT, BX count = register2
  • 8. Now consider this interleaving…  Consider this execution interleaving with “count = 5” initially:  S0: producer execute register1 = count {register1 = 5}  S1: producer execute register1 = register1 + 1 {register1 = 6}  S2: consumer execute register2 = count {register2 = 5}  S3: consumer execute register2 = register2 - 1 {register2 = 4}  S4: producer execute count = register1 {count = 6 }  S5: consumer execute count = register2 {count = 4}
  • 9. Race Condition  Situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last.  To prevent race conditions, concurrent processes must be synchronized.
  • 10. The Critical Section (CS) Problem  Consider n processes all competing to use some shared data.  Each process has a code segment, called critical section in which shared data is accessed.  Ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical condition.
  • 11. The Critical Section (CS) Problem  Execution of critical sections by the processes is mutually exclusive in time.  Design protocol:  Each process must request permission to enter its CS (entry section)  The CS is followed by an exit section  The remaining code is in the remainder section.
  • 12. Solution to Critical-Section Problem 1.Mutual Exclusion 2.Progress 3.Bounded Waiting
  • 13. Solution to Critical-Section Problem 1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections CS P1 P2 P n
  • 14. Solution to Critical-Section Problem Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. CS P P 2 1 P
  • 15. Solution to Critical-Section Problem 1. Bounded Waiting - No process should wait arbitrarily long to enter its CS. CS P P 2 1 P n
  • 16. Note:  No assumptions are made about relative process speeds or number of CPU’s  A process in one CS should not block others in entering a different CS.
  • 17. Sample Scenario – Milk Issue Time Person A Person B 3:00 Look in fridge, Out of milk ------------- 3:05 Leave for store ------------- 3:10 Arrive at store Look in fridge, Out of Milk 3:15 Buy Milk Leave for store 3:20 Leave store Arrive at store 3:25 Arrive home, put milk away Buy milk 3:30 ------------- Leave Store 3:35 ------------- Arrive home, oops
  • 18. Observation  Someone gets milk, but not EVERYONE (too much milk!)  This shows that when cooperating processes are not synchronized, they may face unexpected “timing errors”  Mutual Exclusion - ensures that only one process (or person) is doing a thing at one time, thus avoiding data inconsistency. All others should be prevented from modifying shared data until the current process finishes. E.g. only one person buys milk at a time.
  • 19. Solution 1 – Attempt to computerize Milk Buying PROCESS A AND B This solution works for some If (NOmilk) people because the first { three lines are performed if (NOnote) ATOMICALLY but does { not work otherwise. Leave note; Buy milk; What happens when the 2 Remove note; processes execute } concurrently? }
  • 20. Solution 2 – Attempt to use two notes PROCESS A PROCESS B Leave (NOTEa); Leave (NOTEb); If (NO_NOTEb) If (NO_NOTEa) { { if (NOmilk) Buy Milk; if (NOmilk) Buy Milk; } } Remove (NOTEa); Remove (NOTEb); WHAT CAN YOU SAY ABOUT THIS SOLUTION?
  • 21. Solution 3: PROCESS A PROCESS B Leave (NOTEa); Leave (NOTEb) If (NOnoteB) While (NOTEa) do { nothing; if (NOmilk) Buy Milk; } if (NOmilk) Buy Milk; Remove (NOTEa); Remove (NOTEb); In case of tie, who will buy first?
  • 22. Critique for Solution 3  Process B will always be the first to buy the milk in case of a tie.  Process B consumes CPU cycles while waiting.  More complicated if extended to many processes.
  • 23. Algorithm Template do { ENTRY SECTION – Each process must request ENTRY_SECTION permission to enter its CS CRITICAL_SECTION EXIT_SECTION EXIT SECTION – Process leaves the CS thereby informing other processes Remainder_Section that the CS is free for some other process to enter } while (TRUE)
  • 24. Two Process Solution Algo 1: Strict Alteration Shared var Turn <either a or b> PROCESS A PROCESS B repeat repeat While (turn==B) do While (turn==A) do nothing; nothing; <critical section A> <critical section B> Turn = B; Turn = A; until false until false Which of the following requirement is violated? Mutual Exclusion Progress Bounded Waiting
  • 25. Algorithm 2 Shared Var pAinside, pBinside; FALSE PROCESS A PROCESS B While (TRUE) { While (TRUE) { While (pBinside) do While (pAinside) do nothing; nothing; pAinside = True; pBinside = True; <critical section A> <critical section B> pAinside = False; pBinside = False; } } Which of the following requirement is violated? Mutual Exclusion Progress Bounded Waiting
  • 26. Algorithm 3 Shared Var pAtrying, pBtrying : FALSE PROCESS A PROCESS B While (TRUE) { While (TRUE) { pAtrying = True; pBtrying = True; While (pBtrying) do While (pAtrying) do nothing; nothing; <critical section A> <critical section B> pAtrying = False; pBtrying = False; } } Which of the following requirement is violated? Mutual Exclusion Progress Bounded Waiting
  • 27. Dekker’s Algorithm - Elegant solution to mutual exclusion problem Shared var pAtrying pBtrying = False PROCESS A PROCESS B While (TRUE) { While (TRUE) { pAtrying = True; pBtrying = True; While (pBtrying) While (pAtrying) if (turn==B) { if (turn==A) { pAtrying = False; pBtrying = False; while (turn==B) do nothing; while (turn==A) do nothing; pAtrying = True pBtrying = True } } <critical section> <critical section> turn==B; turn==A; pAtrying = False; pBtrying = False; } }
  • 28. Peterson’s Algorithm: Much simpler than Dekker’s Algorithm Shared var pAtrying pBtrying = False PROCESS A PROCESS B While (TRUE) While (TRUE) { { pAtrying = True; pBtrying = True; turn= B; turn= A; While (pBtrying && turn ==B) While (pAtrying && turn ==A) do nothing; do nothing; <critical section A> <critical section A> pAtrying = False; pBtrying = False; } }
  • 29. Note  Both Dekker’s and Peterson’s algorithms are correct but they only work for 2 processes, similar to the last solution of the “too-much- milk” problem.
  • 30. Multiple Process Synchronization Solution  Hardware Support – special instructions Many CPU’s today provide hardware instructions to read, modify and store a word atomically. Most common instructions with this capability are:  TAS – (Test and Set ) – Motorolla 68k  CAS – (Compare and Swap) – IBM 370 and Motorola 68k  XCHG – (exchange) –x86
  • 31. Mutual Exclusion with Test-&-Set (TAS) Shared data: boolean TestAndSet boolean lock = false; (boolean &target) Process Pi { do { boolean rv = target; while (TestAndSet(lock)) do target = true; nothing( ); return rv; } <critical section> lock = false; remainder section }
  • 32. Mutual Exclusion with SWAP and XCHG Atomically swap two Shared data (initialized to false): variables. boolean lock; void Swap(boolean &a, boolean waiting[n]; boolean &b) Process Pi do { { key = true; boolean temp = a; while (key == true) a = b; Swap(lock,key); b = temp; critical section } lock = false; remainder section }
  • 33. Multiple Process Synchronization Solution  The basic idea is to be able to read the contents of a variable (memory location) and set it to something else all in one execution cycle hence not interruptible.  The use of these special instructions makes the programming task easier and improves efficiency.
  • 34. Semaphores  Synchronization tool that does not require busy waiting  Semaphore S – integer variable  Two standard operations modify S: wait() and signal()
  • 35. A Semaphore…  Essentially an integer S (initially S>0) and a queue of processes, initially empty  Synchronization variable (guard) that takes on non-negative integer values with only two atomic operations
  • 36. Two Atomic Operations Wait (S) PROBEREN:  While s ≤ 0 do no_op; Probe/Test  S-- Signal (S) VERHOGEN:  S++ release “make higher”
  • 37. Solution to too much milk using semaphore Process A & B Semaphore OkToBuyMilk = 1; Wait (OkToBuyMilk); If (NoMilk) { Buy Milk; } Signal (OkToBuyMilk)
  • 38. Critical Section of n Processes  Shared data: semaphore mutex (initially 1) do{ wait (mutex); < critical section > signal (mutex); remainder section }while (true)
  • 39. Critique: CS of n Processes  The above algorithm requires busy-wait.  To overcome busy waiting, the process will block itself (placed in the waiting - queue associated with the semaphore) Define a semaphore as a record typedef struct { int value; struct process *L; } semaphore;
  • 40. Example #1:  There are 6 Synchronize the execution of the processes waiting two CPUs: in the ready queue CPU1: CPU2: (P1,P2…P6) P1; P4;  Let us assume that wait (s1);  P5 can only be P2; P5; executed if P1 and P2 have already signal (s1); completed their wait (s2); tasks  P3 will only be P3; P6; executed if P6 is signal (s2); finished
  • 41. Exercise #2:  Consider the following code which is being executed by 3 CPU’s. This program computes the value of X using the Quadratic formula.  Synchronize the execution of the CPU’s by using semaphore.
  • 42. X = -B± √(B2 – 4 A C) Exercise #2 Codes: 2A CPU1: Readln (C); CPU2: Readln (A); FourAC = 4*A*C; TwiceA = A + A; If (FourAC > BSquare); Imaginary Readln (B); SquareRoot = sqrt(abs(Bsquare – FourAC) Bsquare = B * B; Root1 = (MinusB + SquareRoot)/TwiceA; MinusB = -1 * B; Root2 = (MinusB - SquareRoot)/TwiceA; CPU3: If (Imaginary) writeln (“No real roots”) Else writeln (“the roots are”, root1, root2”)
  • 43. Solution to Exercise 2 X = -B± √(B2 – 4 A C) 2A CPU1: Readln (C); CPU2: Readln (A); Wait(S1); Signal(S1); FourAC = 4*A*C; TwiceA = A + A; Wait(S2); Readln (B); If (FourAC > BSquare) then Imaginary =TRUE; Bsquare = B * B; SquareRoot = sqrt(abs(Bsquare – FourAC) Signal (S2); Wait(S3); MinusB = -1 * B; Root1 = (MinusB + SquareRoot)/TwiceA; Signal (S3); Root2 = (MinusB - SquareRoot)/TwiceA; Signal (S4); Wait (S4); CPU3 If (Imaginary) writeln (“No real roots”) Else writeln (“the roots are”, root1, root2”)
  • 44. Possible uses of semaphores  Mutual Exclusion – Initializes the semaphore to one (1)  Example: Wait (Mutex); < Critical section; > Signal (Mutex);
  • 45. Possible uses of semaphores  Synchronization of cooperating processes (signalling) – initializes the semaphore to zero.  Example Pi: Pj … … … … A wait (flag) Signal (flag) B
  • 46. Possible uses of semaphore  Managing multiple instances of a resource - Initializes the semaphore to the number of instances.
  • 47. Types of semaphores  Binary – semaphore with an integer value of 0 or 1  Counting – semaphore with an integer value ranging between 0 and an arbitrarily large number. - Initial value represents the number of units of the critical resources that are available. - Also known as general semaphore.
  • 48. Classical Problems of Synchronization
  • 49. Bounded-Buffer Problem:  Two processes share a common, fixed size buffer. One of them, the producer puts information into the buffer, and the other, the consumer, takes out information from the buffer. When the producer wants to put a new item in the buffer but it is already full then allow the producer to sleep to be awakened when the consumer has removed one or more items. Similarly to the consumer, it goes to sleep if the buffer is empty until the producer puts something in the buffer.
  • 50. shared data semaphore full = 0, empty = n, mutex = 1; Producer: Consumer: do { do { … wait ( full ) produce an item in nextp wait ( mutex ); … … remove item from buffer to wait ( empty ); nextc wait ( mutex ); … … signal ( mutex ); add nextp to buffer signal ( empty ); … … signal ( mutex ); consume item in nextc signal ( full ); … } while (TRUE); } while (TRUE);
  • 51. Readers-Writers Problem  This problem models access to a database with many competing processes wishing to read and write into it (imagine an airline reservation system). It is possible to have many processes reading the database at the same time (for query purposes). But if one process is writing (modifying), no other process shall access to the database, not even readers. With this condition in mind, how will you program the reader and writers?
  • 52. Illustration: Readers and Writers Writers Area wrt Readers Area Critical section wrt
  • 53. Readers-Writers: Solution Types  Algorithm 1: Readers have higher priority than writers  Algorithm 2: Writers have higher priority than readers
  • 54. Algorithm #1:  Readers have higher priority than writers If a writer appears while several readers are in the database, the writer must wait. If new readers keep appearing so that there is always at least one reader in the database, the writer must keep waiting until no more readers are interested in the database.
  • 55. Solution to Readers/Writers: Algorithm #1 shared data semaphore mutex = 1, wrt = 1; int readcount = 0; Readers: Writers: do { wait ( mutex ); do { readcount + +; wait(wrt); if ( readcount == 1 ) wait ( wrt ); … signal ( mutex ); … writing is performed reading is performed … … signal(wrt); wait ( mutex ); readcount - -; } while (TRUE) if (readcount == 0 ) signal ( wrt ); signal ( mutex ): } while (TRUE)
  • 56. Algorithm 2:  How about if we prioritize Writers over readers? Reprogram the above codes.  Seatwork…. Writers have higher priority than readers
  • 57. Dining-Philosophers Problem  Five philosophers spend their lives thinking and eating.  Philosophers share a common circular table surrounded by five chairs, each belonging to one philosopher.  At the center of the table, there is a bowl of rice, and the table is laid with five single chopsticks.
  • 58. Dining-Philosophers cont…  When a philosopher thinks, he does not interact with his colleagues.  When a philosopher gets hungry, he tries to pick up two chopsticks that are closest to him.  Philosopher could pick up only one chopstick at a time.  Obviously, he cannot pick up a chopstick that is already in the hand of his neighbor.  When a hungry philosopher has his both chopsticks, he eats without releasing them.  When he is finished, he puts down the chopsticks and starts thinking again.
  • 60. Dining Philosophers: Initial Solution Shared data semaphore chopstick[5]; {Initially all values are 1} Philosopher i: do { wait ( chopstick [ i ] ) { his left chopstick} wait ( chopstick [ ( i + 1) % 5 ] ) { his right chopstick} … eat … signal ( chopstick [ i ] ); 0 4 signal ( chopstick [ ( i + 1) % 5 ] ); 0 … 1 4 think … 1 2 3 3 } while (TRUE); 2 Question: What is wrong with this solution?
  • 61. Solution for Dining Philosophers… Shared data: state [ 5 ] of (thinking, hungry, eating); // init to all thinking// self [ 5 ]: semaphore; { init to all 0 } mutex: semaphore; { init to 1 } left = (i + 4) % 5; { left neighbor } right = (i + 1) % 5; { right neighbor }
  • 62. Solution for Dining Philosophers… Philosopher ( i ) { void test ( int i ) { if ( (state [ left ] ! = eating) && state [ i ] = hungry; (state [ i ] == hungry) && wait ( mutex ); (state [ right ] != eating)) { test [ i ]; state [ i ] = eating; signal (mutex); signal (self [ i ] ); if (state [ i ] != eating ) } wait ( self [ i ] ); } void putdown ( int i ) { … eating … state [ i ] = thinking; wait ( mutex ); wait ( self [ i ] ); putdown [ i ]; signal ( mutex ); // test left and right neighbors } test ( left ); test ( ( right ); }
  • 63. Sleeping – Barbers Problem:  A barber shop consists of a waiting room with 5 chairs and the barber room containing the barber chair.  If no customer to serve, the barber goes to sleep to be awakened by the customer.  If a customer enters the barber shop but all chairs are occupied, the customer leaves the shop; otherwise he sits in one of the free chairs.  Write the program to coordinate the actions of the barber and the customer.
  • 64. Sleeping – Barbers Solution Shared data: customer = 0, barber = 0, mutex = 1; int waiting = 0; CHAIR = 5; Barber: Customer: while (TRUE) { while ( TRUE ) { wait ( customer ); wait ( mutex ); wait ( mutex ); if ( waiting < CHAIR) { waiting - -; waiting + +; signal ( barber ); signal ( customer ); signal ( mutex ); signal ( mutex ); .... wait ( barber ); cut hair(); ….. } get haircut(); } else signal ( mutex); }
  • 65. Exercise #1:  Discuss the correctness of the Use_resource( ) function below for two process solution: int free = TRUE; use_resource ( ) { while ( ! free ); /* wait */ free = TRUE; /* use the resource – CRITICAL SECTION */ free = FALSE; }
  • 66. Exercise #2:  Discuss the correctness of the Use_resource( ) function below for two process solution: char buffer [ n ]; int full, empty; { initially full = empty = TRUE } empty_buffer( ) fill_buffer( ) { { while ( ! full ) do nothing( ); while ( ! empty ) do nothing( ); full = TRUE; empty = TRUE; /* critical section */ /* critical section */ empty ( buffer ); fill (buffer ); empty = FALSE; full = FALSE; } }
  • 67. Exercise #3: Crossing Baboons Problem  There are two groups of baboons, one from the east and the other from the west.  A one-way bridge is attached between the east cave where the east baboons live and west cave where the west baboons reside.  Whoever group reaches the foot of the bridge first, will cross the bridge first signaling the other group not to cross.  With this condition in mind, how will you synchronize the action of the two groups of baboons to cross the bridge?
  • 68. Exercise #4:Cigarette-Smoker Problem  There are three smoker processes and one agent process.  Each smoker continuously rolls a cigarette and then smokes it. But to roll and smoke a cigarette, the smoker needs three ingredients: tobacco, paper, and matches.  One of the smoker processes has paper, another has tobacco, and the third has matches.  Agent has an infinite supply of all three materials. He places two of the ingredients on the table.  The smoker who has the remaining ingredient then makes and smokes a cigarette, signaling the agent on completion.  The agent then puts out another two of the three ingredients and the cycle repeats. Write a program to synchronize the agent and the smokers.