How to resolve the algorithm Checkpoint synchronization step by step in the Oforth programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Checkpoint synchronization step by step in the Oforth 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 Oforth programming language

Source code in the oforth programming language

: task(n, jobs, myChannel)
   while(true) [
      System.Out "TASK " << n << " : Beginning my work..." << cr
      System sleep(1000 rand)
      System.Out "TASK " << n << " : Finish, sendind done and waiting for others..." << cr
      jobs send($jobDone) drop
      myChannel receive drop
      ] ;

: checkPoint(n, jobs, channels)
   while(true) [
      #[ jobs receive drop ] times(n)
      "CHECKPOINT : All jobs done, sending done to all tasks" println
      channels apply(#[ send($allDone) drop ])
      ] ;

: testCheckPoint(n) 
| jobs channels i |
   ListBuffer init(n, #[ Channel new ]) dup freeze ->channels   
   Channel new ->jobs 

   #[ checkPoint(n, jobs, channels) ] &
   n loop: i [ #[ task(i, jobs, channels at(i)) ] & ] ;

  

You may also check:How to resolve the algorithm Perfect totient numbers step by step in the Dart programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the Oforth programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the min programming language
You may also check:How to resolve the algorithm Gapful numbers step by step in the Frink programming language
You may also check:How to resolve the algorithm Leap year step by step in the ALGOL-M programming language