-
-
Save CapitanLiteral/6821691 to your computer and use it in GitHub Desktop.
Ejercicio practico en el que un proceso crea dos procesos hijo que hacen el factorial, uno de los pares de 1 a 25 y el otro de los impares de 1 a 25.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
#include <stdlib.h> | |
double factorial(int); | |
int main(void) | |
{ | |
pid_t pid; | |
int i; | |
pid = fork(); | |
switch(pid) | |
{ | |
case -1: /* error del fork() */ | |
perror("fork 1\n"); | |
break; | |
case 0: /* proceso hijo */ | |
//printf("proces fill 1\n"); | |
for (i = 1; i <= 25; i++) | |
{ | |
if (i % 2 == 0) | |
{ | |
printf("El factorial %d es: %.0f", i, factorial(i)); | |
printf("\n"); | |
//sleep(5); | |
} | |
} | |
break; | |
default: /* padre */ | |
pid = fork(); | |
switch(pid) | |
{ | |
case -1: /* error del fork() */ | |
perror("fork 2\n"); | |
break; | |
case 0: /* proceso hijo */ | |
//printf("proces fill 2\n"); | |
for (i = 1; i <= 25 ; i++) | |
{ | |
if (i % 2 != 0) | |
{ | |
printf("El factorial %d es: %.0f", i, factorial(i)); | |
printf("\n"); | |
//sleep(5); | |
} | |
} | |
break; | |
default: /* padre */ | |
printf("\n"); | |
} | |
} | |
sleep(5); | |
exit(0); | |
} | |
double factorial(int n) | |
{ | |
int i; | |
double res; | |
//printf("hola\n"); | |
res = 1; | |
for (i = n; i > 0 ; i--) | |
{ | |
res = i*res; | |
} | |
return (res); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment