PDA

View Full Version : Help with fork()



MrDiaz
May 26th, 2010, 02:15 PM
Hi everyone,

I'm trying to accomplish the following; I need to create 2 subprocesses and each subprocess should create a process of their own.

Should look like this:

I'm child xxxx parent xxxx
I'm the subchild xxxx parent xxxx
I'm the second child xxxx parent xxxx
I'm the second subchild xxxx parent xxxx

So far I have this:


#include <stdio.h>
#include <unistd.h>

int main(void) {
int pid;
pid = fork();
if (pid == 0) { // Child process
printf("I'm the child %d parent %d\n", getpid(), getppid());
pid = fork();
if (pid == 0) { // Subchild process
printf("I'm the subchild %d parent %d\n", getpid(), getppid());
}
}
wait();
return 0;
}

So far I've only been able to create one child from a process, I need to create two childs and two subchilds. The logic is below, hope it makes sense

PARENT PROCESS
CHILD PROCESS CHILD PROCESS
SUBCHILD OF 1ST CHILD SUBCHILD OF SECOND CHILD

gmargo
May 26th, 2010, 04:41 PM
So far I've only been able to create one child from a process, I need to create two childs and two subchilds.

I don't see what your question is. You created one child and one grandchild. Now do it again.

johnl
May 26th, 2010, 06:21 PM
Untested. The break is probably important (see if you can figure out why :) )


#include <stdio.h>
#include <unistd.h>

int main(void) {
int pid, i;
pid = fork();

if (pid == 0) { // Child process
printf("I'm the child %d parent %d\n", getpid(), getppid());
for(i = 0; i < 2; ++i) {
pid = fork();
if (pid == 0) { // Subchild process
printf("I'm the subchild %d parent %d\n", getpid(), getppid());
break;
}
}
}
wait();
return 0;
}

MrDiaz
May 29th, 2010, 05:52 PM
That's an interesting way of doing it. Anyway I did it like this



#include <stdio.h>

int main(void) {

int pID;

pID = fork();
if (pID == 0) { // First Child Process
printf("I am the child %d parent %d\n", getpid(), getppid());

pID = fork();
if (pID == 0)
printf("I am the subchild %d parent %d\n", getpid(), getppid());
}
else {
pID = fork();
if (pID == 0) { // Second Child Process
printf("I am the second child %d parent %d\n", getpid(), getppid());
pID = fork();
if(pID == 0)
printf("I am the second subchild %d parent %d\n", getpid(), getppid());
}
}
return 0;
}