Fork & Children Processes

The fork() function is a little fuzzy to me. Particularly when you have if/else statements like the one below:

if(fork())
printf("I'm the parent with pid: %d\n", getpid());
else
printf("I'm the daughter with pid: %d\n", getpid());

How do both statements get executed exactly? Also, the main question I want to ask is is there any way to be able to specifically control a certain child process.

For example I created two children from a single parent. I want the children to communicate through Shared Memory, Pipes, Sockets, etc. One child has to send a particular message to the other and vice versa. The messages are distinct and the necessary pid of the "sender" has to be included in the message, which is where I get stuck. Is there any way of doing this? Any help is greatly appreciated.
Last edited on
fork() creates a child process that is a copy of the parent process and starts running at the return fork() (i.e. the same place as the parent).

In the parent process, fork() returns the process identifier (pid) of the child process.

In the child process, fork() returns zero.

If an error occurred, fork() returns -1 and presumably doesn't create the child.

So, back to your code, it could be:
1
2
3
4
5
6
7
8
9
10
11
12
13
int pid = fork();
if (pid > 0)
{
    printf("I'm the parent with pid: %d, and created child with %d\n", getpid(), pid);
}
else if (pid == 0)
{
    printf("I'm the daughter with pid: %d and parent is %d\n", getpid(), getppid());
}
else // pid == -1
{
    printf("fork() faled\n");
}
Last edited on
fork() creates a child process that is a copy of the parent process and starts running at the return fork() (i.e. the same place as the parent).

So no matter what if/else statement you're in, when the parent process prints out its' ID, as in my code, then all child processes do the same? So if I created 2 children from one parent then the children will execute the same in order of their pid's?

So what about being able to control a particular process? Say I have similiar code as originally stated but I create a 2nd child:

if(fork()) // parent
if(fork()) // parent
else //2nd child
else // 1st child

Is there any way I can tell the 2nd child to send a message to the 1st & then the 1st replies back with a different message? I considered putting it inside their respective loops (because they would be able to getpid() & getppid()) but I assumed that printing would get messed up because it would execute the 1st child before the 2nd
So no matter what if/else statement you're in, when the parent process prints out its' ID, as in my code, then all child processes do the same?
The parent and child run the same program, but we use the return value from fork() to distinguish between the parent and child. So the parent and child execude different code in both your example and mine.

So if I created 2 children from one parent then the children will execute the same in order of their pid's?
No. The operating system scheduler decides what to run when. A program cannot know when it'll be run; we're talking standard installations here without R/T extensions.

So what about being able to control a particular process? ... Is there any way I can tell the 2nd child to send a message to the 1st & then the 1st replies back with a different message?
Yes. there are many ways to do this. In a sense, the parent/child thing is just a distraction; they're different processes that can use standard IPC mechanisms to communicate.

You can set up a pipe that child processes can use to communicate with each other. You have to create the pipe and fiddle around with the end so that they point to each other's stdin/stdout streams. Then when one writes to it's stdout, it turns up on the other's stdin. There's some stuff about that here: http://cse.csusb.edu/tong/courses/cs460/labs/rings/token-lab.pdf
fork is a very tricky function to use. Beware, that open file descriptors are inherited by the child process, and often you do not want that, and there is no standard way to close them...
Thanks kbw, that helped. Believe it or not now I am having trouble with pipes which is part of my assignment.

rapidcoder - I've found that out!
Topic archived. No new replies allowed.