Duda fork()
Publicado por Miguel Ángel (5 intervenciones) el 10/05/2019 16:21:39
Hola a todos,
tengo que hacer una práctica y me ha surgido una duda. A ver si me podeis ayudar.
El caso es que estoy con las llamadas al sistema fork(), exec(), etc.
Intento que un archivo ejecutable me imprima "I'm the son" o "I'm the father" dependiendo del pid_t que tengan (id del proceso). El caso es que se me imprime 2 veces "I'm the son".... luego, a no ser que haya algún error en el archvio....
se me crean 2 procesos hijo y el padre no me lo imprime?
Aquí os dejo el archivo ejecutable:
tengo que hacer una práctica y me ha surgido una duda. A ver si me podeis ayudar.
El caso es que estoy con las llamadas al sistema fork(), exec(), etc.
Intento que un archivo ejecutable me imprima "I'm the son" o "I'm the father" dependiendo del pid_t que tengan (id del proceso). El caso es que se me imprime 2 veces "I'm the son".... luego, a no ser que haya algún error en el archvio....
se me crean 2 procesos hijo y el padre no me lo imprime?
Aquí os dejo el archivo ejecutable:
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
#include "rutines.h"
int main(int argc, char *argv[])
{
char cmd[256];
int a;
pid_t x = fork();
if (x=!-1){
perror("Error Fork\n");
} else {
if(x==0){
printf("I'm the son\n");
}
if (x>0){
wait(0);
printf("I'm the father\n");
}
}
if (argc<2)
Error("Insufficient arguments: exec_in <sec> <cmd> [args...]");
sleep(atoi(argv[1]));
cmd[0]='\0';
for(a=2;a<argc;a++)
{
strcat(cmd,argv[a]);
strcat(cmd," ");
}
system(cmd);
exit(0);
}
Valora esta pregunta


0