Hi guys!
I'll simplify my problem. I have the following code:
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#define max 25
#define buffdim 50
void p1();
void p2();
void wait_child();
void done(int sinal);
int main(int argc, char** argv)
{
int pid[1], i,j;
for (i = 0; i < 2; i++)
{
pid = fork();
if(pid==0)
{
if(i==0)
{
printf("I'm %d and i=0\n", pid);
p1();
}
else if (i==1)
{
printf("I'm %d and i=1\n", pid);
p2(argv[1]);
}
}
else
{
printf("P0 : Created child %d\n", i + 1,pid);
}
}
for (j=0; j<2; j++)
{
wait_child();
}
return 0;
}
void p1()
{
signal(SIGUSR1, done);
}
void p2(char* file_in)
{
int fd;
int a=8, c=10;
printf("%d e %d.\n", a,c);
exit(0);
}
void wait_child()
{
int death, status;
death=wait(&status);
printf("Child %d dead. Status %d.\n", death, status);
}
void done(int sinal)
{
printf("Done\n");
}
In my head I created a for cicle that supposed to fork 2 sub processes. But, intstead of it it forks 3!!
I putted all these printf's in the middle of the for cicle to understand what he was doing, but even with the print'fs I wasn't able to do so.
what I get running the program is:
piero@ubuntu:~$ ./correggi test_input a
P0: Created child 1
I'm 0 and i=0
P0: Created child 2
P0: Created child 2
I'm 0 and i=1
8 e 10.
Child 2457 dead. Status 0.
I'm 0 and i=1
8 e 10.
Child 2458 dead. Status 0.
Child -1 dead. Status 0.
Child 2456 dead. Status 0.
piero@ubuntu:~$
Could anyone explain me what is going on ? why does he forks a 3rd process and a -1 process ??? Is the sub process getting inside the for cicle and forking someone else ?