-
Notifications
You must be signed in to change notification settings - Fork 0
/
ejercicioTarea7.cpp
44 lines (34 loc) · 910 Bytes
/
ejercicioTarea7.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Uso: %s <num_children>\n", argv[0]);
exit(1);
}
int num_hijos = atoi(argv[1]);
if (num_hijos <= 0) {
fprintf(stderr, "Error: Numero de hijos invalidos\n");
exit(1);
}
printf("Proceso padre: PID=%d, PPID=%d\n", getpid(), getppid());
// Declarar una variable para contar los hijos creados
int cont = 1;
while (cont <= num_hijos) {
pid_t pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed\n");
exit(1);
} else if (pid == 0) {
printf("Hijos #%d: PID=%d, PPID=%d\n", cont, getpid(), getppid());
exit(0);
} else {
wait(NULL);
}
// Incrementar la variable en cada iteración
cont++;
}
return 0;
}