Skip to content

Instantly share code, notes, and snippets.

@Jviejo
Created December 27, 2017 21:22
Show Gist options
  • Save Jviejo/2444d3748bdaa319908720a45d0fcc86 to your computer and use it in GitHub Desktop.
Save Jviejo/2444d3748bdaa319908720a45d0fcc86 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <wait.h>
#include <signal.h>
int child1;
int child2;
void cerrar (int pipe1[2], int pipe2[2])
{
close(pipe1[0]);
close(pipe1[1]);
close(pipe2[0]);
close(pipe2[1]);
}
// manejador de la señal SIGOUT
// que envía a los hijos SIGTERM
void manejador(int signo) {
kill(child1, SIGTERM);
kill(child2, SIGTERM);
exit(0);
}
int main()
{
if (signal(SIGQUIT, manejador) == SIG_ERR)
perror("error al establecer el manejador");
int pipe1[2];
int pipe2[2];
if (pipe(pipe2) == -1) {
perror("bad pipe2");
exit(1);
}
if (pipe(pipe1) == -1) {
perror("bad pipe1");
exit(1);
}
if ((child1 = fork()) == 0) {
// este proceso lee del pipe, por eso duplico el 0
dup2(pipe1[0], 0);
// va a escribir el en el pipe 1
dup2(pipe2[1], 1);
// cierro todos los pipes
cerrar(pipe1, pipe2);
execl("./div2.o", "div2.o", NULL);
}
if ((child2 = fork()) == 0) {
// este proceso lee del pipe2
// el proceso hijo 1 escribe en el pipe
dup2(pipe2[0], 0);
cerrar(pipe1, pipe2);
execl("./div3.o", "div3.o", NULL);
}
if (child1 != 0 && child2 != 0) {
// el padre escriben en el pipe1 que lee el hijo 1
dup2(pipe1[1], 1);
cerrar(pipe1, pipe2);
// leemos de la entrada standard
int n;
while (scanf("%d", &n) != EOF) {
printf("%d\n", n);
}
fflush(stdout);
// cierro el pipe de escritura
close(1);
// esparamos
int status;
wait(&status);
wait(&status);
exit(0);
}
}
#include <stdio.h>
int main()
{
int n;
while (scanf("%d", &n) != EOF) {
if (n % 3 != 0)
printf("%d\n", n);
}
}
#include <stdio.h>
int main()
{
int n;
while (scanf("%d", &n) != EOF) {
if (n % 2 != 0)
printf("%d\n", n);
}
}
fichero n1.txt
1
2
3
4
5
6
7
8
9
10
gcc -o div2.o div2.c
gcc -o div3.o div3.c
gcc -o padre.o padre.c && ./padre.o < n1.txt >numbers.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment