comp.lang.ada
 help / color / mirror / Atom feed
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!usen et.coe.montana.edu!milton!mfeldman@ucbvax.Berkeley.EDU  (Michael Feldman)
Subject: Re: Ada Tasking problem
Date: 23 Nov 91 00:03:53 GMT	[thread overview]
Message-ID: <1991Nov23.000353.1643@milton.u.washington.edu> (raw)

In article <9111211304.AA10751@m11.sews.wpafb.af.mil> maysee@M11.SEWS.WPAFB.AF.
MIL (Eric E. Mays 52202) writes:
>Info-Ada Folks,
>
>   I am currently taking a computer aided Ada course developed by 
>keesler afb.  The last topic in this course involves tasking.  I am
>trying to solve the following problem using tasking with entry point.
>
>   The output i'm trying to display is the following:
>
>           1          5
>           2          4
>           3          3
>           4          2
>           5          1
>
  [stuff deleted]
>   Here is my code, am I missing something?
>
>with TEXT_IO;
>
>procedure MAIN_TW1 is
>
>   package MY_INT_IO is new TEXT_IO.INTEGER_IO (INTEGER);
>
>   LAST_NUMBER : INTEGER;
>
>   task REV_NUM_OUT is
>      entry RECEIVE_NUM (MAX_NUMBER : in INTEGER);
>   end REV_NUM_OUT;
>
>   task body REV_NUM_OUT is
>
>      MAX_NUM : INTEGER;
>
>   begin
>
>      accept RECEIVE_NUM (MAX_NUMBER : in INTEGER) do
>
>         MAX_NUM := MAX_NUMBER;
>
>         for I in reverse 1 .. MAX_NUM loop
>
>             TEXT_IO.SET_COL (35);
>             MY_INT_IO.PUT (I,WIDTH=>1); 
>             text_io.new_line;
>
>         end loop;
>
>      end RECEIVE_NUM;
>
>   end REV_NUM_OUT;
>
>
>begin
>
>   TEXT_IO.PUT ("Enter an integer number: ");
>   MY_INT_IO.GET (LAST_NUMBER);
>   TEXT_IO.NEW_PAGE;
>
>   REV_NUM_OUT.RECEIVE_NUM (LAST_NUMBER);
>
>   for J in 1 .. LAST_NUMBER loop
>
>      TEXT_IO.SET_COL (1);
>      MY_INT_IO.PUT (J);
>
>   end loop;
>
>end MAIN_TW1; 
>
>   Any helpful hints or comments would be appreciated.
>
Aha! This is nearly an FAQ, that could be phrased "how does the Ada
runtime decide when to switch among tasks?"

Nothing in the LRM requires time-slicing, though of course many
compilers implement it (sometimes making it optional via a pragma 
or whatever). It is quite legal to schedule tasks of equal
priority (which yours are - MAIN is treated as a task) in
"run-till-blocked" fashion, meaning that the first task to
get control keeps it until it gives it up voluntarily.

Once your main program called the task's entry, causing the
task to get control, it simply kept it, finished its loop,
then gave the CPU back to MAIN. The standard Ada recommendation
to prevent this from happening is to put a brief DELAY in each loop
iteration, forcing (in your case) MAIN and the task to ping-pong 
control. You need to add, say, DELAY 0.1 just before both END LOOP
statements.

This is a portable solution: it forces the processor
to be shared even in the absence of time-slicing.

Mike

-------------------------------------------------------------------------------
Michael B. Feldman                       Co-chair, SIGAda Education Committee

Visiting Professor 1991-92               Professor
Dept. of Comp. Sci. and Engrg.           Dept. of Elect. Engrg. and Comp. Sci.
University of Washington FR-35           The George Washington University
Seattle, WA 98105                        Washington, DC 20052

mfeldman@cs.washington.edu               mfeldman@seas.gwu.edu
(206) 632-3794 (voice)                   (202) 994-5253 (voice)
(206) 543-2969 (fax)                     (202) 994-5296 (fax)
-------------------------------------------------------------------------------

             reply	other threads:[~1991-11-23  0:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1991-11-23  0:03 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!usen [this message]
  -- strict thread matches above, loose matches on Subject: below --
1991-11-26  9:10 Ada Tasking problem paul goffin
1991-11-25 17:02 agate!spool.mu.edu!wupost!zaphod.mps.ohio-state.edu!ub!dsinc!gvlf3.gvl.un
1991-11-25  4:04 cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uwm.edu!ogicse!milton
1991-11-25  0:04 Bob Kitzberger
1991-11-21 13:04 Eric E. Mays 52202
replies disabled

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