{"id":2630,"date":"2023-12-23T18:18:49","date_gmt":"2023-12-23T17:18:49","guid":{"rendered":"https:\/\/oopm.org\/?page_id=2630"},"modified":"2024-11-26T14:36:01","modified_gmt":"2024-11-26T13:36:01","slug":"11-3-implementing-a-monitor-system","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=2630","title":{"rendered":"16.3 Implementing a Monitor System"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages2630&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages2630&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">Here we will show how to implement a <code>Monitor<\/code> system using preemptive coroutines and Semaphore.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Below we show our first version of class <code>Monitor<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Monitor:\n   entry:\n      mutex.wait\n      inner(entry)\n      mutex.signal\n   %private\n   mutex: <strong>obj<\/strong> Semaphore(1)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A&nbsp;<code>Monitor<\/code>-object has a <code>Semaphore<\/code> attribute and a local method pattern,&nbsp;<code>entry<\/code>, which is to be used as a supermethod for methods in subclasses of <code>Monitor<\/code>. As can be seen, <code>entry<\/code> has an <code>inner(entry)<\/code>; <code>mutex.wait<\/code> is executed before <code>inner(entry) <\/code>and<code> mutex.signal<\/code> is executed after <code>inner(entry)<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>Account<\/code>&nbsp;class may be described as a subclass of&nbsp;<code>Monitor<\/code>&nbsp;in the following way:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Account: Monitor\n   deposit(amount: <strong>var<\/strong> float): entry\n      balance := balance + amount\n   withdraw(amount: <strong>var<\/strong> float): entry\n      balance := balance - amount\n   %private\n   balance: <strong>var<\/strong> float<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1133\" height=\"251\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/08\/MonitorAndAccount-1.jpg\" alt=\"\" class=\"wp-image-7216\" style=\"width:1026px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/08\/MonitorAndAccount-1.jpg 1133w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/08\/MonitorAndAccount-1-300x66.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/08\/MonitorAndAccount-1-1024x227.jpg 1024w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/08\/MonitorAndAccount-1-768x170.jpg 768w\" sizes=\"(max-width: 1133px) 100vw, 1133px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>anAccount<\/code> refers to an <code>Account<\/code>-object, then execution of <code>anAccount.deposit(200)<\/code> has the effect  that the supermethod <code>entry<\/code> of <code>deposit<\/code> works like a wrapper around the statement <code>balance := balance + amount<\/code>. This ensures that <code>mutex.signal<\/code> is executed before the statement and <code>mutex.signal<\/code> is executed afterwards. The same is the case for execution of a <code>anAccount.withDraw(300)<\/code>. All in all, using <code>entry<\/code> as a supermethod ensures that at most one <code>deposit<\/code> or <code>withdraw<\/code> may be executed at the same time, which guarantees exclusive acces to data-items within the <code>Monitor<\/code> object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In section <script>mkRef(\"A simple search system\");<\/script>, <code>Monitor<\/code> is used as part of class that also defines a <code>MonitorProcess<\/code> class. We will show how to define such a system. It will contain the elements shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> MonitorSystem\n   <strong>class<\/strong> Monitor: ...\n   <strong>class<\/strong> MonitorProcess: ...\n   %private\n   scheduler: <strong>obj<\/strong> ...\n   SQS: <strong>obj<\/strong> ProcessQueue<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>Monitor<\/code> is defined as shown above. Class <code>MonitorProcess<\/code> is supposed to be a superclass of all parallel objects in the <code>MonitorSystem<\/code>. The scheduler object handles scheduling of <code>MonitorProcess<\/code>-objects and <code>SQS<\/code> is a queue of <code>MonitorProcess<\/code>-objects that are ready for being executed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>MonitorProcess<\/code> may be defined as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> MonitorProcess:\n   start:  \n      status := ACTIVE\n      SQS.insert(this(MonitorProcess)) \n   status: <strong>var<\/strong> integer\n   inner(MonitorProcess)\n   status := TERMINATED\n<\/code><\/pre>\n\n\n\n<p class=\"has-normal-font-size wp-block-paragraph\">A <code>MonitorProcess<\/code> has four attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <code>start<\/code> method that sets the status of the <code>MonitorProcess<\/code> object to <code>ACTIVE<\/code>.<\/li>\n\n\n\n<li>An integer variable <code>status<\/code> holding the status of the <code>MonitorProcess<\/code>.<\/li>\n\n\n\n<li>An <code>inner<\/code>-statement that implies execution of items in a subclass of <code>MonitorProcess<\/code>.<\/li>\n\n\n\n<li>Finally a statement setting <code>status<\/code> to <code>TERMINATED<\/code> whereafter execution of the <code>MonitorProcess<\/code> object ends.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Next we describe the <code>Scheduler<\/code> object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>scheduler: <strong>obj<\/strong>\n  active: <strong>ref<\/strong> MonitorProcess\n  cycle\n     active := SQS.next\n     if (active &lt;&gt; none) :then\n        active.attach(100)\n        if (active.status = ACTIVE) then \n           SQS.insert(active)<\/code><\/pre>\n\n\n\n<p class=\"has-normal-font-size wp-block-paragraph\">The <code>scheduler<\/code> has a data-item active that is a reference to the <code>MonitorProcess<\/code> object currently being executed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then it has a <code>cycle<\/code>-statement that forever executes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A reference to the next <code>MonitorProcess<\/code> in the queue <code>SQS<\/code> of active <code>MonitorProcess<\/code>-objects is assigned to <code>active<\/code>.<\/li>\n\n\n\n<li>If there are no references in <code>SQS<\/code>, <code>active<\/code> will get the datum <code>none<\/code>.<\/li>\n\n\n\n<li>If <code>active<\/code> is not <code>none<\/code>, the method <code>active.attach(100)<\/code> is invoked implying that execution of <code>active<\/code> is resumed. If <code>active<\/code> has not bee executed before, execution starts from the beginning of <code>active<\/code>; if <code>active<\/code> has been executed before ans execution has been suspended, execution is resumed after the point of suspension.<\/li>\n\n\n\n<li><code>Active<\/code> will be preemptively suspended after 100 time units, but it may also terminate before the 100 time units have appeared.<\/li>\n\n\n\n<li>If active<code> <\/code>is preemptively suspended, <code>active.status = ACTIVE<\/code> and <code>active<\/code> is re-inserted into SQS.<\/li>\n\n\n\n<li>Otherwise <code>active<\/code> has terminated (<code>status = TERMINATED<\/code>) execution and thus not re-inserted into <code>SQS<\/code>.<\/li>\n<\/ul>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=2630\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>Here we will show how to implement a Monitor system using preemptive coroutines and Semaphore. Below we show our first version of class Monitor: A&nbsp;Monitor-object has a Semaphore attribute and a local method pattern,&nbsp;entry, which is to be used as a supermethod for methods in subclasses of Monitor. As can be seen, entry has an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1972,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2630","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2630"}],"version-history":[{"count":44,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2630\/revisions"}],"predecessor-version":[{"id":9879,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2630\/revisions\/9879"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1972"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}