0

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>

int main()
{
    int pid;
    pid = fork();
    if (pid) {
	printf("I'm the parent %d\n", getpid());
    } else {
        printf("I'm the child %d parent %d\n", getpid(), getppid());
	pid = fork();
	if (pid) {

        } else {
		printf("I'm the subchild %d parent %d\n", getpid(), getppid());
        }
    }
    return 0;
}

The above creates a child process and another process but for some reason it does not get the PPID from the child it's been created from. I wonder why is this.

The current output looks like:

I'm the parent 6213
I'm the child 6214 parent 6213
I'm the subchild 6215 parent 1

The subchild's parent should be 6214. Some help would be greatly appreciated

Edited by MrDiaz: n/a

4
Contributors
7
Replies
9
Views
7 Years
Discussion Span
Last Post by MrDiaz
0

Notice parent 1 . Process with pid 1, aka init, besides other things, adopts every orphan. This means that by the time 6215 prints its message, 6214 is already dead.

Edited by nezachem: n/a

0

Yes I noticed that from the output, and so would any process derived from there as well. How can I fix this?

0

You have to make the parent wait for the child to die before killing itself. A simple solution will be to use wait(NULL) in the parent

Now the parent waits for the child to die before terminating

int main()
{
    int pid;
    pid = fork();
    if (pid)
    {
                printf("I'm the parent %d\n", getpid());
                wait(NULL);
                printf("The child has died\n");
    }
    else
    {
                printf("I am the child\n");
    }
    return 0;
}
0

You have to make the parent wait for the child to die before killing itself. A simple solution will be to use wait(NULL) in the parent

Now the parent waits for the child to die before terminating

int main()
{
    int pid;
    pid = fork();
    if (pid)
    {
                printf("I'm the parent %d\n", getpid());
                wait(NULL);
                printf("The child has died\n");
    }
    else
    {
                printf("I am the child\n");
    }
    return 0;
}

Yeah! i was to reply the same...

Very good answer....

0

Yeah that's interesting, I also did it some other way which basically does what I want but only creates one child and a subchild. I need a second child and a subchild for this second child.

#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;
}

I'm trying to start a new process but I'm stuck there, it does the whole thing twice and output isn't right

0

When you fork for the second time, the parent is not waiting. Also only the parent should wait (See my example above)

0

Just finished the code, got it working the way I wanted to. Take a look and now maybe it will make sense what I was trying to do

#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;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.