Blob


1 #include "u.h"
2 #include "libc.h"
3 #include "thread.h"
5 enum
6 {
7 STACK = 8192
8 };
10 int max = 10000;
11 int (*mk)(void (*fn)(void*), void *arg, uint stack);
13 void
14 countthread(void *v)
15 {
16 uint i;
17 Channel *c;
19 c = v;
20 for(i=2;; i++){
21 sendul(c, i);
22 }
23 }
25 void
26 filterthread(void *v)
27 {
28 uint i, p;
29 Channel *c, *nextc;
31 c = v;
32 p = recvul(c);
33 print("%d\n", p);
34 if(p > max)
35 threadexitsall(0);
36 nextc = chancreate(sizeof(ulong), 0);
37 mk(filterthread, nextc, STACK);
38 for(;;){
39 i = recvul(c);
40 if(i%p)
41 sendul(nextc, i);
42 }
43 }
45 void
46 usage(void)
47 {
48 fprint(2, "usage: tprimes [-p] [max]\n");
49 threadexitsall("usage");
50 }
52 void
53 threadmain(int argc, char **argv)
54 {
55 Channel *c;
56 int nbuf;
58 nbuf = 0;
59 mk = threadcreate;
60 ARGBEGIN{
61 default:
62 usage();
63 case 'b':
64 nbuf = atoi(EARGF(usage()));
65 break;
66 case 'p':
67 mk = proccreate;
68 max = 1000;
69 break;
70 }ARGEND
72 if(argc == 1)
73 max = atoi(argv[0]);
74 else if(argc)
75 usage();
77 c = chancreate(sizeof(ulong), nbuf);
78 mk(countthread, c, STACK);
79 mk(filterthread, c, STACK);
80 recvp(chancreate(sizeof(void*), 0));
81 }