[Shootout-list] process creation & message passing chain

skaller skaller@users.sourceforge.net
06 Nov 2004 15:22:47 +1100


FWIW, chainmsg as outlined here:
http://lists.alioth.debian.org/pipermail/shootout-list/2004-November/000813.html

[skaller@pelican] /mnt/user2/work/flx>time flx thr 2000 200
2000 threads
200 messages
400000
 
real    0m0.541s
user    0m0.380s
sys     0m0.040s

[Segfaults on large values]
-------------------------------------------
include "std";

thread_max := atoi(System::argv 1);
message_max := atoi(System::argv 2);

var thread_count = thread_max; 
var message_count = message_max;

print thread_count; print " threads"; endl;
print message_count; print " messages"; endl;

proc final() {
  var msg:int;
  var total = 0;
  whilst true do
    read msg;
    total += msg;

    if message_count == 0 do
      print total; endl;
    done;
  done;
}

proc agent() {
  us := thread_count;
  --thread_count;
  var next = 
    if us > 1 then start the agent ()
    else start the final () endif
  ;
  var msg:int;
  whilst true do
    read msg;
    send[int] (&next) (msg+1);
  done;
}

var first = start the agent ();

whilst message_count >0 do
  --message_count;
  send[int] (&first) 0;
done;



-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net