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
| #include "kernel/types.h" #include "user/user.h"
#define N 35 #define READEND 0 #define WRITEEND 1
void child(int *p1);
int main(int argc, char *argv[]) { int p[2]; pipe(p); if(fork() == 0) { child(p); } else { close(p[READEND]); for (int i = 2; i <= N; i++) { write(p[WRITEEND], &i, sizeof(int)); } close(p[WRITEEND]); wait((int*)0); }
exit(0); }
void child(int *p1) { int pr[2]; int n; close(p1[WRITEEND]); int read_result = read(p1[READEND], &n, sizeof(int)); if(read_result == 0) { exit(0); } pipe(pr); if (fork() == 0) { child(pr); } else { close(pr[READEND]); printf("prime %d\n", n); int prime = n; while(read(p1[READEND], &n, sizeof(int)) != 0) { if(n % prime != 0) { write(pr[WRITEEND], &n, sizeof(int)); } } close(pr[WRITEEND]); wait((int*)0); exit(0); } }
|