BUT2/SCR/TP05/prime.c
2023-10-23 13:07:05 +02:00

77 lines
1.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.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[])
{
pid_t p;
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);
p = fork();
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;
}