diff options
Diffstat (limited to 'src/libthread/test/tprimes.c')
-rw-r--r-- | src/libthread/test/tprimes.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/libthread/test/tprimes.c b/src/libthread/test/tprimes.c new file mode 100644 index 00000000..91af5a73 --- /dev/null +++ b/src/libthread/test/tprimes.c @@ -0,0 +1,80 @@ +#include "u.h" +#include "libc.h" +#include "thread.h" + +enum +{ + STACK = 8192 +}; + +int max = 10000; +int (*mk)(void (*fn)(void*), void *arg, uint stack); + +void +countthread(void *v) +{ + uint i; + Channel *c; + + c = v; + for(i=2;; i++){ + sendul(c, i); + } +} + +void +filterthread(void *v) +{ + uint i, p; + Channel *c, *nextc; + + c = v; + p = recvul(c); + print("%d\n", p); + if(p > max) + threadexitsall(0); + nextc = chancreate(sizeof(ulong), 0); + mk(filterthread, nextc, STACK); + for(;;){ + i = recvul(c); + if(i%p) + sendul(nextc, i); + } +} + +void +usage(void) +{ + fprint(2, "usage: tprimes [-p] [max]\n"); + threadexitsall("usage"); +} + +void +threadmain(int argc, char **argv) +{ + Channel *c; + int nbuf; + + nbuf = 0; + mk = threadcreate; + ARGBEGIN{ + default: + usage(); + case 'b': + nbuf = atoi(EARGF(usage())); + break; + case 'p': + mk = proccreate; + max = 1000; + break; + }ARGEND + + if(argc == 1) + max = atoi(argv[0]); + else if(argc) + usage(); + + c = chancreate(sizeof(ulong), nbuf); + mk(countthread, c, STACK); + mk(filterthread, c, STACK); +} |