How to resolve the algorithm Checkpoint synchronization step by step in the Icon and Unicon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Checkpoint synchronization step by step in the Icon and Unicon programming language

Table of Contents

Problem Statement

The checkpoint synchronization is a problem of synchronizing multiple tasks. Consider a workshop where several workers (tasks) assembly details of some mechanism. When each of them completes his work they put the details together. There is no store, so a worker who finished its part first must wait for others before starting another one. Putting details together is the checkpoint at which tasks synchronize themselves before going their paths apart. The task Implement checkpoint synchronization in your language. Make sure that the solution is race condition-free. Note that a straightforward solution based on events is exposed to race condition. Let two tasks A and B need to be synchronized at a checkpoint. Each signals its event (EA and EB correspondingly), then waits for the AND-combination of the events (EA&EB) and resets its event. Consider the following scenario: A signals EA first and gets blocked waiting for EA&EB. Then B signals EB and loses the processor. Then A is released (both events are signaled) and resets EA. Now if B returns and enters waiting for EA&EB, it gets lost. When a worker is ready it shall not continue before others finish. A typical implementation bug is when a worker is counted twice within one working cycle causing its premature completion. This happens when the quickest worker serves its cycle two times while the laziest one is lagging behind. If you can, implement workers joining and leaving.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Checkpoint synchronization step by step in the Icon and Unicon programming language

Source code in the icon programming language

global nWorkers, workers, cv

procedure main(A)
    nWorkers := integer(A[1]) | 3
    cv  := condvar()
    every put(workers := [], worker(!nWorkers))
    every wait(!workers)
end
    
procedure worker(n)
    return thread every !3 do {       # Union limits each worker to 3 pieces
        write(n," is working")
        delay(?3 * 1000)
        write(n," is done")
        countdown()
        }
end

procedure countdown()
    critical cv: {
        if (nWorkers -:= 1) <= 0 then {
            write("\t\tAll done")
            nWorkers := *workers
            return (unlock(cv),signal(cv, 0))
            }
        wait(cv)
        }
end


  

You may also check:How to resolve the algorithm User input/Graphical step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the Haskell programming language
You may also check:How to resolve the algorithm P-Adic numbers, basic step by step in the Wren programming language
You may also check:How to resolve the algorithm XML/XPath step by step in the Delphi programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the PicoLisp programming language