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);
12 void printmsg(void*, char*);
14 void
15 countthread(void *v)
16 {
17 uint i;
18 Channel *c;
20 c = v;
21 for(i=2;; i++){
22 sendul(c, i);
23 }
24 }
26 void
27 filterthread(void *v)
28 {
29 uint i, p;
30 Channel *c, *nextc;
32 c = v;
33 p = recvul(c);
34 print("%d\n", p);
35 if(p > max)
36 threadexitsall(0);
37 nextc = chancreate(sizeof(ulong), 0);
38 mk(filterthread, nextc, STACK);
39 for(;;){
40 i = recvul(c);
41 if(i%p)
42 sendul(nextc, i);
43 }
44 }
46 void
47 usage(void)
48 {
49 fprint(2, "usage: tprimes [-p] [max]\n");
50 threadexitsall("usage");
51 }
53 void
54 threadmain(int argc, char **argv)
55 {
56 Channel *c;
57 int nbuf;
59 notify(printmsg);
60 nbuf = 0;
61 mk = threadcreate;
62 ARGBEGIN{
63 default:
64 usage();
65 case 'b':
66 nbuf = atoi(EARGF(usage()));
67 break;
68 case 'p':
69 mk = proccreate;
70 max = 1000;
71 break;
72 }ARGEND
74 if(argc == 1)
75 max = atoi(argv[0]);
76 else if(argc)
77 usage();
79 c = chancreate(sizeof(ulong), nbuf);
80 mk(countthread, c, STACK);
81 mk(filterthread, c, STACK);
82 recvp(chancreate(sizeof(void*), 0));
83 threadexitsall(0);
84 }
86 void
87 printmsg(void *v, char *msg)
88 {
89 print("note: %s\n", msg);
90 }