comp.lang.ada
 help / color / mirror / Atom feed
* A common array
@ 2003-12-17 22:08 Ratson Janiv
  2003-12-18  2:25 ` Stephen Leake
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Ratson Janiv @ 2003-12-17 22:08 UTC (permalink / raw)


Hi,
I need to write a program that creates a random number of tasks.
I need the tasks to update a common array (1 array for all the tasks).
do I need keep the array updating in critical section (I know that each task
will update a differnet cell in the array), if so how ?
Also I need to write a mechanism that enables the task to perform the check
twice with two differnet parameters, e.g. I call Task X to  check Numer 4 in
the array, and after it finishes, I want it to notify me, and also I want
teh task X to check the 18 Number.

for the decency, I M studying Ada, and it is part of my work, so please try
to help me as much as U can without writing the answers to me, I think some
guide lines will be helpful.

Many Many thanks,
Janiv Ratson.





^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A common array
  2003-12-17 22:08 A common array Ratson Janiv
@ 2003-12-18  2:25 ` Stephen Leake
  2003-12-18  3:10   ` Wes Groleau
  2003-12-19 15:10 ` Ratson Janiv
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Stephen Leake @ 2003-12-18  2:25 UTC (permalink / raw)
  To: comp.lang.ada

"Ratson Janiv" <janiv@013.net.il> writes:

> I need to write a program that creates a random number of tasks.

Hmm. It usually helps if you tell us what you are _really_ trying to
do; often Ada has a totally different approach than you are used to.

> I need the tasks to update a common array (1 array for all the
> tasks). do I need keep the array updating in critical section (I
> know that each task will update a differnet cell in the array), if
> so how ? 

Put that common array in yet another task, or in a protected object.

> Also I need to write a mechanism that enables the task to perform
> the check twice with two differnet parameters, e.g. I call Task X to
> check Numer 4 in the array, and after it finishes, I want it to
> notify me, and also I want teh task X to check the 18 Number.

You'll need a loop in the tasks; at the top, they wait on a rendezvous
to get the number to check. Then do the check, then loop back.

You have to make sure all the tasks can terminate nicely; sometimes
that requires a Quit rendezvous.

> for the decency, I M studying Ada, and it is part of my work, so please try
> to help me as much as U can without writing the answers to me, I think some
> guide lines will be helpful.

Hope this helps.

-- 
-- Stephe




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A common array
  2003-12-18  2:25 ` Stephen Leake
@ 2003-12-18  3:10   ` Wes Groleau
  2003-12-19 15:13     ` Mr. J.
  0 siblings, 1 reply; 9+ messages in thread
From: Wes Groleau @ 2003-12-18  3:10 UTC (permalink / raw)


Stephen Leake wrote:
>>I need the tasks to update a common array (1 array for all the
>>tasks). do I need keep the array updating in critical section (I
>>know that each task will update a differnet cell in the array), if
>>so how ? 
> 
> Put that common array in yet another task, or in a protected object.

Or, make each 'cell' a protected object.  But, since each
cell has it's own task updating it, maybe the protection
is not needed?  (As Stephen said, you haven't given enough
information.)

-- 
Wes Groleau
-----------
I've been framed! ...
http://www.useit.com/alertbox/9612.html




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A common array
  2003-12-17 22:08 A common array Ratson Janiv
  2003-12-18  2:25 ` Stephen Leake
@ 2003-12-19 15:10 ` Ratson Janiv
  2003-12-20  9:24 ` Ratson Janiv
  2003-12-20 18:56 ` @Fill(NOSPAM)YourBrain.com
  3 siblings, 0 replies; 9+ messages in thread
From: Ratson Janiv @ 2003-12-19 15:10 UTC (permalink / raw)


Thanks.
Well, U ask me to be more specific.
I'll do that.

I need to write a procedure that checks and prints  the prime numbers
between 1 and m.
The procedure gets 2 parameter, m- the numbers to check 1..m and n - the
number of tasks to perform it.

each tak will check if a number is a prime number, and will update an array
of booleans (size 1..m).
At the end of all tasks the procedure prints the prime numbers.

A managment task should manage all the tasks allocations ...

Regards,
Janiv Ratson.




"Ratson Janiv" <janiv@013.net.il> wrote in message
news:3fe0d45e$1@news.barak.net.il...
> Hi,
> I need to write a program that creates a random number of tasks.
> I need the tasks to update a common array (1 array for all the tasks).
> do I need keep the array updating in critical section (I know that each
task
> will update a differnet cell in the array), if so how ?
> Also I need to write a mechanism that enables the task to perform the
check
> twice with two differnet parameters, e.g. I call Task X to  check Numer 4
in
> the array, and after it finishes, I want it to notify me, and also I want
> teh task X to check the 18 Number.
>
> for the decency, I M studying Ada, and it is part of my work, so please
try
> to help me as much as U can without writing the answers to me, I think
some
> guide lines will be helpful.
>
> Many Many thanks,
> Janiv Ratson.
>
>





^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A common array
  2003-12-18  3:10   ` Wes Groleau
@ 2003-12-19 15:13     ` Mr. J.
  2003-12-20  3:23       ` Wes Groleau
  0 siblings, 1 reply; 9+ messages in thread
From: Mr. J. @ 2003-12-19 15:13 UTC (permalink / raw)


Thanks.
Well, U ask me to be more specific.
I'll do that.

I need to write a procedure that checks and prints  the prime numbers
between 1 and m.
The procedure gets 2 parameter, m- the numbers to check 1..m and n -
the number of tasks to perform it.

each tak will check if a number is a prime number, and will update an
array of booleans (size 1..m).
At the end of all tasks the procedure prints the prime numbers.

A managment task should manage all the tasks allocations ...

Regards,
Janiv Ratson.




Wes Groleau <groleau@freeshell.org> wrote in message news:<RvidnZMSjN8Hh3yiRVn-sQ@gbronline.com>...
> Stephen Leake wrote:
> >>I need the tasks to update a common array (1 array for all the
> >>tasks). do I need keep the array updating in critical section (I
> >>know that each task will update a differnet cell in the array), if
> >>so how ? 
> > 
> > Put that common array in yet another task, or in a protected object.
> 
> Or, make each 'cell' a protected object.  But, since each
> cell has it's own task updating it, maybe the protection
> is not needed?  (As Stephen said, you haven't given enough
> information.)



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A common array
  2003-12-19 15:13     ` Mr. J.
@ 2003-12-20  3:23       ` Wes Groleau
  0 siblings, 0 replies; 9+ messages in thread
From: Wes Groleau @ 2003-12-20  3:23 UTC (permalink / raw)


Mr. J. wrote:
> I need to write a procedure that checks and prints  the prime numbers
> between 1 and m.
> The procedure gets 2 parameter, m- the numbers to check 1..m and n -
> the number of tasks to perform it.
> 
> each tak will check if a number is a prime number, and will update an
> array of booleans (size 1..m).
> At the end of all tasks the procedure prints the prime numbers.

Still not quite clear.  Does each task update one cell,
and no other task updates that cell?  IF that is the case,
then the cells do not need synchronization.

But IF one task implements a divisor, for example, setting
false every third cell, while another (for example) sets false
every fifth cell, then they could theoretically collide on
the fifteenth cell.  You could say, "it's so unlikely for
that to happen at the same time that I don't need to protect
the cells."  Or you could play it safe and make each cell a
protected object.  (Not the whole array--why lock all tasks
out of cell X just because somebody is writing to cell Y?)

However, IF each task is only writing False into Boolean
cells initialized to True, then a collision can't hurt anything.
It ends up False no matter who gets there first.

-- 
Wes Groleau
When all you have is a perl, everything looks like a string.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A common array
  2003-12-17 22:08 A common array Ratson Janiv
  2003-12-18  2:25 ` Stephen Leake
  2003-12-19 15:10 ` Ratson Janiv
@ 2003-12-20  9:24 ` Ratson Janiv
  2003-12-20 18:06   ` Robert I. Eachus
  2003-12-20 18:56 ` @Fill(NOSPAM)YourBrain.com
  3 siblings, 1 reply; 9+ messages in thread
From: Ratson Janiv @ 2003-12-20  9:24 UTC (permalink / raw)


Thanks I understand that, in my case a cell in the arrray will be updated
only once.
The other unsolved problem is that I dont know how to reallocate a number to
a task.
If I check numbers between 1..100, and I only have 8 tasks, The manager task
willhave to reuse teh tasks to keep checking the other numbers. hence, a
manager should reallocate a new number to a task that has finished checking
the previous number.
I though about a notification mechanism, hence, each check task will notify
the manager task that it finished checking, and the manager task will
reallocate a new number to that task. My problem is that I do not know how
the Manager Task will know which task has notified it.
Any ideas how to solve this problem ? M I in the right though about
notification, or there is a better way ?

10x,
Janiv.




"Ratson Janiv" <janiv@013.net.il> wrote in message
news:3fe0d45e$1@news.barak.net.il...
> Hi,
> I need to write a program that creates a random number of tasks.
> I need the tasks to update a common array (1 array for all the tasks).
> do I need keep the array updating in critical section (I know that each
task
> will update a differnet cell in the array), if so how ?
> Also I need to write a mechanism that enables the task to perform the
check
> twice with two differnet parameters, e.g. I call Task X to  check Numer 4
in
> the array, and after it finishes, I want it to notify me, and also I want
> teh task X to check the 18 Number.
>
> for the decency, I M studying Ada, and it is part of my work, so please
try
> to help me as much as U can without writing the answers to me, I think
some
> guide lines will be helpful.
>
> Many Many thanks,
> Janiv Ratson.
>
>





^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A common array
  2003-12-20  9:24 ` Ratson Janiv
@ 2003-12-20 18:06   ` Robert I. Eachus
  0 siblings, 0 replies; 9+ messages in thread
From: Robert I. Eachus @ 2003-12-20 18:06 UTC (permalink / raw)


First, this looks like a homework assignment.  Doesn't mean you won't 
get help here, just that people are less likely to provide code instead 
of understanding.

Ratson Janiv wrote:

> Thanks I understand that, in my case a cell in the arrray will be updated
> only once.

Don't forget to mark the array in some way so that the elements can be 
separately addressed.  Look at pragma Atomic, and pragma Atomic_Elements 
to decide how you want to address this.

> The other unsolved problem is that I dont know how to reallocate a number to
> a task.
> If I check numbers between 1..100, and I only have 8 tasks, The manager task
> willhave to reuse teh tasks to keep checking the other numbers. hence, a
> manager should reallocate a new number to a task that has finished checking
> the previous number.
> I though about a notification mechanism, hence, each check task will notify
> the manager task that it finished checking, and the manager task will
> reallocate a new number to that task. My problem is that I do not know how
> the Manager Task will know which task has notified it.
> Any ideas how to solve this problem ? M I in the right though about
> notification, or there is a better way ?

There are several ways to do this. First you can have a task (or tasks) 
that hands out numbers to be checked during a rendezvous.  Second you 
can have a protected object, or objects that hand out numbers to be 
checked.  In other words the protected object would contain a procedure
that returns a new value to check as an out parameter.  You can have the 
value returned by a function in the protected object, but this is 
tricky.  If you can figure it out, you will see that the actual function 
called need not be protected at all.

Finally you can pass each task a set of numbers to check when it is 
created.  This is a much more efficient solution since it allows you to 
minimize the interactions between tasks.  If I was writing this, this is 
the way I would go.  Further, I would have the task provide results in 
batches--IF that made things faster.  It may not, that will depend on 
the compiler you use, the hardware architecture, and other decisions you 
make in your program design.

-- 
                                           Robert I. Eachus

"The war on terror is a different kind of war, waged capture by capture, 
cell by cell, and victory by victory. Our security is assured by our 
perseverance and by our sure belief in the success of liberty." -- 
George W. Bush




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: A common array
  2003-12-17 22:08 A common array Ratson Janiv
                   ` (2 preceding siblings ...)
  2003-12-20  9:24 ` Ratson Janiv
@ 2003-12-20 18:56 ` @Fill(NOSPAM)YourBrain.com
  3 siblings, 0 replies; 9+ messages in thread
From: @Fill(NOSPAM)YourBrain.com @ 2003-12-20 18:56 UTC (permalink / raw)


Ratson Janiv wrote:
> Hi,
> I need to write a program that creates a random number of tasks.
> I need the tasks to update a common array (1 array for all the tasks).
> do I need keep the array updating in critical section (I know that each task
> will update a differnet cell in the array), if so how ?
> Also I need to write a mechanism that enables the task to perform the check
> twice with two differnet parameters, e.g. I call Task X to  check Numer 4 in
> the array, and after it finishes, I want it to notify me, and also I want
> teh task X to check the 18 Number.
> 
> for the decency, I M studying Ada, and it is part of my work, so please try
> to help me as much as U can without writing the answers to me, I think some
> guide lines will be helpful.
> 
> Many Many thanks,
> Janiv Ratson.
> 
> 
Hi, I'm new at this too.  But I just got through "Ada as a Second 
Language" and I believe your answer is to just make the array of tasks a 
protected type.  Procedures for protected types are critical sections 
for the type.




^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2003-12-20 18:56 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-17 22:08 A common array Ratson Janiv
2003-12-18  2:25 ` Stephen Leake
2003-12-18  3:10   ` Wes Groleau
2003-12-19 15:13     ` Mr. J.
2003-12-20  3:23       ` Wes Groleau
2003-12-19 15:10 ` Ratson Janiv
2003-12-20  9:24 ` Ratson Janiv
2003-12-20 18:06   ` Robert I. Eachus
2003-12-20 18:56 ` @Fill(NOSPAM)YourBrain.com

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