comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Help with tasks
Date: Sat, 7 May 2016 13:44:12 -0700
Date: 2016-05-07T13:44:12-07:00	[thread overview]
Message-ID: <ngljse$md3$1@dont-email.me> (raw)
In-Reply-To: <410b222c-9443-46b7-8a18-2fc3d05772a9@googlegroups.com>

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

  parent reply	other threads:[~2016-05-07 20:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-07 14:53 Help with tasks danifreecs
2016-05-07 17:43 ` Dennis Lee Bieber
2016-05-07 18:50 ` Anh Vo
2016-05-07 20:44 ` Jeffrey R. Carter [this message]
2016-05-08 12:23 ` danifreecs
2016-05-08 18:28   ` Anh Vo
2016-05-09  2:24 ` rieachus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox