comp.lang.ada
 help / color / mirror / Atom feed
From: mcsun!uknet!stl!crosfield!pdg@uunet.uu.net  (paul goffin)
Subject: Re: Ada Tasking problem
Date: 26 Nov 91 09:10:43 GMT	[thread overview]
Message-ID: <11973@suns3.crosfield.co.uk> (raw)

In article <1991Nov23.000353.1643@milton.u.washington.edu>, mfeldman@milton.u.w
ashington.edu (Michael Feldman) writes:
> In article <9111211304.AA10751@m11.sews.wpafb.af.mil> maysee@M11.SEWS.WPAFB.A
F.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
> >
[code deleted]

Mike> Aha! This is nearly an FAQ, that could be phrased "how does the Ada
Mike> runtime decide when to switch among tasks?"
Mike> 
Mike> Nothing in the LRM requires time-slicing, though of course many
Mike> compilers implement it (sometimes making it optional via a pragma 
Mike> or whatever). It is quite legal to schedule tasks of equal
Mike> priority (which yours are - MAIN is treated as a task) in
Mike> "run-till-blocked" fashion, meaning that the first task to
Mike> get control keeps it until it gives it up voluntarily.
Mike> 
Mike> Once your main program called the task's entry, causing the
Mike> task to get control, it simply kept it, finished its loop,
Mike> then gave the CPU back to MAIN. The standard Ada recommendation
Mike> to prevent this from happening is to put a brief DELAY in each loop
Mike> iteration, forcing (in your case) MAIN and the task to ping-pong 
Mike> control. You need to add, say, DELAY 0.1 just before both END LOOP
Mike> statements.
Mike> 
Mike> This is a portable solution: it forces the processor
Mike> to be shared even in the absence of time-slicing.

Yes, I agree, but perhaps the delay should be

	delay DURATION'SMALL;  -- smallest time slice possible

or 

	delay 0.0;             -- (I think) forces a scheduling event


Actualy, the origional question reveals a possible misunderstanding
of multi-task systems in general.  Even if the Ada run time system
does support time-slicing, there is still no guarantee of the origional
desired output.  The time slices may be long enough for each task to
run to conclusion.  (In fact, with the small tasks in the question, it
would be a very inefficient processor/compiler combination indeed which
would not complete each task in, say, 10ms.)

The only way to guarantee the required output is for the tasks to
rendezvous and 'agree' the output.  - Preferably with a third task
which manages the output only.

i.e. Task1 - counts up 
     Task2 - counts down
     Task3 - receives data from 1 and 2 and prints it in the required
             format.

Task3 acts as a server (or spooler).
The various rendezvous required would force the context switches, and
so the DELAY's would no longer be necessary.

The use of server (or spooler or 'resource manager') tasks is quite
important in avoiding both deadlock and garbeled output when
multi-tasking.

Of course, you could just forget multi-tasking and use a single thread.

Paul.

-- 
+-------------+------------------------------------------------------+
+ Paul Goffin +  Crosfield Electronics Ltd.                          +
+             +  U.K.   +44 442 230000    -  My opinions are my OWN. +
+-------------+------------------------------------------------------+

             reply	other threads:[~1991-11-26  9:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1991-11-26  9:10 paul goffin [this message]
  -- strict thread matches above, loose matches on Subject: below --
1991-11-25 17:02 Ada Tasking problem 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-23  0:03 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!usen
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