-
Notifications
You must be signed in to change notification settings - Fork 11
/
primes.c
55 lines (47 loc) · 797 Bytes
/
primes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Copyright (c) 2005 Russ Cox, MIT; see COPYRIGHT */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <task.h>
int quiet;
int goal;
int buffer;
void
primetask(void *arg)
{
Channel *c, *nc;
int p, i;
c = arg;
p = chanrecvul(c);
if(p > goal)
taskexitall(0);
if(!quiet)
printf("%d\n", p);
nc = chancreate(sizeof(unsigned long), buffer);
taskcreate(primetask, nc, 32768);
for(;;){
i = chanrecvul(c);
if(i%p)
chansendul(nc, i);
}
}
void
taskmain(int argc, char **argv)
{
int i;
Channel *c;
if(argc>1)
goal = atoi(argv[1]);
else
goal = 100;
printf("goal=%d\n", goal);
c = chancreate(sizeof(unsigned long), buffer);
taskcreate(primetask, c, 32768);
for(i=2;; i++)
chansendul(c, i);
}
void*
emalloc(unsigned long n)
{
return calloc(n ,1);
}