-
Notifications
You must be signed in to change notification settings - Fork 0
/
primes.txt
74 lines (59 loc) · 1.07 KB
/
primes.txt
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
int sieve(int p)
{
int num;
int pipe_read;
int pipe_write;
int pid;
int n;
pipe_read = p;
while(1)
{
if(read(pipe_read,&num,sizeof(num) == 0)
{
exit(0);
}
printf("primes %d\n",num);
int pipe_fds[2];
pipe(pipe_fds);
pipe_write = pipe_fds[1];
pid = fork();
if(pid < 0)
{
printf("fail\n");
exit(-1);
}
else(pid == 0)
{
close(pipe_fds[0]);
while(read(pipe_read,&n,sizeof(n) != 0))
{
if(n % m != 0)
{
write(pipe_write,%n,sizeof(n));
}
}
}
else
{
close(pipe_read);
close(pipe_write);
wait(0);
}
pipe_read = pipe_fds[0];
}
return pipe_read;
}
int main(int argc,char *argv[])
{
int pipe_fds[2];
for(int i = 2;i <= 35; i++)
{
write(pipe_fds[1],&i,sizeof(i));
}
close(pipe_fds[1]);
close(sieve(pipe_fds[0]));
exit(0);
}