3 Replies - 22568 Views - Last Post: 21 April 2009 - 04:25 AM

#1 sudoNut   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 19-April 09

fork() trees

Posted 19 April 2009 - 04:36 AM

Hey,

I'm currently revising for an concurrent programming exam and would like some thoughts on the following questions.

1. Given the following tree, write a fragment of code that produce processes (using fork()) given the form of the tree.

								  * parent
								 / \
								/   \
							   /	 \
					  child 1 *	   * child 2
							 / \
							/   \
						   /	 \
				  child 1 *	   * child 2
						 .
						.
					   .




2. What is the effect of the following fragments of unix/c code? Illustrate with a binary tree

while(fork() == 0)


Here I'm guessing only the child will continue forking, but what would the tree look like?

while(fork() > 0)


In this one the think the parent would continue forking so how would a tree be form?

So far I have a general understanding that processes calling (parent) get the child pid returned from the fork() call and new process created (child) has 0 returned from the fork() call.

However, I can't seem to get my head around how to take these code fragments and make them into trees and vice-versa.

Any ideas? Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: fork() trees

#2 sudoNut   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 19-April 09

Re: fork() trees

Posted 19 April 2009 - 05:40 AM

Ok so had a thought about number 2:

while(fork() == 0)

										 * parent
										/  
									   /   
									  /
							   child *
									/
								   /
								  /
						  child *
							   .
							  .
							 .



while(fork() > 0)

										 * parent
										/ \
									   /   \
									  /	 \
							   child *	   * 
									/ \	 / \
								   /   \   /   \
								  /	 \ /	 \
										etc



Still not sure about the first question. Any thoughts?
Was This Post Helpful? 0
  • +
  • -

#3 Kiriran   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 11-April 07

Re: fork() trees

Posted 20 April 2009 - 01:53 PM

the fork() call will return a pid_t > 0 if you are in the child process and 0 if you are in the parent process.
Hence
while(fork() == 0)

will always create a new child process from the parent process
						parent
|---------------------------|-------------------------|
child1				   child2				   child3


The created child process won't do anything inside the while loop, because fork() returned a pid_t > 0.

while(fork() > 0)

on the otherhand will be called by each child process once, because fork() > 0 is true the first time. The second time it enters the while condition, fork() == 0 (and a new child process is created) The result is
							   parent
									|
							   child1
									|
							   child2
									|
							   child3
									|
							   childN


And if you remeber that fork() returns pid_t > 0 for children and 0 for parent, the first one is easy to solve B)
Was This Post Helpful? 0
  • +
  • -

#4 sudoNut   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 19-April 09

Re: fork() trees

Posted 21 April 2009 - 04:25 AM

View PostKiriran, on 20 Apr, 2009 - 12:53 PM, said:

the fork() call will return a pid_t > 0 if you are in the child process and 0 if you are in the parent process.


That's incorrect. The fork() call will return > 0 if your are in the parent process (returning the pid of the child created) and 0 if you are in the child process.

The answers

1)
while(fork() == 0){

   if(fork() == 0) break;

}



2)
while(fork() == 0)

										 * parent
										/  
									   /   
									  /
							   child *
									/
								   /
								  /
						  child *
							   .
							  .
							 .



3)
while(fork() > 0)
							   parent
|---------------------------|-------------------------|
child1					   child2						 child3




These answers are confirmed by my lecture

We got there in the end :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1