91 lines
1.6 KiB
C
91 lines
1.6 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
#include <signal.h>
|
||
|
#include <assert.h>
|
||
|
|
||
|
void filter(int p)
|
||
|
{
|
||
|
while (1)
|
||
|
{
|
||
|
int n;
|
||
|
ssize_t nb_read = read(0, &n, sizeof(int));
|
||
|
|
||
|
if (nb_read <= 0)
|
||
|
break;
|
||
|
|
||
|
if ((n % p) != 0)
|
||
|
write(1, &n, sizeof(int));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void seq(int n)
|
||
|
{
|
||
|
for (int i = 2; i <= n; i++)
|
||
|
write(1, &i, sizeof(int));
|
||
|
}
|
||
|
|
||
|
void chef(void)
|
||
|
{
|
||
|
while (1)
|
||
|
{
|
||
|
int p;
|
||
|
pid_t f;
|
||
|
int t[2];
|
||
|
ssize_t nb_read = read(0, &p, sizeof(int));
|
||
|
|
||
|
if (nb_read <= 0)
|
||
|
break;
|
||
|
|
||
|
printf("new prime %d\n", p);
|
||
|
pipe(t);
|
||
|
f = fork();
|
||
|
|
||
|
if (f == 0)
|
||
|
{
|
||
|
close(t[0]);
|
||
|
dup2(t[1], 1);
|
||
|
close(t[1]);
|
||
|
filter(p);
|
||
|
exit(0);
|
||
|
}
|
||
|
|
||
|
close(t[1]);
|
||
|
dup2(t[0], 0);
|
||
|
close(t[0]);
|
||
|
}
|
||
|
}
|
||
|
int main(int argc, char *argv[])
|
||
|
{
|
||
|
if (argc < 2)
|
||
|
{
|
||
|
printf("Usage: %s <N>\n", argv[0]);
|
||
|
return EXIT_FAILURE;
|
||
|
}
|
||
|
|
||
|
struct sigaction sa = {0};
|
||
|
sigemptyset(&sa.sa_mask);
|
||
|
sa.sa_flags = SA_NOCLDWAIT;
|
||
|
sigaction(SIGCHLD, &sa, NULL);
|
||
|
|
||
|
int t[2];
|
||
|
int N = strtol(argv[1], NULL, 0);
|
||
|
pipe(t);
|
||
|
pid_t p = fork();
|
||
|
assert(p != -1);
|
||
|
|
||
|
if (p == 0)
|
||
|
{
|
||
|
close(t[0]);
|
||
|
dup2(t[1], 1);
|
||
|
close(t[1]);
|
||
|
seq(N);
|
||
|
exit(0);
|
||
|
}
|
||
|
|
||
|
close(t[1]);
|
||
|
dup2(t[0], 0);
|
||
|
close(t[0]);
|
||
|
chef();
|
||
|
return 0;
|
||
|
}
|