From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Help with tasks Date: Sat, 7 May 2016 13:44:12 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <410b222c-9443-46b7-8a18-2fc3d05772a9@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 7 May 2016 20:40:48 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="48b46be33beed75863f69afa437f956b"; logging-data="22947"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18WBVypLHGGIyQqRPp2yo3rzOOjQmNEUUs=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.0 In-Reply-To: <410b222c-9443-46b7-8a18-2fc3d05772a9@googlegroups.com> Cancel-Lock: sha1:wltE3BaIFf8bWTcoTBYW/am6S84= Xref: news.eternal-september.org comp.lang.ada:30352 Date: 2016-05-07T13:44:12-07:00 List-Id: On 05/07/2016 07:53 AM, danifreecs@gmail.com wrote: > Hi! i'm a student, and got stucked with an ada home project, and i need a lot > of help sadly, i can't even ask a specific question, because i have a lot, > but it's about tasks and protected objects. It would be nice to have some idea of how much training/experience you have with Ada, and how much with other programming languages. I fear that if your attitude is that you "got stucked [sic]" with an Ada project, you'll never develop a reasonable solution. Ada is the language of choice for knowledgeable software engineers, especially for safety- or security-critical systems, and has features that make solving problems like yours simple compared to every other mainstream language. If you welcome the opportunity to find out why Ada is the language of choice, what those features are, and how they work, it will make you a better software engineer, even if you end up using another language. > The project: There's a garden with 10 plots, and a locust gets in 1 of the > plots, so the gardener wants to kill it with poison. The locust randomly > changes plots in a time, and the gardener can spray 1 plot/day (either > randomly), which gets clean from poison after that day has passed. The locust > dies if it jumps into the sprayed plot, or is in it already. The simulation > goes until the locust dies. For the random number generation and the console > output use a common protected object. (Monitor) So you have a Gardener that does Until_Locust_Dead : loop wait until locust is dead or next spray time if locust is dead then exit Until_Locust_Dead; end if; update next spray time Plot := Random.Plot; display "Gardener sprays plot " & Image (Plot) if Plot = locust plot then set Locust as dead display "Locust is dead" exit Until_Locust_Dead; end if; end loop Until_Locust_Dead; and a Locust that does Until_I_Am_Dead : loop wait until locust is dead or next jump time if locust is dead then exit Until_I_Am_Dead; end if; update next jump time Plot := Random.Plot; display "Locust in plot " & Image (Plot) if Plot = poisoned plot then set Locust as dead display "Locust is dead" exit Until_I_Am_Dead; end if; end loop Until_I_Am_Dead; Each of these operates independently, so would be a task. Ada has features for waiting for either of an event or a time (see select statements), so you'll want to read about those. How they work will help you decide how to structure your solution. I'd think in terms of a protected object for the plot states, allowing the Gardener to set which plot is poisoned and to find out which plot the Locust is in, and the Locust to set which plot it is in and to find out which plot is poisoned. Another protected object would control the Locust's live/dead state, allowing either actor to set the state to dead, and allowing both to wait until the state is dead. Finally, I'd have a task for the display operations, since I/O is not task safe, you can't do I/O from a protected operation, and you don't need to queue messages. HTH -- Jeff Carter "I'm particularly glad that these lovely children were here today to hear that speech. Not only was it authentic frontier gibberish, it expressed a courage little seen in this day and age." Blazing Saddles 88