From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 23 Nov 91 00:03:53 GMT 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 Message-ID: <1991Nov23.000353.1643@milton.u.washington.edu> List-Id: 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) -------------------------------------------------------------------------------