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
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