-
Notifications
You must be signed in to change notification settings - Fork 0
/
1
64 lines (58 loc) · 994 Bytes
/
1
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
//prime
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
int prime(int fd);
int
main(int argc, char const *argv[])
{
int pfd[2];
pipe(pfd);
int pid;
pid = fork();
if(pid==0)
{
close(pfd[1]);
prime(pfd[0]);
exit(0);
}
else
{
close(pfd[0]);
int i;
for(i=2; i<36; i++)
{
write(pfd[1], &i, sizeof(i));
}
close(pfd[1]);
wait(0);
exit(0);
}
}
int prime(int fd){
int base;
if(read(fd, &base, sizeof(base))==0){
exit(0);
}
printf("prime %d/n", base);
int p[2];
pipe(p);
if(fork()==0){
close(p[1]);
prime(p[0]);
}
else{
close(p[0]);
int n;
int eof;
do{
eof = read(fd, &n, sizeof(int));
if(n%base!=0){
write(p[1], &n, sizeof(int));
}
}while(eof);
close(p[1]);
}
wait(0);
exit(0);
}