-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathejercicio_procesos_collatz.c
105 lines (79 loc) · 2.08 KB
/
ejercicio_procesos_collatz.c
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
int max_size = 2048;
void collatz(int numero, char **arreglo)
{
int i = 0;
char *resultado = (*arreglo);
sprintf(resultado, "%d ", numero);
do
{
if (numero % 2 == 0)
{
//*Es par
numero /= 2;
}
else
{
//* Es Impar
numero = 3 * numero + 1;
}
//*Lo paso a char y lo concateno
char str[11];
sprintf(str, "%d ", numero);
strcat(resultado, str);
} while (numero != 1);
}
int main(int argc, char **args)
{
int pipe_salida[2];
pid_t pid;
int numero_entrada;
if (argc > 2 || argc == 1)
{
printf("Por favor ingresar un parametro\n");
}
else
{
//*Convierte un string a entero
numero_entrada = atoi(*(args + 1));
//! atoi devuelve 0 si no pudo realizar la conversion.
if (numero_entrada <= 0)
{
printf("Por favor ingrese como parametro un numero entero positivo.\n");
}
else
{
pipe(pipe_salida);
pid = fork();
if ((int)pid != 0)
{
//Estoy en el proceso padre
wait(NULL);
char imprimir[max_size];
close(pipe_salida[1]);
//*ACA SE RECIBE RESULTADO DESDE PROCESO HIJO
read(pipe_salida[0], imprimir, max_size);
printf("%s\n", imprimir);
close(pipe_salida[0]);
}
else
{
//proceso hijo
char *resultado = malloc(max_size * sizeof(char));
char numero[max_size];
collatz(numero_entrada, &resultado);
close(pipe_salida[0]);
//*PASO RESULTADO AL PADRE
write(pipe_salida[1], resultado, max_size);
close(pipe_salida[1]);
free(resultado);
}
}
}
return 0;
}